Newer
Older
/*
* _____________________________________________________________________________
*
* INAF - OATS National Institute for Astrophysics - Astronomical Observatory of
* Trieste INAF - IA2 Italian Center for Astronomical Archives
* _____________________________________________________________________________
*
* Copyright (C) 2016 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.
*/
Sonia Zorba
committed
package it.inaf.ia2.tsm.api;
Sonia Zorba
committed
import it.inaf.ia2.tsm.api.contract.Column;
import it.inaf.ia2.tsm.api.contract.Key;
import it.inaf.ia2.tsm.api.contract.KeyColumn;
import it.inaf.ia2.tsm.api.contract.Schema;
import it.inaf.ia2.tsm.api.contract.Table;
import it.inaf.ia2.tsm.api.contract.TapSchema;
import it.inaf.ia2.tsm.api.contract.TapSchemaVersion;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Describes the mapping between the
Sonia Zorba
committed
* {@link it.inaf.ia2.tsm.api.contract.TapSchemaEntity} instances
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
* and their related table columns.
*
* @author Sonia Zorba {@literal <zorba at oats.inaf.it>}
*/
public class EntityPropertyInfo {
private static final Logger log = LoggerFactory.getLogger(EntityPropertyInfo.class);
private static final Map<String, List<EntityPropertyInfo>> propertiesMap = new HashMap<>();
static {
List<EntityPropertyInfo> schemaProperties = new ArrayList<>();
propertiesMap.put(TapSchema.SCHEMAS_TABLE, schemaProperties);
schemaProperties.add(new EntityPropertyInfo(Schema.SCHEMA_NAME_KEY, String.class, false));
schemaProperties.add(new EntityPropertyInfo(Schema.UTYPE_KEY, String.class, true));
schemaProperties.add(new EntityPropertyInfo(Schema.DESCRIPTION_KEY, String.class, true));
schemaProperties.add(new EntityPropertyInfo(Schema.SCHEMA_ID, Long.class, true, TapSchemaVersion.TAP_SCHEMA_1_IA2, TapSchemaVersion.TAP_SCHEMA_1_1_IA2));
List<EntityPropertyInfo> tableProperties = new ArrayList<>();
propertiesMap.put(TapSchema.TABLES_TABLE, tableProperties);
tableProperties.add(new EntityPropertyInfo(Table.SCHEMA_NAME_KEY, String.class, false));
tableProperties.add(new EntityPropertyInfo(Table.TABLE_NAME_KEY, String.class, false));
tableProperties.add(new EntityPropertyInfo(Table.TABLE_TYPE_KEY, String.class, false));
tableProperties.add(new EntityPropertyInfo(Table.UTYPE_KEY, String.class, true));
tableProperties.add(new EntityPropertyInfo(Table.DESCRIPTION_KEY, String.class, true));
tableProperties.add(new EntityPropertyInfo(Table.TABLE_INDEX, Integer.class, true, TapSchemaVersion.TAP_SCHEMA_1_1, TapSchemaVersion.TAP_SCHEMA_1_1_IA2));
tableProperties.add(new EntityPropertyInfo(Table.TABLE_ID, Long.class, true, TapSchemaVersion.TAP_SCHEMA_1_IA2, TapSchemaVersion.TAP_SCHEMA_1_1_IA2));
List<EntityPropertyInfo> columnProperties = new ArrayList<>();
propertiesMap.put(TapSchema.COLUMNS_TABLE, columnProperties);
columnProperties.add(new EntityPropertyInfo(Column.TABLE_NAME_KEY, String.class, false));
columnProperties.add(new EntityPropertyInfo(Column.COLUMN_NAME_KEY, String.class, false));
columnProperties.add(new EntityPropertyInfo(Column.DATATYPE_KEY, String.class, false));
columnProperties.add(new EntityPropertyInfo(Column.ARRAYSIZE_KEY, Integer.class, false, TapSchemaVersion.TAP_SCHEMA_1_1, TapSchemaVersion.TAP_SCHEMA_1_1_IA2));
columnProperties.add(new EntityPropertyInfo(Column.SIZE_KEY, Integer.class, false));
columnProperties.add(new EntityPropertyInfo(Column.DESCRIPTION_KEY, String.class, true));
columnProperties.add(new EntityPropertyInfo(Column.UTYPE_KEY, String.class, true));
columnProperties.add(new EntityPropertyInfo(Column.UNIT_KEY, String.class, true));
columnProperties.add(new EntityPropertyInfo(Column.UCD_KEY, String.class, true));
columnProperties.add(new EntityPropertyInfo(Column.INDEXED_KEY, Boolean.class, false));
columnProperties.add(new EntityPropertyInfo(Column.PRINCIPAL_KEY, Boolean.class, true));
columnProperties.add(new EntityPropertyInfo(Column.STD_KEY, Boolean.class, true));
columnProperties.add(new EntityPropertyInfo(Column.COLUMN_INDEX_KEY, Integer.class, true, TapSchemaVersion.TAP_SCHEMA_1_1, TapSchemaVersion.TAP_SCHEMA_1_1_IA2));
columnProperties.add(new EntityPropertyInfo(Column.ID_KEY, Integer.class, true, TapSchemaVersion.TAP_SCHEMA_1_IA2, TapSchemaVersion.TAP_SCHEMA_1_1_IA2));
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
columnProperties.add(new EntityPropertyInfo(Column.COLUMN_ID_KEY, Long.class, true, TapSchemaVersion.TAP_SCHEMA_1_IA2, TapSchemaVersion.TAP_SCHEMA_1_1_IA2));
List<EntityPropertyInfo> keyProperties = new ArrayList<>();
propertiesMap.put(TapSchema.KEYS_TABLE, keyProperties);
keyProperties.add(new EntityPropertyInfo(Key.ID_KEY, String.class, true));
keyProperties.add(new EntityPropertyInfo(Key.FROM_TABLE_KEY, String.class, false));
keyProperties.add(new EntityPropertyInfo(Key.TARGET_TABLE_KEY, String.class, false));
keyProperties.add(new EntityPropertyInfo(Key.DESCRIPTION_KEY, String.class, true));
keyProperties.add(new EntityPropertyInfo(Key.UTYPE_KEY, String.class, true));
keyProperties.add(new EntityPropertyInfo(Key.KEY_ID_KEY, Long.class, true, TapSchemaVersion.TAP_SCHEMA_1_IA2, TapSchemaVersion.TAP_SCHEMA_1_1_IA2));
List<EntityPropertyInfo> keyColumnProperties = new ArrayList<>();
propertiesMap.put(TapSchema.KEY_COLUMNS_TABLE, keyColumnProperties);
keyColumnProperties.add(new EntityPropertyInfo(KeyColumn.KEY_ID_KEY, String.class, true));
keyColumnProperties.add(new EntityPropertyInfo(KeyColumn.FROM_COLUMN_KEY, String.class, false));
keyColumnProperties.add(new EntityPropertyInfo(KeyColumn.TARGET_COLUMN_KEY, String.class, false));
keyColumnProperties.add(new EntityPropertyInfo(KeyColumn.KEY_COLUMN_ID_KEY, Long.class, true, TapSchemaVersion.TAP_SCHEMA_1_IA2, TapSchemaVersion.TAP_SCHEMA_1_1_IA2));
}
/**
* Obtains all the {@link EntityPropertyInfo} for a given TAP_SCHEMA table
* name.
*/
public static List<EntityPropertyInfo> getEntityPropertiesInfo(String tapSchemaTable) {
return propertiesMap.get(tapSchemaTable);
}
/**
* Obtains the {@link EntityPropertyInfo} for a given TAP_SCHEMA table name
* and a given column name.
*/
public static EntityPropertyInfo getEntityPropertyInfo(String tapSchemaTable, String key) {
for (EntityPropertyInfo propertyInfo : getEntityPropertiesInfo(tapSchemaTable)) {
if (propertyInfo.getPropertyKey().equals(key)) {
return propertyInfo;
}
}
log.debug("property {} not found for {} table", key, tapSchemaTable);
return null;
}
private final String propertyKey;
private final Class type;
private final int sqlType;
private final boolean updatable;
private final TapSchemaVersion[] versions;
protected EntityPropertyInfo(String propertyKey, Class type, boolean updatable, TapSchemaVersion... versions) {
this.propertyKey = propertyKey;
this.type = type;
if (type == String.class) {
sqlType = Types.VARCHAR;
} else if (type == Integer.class) {
sqlType = Types.INTEGER;
} else if (type == Long.class) {
sqlType = Types.BIGINT;
} else if (type == Boolean.class) {
sqlType = Types.BIT;
} else {
throw new IllegalArgumentException("EntityPropertyInfo doesn't support class type " + type.getCanonicalName());
}
this.updatable = updatable;
this.versions = versions;
}
/**
* The name of the property (the column name).
*/
public String getPropertyKey() {
return propertyKey;
}
/**
* The class type of the property value (the value stored in a
* {@link EntityProperty}).
*/
public Class getPropertyType() {
return type;
}
/**
* The {@link java.sql.Types} integer related to the class type of the
* property value.
*/
public int getSqlType() {
return sqlType;
}
/**
* Returns true if the user can update the value of this property.
*/
public boolean isUpdatable() {
return updatable;
}
/**
* Returns all the TAP_SCHEMA versions that uses this property.
*/
public TapSchemaVersion[] getVersions() {
return versions;
}
/**
* Returns true if the specified {@link TapSchemaVersion} uses this
* property.
*/
public boolean acceptVersion(TapSchemaVersion version) {
if (versions == null || versions.length == 0) {
return true;
}
for (TapSchemaVersion v : versions) {
if (v == version) {
return true;
}
}
return false;
}
}