Skip to main content
summaryrefslogtreecommitdiffstats
blob: c255d0f21a985a2ec1aacbaea19586db35e85b20 (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
/*******************************************************************************
 * 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.orcs.db.internal.exchange.handler;

import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import org.eclipse.osee.framework.core.exception.OseeCoreException;
import org.eclipse.osee.framework.database.IOseeDatabaseService;
import org.eclipse.osee.framework.database.core.OseeConnection;
import org.eclipse.osee.framework.database.core.SQL3DataType;
import org.eclipse.osee.framework.database.core.SupportedDatabase;
import org.eclipse.osee.framework.jdk.core.util.Strings;
import org.eclipse.osee.framework.jdk.core.util.io.xml.AbstractSaxHandler;
import org.eclipse.osee.orcs.db.internal.exchange.ExportImportXml;
import org.xml.sax.Attributes;

/**
 * @author Roberto E. Escobar
 */
public class MetaDataSaxHandler extends AbstractSaxHandler {

   private final Map<String, MetaData> importMetadataMap;
   private final Map<String, MetaData> targetMetadataMap;
   private final IOseeDatabaseService service;
   private MetaData currentMetadata;

   public MetaDataSaxHandler(IOseeDatabaseService service) {
      this.service = service;
      this.importMetadataMap = new HashMap<String, MetaData>();
      this.targetMetadataMap = new HashMap<String, MetaData>();
   }

   public MetaData getMetadata(String source) {
      return targetMetadataMap.get(source);
   }

   @Override
   public void startElementFound(String uri, String localName, String name, Attributes attributes) {
      if (localName.equalsIgnoreCase(ExportImportXml.METADATA)) {
         this.importMetadataMap.clear();
      } else if (localName.equalsIgnoreCase(ExportImportXml.TABLE)) {
         String tableName = attributes.getValue(ExportImportXml.TABLE_NAME);
         if (Strings.isValid(tableName)) {
            this.currentMetadata = new MetaData(tableName);
            this.importMetadataMap.put(tableName, currentMetadata);
         } else {
            this.currentMetadata = null;
         }
      } else if (localName.equalsIgnoreCase(ExportImportXml.COLUMN)) {
         String columnName = attributes.getValue(ExportImportXml.ID);
         String typeName = attributes.getValue(ExportImportXml.TYPE);
         SQL3DataType sql3DataType = SQL3DataType.valueOf(typeName);
         this.currentMetadata.addColumn(columnName, sql3DataType);
      }
   }

   @Override
   public void endElementFound(String uri, String localName, String name) throws Exception {
      if (localName.equalsIgnoreCase(ExportImportXml.TABLE)) {
         this.currentMetadata = null;
      }
   }

   public void checkAndLoadTargetDbMetadata() throws OseeCoreException, SQLException {
      Map<String, MetaData> targetTables = getTargetDbMetadata();

      StringBuffer errorMessage = new StringBuffer();
      for (String tableName : targetTables.keySet()) {
         MetaData sourceMeta = this.importMetadataMap.get(tableName);
         MetaData destinationMeta = targetTables.get(tableName);
         Collection<String> sourceColumns = sourceMeta.getColumnNames();
         for (String destinationColumn : destinationMeta.getColumnNames()) {
            if (!sourceColumns.contains(destinationColumn)) {
               errorMessage.append(String.format(
                  "Target column not found in source database.\nTable:[%s] - [%s not in (%s)]\n", tableName,
                  destinationColumn, sourceColumns));
            }
         }
      }
      if (errorMessage.length() > 0) {
         throw new OseeCoreException(errorMessage.toString());
      }
      this.targetMetadataMap.putAll(targetTables);
   }

   private Map<String, MetaData> getTargetDbMetadata() throws SQLException, OseeCoreException {
      Map<String, MetaData> targetDbMetadata = new HashMap<String, MetaData>();
      OseeConnection connection = service.getConnection();
      try {
         DatabaseMetaData dbMetaData = connection.getMetaData();
         for (String sourceTables : importMetadataMap.keySet()) {
            processMetaData(targetDbMetadata, dbMetaData, sourceTables);
         }
      } finally {
         connection.close();
      }
      return targetDbMetadata;
   }

   private void processMetaData(Map<String, MetaData> targetDbMetadata, DatabaseMetaData dbMetaData, String targetTable) throws SQLException, OseeCoreException {
      ResultSet resultSet = null;
      try {
         resultSet = dbMetaData.getTables(null, null, null, new String[] {"TABLE"});
         if (resultSet != null) {
            while (resultSet.next()) {
               String tableName = resultSet.getString("TABLE_NAME");
               String schemaName = resultSet.getString("TABLE_SCHEM");
               if (targetTable.equalsIgnoreCase(tableName)) {
                  String name = tableName.toLowerCase();
                  MetaData currentMetadata = new MetaData(name);
                  targetDbMetadata.put(name, currentMetadata);
                  processColumnMetaData(currentMetadata, dbMetaData, schemaName, tableName);
               }
            }
         }
      } finally {
         if (resultSet != null) {
            resultSet.close();
         }
      }
   }

   private void processColumnMetaData(MetaData currentMetadata, DatabaseMetaData dbMetaData, String schema, String tableName) throws SQLException, OseeCoreException {
      ResultSet resultSet = null;

      try {
         try {
            resultSet = dbMetaData.getColumns(null, schema, tableName, null);
         } catch (SQLException ex) {
            resultSet = dbMetaData.getColumns(null, null, tableName, null);
         }
         if (resultSet != null) {
            while (resultSet.next()) {
               String columnId = resultSet.getString("COLUMN_NAME").toLowerCase();
               int dataType = resultSet.getInt("DATA_TYPE");
               if (SupportedDatabase.isDatabaseType(dbMetaData, SupportedDatabase.foxpro)) {
                  if (dataType == Types.CHAR) {
                     dataType = Types.VARCHAR;
                  }
               }
               currentMetadata.addColumn(columnId, SQL3DataType.get(dataType));
            }
         }
      } finally {
         if (resultSet != null) {
            resultSet.close();
         }
      }
   }
}

Back to the top