001package fr.ifremer.adagio.synchro.action; 002 003/* 004 * #%L 005 * SIH-Adagio :: Shared 006 * $Id:$ 007 * $HeadURL:$ 008 * %% 009 * Copyright (C) 2012 - 2014 Ifremer 010 * %% 011 * This program is free software: you can redistribute it and/or modify 012 * it under the terms of the GNU Affero General Public License as published by 013 * the Free Software Foundation, either version 3 of the License, or 014 * (at your option) any later version. 015 * 016 * This program is distributed in the hope that it will be useful, 017 * but WITHOUT ANY WARRANTY; without even the implied warranty of 018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 019 * GNU General Public License for more details. 020 * 021 * You should have received a copy of the GNU Affero General Public License 022 * along with this program. If not, see <http://www.gnu.org/licenses/>. 023 * #L% 024 */ 025 026import java.sql.SQLException; 027import java.util.Properties; 028import java.util.Set; 029 030import org.apache.commons.logging.Log; 031import org.apache.commons.logging.LogFactory; 032import org.nuiton.i18n.I18n; 033 034import fr.ifremer.adagio.synchro.SynchroTechnicalException; 035import fr.ifremer.adagio.synchro.config.SynchroConfiguration; 036import fr.ifremer.adagio.synchro.dao.DaoUtils; 037import fr.ifremer.adagio.synchro.service.SynchroContext; 038import fr.ifremer.adagio.synchro.service.SynchroResult; 039import fr.ifremer.adagio.synchro.service.referential.ReferentialSynchroServiceImpl; 040 041public class SynchroImportReferentialAction { 042 /* Logger */ 043 private static final Log log = LogFactory.getLog(SynchroImportReferentialAction.class); 044 045 public void run() { 046 SynchroConfiguration config = SynchroConfiguration.getInstance(); 047 048 // Get connections properties : 049 Properties sourceConnectionProperties = config.getImportConnectionProperties(); 050 Properties targetConnectionProperties = config.getConnectionProperties(); 051 052 // Log target connection 053 if (log.isInfoEnabled()) { 054 log.info("Starting database referential importation..."); 055 } 056 057 // Check connections 058 if (!checkConnection(config, sourceConnectionProperties, targetConnectionProperties)) { 059 return; 060 } 061 062 // Create the result 063 SynchroResult result = new SynchroResult(); 064 065 // Get tables to includes from configuration 066 Set<String> tableToIncludes = config.getImportReferentialTablesIncludes(); 067 068 // Create the context 069 SynchroContext context = SynchroContext.newContext( 070 tableToIncludes, 071 sourceConnectionProperties, 072 targetConnectionProperties, 073 result); 074 075 // Prepare synchro 076 ReferentialSynchroServiceImpl service = new ReferentialSynchroServiceImpl(); 077 service.prepare(context); 078 079 if (!result.isSuccess()) { 080 throw new SynchroTechnicalException(I18n.t("adagio.synchro.prepare.error"), 081 result.getError()); 082 } 083 084 // Run the synchro 085 service.synchronize(context); 086 087 if (!result.isSuccess()) { 088 throw new SynchroTechnicalException(I18n.t("adagio.synchro.synchro.error"), 089 result.getError()); 090 } 091 092 // Shutdown database at end 093 try { 094 DaoUtils.shutdownDatabase(targetConnectionProperties); 095 } catch (SQLException e) { 096 throw new SynchroTechnicalException(I18n.t("adagio.synchro.shutdown.error"), 097 result.getError()); 098 } 099 } 100 101 protected boolean checkConnection( 102 SynchroConfiguration config, 103 Properties sourceConnectionProperties, 104 Properties targetConnectionProperties) { 105 106 // Log target connection 107 if (log.isInfoEnabled()) { 108 log.info("Connecting to target database..."); 109 log.info(String.format(" Database directory: %s", config.getDbDirectory())); 110 log.info(String.format(" JDBC Driver: %s", config.getJdbcDriver())); 111 log.info(String.format(" JDBC URL: %s", config.getJdbcURL())); 112 log.info(String.format(" JDBC Username: %s", config.getJdbcUsername())); 113 } 114 115 // Check target connection 116 boolean isValidConnection = DaoUtils.isValidConnectionProperties(targetConnectionProperties); 117 if (!isValidConnection) { 118 log.error("Connection error: could not connect to target database."); 119 return false; 120 } 121 122 // Log source connection 123 if (log.isInfoEnabled()) { 124 log.info("Connecting to source database..."); 125 log.info(String.format(" JDBC Driver: %s", config.getImportJdbcDriver())); 126 log.info(String.format(" JDBC URL: %s", config.getImportJdbcURL())); 127 log.info(String.format(" JDBC Username: %s", config.getImportJdbcUsername())); 128 } 129 130 // Check source connection 131 String sourceJdbcUrl = DaoUtils.getUrl(sourceConnectionProperties); 132 if (!sourceJdbcUrl.startsWith("jdbc:oracle:")) { 133 // Source database = an oracle connection (see file synchro-test-write.properties) 134 log.warn("Source database is not a Oracle database. Make sure your configuration file is correct."); 135 } 136 isValidConnection = DaoUtils.isValidConnectionProperties(sourceConnectionProperties); 137 if (!isValidConnection) { 138 log.warn("Connection error: could not connect to source database."); 139 return false; 140 } 141 142 return true; 143 } 144}