Newer
Older
/*
* _____________________________________________________________________________
*
* INAF - OATS National Institute for Astrophysics - Astronomical Observatory of
* Trieste INAF - IA2 Italian Center for Astronomical Archives
* _____________________________________________________________________________
*
* Copyright (C) 2017 Istituto Nazionale di Astrofisica
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License Version 3 as published by the
* Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc., 51
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package it.inaf.ia2.tsm;
import static it.inaf.ia2.tsm.TapSchema.COLUMNS_TABLE;
import static it.inaf.ia2.tsm.TapSchema.KEYS_TABLE;
import static it.inaf.ia2.tsm.TapSchema.KEY_COLUMNS_TABLE;
import static it.inaf.ia2.tsm.TapSchema.SCHEMAS_TABLE;
import static it.inaf.ia2.tsm.TapSchema.TABLES_TABLE;
import it.inaf.ia2.tsm.datalayer.DBBroker;
import it.inaf.ia2.tsm.model.ColumnModel;
import it.inaf.ia2.tsm.model.SchemaModel;
import it.inaf.ia2.tsm.model.TableModel;
import it.inaf.ia2.tsm.model.TypeMapping;
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
import it.inaf.ia2.tsm.model.TypesMapping;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.regex.Pattern;
/**
* Loads the TAP_SCHEMA and performs consistency checking.
*
* @author Sonia Zorba {@literal <zorba at oats.inaf.it>}
*/
public class TapSchemaLoader {
private final TapSchema tapSchema;
private final DBBroker tapSchemaDBBroker;
private ConsistencyChecks consistencyChecks;
public static ConsistencyChecks loadExistingTapSchema(TapSchema tapSchema) throws SQLException {
TapSchemaLoader loader = new TapSchemaLoader(tapSchema);
return loader.loadExistingTapSchema();
}
private TapSchemaLoader(TapSchema tapSchema) {
this.tapSchema = tapSchema;
tapSchemaDBBroker = tapSchema.getTapSchemaDBBroker();
}
private ConsistencyChecks loadExistingTapSchema() throws SQLException {
consistencyChecks = new ConsistencyChecks();
loadAndCheckSchemata();
loadAndCheckTables();
loadAndCheckColumns();
loadAndCheckKeys();
checkModel(tapSchema.getTapSchemaModel());
checkObscore();
return consistencyChecks;
}
private void checkObscore() {
if (tapSchema.isHasObscore()) {
String ivoaSchemaName = tapSchema.getIvoaSchemaModel().getName();
if (tapSchema.getChild(ivoaSchemaName) == null) {
if (tapSchema.isAddable(ivoaSchemaName)) {
consistencyChecks.setObscoreToAdd(true);
} else {
consistencyChecks.setMissingObscore(true);
}
} else {
checkModel(tapSchema.getIvoaSchemaModel());
}
}
}
private void checkModel(SchemaModel schemaModel) {
String schemaName = schemaModel.getName();
if ("tap_schema".equals(schemaName)) {
schemaName = tapSchema.getName();
}
Schema schema = tapSchema.getChild(schemaName);
for (TableModel tableModel : schemaModel.getTables()) {
String tableName = tableModel.getName();
Table table = schema.getChild(tableName);
if (table == null) {
if (schema.isAddable(tableModel.getName())) {
consistencyChecks.addTableToAdd(schema.getName(), tableName);
} else {
consistencyChecks.addMissingTable(schema.getName(), tableName);
}
} else {
for (ColumnModel columnModel : tableModel.getColumns()) {
Column column = table.getChild(columnModel.getName());
ColumnHolder ch = new ColumnHolder(schemaName,
table.getName(), columnModel.getName());
if (column == null) {
if (columnModel.isMandatory()) {
if (table.isAddable(columnModel.getName())) {
consistencyChecks.addColumnToAdd(ch);
} else {
consistencyChecks.addMissingColumn(ch, columnModel);
}
} else if (table.isAddable(columnModel.getName())) {
consistencyChecks.addUnaddedOptionalColumn(ch);
}
} else {
// Data type checking
String originalDataType = (String) column.getMetadata(Column.ORIGINAL_DATATYPE_KEY);
String modelDataType = (String) column.getMetadata(Column.DATATYPE_KEY);
TypeMapping originalType = TypesMapping.getTypeMapping(originalDataType, tapSchema.getDataTypeMode());
TypeMapping modelType = TypesMapping.getTypeMapping(modelDataType, tapSchema.getDataTypeMode());
if (originalType.getJavaType() != modelType.getJavaType()) {
consistencyChecks.addWrongDataType(ch, originalDataType, modelDataType, columnModel.getType(), columnModel.getSize());
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
}
}
}
}
}
}
private boolean equalsOneOf(String key, String... names) {
for (String name : names) {
if (name.equals(key)) {
return true;
}
}
return false;
}
private boolean hasFixedValue(Column column, String key) {
EntityProperty ep = column.getProperty(key);
if (!ep.isUpdatable()) {
return true;
}
Schema parentSchema = column.getParent().getParent();
if (parentSchema.getName().equals(tapSchema.getName())) {
return equalsOneOf(key, Column.STD_KEY, Column.PRINCIPAL_KEY);
} else if (parentSchema.getName().equals(tapSchema.getIvoaSchemaModel().getName())) {
return equalsOneOf(key, Column.STD_KEY, Column.PRINCIPAL_KEY,
Column.UCD_KEY, Column.UNIT_KEY, Column.UTYPE_KEY, Column.DATATYPE_KEY);
}
return false;
}
private Object getCorrectValue(Column column, String key) {
EntityProperty ep = column.getProperty(key);
if (!ep.isUpdatable()) {
return column.getMetadata(key);
}
Schema parentSchema = column.getParent().getParent();
SchemaModel schemaModel;
if (parentSchema.getName().equals(tapSchema.getName())) {
schemaModel = tapSchema.getTapSchemaModel();
} else {
schemaModel = tapSchema.getIvoaSchemaModel();
}
ColumnModel columnModel = schemaModel.getTable(column.getParent().getName()).get(column.getName());
switch (key) {
case Column.STD_KEY:
return columnModel.isStandard();
case Column.PRINCIPAL_KEY:
return columnModel.isPrincipal();
case Column.UCD_KEY:
return columnModel.getUcd();
case Column.UNIT_KEY:
return columnModel.getUnit();
case Column.UTYPE_KEY:
return columnModel.getUtype();
case Column.DATATYPE_KEY:
return TypesMapping.getDataType(columnModel.getType(), tapSchema.getDataTypeMode());
}
throw new RuntimeException("Unable to retrieve correct value for " + key);
}
private void loadSavedProperties(TapSchemaEntity tapSchemaEntity, Map<String, Object> savedProperties) {
for (Map.Entry<String, Object> entry : savedProperties.entrySet()) {
String key = entry.getKey();
Object savedValue = entry.getValue();
if (tapSchemaEntity instanceof Column) {
Column column = (Column) tapSchemaEntity;
if (hasFixedValue(column, key)) {
Object correctValue = getCorrectValue(column, key);
if (!Objects.equals(savedValue, correctValue)) {
InconsistentColumnProperty inconsistentValue = new InconsistentColumnProperty(
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
key,
savedValue,
correctValue
);
consistencyChecks.addInconsistency(inconsistentValue);
}
}
}
tapSchemaEntity.initProperty(key, savedValue);
}
}
private void loadAndCheckSchemata() throws SQLException {
for (Map<String, Object> schemaProps : tapSchemaDBBroker.getSavedItems(tapSchema.getName(), tapSchema.getTableModel(SCHEMAS_TABLE))) {
String schemaName = (String) schemaProps.get(Schema.SCHEMA_NAME_KEY);
Schema schema = tapSchema.addChild(schemaName);
if (schema == null) {
consistencyChecks.addUnexistingSchema(schemaName);
} else {
loadSavedProperties(schema, schemaProps);
schema.setStatus(Status.ADDED_PERSISTED);
}
}
}
private void loadAndCheckTables() throws SQLException {
for (Map<String, Object> tableProps : tapSchemaDBBroker.getSavedItems(tapSchema.getName(), tapSchema.getTableModel(TABLES_TABLE))) {
String tableCompleteName = (String) tableProps.get(Table.TABLE_NAME_KEY);
String[] tableNameSplit = tableCompleteName.split(Pattern.quote("."));
String schemaName = tableNameSplit[0];
String tableName = tableNameSplit[1];
Schema schema = tapSchema.getChild(schemaName, Status.ADDED_PERSISTED);
if (schema == null) {
consistencyChecks.addUnexistingSchema(schemaName);
} else {
Table table = schema.addChild(tableName);
if (table == null) {
consistencyChecks.addUnexistingTable(schemaName, tableName);
} else {
loadSavedProperties(table, tableProps);
table.setStatus(Status.ADDED_PERSISTED);
}
}
}
}
private void loadAndCheckColumns() throws SQLException {
for (Map<String, Object> columnProps : tapSchemaDBBroker.getSavedItems(tapSchema.getName(), tapSchema.getTableModel(COLUMNS_TABLE))) {
String tableCompleteName = (String) columnProps.get(Column.TABLE_NAME_KEY);
String[] tableNameSplit = tableCompleteName.split(Pattern.quote("."));
String schemaName = tableNameSplit[0];
String tableName = tableNameSplit[1];
String columnName = (String) columnProps.get(Column.COLUMN_NAME_KEY);
Schema schema = tapSchema.getChild(schemaName, Status.ADDED_PERSISTED);
if (schema == null) {
consistencyChecks.addUnexistingSchema(schemaName);
} else {
Table table = schema.getChild(tableName, Status.ADDED_PERSISTED);
if (table == null) {
consistencyChecks.addUnexistingTable(schemaName, tableName);
} else {
Column column = table.addChild(columnName);
if (column == null) {
consistencyChecks.addUnexistingColumn(schemaName, tableName, columnName);
} else {
loadSavedProperties(column, columnProps);
column.setStatus(Status.ADDED_PERSISTED);
}
}
}
}
}
private void loadAndCheckKeys() throws SQLException {
List<Map<String, Object>> keysProps = tapSchemaDBBroker.getSavedItems(tapSchema.getName(), tapSchema.getTableModel(KEYS_TABLE));
List<Map<String, Object>> keysColumnsProps = tapSchemaDBBroker.getSavedItems(tapSchema.getName(), tapSchema.getTableModel(KEY_COLUMNS_TABLE));
for (Map<String, Object> keyProp : keysProps) {
String fromTable = (String) keyProp.get(Key.FROM_TABLE_KEY);
String targetTable = (String) keyProp.get(Key.TARGET_TABLE_KEY);
String keyId = (String) keyProp.get(Key.ID_KEY);
assert keyId != null;
List<Map<String, Object>> kcPropsById = new ArrayList<>();
for (Map<String, Object> kcp : keysColumnsProps) {
String keyColumnId = (String) kcp.get(KeyColumn.KEY_ID_KEY);
assert keyColumnId != null;
if (keyColumnId.equals(keyId)) {
kcPropsById.add(kcp);
}
}
// Searching the key
boolean keyFound = false;
for (Key key : tapSchema.getAllKeys()) {
if (key.getFromTableCompleteName().equals(fromTable) && key.getTargetTableCompleteName().equals(targetTable)) {
// Search the key columns having proper key id
// Verifying the matching
List<KeyColumn> matchedKeyColumns = new ArrayList<>();
if (kcPropsById.size() == key.getKeyColumns().size()) {
for (Map<String, Object> kcp : kcPropsById) {
String fromColumn = (String) kcp.get(KeyColumn.FROM_COLUMN_KEY);
String targetColumn = (String) kcp.get(KeyColumn.TARGET_COLUMN_KEY);
for (KeyColumn keyColumn : key.getKeyColumns()) {
if (keyColumn.getFromColumn().equals(fromColumn)
&& keyColumn.getTargetColumn().equals(targetColumn)) {
matchedKeyColumns.add(keyColumn);
}
}
}
}
if (kcPropsById.size() == matchedKeyColumns.size()) {
keyFound = true;
int index = 0;
loadSavedProperties(key, keyProp);
for (Map<String, Object> kcp : kcPropsById) {
KeyColumn kc = matchedKeyColumns.get(index);
loadSavedProperties(kc, kcp);
index++;
}
}
}
}
if (!keyFound) {
boolean setKeyToRemove = true;
if (setKeyToRemove) {
String[] fromColumns = new String[kcPropsById.size()];
String[] targetColumns = new String[kcPropsById.size()];
int i = 0;
for (Map<String, Object> kcp : kcPropsById) {
fromColumns[i] = (String) kcp.get(KeyColumn.FROM_COLUMN_KEY);
targetColumns[i] = (String) kcp.get(KeyColumn.TARGET_COLUMN_KEY);
i++;
}
consistencyChecks.addUnexistingKey(keyId, fromTable, fromColumns, targetTable, targetColumns);
}
}
}
}
}