Skip to main content
summaryrefslogtreecommitdiffstats
blob: c2e311f1978603288eaa71639103b471110b19cc (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
/*******************************************************************************
 * 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.framework.core.datastore.schema.operations;

import java.io.File;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.logging.Level;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.eclipse.osee.framework.core.datastore.internal.Activator;
import org.eclipse.osee.framework.core.datastore.schema.data.ColumnDbData;
import org.eclipse.osee.framework.core.datastore.schema.data.ColumnMetadata;
import org.eclipse.osee.framework.core.datastore.schema.data.TableElement;
import org.eclipse.osee.framework.core.datastore.schema.data.TableElement.ColumnFields;
import org.eclipse.osee.framework.core.datastore.schema.data.TableElement.TableDescriptionFields;
import org.eclipse.osee.framework.core.datastore.schema.data.TableElement.TableTags;
import org.eclipse.osee.framework.core.datastore.schema.sql.SqlManager;
import org.eclipse.osee.framework.core.exception.OseeDataStoreException;
import org.eclipse.osee.framework.logging.OseeLog;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 * @author Roberto E. Escobar
 */
public class DatabaseDataImporter {
   private final File directory;
   private final SqlManager sqlManager;
   private List<String> tableOrder;
   private String schemaToImportTo;
   private final Set<String> tableFilter;

   private class TableData extends TableElement {
      private final List<List<ColumnDbData>> rowData;

      public TableData() {
         rowData = new ArrayList<List<ColumnDbData>>();
      }

      public void addRow(List<ColumnDbData> row) {
         rowData.add(row);
      }

      public List<List<ColumnDbData>> getRows() {
         return rowData;
      }
   }

   public DatabaseDataImporter(File directory, SqlManager sqlManager) {
      this.directory = directory;
      this.sqlManager = sqlManager;
      this.tableFilter = new TreeSet<String>();
   }

   public void setImportOrder(List<String> tableOrder) {
      this.tableOrder = tableOrder;
   }

   public void setSchemaToImportTo(String schema) {
      this.schemaToImportTo = schema;
   }

   public void addToTableFilter(String fullyQualifiedTableName) {
      this.tableFilter.add(fullyQualifiedTableName);
   }

   public void clearTableFilter() {
      this.tableFilter.clear();
   }

   public List<File> orderFilesByImportOrder(Map<String, File> toOrder) {
      List<File> orderedSet = new ArrayList<File>();
      if (tableOrder != null && tableOrder.size() != 0) {
         for (String tableName : tableOrder) {
            String fileName = tableName + FileUtility.DB_DATA_EXTENSION;
            File file = toOrder.get(fileName);
            if (file != null) {
               orderedSet.add(file);
            }
         }
      } else {
         Set<String> keys = toOrder.keySet();
         for (String key : keys) {
            orderedSet.add(toOrder.get(key));
         }
      }
      return orderedSet;
   }

   public Map<String, File> filterDataToImport(Map<String, File> toProcess) {
      Map<String, File> filteredList = new HashMap<String, File>();
      if (tableFilter != null && tableFilter.size() != 0) {
         for (String tableName : tableFilter) {
            String fileName = tableName + FileUtility.DB_DATA_EXTENSION;
            File file = toProcess.get(fileName);
            if (file != null) {
               filteredList.put(fileName, file);
            }
         }
      } else {
         return toProcess;
      }
      return filteredList;
   }

   public void importDataIntoDatabase() {
      if (FileUtility.isValidDirectory(directory)) {
         Map<String, File> filesToProcess = getFilesToProcess();
         Map<String, File> filteredFiles = filterDataToImport(filesToProcess);
         List<File> files = orderFilesByImportOrder(filteredFiles);
         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
         DocumentBuilder builder;
         Document document;
         for (File file : files) {
            try {
               builder = factory.newDocumentBuilder();
               document = builder.parse(file);
               processData(parseXMLDbDataFile(document));
            } catch (ParserConfigurationException ex) {
               OseeLog.log(Activator.class, Level.SEVERE, "Unable to Parse File. ", ex);
            } catch (Exception ex) {
               OseeLog.log(Activator.class, Level.SEVERE, "Exception: \n", ex);
            }
         }
      }
   }

   private Map<String, File> getFilesToProcess() {
      Map<String, File> toReturn = new HashMap<String, File>();
      List<File> files = FileUtility.getDBDataFileList(directory);
      for (File fileName : files) {
         toReturn.put(fileName.getName(), new File(directory + File.separator + fileName.getName()));
      }
      return toReturn;
   }

   private void processData(List<TableData> tables) throws OseeDataStoreException, ParseException {
      if (tables.size() != 0) {
         for (TableData tableData : tables) {
            OseeLog.log(Activator.class, Level.INFO, "Populating: [ " + tableData.getFullyQualifiedTableName() + "]\n");
            List<List<ColumnDbData>> rows = tableData.getRows();
            if (!rows.isEmpty()) {
               for (List<ColumnDbData> rowData : rows) {
                  sqlManager.insertData(rowData, tableData);
               }
            }
         }
      }
   }

   private void parseColumnMetadata(Element tableElement, TableData tableData) {
      NodeList columnElements = tableElement.getElementsByTagName(TableTags.ColumnInfo.name());
      if (columnElements != null) {
         for (int index = 0; index < columnElements.getLength(); index++) {
            Element columnElement = (Element) columnElements.item(index);
            if (columnElement != null) {

               NamedNodeMap attributeMap = columnElement.getAttributes();
               if (attributeMap != null && attributeMap.getLength() != 0) {
                  ColumnMetadata columnMetaData = new ColumnMetadata("");
                  for (int attributeIndex = 0; attributeIndex < attributeMap.getLength(); attributeIndex++) {
                     Node node = attributeMap.item(attributeIndex);
                     String nodeName = node.getNodeName();
                     String nodeValue = node.getTextContent();
                     if (nodeName != null && !nodeName.equals("")) {
                        columnMetaData.addColumnField(ColumnFields.valueOf(nodeName),
                              (nodeValue != null ? nodeValue : ""));
                     }
                  }
                  tableData.addColumn(columnMetaData);
               }
            }
         }
      }
   }

   private void parseRowInformation(Element tableElement, TableData tableData) {
      NodeList rowElements = tableElement.getElementsByTagName(TableTags.Row.name());
      if (rowElements != null) {
         for (int rowIndex = 0; rowIndex < rowElements.getLength(); rowIndex++) {
            Element row = (Element) rowElements.item(rowIndex);
            if (row != null) {
               NamedNodeMap attributeMap = row.getAttributes();
               if (attributeMap != null && attributeMap.getLength() != 0) {
                  List<ColumnDbData> rowData = new ArrayList<ColumnDbData>();
                  tableData.addRow(rowData);
                  for (int attributeIndex = 0; attributeIndex < attributeMap.getLength(); attributeIndex++) {
                     Node node = attributeMap.item(attributeIndex);
                     String nodeName = node.getNodeName();
                     String nodeValue = node.getTextContent();
                     if (nodeName != null && !nodeName.equals("")) {
                        rowData.add(new ColumnDbData(nodeName, (nodeValue != null ? nodeValue : "")));
                     }
                  }
               }
            }
         }
      }
   }

   private List<TableData> parseXMLDbDataFile(Document document) {
      NodeList tableElements = document.getElementsByTagName(TableTags.Table.name());
      List<TableData> tables = new ArrayList<TableData>();
      for (int index = 0; index < tableElements.getLength(); index++) {
         Element tableXmlElement = (Element) tableElements.item(index);
         if (tableXmlElement != null) {
            NamedNodeMap map = tableXmlElement.getAttributes();
            if (map != null && map.getLength() != 0) {
               Node tableName = map.getNamedItem(TableDescriptionFields.name.name());
               Node tableAddress = map.getNamedItem(TableDescriptionFields.schema.name());
               String tableNameString = "";
               String tableAddressString = "";
               if (tableName != null) {
                  tableNameString = tableName.getTextContent();
               }
               if (tableAddress != null) {
                  tableAddressString = tableAddress.getTextContent();
               }

               if (tableNameString != null && !tableNameString.equals("") && tableAddressString != null && !tableAddressString.equals("")) {
                  TableData tableData = new TableData();
                  tableData.addTableDescription(TableDescriptionFields.name, tableNameString);
                  if (this.schemaToImportTo != null && !this.schemaToImportTo.equals("")) {
                     tableData.addTableDescription(TableDescriptionFields.schema, schemaToImportTo);
                  } else {
                     tableData.addTableDescription(TableDescriptionFields.schema, tableAddressString);
                  }
                  parseRowInformation(tableXmlElement, tableData);
                  parseColumnMetadata(tableXmlElement, tableData);
                  tables.add(tableData);
               }
            }
         }
      }
      return tables;
   }
}

Back to the top