Skip to main content
summaryrefslogtreecommitdiffstats
blob: 9cc8d63dd400f1adecb21e77e79ab915eca40b49 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*******************************************************************************
 * 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.sql.DatabaseMetaData;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.eclipse.osee.database.schema.DatabaseCallable;
import org.eclipse.osee.database.schema.SchemaOptions;
import org.eclipse.osee.database.schema.SchemaResourceProvider;
import org.eclipse.osee.database.schema.internal.data.ColumnMetadata;
import org.eclipse.osee.database.schema.internal.data.ConstraintElement;
import org.eclipse.osee.database.schema.internal.data.ForeignKey;
import org.eclipse.osee.database.schema.internal.data.IndexElement;
import org.eclipse.osee.database.schema.internal.data.ReferenceClause;
import org.eclipse.osee.database.schema.internal.data.SchemaData;
import org.eclipse.osee.database.schema.internal.data.SchemaXmlParser;
import org.eclipse.osee.database.schema.internal.data.TableElement;
import org.eclipse.osee.database.schema.internal.data.TableElement.TableDescriptionFields;
import org.eclipse.osee.framework.core.exception.OseeExceptions;
import org.eclipse.osee.framework.database.IOseeDatabaseService;
import org.eclipse.osee.framework.database.core.OseeConnection;
import org.eclipse.osee.framework.jdk.core.type.OseeCoreException;
import org.eclipse.osee.framework.jdk.core.util.Strings;
import org.eclipse.osee.logger.Log;

/**
 * @author Roberto E. Escobar
 */
public class LoadUserSchemasCallable extends DatabaseCallable<Object> {

   private final Map<String, SchemaData> schemas;
   private final SchemaResourceProvider provider;
   private final SchemaOptions options;

   public LoadUserSchemasCallable(Log logger, IOseeDatabaseService dbService, Map<String, SchemaData> schemas, SchemaResourceProvider provider, SchemaOptions options) {
      super(logger, dbService);
      this.schemas = schemas;
      this.provider = provider;
      this.options = options;
   }

   private DatabaseMetaData getMetaData() throws OseeCoreException {
      OseeConnection connection = getDatabaseService().getConnection();
      try {
         return connection.getMetaData();
      } finally {
         connection.close();
      }
   }

   @Override
   public Object call() throws Exception {
      SchemaXmlParser parser = new SchemaXmlParser(getLogger());
      parser.parse(provider.getSchemaResources(), schemas);

      if (!options.isUseFileSpecifiedSchemas()) {
         try {
            DatabaseMetaData meta = getMetaData();
            if (meta != null) {
               String userName = meta.getUserName();
               if (Strings.isValid(userName)) {
                  int index = userName.indexOf('@');
                  if (index > 1) {
                     userName = userName.substring(0, index);
                  }
                  Map<String, SchemaData> newData = useUserNameAsSchema(userName.toUpperCase(), schemas);
                  schemas.clear();
                  schemas.putAll(newData);
               }
            }
         } catch (SQLException ex) {
            OseeExceptions.wrapAndThrow(ex);
         }
      }

      if (options.isTableDataSpaceValid()) {
         for (SchemaData schemaData : schemas.values()) {
            schemaData.setTableDataSpaceName(options.getTableDataSpace());
         }
      }

      if (options.isIndexDataSpaceValid()) {
         for (SchemaData schemaData : schemas.values()) {
            schemaData.setIndexDataSpaceName(options.getIndexDataSpace());
         }
      }
      return null;
   }

   private Map<String, SchemaData> useUserNameAsSchema(String userName, Map<String, SchemaData> userSchemas) {
      Map<String, SchemaData> newData = new HashMap<String, SchemaData>();
      SchemaData newSchemaData = new SchemaData();
      Set<String> keys = userSchemas.keySet();
      for (String key : keys) {
         SchemaData schemaData = userSchemas.get(key);
         List<TableElement> tables = schemaData.getTablesOrderedByDependency();
         for (TableElement table : tables) {
            TableElement newTable = useUserNameAsSchemaForTable(userName, table);
            newSchemaData.addTableDefinition(newTable);
         }
      }
      newData.put(userName, newSchemaData);
      return newData;
   }

   private TableElement useUserNameAsSchemaForTable(String userName, TableElement table) {
      TableElement newTable = new TableElement();

      Map<TableDescriptionFields, String> tableDescription = table.getDescription();
      Map<String, ColumnMetadata> columns = table.getColumns();
      List<ConstraintElement> constraints = table.getConstraints();
      List<ForeignKey> foreignKeys = table.getForeignKeyConstraints();
      List<IndexElement> indexElements = table.getIndexData();

      TableDescriptionFields[] descriptors = TableDescriptionFields.values();
      for (TableDescriptionFields field : descriptors) {
         String value = tableDescription.get(field);
         if (field.equals(TableDescriptionFields.schema)) {
            value = userName;
         }
         if (Strings.isValid(value)) {
            newTable.addTableDescription(field, value);
         }
      }

      Set<String> columnKeys = columns.keySet();
      for (String key : columnKeys) {
         newTable.addColumn(columns.get(key));
      }
      for (ConstraintElement constraint : constraints) {
         constraint.setSchema(userName);
         newTable.addConstraint(constraint);
      }
      for (ForeignKey constraint : foreignKeys) {
         constraint.setSchema(userName);
         List<ReferenceClause> references = constraint.getReferences();
         for (ReferenceClause clause : references) {
            clause.setSchema(userName);
         }
         newTable.addConstraint(constraint);
      }
      for (IndexElement indexElement : indexElements) {
         newTable.addIndexData(indexElement);
      }
      return newTable;
   }

}

Back to the top