Newer
Older
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
/*
* _____________________________________________________________________________
*
* 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 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 java.sql.SQLException;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
/**
* This fixes TAP_SCHEMA inconsistencies.
*
* @author Sonia Zorba {@literal <zorba at oats.inaf.it>}
*/
public class TapSchemaMender {
private final TapSchema tapSchema;
private final ConsistencyChecks consistencyChecks;
public TapSchemaMender(TapSchema tapSchema) {
this.tapSchema = tapSchema;
consistencyChecks = tapSchema.getConsistencyChecks();
}
public static void amendTapSchema(TapSchema tapSchema) throws SQLException {
new TapSchemaMender(tapSchema).amendTapSchema();
}
private void amendTapSchema() throws SQLException {
fixObscore();
createMissingTables();
createMissingColumns();
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
deleteUnexistingEntities();
addUnaddedTables();
addUnaddedColumns();
fixInconsistentValues();
}
private void fixObscore() throws SQLException {
SchemaModel ivoaSchemaModel = tapSchema.getIvoaSchemaModel();
if (tapSchema.isHasObscore()) {
if (consistencyChecks.isMissingObscore()) {
tapSchema.createAndAddIvoaSchema();
}
if (consistencyChecks.isMissingObscore()
|| consistencyChecks.isObscoreToAdd()) {
tapSchema.addEntireSchema(ivoaSchemaModel.getName());
tapSchema.fillColumnsProperties(ivoaSchemaModel);
}
}
}
private void createMissingTables() throws SQLException {
for (Map.Entry<String, Set<String>> entry : consistencyChecks.getMissingTables().entrySet()) {
String schemaName = entry.getKey();
DBBroker broker = tapSchema.getDBBroker(schemaName);
for (String tableName : entry.getValue()) {
SchemaModel schemaModel = schemaName.equals(tapSchema.getName())
? tapSchema.getTapSchemaModel() : tapSchema.getIvoaSchemaModel();
TableModel tableModel = schemaModel.getTable(tableName);
broker.createTable(schemaName, tableModel);
}
}
}
private void createMissingColumns() throws SQLException {
for (Map.Entry<ColumnHolder, ColumnModel> entry : consistencyChecks.getMissingColumns().entrySet()) {
ColumnHolder columnHolder = entry.getKey();
ColumnModel columnModel = entry.getValue();
DBBroker broker = tapSchema.getDBBroker(columnHolder.getSchemaName());
broker.addColumn(columnHolder, columnModel);
}
}
private void fixDataTypes() throws SQLException {
for (WrongDataType wrongDataType : consistencyChecks.getWrongDataTypes()) {
ColumnHolder columnHolder = wrongDataType.getColumnHolder();
DBBroker broker = tapSchema.getDBBroker(columnHolder.getSchemaName());
broker.alterDataType(columnHolder.getSchemaName(),
columnHolder.getTableName(), columnHolder.getColumnName(),
wrongDataType.getAdqlCorrectDataType(), wrongDataType.getSize());
}
}
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
private void deleteUnexistingEntities() throws SQLException {
String tapSchemaName = tapSchema.getName();
DBBroker tapSchemaDBBroker = tapSchema.getTapSchemaDBBroker();
Set<String> keysToRemoveIds = new HashSet<>();
for (String schema : consistencyChecks.getUnexisingSchemas()) {
keysToRemoveIds.addAll(tapSchemaDBBroker.getKeysToRemove(tapSchemaName, schema));
}
for (String table : consistencyChecks.getUnexisingTables()) {
keysToRemoveIds.addAll(tapSchemaDBBroker.getKeysToRemove(tapSchemaName, table));
}
for (ColumnHolder unexistingColumn : consistencyChecks.getUnexisingColumns()) {
keysToRemoveIds.addAll(tapSchemaDBBroker.getKeysToRemoveFromUnexistingColumn(tapSchemaName, unexistingColumn));
}
for (KeyHolder unexistingKey : consistencyChecks.getUnexistingKeys()) {
keysToRemoveIds.add(unexistingKey.getKeyId());
}
tapSchemaDBBroker.deleteUnexistingEntities(tapSchemaName, consistencyChecks, keysToRemoveIds);
}
private void addUnaddedTables() throws SQLException {
for (Map.Entry<String, Set<String>> entry : consistencyChecks.getTablesToAdd().entrySet()) {
String schemaName = entry.getKey();
Schema schema = tapSchema.getChild(schemaName);
for (String tableName : entry.getValue()) {
schema.addChild(tableName);
}
}
}
private void addUnaddedColumns() throws SQLException {
for (ColumnHolder columnHolder : consistencyChecks.getColumnsToAdd()) {
Schema schema = tapSchema.getChild(columnHolder.getSchemaName());
Table table = schema.getChild(columnHolder.getTableName());
table.addChild(columnHolder.getColumnName());
}
}
private void fixInconsistentValues() throws SQLException {
for (InconsistentColumnProperty inconsistency : consistencyChecks.getInconsistencies()) {
tapSchema.getTapSchemaDBBroker().updateTapSchemaColumnValue(tapSchema.getName(),
inconsistency.getTableCompleteName(), inconsistency.getColumnName(),
inconsistency.getKey(), inconsistency.getCorrectValue());
}
}
}