diff options
Diffstat (limited to 'plugins/org.eclipse.osee.database.schema/src/org/eclipse/osee/database/schema/internal/callable/ImportDataFromDbCallable.java')
-rw-r--r-- | plugins/org.eclipse.osee.database.schema/src/org/eclipse/osee/database/schema/internal/callable/ImportDataFromDbCallable.java | 184 |
1 files changed, 184 insertions, 0 deletions
diff --git a/plugins/org.eclipse.osee.database.schema/src/org/eclipse/osee/database/schema/internal/callable/ImportDataFromDbCallable.java b/plugins/org.eclipse.osee.database.schema/src/org/eclipse/osee/database/schema/internal/callable/ImportDataFromDbCallable.java new file mode 100644 index 00000000000..78fe3235b3e --- /dev/null +++ b/plugins/org.eclipse.osee.database.schema/src/org/eclipse/osee/database/schema/internal/callable/ImportDataFromDbCallable.java @@ -0,0 +1,184 @@ +/******************************************************************************* + * Copyright (c) 2004, 2007 Boeing. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Boeing - initial API and implementation + *******************************************************************************/ +package org.eclipse.osee.database.schema.internal.callable; + +import java.io.File; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.TreeSet; +import org.eclipse.osee.database.schema.DatabaseCallable; +import org.eclipse.osee.database.schema.internal.data.SchemaData; +import org.eclipse.osee.database.schema.internal.data.TableElement; +import org.eclipse.osee.database.schema.internal.util.FileUtility; +import org.eclipse.osee.framework.core.data.IDatabaseInfo; +import org.eclipse.osee.framework.database.IOseeDatabaseService; +import org.eclipse.osee.framework.database.core.DatabaseInfoManager; +import org.eclipse.osee.framework.jdk.core.util.Strings; +import org.eclipse.osee.logger.Log; + +/** + * @author Roberto E. Escobar + */ +public class ImportDataFromDbCallable extends DatabaseCallable<Object> { + + private static final File backupDirectory = new File("BackupDirectory"); + + private final Map<String, SchemaData> userSpecifiedConfig; + private final String tableImportSource; + + public ImportDataFromDbCallable(Log logger, IOseeDatabaseService databaseService, Map<String, SchemaData> userSpecifiedConfig, String tableImportSource) { + super(logger, databaseService); + this.userSpecifiedConfig = userSpecifiedConfig; + this.tableImportSource = tableImportSource; + } + + @Override + public Object call() throws Exception { + Set<String> importConnections = getImportConnections(); + for (String importFromDbService : importConnections) { + getLogger().info("Import Table Data from Db: [%s]", importFromDbService); + + IDatabaseInfo dbInfo = DatabaseInfoManager.getDataStoreById(importFromDbService); + getLogger().info("Gathering information from ... [%s]", importFromDbService); + + String userName = dbInfo.getDatabaseLoginName(); + if (Strings.isValid(userName)) { + + Set<String> schemasToGet = new TreeSet<String>(); + schemasToGet.add(userName.toUpperCase()); + + Map<String, Set<String>> dataToImport = getTablesToImport(userName.toUpperCase(), schemasToGet); + if (dataToImport.size() > 0) { + getLogger().info(dataToImport.toString().replaceAll(", ", "\n")); + makeBackupDirectoryIfItDoesntExist(); + + getLogger().info("Backing up Files to: [%s]", backupDirectory.getAbsolutePath()); + DatabaseDataExtractorCallable dbDataExtractor = + new DatabaseDataExtractorCallable(getLogger(), getDatabaseService(), schemasToGet, backupDirectory); + + Set<String> tablesToImport; + if (importFromDbService.equals(determineDefaultConnection())) { + tablesToImport = dataToImport.get(tableImportSource); + } else { + tablesToImport = dataToImport.get(importFromDbService); + } + + for (String importTable : tablesToImport) { + dbDataExtractor.addTableNameToExtract(importTable); + } + dbDataExtractor.call(); + dbDataExtractor.waitForWorkerThreads(); + + prepareFilesForImport(); + } + } + } + return null; + } + + private void prepareFilesForImport() { + Set<String> keys = userSpecifiedConfig.keySet(); + if (keys.size() == 1) { + String userName = ""; + for (String temp : keys) { + userName = temp; + } + List<File> files = FileUtility.getDBDataFileList(backupDirectory); + for (File fileName : files) { + String filename = fileName.getAbsolutePath().toString(); + filename = filename.substring(filename.lastIndexOf(File.separator) + 1, filename.length()); + filename = filename.substring(filename.indexOf(".") + 1, filename.length()); + fileName.renameTo(new File(backupDirectory + File.separator + userName + "." + filename)); + } + } + } + + private String determineDefaultConnection() { + String importFromDbService = System.getProperty(tableImportSource); + if (!Strings.isValid(importFromDbService)) { + importFromDbService = "oracle"; + } + return importFromDbService; + } + + private Set<String> getImportConnections() { + String defaultConnection = determineDefaultConnection(); + Set<String> userSchemas = userSpecifiedConfig.keySet(); + Set<String> connectionsNeeded = new TreeSet<String>(); + for (String key : userSchemas) { + SchemaData schemaDataInUserConfig = userSpecifiedConfig.get(key); + Map<String, Set<String>> tableNamesToImport = schemaDataInUserConfig.getTablesToImport(tableImportSource); + Set<String> keys = tableNamesToImport.keySet(); + for (String connectionString : keys) { + if (connectionString.equals(tableImportSource)) { + connectionsNeeded.add(defaultConnection); + } else { + connectionsNeeded.add(connectionString); + } + } + } + return connectionsNeeded; + } + + private Map<String, SchemaData> getAvailableSchemasFromImportDb(Set<String> schemas) throws Exception { + Map<String, SchemaData> schemaMap = new HashMap<String, SchemaData>(); + ExtractSchemaCallable schemaExtractor = + new ExtractSchemaCallable(getLogger(), getDatabaseService(), schemas, schemaMap); + schemaExtractor.call(); + return schemaMap; + } + + private Map<String, Set<String>> getTablesToImport(String userName, Set<String> schemasToGet) throws Exception { + Map<String, SchemaData> currentDbSchemas = getAvailableSchemasFromImportDb(schemasToGet); + Set<String> userSchemas = userSpecifiedConfig.keySet(); + + SchemaData schemaData = currentDbSchemas.get(userName); + Map<String, TableElement> tableMap = schemaData.getTableMap(); + + Map<String, Set<String>> importTables = new HashMap<String, Set<String>>(); + for (String key : userSchemas) { + SchemaData schemaDataInUserConfig = userSpecifiedConfig.get(key); + Map<String, Set<String>> tableNamesToImport = schemaDataInUserConfig.getTablesToImport(tableImportSource); + + Set<String> keys = tableNamesToImport.keySet(); + for (String importKey : keys) { + Set<String> namesToImport = tableNamesToImport.get(importKey); + + for (String tableName : namesToImport) { + tableName = tableName.replaceAll(key + "\\.", userName + "."); + + if (tableMap.containsKey(tableName)) { + Set<String> tableSet; + if (importTables.containsKey(importKey)) { + tableSet = importTables.get(importKey); + } else { + tableSet = new TreeSet<String>(); + } + tableSet.add(tableName); + importTables.put(importKey, tableSet); + } + } + } + } + return importTables; + } + + private void makeBackupDirectoryIfItDoesntExist() { + if (backupDirectory != null && backupDirectory.exists() && backupDirectory.canWrite()) { + return; + } else { + backupDirectory.mkdirs(); + } + } + +}
\ No newline at end of file |