Skip to main content
summaryrefslogtreecommitdiffstats
blob: f2b5466745f1e8d49b243222448abac1cd30c5d2 (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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*******************************************************************************
 * 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.data;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import javax.xml.parsers.ParserConfigurationException;
import org.eclipse.osee.database.schema.internal.data.TableElement.ColumnFields;
import org.eclipse.osee.framework.jdk.core.persistence.Xmlizable;
import org.eclipse.osee.framework.jdk.core.util.Strings;
import org.eclipse.osee.framework.jdk.core.util.xml.Jaxp;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

/**
 * @author Roberto E. Escobar
 */
public class SchemaData implements Xmlizable {

   public static final String ROOT_TAG = "TableConfig";
   private List<TableElement> tableDefinitions;
   private final SchemaDataLookup schemaLookup;

   private final boolean isSorted;

   public SchemaData() {
      this.tableDefinitions = new ArrayList<TableElement>();
      this.isSorted = false;
      this.schemaLookup = new SchemaDataLookup(this);
   }

   public void setTableDataSpaceName(String tableDataSpace) {
      for (TableElement table : getTableMap().values()) {
         table.setTablespace(tableDataSpace);
      }
   }

   public void setIndexDataSpaceName(String indexDataSpace) {
      for (TableElement table : getTableMap().values()) {
         for (IndexElement indexElement : table.getIndexData()) {
            indexElement.setTablespace(indexDataSpace);
         }
      }
   }

   public List<TableElement> getTablesOrderedByDependency() {
      sortTablesForConstraints();
      markIdentityColumns();
      return tableDefinitions;
   }

   public Set<String> getTablesToBackup() {
      Set<String> backupTables = new TreeSet<String>();
      for (TableElement table : tableDefinitions) {
         if (table.isBackupDataSet()) {
            backupTables.add(table.getFullyQualifiedTableName());
         }
      }
      return backupTables;
   }

   public Map<String, Set<String>> getTablesToImport(String defaultSource) {
      Map<String, Set<String>> importTables = new HashMap<String, Set<String>>();
      for (TableElement table : tableDefinitions) {
         if (table.isImportDataSet()) {
            String importFrom = table.getImportFrom();
            if (!Strings.isValid(importFrom)) {
               importFrom = defaultSource;
            }
            Set<String> tableSet;
            if (importTables.containsKey(importFrom)) {
               tableSet = importTables.get(importFrom);
            } else {
               tableSet = new TreeSet<String>();
            }
            tableSet.add(table.getFullyQualifiedTableName());
            importTables.put(importFrom, tableSet);
         }
      }
      return importTables;
   }

   public void addTableDefinition(TableElement table) {
      tableDefinitions.add(table);
   }

   @Override
   public String toString() {
      String toReturn = "";
      for (TableElement table : tableDefinitions) {
         toReturn += table.toString();
      }
      return toReturn;
   }

   public Document getXmlDocument() throws ParserConfigurationException {
      Document doc = Jaxp.newDocumentNamespaceAware();
      Element root = doc.createElement(ROOT_TAG);
      doc.appendChild(root);
      for (TableElement table : tableDefinitions) {
         root.appendChild(table.toXml(doc));
      }
      return doc;
   }

   @Override
   public Element toXml(Document doc) {
      return null;
   }

   private void sortTablesForConstraints() {
      this.tableDefinitions = sortTablesForConstraints(this.tableDefinitions);
   }

   private boolean canCreate(TableElement table, Set<String> canCreate) {
      Set<String> dependencies = table.getTableDependency();
      if (canCreate.containsAll(dependencies)) {
         return true;
      }
      return false;
   }

   private void markIdentityColumns() {
      for (TableElement aTable : tableDefinitions) {
         determineIdentityColumns(aTable);
      }
   }

   private void determineIdentityColumns(TableElement tableDef) {
      List<String> primaryKeys = new ArrayList<String>();

      // first get all of the vars used for the primary key
      List<ConstraintElement> constraints = tableDef.getConstraints();
      for (ConstraintElement constraint : constraints) {
         if (constraint.getConstraintType().equals(ConstraintTypes.PRIMARY_KEY)) {
            List<String> columns = constraint.getColumns();
            for (String column : columns) {
               primaryKeys.add(column);
            }
         }
      }

      // now go through and remove any of the primary key vars that are foreign keys
      List<ForeignKey> foreignKeys = tableDef.getForeignKeyConstraints();
      for (ForeignKey fkConstraint : foreignKeys) {
         List<String> columns = fkConstraint.getColumns();
         for (String column : columns) {
            primaryKeys.remove(column);
         }
      }

      // now we should only be left with those primary keys that are identities.
      // we set them by setting their corresponding identity column to true, a little hokey but that
      // way we don't have to create special data structures for the column
      Map<String, ColumnMetadata> columns = tableDef.getColumns();
      Set<String> columnKeys = columns.keySet();
      for (String key : columnKeys) {
         ColumnMetadata column = columns.get(key);
         if (primaryKeys.contains(column.getId())) {
            column.addColumnField(ColumnFields.identity, Boolean.TRUE.toString());
         }
      }
   }

   private List<TableElement> sortTablesForConstraints(List<TableElement> tables) {
      List<TableElement> sorted = new ArrayList<TableElement>();
      Set<String> canCreate = new HashSet<String>();

      Map<TableElement, Boolean> toSort = new HashMap<TableElement, Boolean>();

      for (TableElement aTable : tables) {
         if (aTable.hasForeignKey()) {
            toSort.put(aTable, false);
         } else {
            sorted.add(aTable);
            canCreate.add(aTable.getFullyQualifiedTableName());
         }
      }

      // Prevent for endless loops caused by
      // foreign/primary key discrepancies
      int guard = toSort.size() * 2;
      int count = 0;
      while (toSort.containsValue(false) && count < guard) {
         Set<TableElement> elements = toSort.keySet();

         for (TableElement aTable : elements) {

            if (!toSort.get(aTable)) {
               if (canCreate(aTable, canCreate)) {
                  canCreate.add(aTable.getFullyQualifiedTableName());
                  sorted.add(aTable);
                  toSort.put(aTable, true);
               }
            }
         }
         count++;
      }

      // If we were stuck in an endless loop copy all unsortable tables
      // to the end of the sorted array and return
      if (toSort.containsValue(false)) {
         Set<TableElement> elements = toSort.keySet();
         for (TableElement aTable : elements) {
            if (!toSort.get(aTable)) {
               canCreate.add(aTable.getFullyQualifiedTableName());
               sorted.add(aTable);
               toSort.put(aTable, true);
            }
         }
      }

      return sorted;
   }

   public Map<String, TableElement> getTableMap() {
      Map<String, TableElement> tableMap = new HashMap<String, TableElement>();
      for (TableElement table : tableDefinitions) {
         tableMap.put(table.getFullyQualifiedTableName(), table);
      }
      return tableMap;
   }

   @Override
   public int hashCode() {
      final int prime = 31;
      int result = 1;
      result = prime * result + (isSorted ? 1231 : 1237);
      result = prime * result + ((schemaLookup == null) ? 0 : schemaLookup.hashCode());
      result = prime * result + ((tableDefinitions == null) ? 0 : tableDefinitions.hashCode());
      return result;
   }

   @Override
   public boolean equals(Object obj) {
      if (this == obj) {
         return true;
      }
      if (obj == null) {
         return false;
      }
      if (getClass() != obj.getClass()) {
         return false;
      }
      SchemaData other = (SchemaData) obj;
      if (isSorted != other.isSorted) {
         return false;
      }
      if (schemaLookup == null) {
         if (other.schemaLookup != null) {
            return false;
         }
      } else if (!schemaLookup.equals(other.schemaLookup)) {
         return false;
      }
      if (tableDefinitions == null) {
         if (other.tableDefinitions != null) {
            return false;
         }
      } else if (!tableDefinitions.equals(other.tableDefinitions)) {
         return false;
      }
      return true;
   }

}

Back to the top