diff --git a/TASMAN-core/src/main/java/it/inaf/ia2/tsm/EntityProperty.java b/TASMAN-core/src/main/java/it/inaf/ia2/tsm/EntityProperty.java index f009cec488d19dbea0a0e6ddc813c0fb82c94eee..beade83be0c2fd0d88c701b08f6a783a8d9af5e6 100644 --- a/TASMAN-core/src/main/java/it/inaf/ia2/tsm/EntityProperty.java +++ b/TASMAN-core/src/main/java/it/inaf/ia2/tsm/EntityProperty.java @@ -46,7 +46,7 @@ public class EntityProperty<T> implements Serializable { public EntityProperty(PropertyModel propertyModel, T defaultValue) { this.propertyModel = propertyModel; - this.type = propertyModel.getType(); + this.type = propertyModel.getJavaType(); this.init(defaultValue); } diff --git a/TASMAN-core/src/main/java/it/inaf/ia2/tsm/TapSchema.java b/TASMAN-core/src/main/java/it/inaf/ia2/tsm/TapSchema.java index 36a4e77739220688bc03960fcb35d64dfd9aba7b..821cd9006e926ffbea470d355fe20a439086ec46 100644 --- a/TASMAN-core/src/main/java/it/inaf/ia2/tsm/TapSchema.java +++ b/TASMAN-core/src/main/java/it/inaf/ia2/tsm/TapSchema.java @@ -27,8 +27,8 @@ import it.inaf.ia2.tsm.datalayer.DBBrokerFactory; import it.inaf.ia2.tsm.datalayer.DBWrapper; import it.inaf.ia2.tsm.model.PropertyModel; import it.inaf.ia2.tsm.model.TableModel; -import it.inaf.ia2.tsm.model.TapSchemaModel; -import it.inaf.ia2.tsm.model.TapSchemaModels; +import it.inaf.ia2.tsm.model.SchemaModel; +import it.inaf.ia2.tsm.model.SchemaModels; import java.io.Serializable; import java.sql.SQLException; import java.util.ArrayList; @@ -67,10 +67,12 @@ public class TapSchema implements EntitiesContainer<Schema>, Serializable { private final ConsistencyChecks consistencyChecks; private boolean loading; - private String version; private DBWrapper dbWrapper; - private String tapSchemaName; private boolean exists; + private String tapSchemaVersion; + private String tapSchemaName; + private boolean obscore; + private String obscoreVersion; private transient DBBroker sourceDBBroker; private transient DBBroker tapSchemaDBBroker; @@ -96,14 +98,16 @@ public class TapSchema implements EntitiesContainer<Schema>, Serializable { consistencyChecks = new ConsistencyChecks(); } - public TapSchema(String version, DBWrapper dbWrapper, String tapSchemaName, boolean exists) throws SQLException { + public TapSchema(DBWrapper dbWrapper, TapSchemaSettings settings, boolean exists) throws SQLException { this(); loading = true; - this.version = version; this.dbWrapper = dbWrapper; - this.tapSchemaName = tapSchemaName; this.exists = exists; + this.tapSchemaVersion = settings.getTapSchemaVersion(); + this.tapSchemaName = settings.getTapSchemaName(); + this.obscore = settings.isHasObscore(); + this.obscoreVersion = settings.getObscoreVersion(); // Initializing schemas map for (String schemaName : getSourceDBBroker().getAllSchemaNames()) { @@ -278,10 +282,6 @@ public class TapSchema implements EntitiesContainer<Schema>, Serializable { } } - public void addFictitiousKey(String fromTable, String[] fromColumns, String targetTable, String[] targetColumns) { - // TODO - } - public DBBroker getDBBroker(String schemaName) { if (schemaName.equals(tapSchemaName)) { return getTapSchemaDBBroker(); @@ -301,7 +301,7 @@ public class TapSchema implements EntitiesContainer<Schema>, Serializable { * The version selected for this TAP_SCHEMA. */ public String getVersion() { - return version; + return tapSchemaVersion; } private void loadSchemaKeysMetadata(String schemaName) throws SQLException { @@ -428,6 +428,23 @@ public class TapSchema implements EntitiesContainer<Schema>, Serializable { schemas.put(schemaName, null); } + private SchemaModel getIvoaSchemaModel() { + if (obscore) { + return SchemaModels.getIvoaSchemaModel(obscoreVersion); + } + return null; + } + + private void addEntireSchema(String schemaName) throws SQLException { + Schema schema = addChild(schemaName); + for (String tableName : schema.getAddableChildrenNames()) { + Table table = schema.addChild(tableName); + for (String columnName : table.getAddableChildrenNames()) { + table.addChild(columnName); + } + } + } + /** * Save or update the TAP_SCHEMA changes into the database. */ @@ -436,18 +453,22 @@ public class TapSchema implements EntitiesContainer<Schema>, Serializable { DBBroker broker = getTapSchemaDBBroker(); if (!exists) { - broker.createTapSchemaStructure(getName(), getTapSchemaModel()); + SchemaModel tapSchemaModel = getTapSchemaModel(); + broker.createTapSchemaStructure(tapSchemaName, tapSchemaModel); // Adding TAP_SCHEMA into TAP_SCHEMA - Schema tapSchemaSchema = addChild(tapSchemaName); - for (String tableName : tapSchemaSchema.getAddableChildrenNames()) { - Table table = tapSchemaSchema.addChild(tableName); - for (String columnName : table.getAddableChildrenNames()) { - table.addChild(columnName); - } - } + addEntireSchema(tapSchemaName); + fillColumnDescriptionsAndStd(tapSchemaModel, tapSchemaName); + + if (obscore) { + SchemaModel ivoaSchemaModel = getIvoaSchemaModel(); - fillTapSchemaDescriptions(); + broker.createIvoaSchemaStructure(ivoaSchemaModel); + + // Add ivoa schema into TAP_SCHEMA + addEntireSchema(ivoaSchemaModel.getName()); + fillColumnDescriptionsAndStd(ivoaSchemaModel); + } } fillKeyIds(); @@ -493,20 +514,6 @@ public class TapSchema implements EntitiesContainer<Schema>, Serializable { } } -// public void addFictitiousKey(Table fromTable, String[] fromColumns, Table targetTable, String[] targetColumns) { -// Key key = new Key(dbWrapper, this, fromTable.getCompleteName(), targetTable.getCompleteName()); -// key.setId((getMaxKeyId() + 1) + ""); -// -// for (int i = 0; i < fromColumns.length; i++) { -// key.addKeyColumn(fromColumns[i], targetColumns[i]); -// } -// -// fromTable.addFromKey(key); -// targetTable.addTargetKey(key); -// -// allKeys.add(key); -// checkKeys(); -// } /** * Set keys visibility based on other entities visibility (a key is visible * if all schemas, tables and columns involved have @@ -646,8 +653,8 @@ public class TapSchema implements EntitiesContainer<Schema>, Serializable { return consistencyChecks; } - public TapSchemaModel getTapSchemaModel() { - return TapSchemaModels.getTapSchemaModel(version); + public SchemaModel getTapSchemaModel() { + return SchemaModels.getTapSchemaModel(tapSchemaVersion); } public final TableModel getTableModel(String tableName) { @@ -671,23 +678,26 @@ public class TapSchema implements EntitiesContainer<Schema>, Serializable { } /** - * Fill descriptions of the TAP_SCHEMA schema entities. + * Fill descriptions of the TAP_SCHEMA schema entities for a given + * SchemaModel (TAP_SCHEMA or ivoa). */ - private void fillTapSchemaDescriptions() { - TapSchemaModel tapSchemaModel = getTapSchemaModel(); - boolean hasStd = tapSchemaModel.get(TapSchema.COLUMNS_TABLE).get(STD_KEY) != null; - Schema tapSchema = getChild(tapSchemaName); - tapSchema.setValue(DESCRIPTION_KEY, tapSchemaModel.getDescription()); + private void fillColumnDescriptionsAndStd(SchemaModel schemaModel, String schemaName) { + Schema schema = getChild(schemaName); + schema.setValue(DESCRIPTION_KEY, schemaModel.getDescription()); for (TableModel tableModel : getTapSchemaModel().getTables().values()) { - Table table = tapSchema.getChild(tableModel.getName()); - tapSchema.setValue(DESCRIPTION_KEY, tableModel.getDescription()); + Table table = schema.getChild(tableModel.getName()); + schema.setValue(DESCRIPTION_KEY, tableModel.getDescription()); for (PropertyModel propertyModel : tableModel.getProperties().values()) { Column column = table.getChild(propertyModel.getName()); - tapSchema.setValue(DESCRIPTION_KEY, propertyModel.getDescription()); - if (hasStd) { + schema.setValue(DESCRIPTION_KEY, propertyModel.getDescription()); + if (propertyModel.isStandard()) { column.setValue(STD_KEY, 1); } } } } + + private void fillColumnDescriptionsAndStd(SchemaModel schemaModel) { + fillColumnDescriptionsAndStd(schemaModel, schemaModel.getName()); + } } diff --git a/TASMAN-core/src/main/java/it/inaf/ia2/tsm/TapSchemaEntity.java b/TASMAN-core/src/main/java/it/inaf/ia2/tsm/TapSchemaEntity.java index 5cb250fadbdeb54ecbd81419715808151da9e16a..5580664b5cab6658f18ef1e66440958d80d0d0af 100644 --- a/TASMAN-core/src/main/java/it/inaf/ia2/tsm/TapSchemaEntity.java +++ b/TASMAN-core/src/main/java/it/inaf/ia2/tsm/TapSchemaEntity.java @@ -24,6 +24,7 @@ package it.inaf.ia2.tsm; import it.inaf.ia2.tsm.model.PropertyModel; import it.inaf.ia2.tsm.model.TableModel; +import it.inaf.ia2.tsm.model.TypesMapping; import java.io.Serializable; import java.util.ArrayList; import java.util.HashMap; @@ -68,12 +69,16 @@ public abstract class TapSchemaEntity implements Serializable { Object defaultValue = null; if (propModel.getLoaderKey() != null) { defaultValue = metadata.get(propModel.getLoaderKey()); - if (defaultValue != null && propModel.getType() != defaultValue.getClass()) { + + if (defaultValue != null) { // Special case for boolean to integer conversion - if (defaultValue.getClass() == Boolean.class && propModel.getType() == Integer.class) { - defaultValue = ((Boolean) defaultValue) ? 1 : 0; - } else { - throw new UnsupportedOperationException("Unable to convert " + defaultValue.getClass().getCanonicalName() + " into " + propModel.getType()); + Class javaType = propModel.getJavaType(); + if (javaType != defaultValue.getClass()) { + if (defaultValue.getClass() == Boolean.class && javaType == Integer.class) { + defaultValue = ((Boolean) defaultValue) ? 1 : 0; + } else { + throw new UnsupportedOperationException("Unable to convert " + defaultValue.getClass().getCanonicalName() + " into " + propModel.getType()); + } } } } diff --git a/TASMAN-core/src/main/java/it/inaf/ia2/tsm/model/Tasman.java b/TASMAN-core/src/main/java/it/inaf/ia2/tsm/TapSchemaSettings.java similarity index 51% rename from TASMAN-core/src/main/java/it/inaf/ia2/tsm/model/Tasman.java rename to TASMAN-core/src/main/java/it/inaf/ia2/tsm/TapSchemaSettings.java index fa1c8f8639b8bc90817a91d6a4bc215a297cb6c4..9a339e1c7f33384f0da18d2980662e59a0cf7544 100644 --- a/TASMAN-core/src/main/java/it/inaf/ia2/tsm/model/Tasman.java +++ b/TASMAN-core/src/main/java/it/inaf/ia2/tsm/TapSchemaSettings.java @@ -20,33 +20,52 @@ * 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.model; +package it.inaf.ia2.tsm; -import java.io.File; -import java.io.IOException; -import java.io.InputStream; -import java.util.Properties; +import java.io.Serializable; /** * * @author Sonia Zorba {@literal <zorba at oats.inaf.it>} */ -public class Tasman { - - public static final String[] XML_MODEL_FILES; - - static { - try (InputStream in = Tasman.class.getClassLoader().getResourceAsStream("core.properties")) { - Properties props = new Properties(); - props.load(in); - String[] models = props.getProperty("models").split(","); - XML_MODEL_FILES = new String[models.length]; - for (int i = 0; i < models.length; i++) { - String suffix = models[i]; - XML_MODEL_FILES[i] = "tap_schema" + File.separator + "tap_schema-" + suffix + ".xml"; - } - } catch (IOException e) { - throw new ExceptionInInitializerError(e); - } +public class TapSchemaSettings implements Serializable { + + private static final long serialVersionUID = -4152480696306984772L; + + private String tapSchemaVersion; + private String tapSchemaName; + private boolean hasObscore; + private String obscoreVersion; + + public String getTapSchemaVersion() { + return tapSchemaVersion; + } + + public void setTapSchemaVersion(String tapSchemaVersion) { + this.tapSchemaVersion = tapSchemaVersion; + } + + public String getTapSchemaName() { + return tapSchemaName; + } + + public void setTapSchemaName(String tapSchemaName) { + this.tapSchemaName = tapSchemaName; + } + + public boolean isHasObscore() { + return hasObscore; + } + + public void setHasObscore(boolean hasObscore) { + this.hasObscore = hasObscore; + } + + public String getObscoreVersion() { + return obscoreVersion; + } + + public void setObscoreVersion(String obscoreVersion) { + this.obscoreVersion = obscoreVersion; } } diff --git a/TASMAN-core/src/main/java/it/inaf/ia2/tsm/datalayer/ADQL.java b/TASMAN-core/src/main/java/it/inaf/ia2/tsm/datalayer/ADQL.java index 830e6f7d2f84523a6818532f24467d07795b7466..3c15c03c7425ef3be2ca991bcc4762bb86f9de16 100644 --- a/TASMAN-core/src/main/java/it/inaf/ia2/tsm/datalayer/ADQL.java +++ b/TASMAN-core/src/main/java/it/inaf/ia2/tsm/datalayer/ADQL.java @@ -24,22 +24,23 @@ package it.inaf.ia2.tsm.datalayer; /** * ADQL data type constants. - * + * * @author Sonia Zorba {@literal <zorba at oats.inaf.it>} */ -public class ADQL { +public enum ADQL { - private static final String ADQL_PREFIX = "adql:"; - - public static final String INTEGER = ADQL_PREFIX + "INTEGER"; - public static final String SMALLINT = ADQL_PREFIX + "SMALLINT"; - public static final String BIGINT = ADQL_PREFIX + "BIGINT"; - public static final String REAL = ADQL_PREFIX + "REAL"; - public static final String CHAR = ADQL_PREFIX + "CHAR"; - public static final String VARCHAR = ADQL_PREFIX + "VARCHAR"; - public static final String TIMESTAMP = ADQL_PREFIX + "TIMESTAMP"; + INTEGER, + SMALLINT, + BIGINT, + REAL, + DOUBLE, + BOOLEAN, + CHAR, + CLOB, + VARCHAR, + TIMESTAMP; public static String getDataType(String dataType) { - return ADQL_PREFIX + dataType.toUpperCase(); + return dataType.toUpperCase(); } } diff --git a/TASMAN-core/src/main/java/it/inaf/ia2/tsm/datalayer/DBBroker.java b/TASMAN-core/src/main/java/it/inaf/ia2/tsm/datalayer/DBBroker.java index 0ead895e415b7931e784147dce815aaa1b65044d..e9b3543d3c5c6e4bd73b2b014bb7a80a6dd074ee 100644 --- a/TASMAN-core/src/main/java/it/inaf/ia2/tsm/datalayer/DBBroker.java +++ b/TASMAN-core/src/main/java/it/inaf/ia2/tsm/datalayer/DBBroker.java @@ -26,7 +26,7 @@ import it.inaf.ia2.tsm.Key; import it.inaf.ia2.tsm.TapSchema; import it.inaf.ia2.tsm.TapSchemaEntity; import it.inaf.ia2.tsm.model.TableModel; -import it.inaf.ia2.tsm.model.TapSchemaModel; +import it.inaf.ia2.tsm.model.SchemaModel; import java.sql.Connection; import java.sql.SQLException; import java.util.List; @@ -64,7 +64,9 @@ public interface DBBroker { void updateItem(String tapSchemaName, TapSchemaEntity entity, Connection conn, String whereCondition, Object... whereParams) throws SQLException; - void createTapSchemaStructure(String tapSchemaName, TapSchemaModel tapSchemaModel) throws SQLException; + void createTapSchemaStructure(String tapSchemaName, SchemaModel tapSchemaModel) throws SQLException; + void createIvoaSchemaStructure(SchemaModel ivoaSchemaModel) throws SQLException; + void save(TapSchema tapSchema) throws SQLException; } diff --git a/TASMAN-core/src/main/java/it/inaf/ia2/tsm/datalayer/DBBrokerTemplate.java b/TASMAN-core/src/main/java/it/inaf/ia2/tsm/datalayer/DBBrokerTemplate.java index 685b04e05f546b5ad64f42041481ff9eac42a005..3ee990c29a45061ddd7e8bc10dae3b23519f35ef 100644 --- a/TASMAN-core/src/main/java/it/inaf/ia2/tsm/datalayer/DBBrokerTemplate.java +++ b/TASMAN-core/src/main/java/it/inaf/ia2/tsm/datalayer/DBBrokerTemplate.java @@ -34,8 +34,9 @@ import it.inaf.ia2.tsm.TapSchemaEntity; import it.inaf.ia2.tsm.UpdateOperations; import it.inaf.ia2.tsm.model.PropertyModel; import it.inaf.ia2.tsm.model.TableModel; -import it.inaf.ia2.tsm.model.TapSchemaModel; -import it.inaf.ia2.tsm.model.TapSchemaModels; +import it.inaf.ia2.tsm.model.SchemaModel; +import it.inaf.ia2.tsm.model.SchemaModels; +import it.inaf.ia2.tsm.model.TypesMapping; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; @@ -45,7 +46,6 @@ import java.sql.Types; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; -import java.util.Iterator; import java.util.List; import java.util.Map; import javax.sql.DataSource; @@ -106,6 +106,10 @@ public abstract class DBBrokerTemplate implements DBBroker { return String.format("%s%s%s", escapeCharacter, name, escapeCharacter); } + protected void appendSize(StringBuilder sb, int size) { + sb.append(String.format("(%s)", size)); + } + protected abstract void createTable(String tapSchemaName, TableModel tableModel, Connection conn) throws SQLException; protected abstract String getAddPrimaryKeyQuery(String tapSchemaName, String tableName, String[] keyColumns); @@ -130,7 +134,7 @@ public abstract class DBBrokerTemplate implements DBBroker { } @Override - public void createTapSchemaStructure(String tapSchemaName, TapSchemaModel tapSchemaModel) throws SQLException { + public void createTapSchemaStructure(String tapSchemaName, SchemaModel tapSchemaModel) throws SQLException { try (Connection conn = dataSource.getConnection()) { @@ -164,6 +168,16 @@ public abstract class DBBrokerTemplate implements DBBroker { } } + @Override + public void createIvoaSchemaStructure(SchemaModel ivoaSchemaModel) throws SQLException { + try (Connection conn = dataSource.getConnection()) { + execute(getCreateDatabaseQuery(ivoaSchemaModel.getName()), conn); + for (TableModel tableModel : ivoaSchemaModel.getTables().values()) { + createTable(ivoaSchemaModel.getName(), tableModel, conn); + } + } + } + @Override public void save(TapSchema tapSchema) throws SQLException { @@ -416,7 +430,7 @@ public abstract class DBBrokerTemplate implements DBBroker { Map<String, Object> item = new HashMap<>(); for (PropertyModel pm : tableModel.getProperties().values()) { - Object value = TSMUtil.getObject(rs, pm.getName(), pm.getType()); + Object value = TSMUtil.getObject(rs, pm.getName(), pm.getJavaType()); item.put(pm.getName(), value); } @@ -487,9 +501,10 @@ public abstract class DBBrokerTemplate implements DBBroker { int i = 1; for (String key : tapSchemaItem.getPropertiesKeys()) { - Class type = tapSchemaItem.getTableModel().get(key).getType(); - Object value = tapSchemaItem.getValue(key, type); - statement.setObject(i, value, getSQLType(type)); + String adqlType = tapSchemaItem.getTableModel().get(key).getType(); + Class javaType = TypesMapping.getClassFromAdqlType(adqlType); + Object value = tapSchemaItem.getValue(key, javaType); + statement.setObject(i, value, getSQLType(javaType)); i++; if (values != null) { @@ -663,10 +678,7 @@ public abstract class DBBrokerTemplate implements DBBroker { keyColumnsColumns = getColumns(tapSchemaName, TapSchema.KEY_COLUMNS_TABLE, connection); } - Iterator<TapSchemaModel> ite = TapSchemaModels.getIterator(); - while (ite.hasNext()) { - TapSchemaModel tapSchemaModel = ite.next(); - + for (SchemaModel tapSchemaModel : SchemaModels.getTapSchemaModels()) { if (match(tapSchemaModel.get(TapSchema.SCHEMAS_TABLE), schemasColumns) && match(tapSchemaModel.get(TapSchema.TABLES_TABLE), tablesColumns) && match(tapSchemaModel.get(TapSchema.COLUMNS_TABLE), columnsColumns) diff --git a/TASMAN-core/src/main/java/it/inaf/ia2/tsm/datalayer/mysql/MySQLDBBroker.java b/TASMAN-core/src/main/java/it/inaf/ia2/tsm/datalayer/mysql/MySQLDBBroker.java index 4acabe5937d7a3065f19fbde14f921f1f3fa7bf2..4297b69debdaa861b4182fd8bb59c889d34be65e 100644 --- a/TASMAN-core/src/main/java/it/inaf/ia2/tsm/datalayer/mysql/MySQLDBBroker.java +++ b/TASMAN-core/src/main/java/it/inaf/ia2/tsm/datalayer/mysql/MySQLDBBroker.java @@ -29,6 +29,7 @@ import it.inaf.ia2.tsm.datalayer.ADQL; import it.inaf.ia2.tsm.datalayer.DBBrokerTemplate; import it.inaf.ia2.tsm.model.PropertyModel; import it.inaf.ia2.tsm.model.TableModel; +import it.inaf.ia2.tsm.model.TypesMapping; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; @@ -68,6 +69,17 @@ public class MySQLDBBroker extends DBBrokerTemplate { return String.format("SHOW COLUMNS FROM %s.%s", escape(schemaName), escape(tableName)); } + private Integer getSize(String typeWithSize) { + int beginIndex = typeWithSize.indexOf('('); + if (beginIndex != -1) { + int endIndex = typeWithSize.indexOf(')'); + if (endIndex != -1) { + Integer.parseInt(typeWithSize.substring(beginIndex + 1, endIndex)); + } + } + return null; + } + @Override public Map<String, Map<String, Object>> getAllColumnsMetadata(String schemaName, String tableName) throws SQLException { @@ -100,32 +112,8 @@ public class MySQLDBBroker extends DBBrokerTemplate { // Datatype and Size String type = resultSet.getString("Type").toLowerCase(); - String datatype; - Integer size = null; - - if (type.startsWith("int")) { - datatype = ADQL.INTEGER; - } else if (type.startsWith("smallint")) { - datatype = ADQL.SMALLINT; - } else if (type.startsWith("bigint")) { - datatype = ADQL.BIGINT; - } else if (type.startsWith("float")) { - datatype = ADQL.REAL; - } else if (type.startsWith("char")) { - int beginIndex = type.indexOf('('); - int endIndex = type.indexOf(')'); - size = Integer.parseInt(type.substring(beginIndex + 1, endIndex)); - datatype = ADQL.CHAR; - } else if (type.startsWith("varchar")) { - int beginIndex = type.indexOf('('); - int endIndex = type.indexOf(')'); - size = Integer.parseInt(type.substring(beginIndex + 1, endIndex)); - datatype = ADQL.VARCHAR; - } else if (type.contains("timestamp")) { - datatype = ADQL.TIMESTAMP; - } else { - datatype = ADQL.getDataType(type); - } + String datatype = TypesMapping.getADQLTypeFromMySQLType(type); + Integer size = getSize(type); cm.put(Column.DATATYPE_KEY, datatype); cm.put(Column.SIZE_KEY, size); @@ -160,17 +148,10 @@ public class MySQLDBBroker extends DBBrokerTemplate { querySb.append(pm.getName()); querySb.append(" "); - Class type = pm.getType(); - if (type == String.class) { - querySb.append("VARCHAR("); - querySb.append(pm.getSize()); - querySb.append(")"); - } else if (type == Integer.class || type == Boolean.class) { - querySb.append("INTEGER"); - } else if (type == Long.class) { - querySb.append("BIGINT"); - } else { - throw new UnsupportedOperationException("Column type " + type.getCanonicalName() + " not supported yet!"); + String mySQLType = TypesMapping.getMySQLTypeFromADQLType(pm.getType()); + querySb.append(mySQLType); + if (pm.getType().equals(ADQL.VARCHAR.name()) || pm.getType().equals(ADQL.CHAR.name())) { + appendSize(querySb, pm.getSize()); } if (pm.isNullable()) { diff --git a/TASMAN-core/src/main/java/it/inaf/ia2/tsm/datalayer/pgsql/PostgresDBBroker.java b/TASMAN-core/src/main/java/it/inaf/ia2/tsm/datalayer/pgsql/PostgresDBBroker.java index c114cd4e8caab84b57130b6152c7169b3610e7cb..6254a4cd7a931645338270e113ac2e0aa9766edd 100644 --- a/TASMAN-core/src/main/java/it/inaf/ia2/tsm/datalayer/pgsql/PostgresDBBroker.java +++ b/TASMAN-core/src/main/java/it/inaf/ia2/tsm/datalayer/pgsql/PostgresDBBroker.java @@ -29,6 +29,7 @@ import it.inaf.ia2.tsm.datalayer.ADQL; import it.inaf.ia2.tsm.datalayer.DBBrokerTemplate; import it.inaf.ia2.tsm.model.PropertyModel; import it.inaf.ia2.tsm.model.TableModel; +import it.inaf.ia2.tsm.model.TypesMapping; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; @@ -77,19 +78,11 @@ public class PostgresDBBroker extends DBBrokerTemplate { querySb.append(pm.getName()); querySb.append(" "); - Class type = pm.getType(); - if (type == String.class) { - querySb.append("character varying("); - querySb.append(pm.getSize()); - querySb.append(")"); - } else if (type == Integer.class) { - querySb.append("integer"); - } else if (type == Boolean.class) { - querySb.append("boolean"); - } else if (type == Long.class) { - querySb.append("bigint"); - } else { - throw new UnsupportedOperationException("Column type " + type.getCanonicalName() + " not supported yet!"); + String pgsqlType = TypesMapping.getPostgresSQLTypeFromADQLType(pm.getType()); + querySb.append(pgsqlType); + if (pm.getType().equals(ADQL.VARCHAR.name()) + || pm.getType().equals(ADQL.CHAR.name())) { + appendSize(querySb, pm.getSize()); } if (pm.isNullable()) { @@ -189,25 +182,9 @@ public class PostgresDBBroker extends DBBrokerTemplate { String datatype; String type = resultSet.getString("data_type"); - - if (type.startsWith("int")) { - datatype = ADQL.INTEGER; - } else if (type.startsWith("smallint")) { - datatype = ADQL.SMALLINT; - } else if (type.startsWith("bigint")) { - datatype = ADQL.BIGINT; - } else if (type.startsWith("double") || type.startsWith("real")) { - datatype = ADQL.REAL; - } else if (type.startsWith("character varying")) { - datatype = ADQL.VARCHAR; - size = resultSet.getInt("character_maximum_length"); - } else if (type.startsWith("char")) { - datatype = ADQL.CHAR; + datatype = TypesMapping.getADQLTypeFromPostgresType(type); + if (datatype.equals(ADQL.VARCHAR.name()) || datatype.equals(ADQL.CHAR.name())) { size = resultSet.getInt("character_maximum_length"); - } else if (type.contains("timestamp")) { - datatype = ADQL.TIMESTAMP; - } else { - datatype = ADQL.getDataType(type); } cm.put(Column.DATATYPE_KEY, datatype); diff --git a/TASMAN-core/src/main/java/it/inaf/ia2/tsm/model/PropertyModel.java b/TASMAN-core/src/main/java/it/inaf/ia2/tsm/model/PropertyModel.java index 393b337839db212845b45e967779266fec3cb8d9..95228f1c574d51d7dab9670f0da022479a720937 100644 --- a/TASMAN-core/src/main/java/it/inaf/ia2/tsm/model/PropertyModel.java +++ b/TASMAN-core/src/main/java/it/inaf/ia2/tsm/model/PropertyModel.java @@ -22,91 +22,122 @@ */ package it.inaf.ia2.tsm.model; -import java.io.Serializable; +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlTransient; /** * * @author Sonia Zorba {@literal <zorba at oats.inaf.it>} */ -public class PropertyModel implements Serializable { - - private static final long serialVersionUID = -3081516272534689428L; +public class PropertyModel { private String name; - private String description; - private Class type; + private String type; private Integer size; private boolean updatable; private boolean nullable; + private boolean standard; + private String defaultValueString; private String loaderKey; - private Object defaultValue; - - private PropertyModel() { - } - - public PropertyModel(PropertyXMLModel propertyXMLModel) { - try { - name = propertyXMLModel.getName(); - description = propertyXMLModel.getDescription(); - type = Class.forName(propertyXMLModel.getType()); - size = propertyXMLModel.getSize(); - updatable = propertyXMLModel.isUpdatable(); - nullable = propertyXMLModel.isNullable(); - loaderKey = propertyXMLModel.getLoaderKey(); - if (propertyXMLModel.getDefaultValue() != null) { - String strDefVal = propertyXMLModel.getDefaultValue(); - if (type == String.class) { - defaultValue = strDefVal; - } else if (type == Integer.class) { - defaultValue = Integer.parseInt(strDefVal); - } else if (type == Double.class) { - defaultValue = Double.parseDouble(strDefVal); - } else if (type == Long.class) { - defaultValue = Long.parseLong(strDefVal); - } else if (type == Float.class) { - defaultValue = Float.parseFloat(strDefVal); - } else if (type == Boolean.class) { - defaultValue = Boolean.parseBoolean(strDefVal); - } else { - throw new UnsupportedOperationException("Default value for type " + type.getCanonicalName() + " not supported yet."); - } - } - } catch (ClassNotFoundException e) { - throw new ExceptionInInitializerError( - "Invalid property type for property " + propertyXMLModel.getName() - + ". ClassNotFoundException: " + propertyXMLModel.getType()); - } + private String description; + + public PropertyModel() { + // default values + updatable = true; + nullable = true; } + @XmlElement(name = "name") public String getName() { return name; } - public String getDescription() { - return description; + public void setName(String name) { + this.name = name; } - public Class getType() { + @XmlElement(name = "type") + public String getType() { return type; } + public void setType(String type) { + this.type = type; + } + + @XmlElement(name = "size") public Integer getSize() { return size; } + public void setSize(Integer size) { + this.size = size; + } + + @XmlElement(name = "updatable", defaultValue = "true") public boolean isUpdatable() { return updatable; } + public void setUpdatable(boolean updatable) { + this.updatable = updatable; + } + + @XmlElement(name = "nullable", defaultValue = "true") public boolean isNullable() { return nullable; } + public void setNullable(boolean nullable) { + this.nullable = nullable; + } + + @XmlElement(name = "standard", defaultValue = "false") + public boolean isStandard() { + return standard; + } + + public void setStandard(boolean standard) { + this.standard = standard; + } + + @XmlElement(name = "default-value") + public String getDefaultValueString() { + return defaultValueString; + } + + public void setDefaultValueString(String defaultValue) { + this.defaultValueString = defaultValue; + } + + @XmlElement(name = "key") public String getLoaderKey() { return loaderKey; } + public void setLoaderKey(String loaderMethod) { + this.loaderKey = loaderMethod; + } + + @XmlElement(name = "description") + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + @XmlTransient public Object getDefaultValue() { - return defaultValue; + if (defaultValueString == null) { + return null; + } + return TypesMapping.parseDefaultValue(defaultValueString, getType()); + } + + @XmlTransient + public Class getJavaType() { + return TypesMapping.getClassFromAdqlType(getType()); } } diff --git a/TASMAN-core/src/main/java/it/inaf/ia2/tsm/model/PropertyXMLModel.java b/TASMAN-core/src/main/java/it/inaf/ia2/tsm/model/PropertyXMLModel.java deleted file mode 100644 index 878c761efaa8bd651f44980bee799ba6cadd30d7..0000000000000000000000000000000000000000 --- a/TASMAN-core/src/main/java/it/inaf/ia2/tsm/model/PropertyXMLModel.java +++ /dev/null @@ -1,119 +0,0 @@ -/* - * _____________________________________________________________________________ - * - * 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.model; - -import javax.xml.bind.annotation.XmlElement; - -/** - * - * @author Sonia Zorba {@literal <zorba at oats.inaf.it>} - */ -public class PropertyXMLModel { - - private String name; - private String type; - private Integer size; - private boolean updatable; - private boolean nullable; - private String defaultValue; - private String loaderKey; - private String description; - - public PropertyXMLModel() { - // default values - updatable = true; - nullable = true; - } - - @XmlElement(name = "name") - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - @XmlElement(name = "type") - public String getType() { - return type; - } - - public void setType(String type) { - this.type = type; - } - - @XmlElement(name = "size") - public Integer getSize() { - return size; - } - - public void setSize(Integer size) { - this.size = size; - } - - @XmlElement(name = "updatable", defaultValue = "true") - public boolean isUpdatable() { - return updatable; - } - - public void setUpdatable(boolean updatable) { - this.updatable = updatable; - } - - @XmlElement(name = "nullable", defaultValue = "true") - public boolean isNullable() { - return nullable; - } - - public void setNullable(boolean nullable) { - this.nullable = nullable; - } - - @XmlElement(name = "default-value") - public String getDefaultValue() { - return defaultValue; - } - - public void setDefaultValue(String defaultValue) { - this.defaultValue = defaultValue; - } - - @XmlElement(name = "key") - public String getLoaderKey() { - return loaderKey; - } - - public void setLoaderKey(String loaderMethod) { - this.loaderKey = loaderMethod; - } - - @XmlElement(name = "description") - public String getDescription() { - return description; - } - - public void setDescription(String description) { - this.description = description; - } -} diff --git a/TASMAN-core/src/main/java/it/inaf/ia2/tsm/model/TapSchemaModel.java b/TASMAN-core/src/main/java/it/inaf/ia2/tsm/model/SchemaModel.java similarity index 83% rename from TASMAN-core/src/main/java/it/inaf/ia2/tsm/model/TapSchemaModel.java rename to TASMAN-core/src/main/java/it/inaf/ia2/tsm/model/SchemaModel.java index 9760e1b5dbdd63667385ba16a3a9d016d243da49..59499c0bb23ead0f00c185181b6d158f0823a82d 100644 --- a/TASMAN-core/src/main/java/it/inaf/ia2/tsm/model/TapSchemaModel.java +++ b/TASMAN-core/src/main/java/it/inaf/ia2/tsm/model/SchemaModel.java @@ -30,22 +30,28 @@ import java.util.Map; * * @author Sonia Zorba {@literal <zorba at oats.inaf.it>} */ -public class TapSchemaModel implements Serializable { +public class SchemaModel implements Serializable { private static final long serialVersionUID = 1087876778125446000L; private final Map<String, TableModel> tables; + private String name; private String version; private String description; - private TapSchemaModel() { + private SchemaModel() { tables = new HashMap<>(); } - public TapSchemaModel(String version, String description) { + public SchemaModel(SchemaXMLModel xmlModel) { this(); - this.version = version; - this.description = description; + this.name = xmlModel.getName(); + this.version = xmlModel.getVersion(); + this.description = xmlModel.getDescription(); + } + + public String getName() { + return name; } public String getVersion() { diff --git a/TASMAN-core/src/main/java/it/inaf/ia2/tsm/model/SchemaModels.java b/TASMAN-core/src/main/java/it/inaf/ia2/tsm/model/SchemaModels.java new file mode 100644 index 0000000000000000000000000000000000000000..192c5136f036b79a4a5b4beb4d11e6ed608d57be --- /dev/null +++ b/TASMAN-core/src/main/java/it/inaf/ia2/tsm/model/SchemaModels.java @@ -0,0 +1,166 @@ +/* + * _____________________________________________________________________________ + * + * 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.model; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Properties; +import javax.xml.bind.JAXB; + +/** + * + * @author Sonia Zorba {@literal <zorba at oats.inaf.it>} + */ +public class SchemaModels { + + private static final String SCHEMA_DEFINITION_FOLDER = "schema_definition"; + private static final String TAP_SCHEMA_NAME = "tap_schema"; + private static final String IVOA_SCHEMA_NAME = "ivoa"; + + // Map keys are version numbers + private static final Map<String, SchemaModel> TAP_SCHEMA_MODELS; + private static final Map<String, SchemaModel> IVOA_SCHEMA_MODELS; + + private SchemaModels() { + } + + static { + try { + String[] xmlModelFileNames = getXMLModelFileNames(); + List<SchemaXMLModel> xmlModels = getXmlModels(xmlModelFileNames); + + Map<String, SchemaXMLModel> tapSchemaXmlModelsMap = getXmlModelMapByVersionAndName(xmlModels, TAP_SCHEMA_NAME); + Map<String, SchemaXMLModel> ivoaSchemaXmlModelsMap = getXmlModelMapByVersionAndName(xmlModels, IVOA_SCHEMA_NAME); + + TAP_SCHEMA_MODELS = getModelMapByVersion(tapSchemaXmlModelsMap); + IVOA_SCHEMA_MODELS = getModelMapByVersion(ivoaSchemaXmlModelsMap); + } catch (IOException e) { + throw new ExceptionInInitializerError(e); + } + } + + private static String[] getXMLModelFileNames() throws IOException { + try (InputStream in = SchemaModels.class.getClassLoader().getResourceAsStream("core.properties")) { + + Properties props = new Properties(); + props.load(in); + + String[] fileNames = props.getProperty("schema_definition_files").split(","); + + String[] modelFiles = new String[fileNames.length]; + for (int i = 0; i < fileNames.length; i++) { + modelFiles[i] = String.format("%s%s%s", SCHEMA_DEFINITION_FOLDER, File.separator, fileNames[i]); + } + + return modelFiles; + } + } + + private static List<SchemaXMLModel> getXmlModels(String[] xmlModelFileNames) throws IOException { + List<SchemaXMLModel> xmlModels = new ArrayList<>(); + + for (String modelFile : xmlModelFileNames) { + try (InputStream in = SchemaModels.class.getClassLoader().getResourceAsStream(modelFile)) { + SchemaXMLModel model = JAXB.unmarshal(in, SchemaXMLModel.class); + xmlModels.add(model); + } + } + + return xmlModels; + } + + private static void loadSchemaModel(SchemaModel model, SchemaXMLModel xmlModel, Map<String, SchemaXMLModel> xmlModels) { + + for (TableXMLModel tableXmlModel : xmlModel.getTables()) { + String tableName = tableXmlModel.getName(); + TableModel tableModel = model.get(tableName); + if (tableModel == null) { + tableModel = new TableModel(tableXmlModel); + } + for (PropertyModel property : tableXmlModel.getAdd()) { + tableModel.getProperties().put(property.getName(), property); + } + model.getTables().put(tableName, tableModel); + } + + if (xmlModel.getExtendsFrom() != null) { + SchemaXMLModel parentModel = xmlModels.get(xmlModel.getExtendsFrom()); + loadSchemaModel(model, parentModel, xmlModels); + } + } + + private static Map<String, SchemaXMLModel> getXmlModelMapByVersionAndName(List<SchemaXMLModel> xmlModels, String schemaName) { + Map<String, SchemaXMLModel> map = new HashMap<>(); + + for (SchemaXMLModel xmlModel : xmlModels) { + if (schemaName.equals(xmlModel.getName())) { + map.put(xmlModel.getVersion(), xmlModel); + } + } + + return map; + } + + private static Map<String, SchemaModel> getModelMapByVersion(Map<String, SchemaXMLModel> xmlModels) { + Map<String, SchemaModel> map = new HashMap<>(); + for (SchemaXMLModel xmlModel : xmlModels.values()) { + SchemaModel model = new SchemaModel(xmlModel); + loadSchemaModel(model, xmlModel, xmlModels); + map.put(model.getVersion(), model); + } + return map; + } + + public static SchemaModel getTapSchemaModel(String version) { + return TAP_SCHEMA_MODELS.get(version); + } + + public static Iterable<SchemaModel> getTapSchemaModels() { + return TAP_SCHEMA_MODELS.values(); + } + + public static TableModel getTapSchemaTableModel(String tableName, String version) { + return getTapSchemaModel(version).getTables().get(tableName); + } + + public static List<String> getAvailableTapSchemaVersions() { + List<String> versions = new ArrayList<>(); + for (SchemaModel tapSchemaModel : TAP_SCHEMA_MODELS.values()) { + versions.add(tapSchemaModel.getVersion()); + } + return versions; + } + + public static SchemaModel getIvoaSchemaModel(String version) { + return IVOA_SCHEMA_MODELS.get(version); + } + + public static Iterable<SchemaModel> getIvoaSchemaModels() { + return IVOA_SCHEMA_MODELS.values(); + } +} diff --git a/TASMAN-core/src/main/java/it/inaf/ia2/tsm/model/TapSchemaXMLModel.java b/TASMAN-core/src/main/java/it/inaf/ia2/tsm/model/SchemaXMLModel.java similarity index 90% rename from TASMAN-core/src/main/java/it/inaf/ia2/tsm/model/TapSchemaXMLModel.java rename to TASMAN-core/src/main/java/it/inaf/ia2/tsm/model/SchemaXMLModel.java index dc561f889d9dff55ef4eff907f2cb385e83975de..6f7e1103d917a4f5b0c45f9e8ec27fb68b72caa4 100644 --- a/TASMAN-core/src/main/java/it/inaf/ia2/tsm/model/TapSchemaXMLModel.java +++ b/TASMAN-core/src/main/java/it/inaf/ia2/tsm/model/SchemaXMLModel.java @@ -32,14 +32,25 @@ import javax.xml.bind.annotation.XmlRootElement; * * @author Sonia Zorba {@literal <zorba at oats.inaf.it>} */ -@XmlRootElement(name = "tap_schema") -public class TapSchemaXMLModel { +@XmlRootElement(name = "schema") +public class SchemaXMLModel { + + private String name; private String version; private String description; private String extendsFrom; private List<TableXMLModel> tables; + @XmlAttribute(name = "name") + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + @XmlAttribute(name = "version") public String getVersion() { return version; @@ -57,7 +68,7 @@ public class TapSchemaXMLModel { public void setDescription(String description) { this.description = description; } - + @XmlAttribute(name = "extends") public String getExtendsFrom() { return extendsFrom; diff --git a/TASMAN-core/src/main/java/it/inaf/ia2/tsm/model/TableModel.java b/TASMAN-core/src/main/java/it/inaf/ia2/tsm/model/TableModel.java index 892972a55d24fdc291d155fdadd0ec564ea8d4d5..b926357a800651afab12df019494012eeacbb17d 100644 --- a/TASMAN-core/src/main/java/it/inaf/ia2/tsm/model/TableModel.java +++ b/TASMAN-core/src/main/java/it/inaf/ia2/tsm/model/TableModel.java @@ -42,10 +42,10 @@ public class TableModel implements Serializable { this.properties = new HashMap<>(); } - public TableModel(String name, String description) { + public TableModel(TableXMLModel xmlModel) { this(); - this.name = name; - this.description = description; + this.name = xmlModel.getName(); + this.description = xmlModel.getDescription(); } public String getName() { diff --git a/TASMAN-core/src/main/java/it/inaf/ia2/tsm/model/TableXMLModel.java b/TASMAN-core/src/main/java/it/inaf/ia2/tsm/model/TableXMLModel.java index 17848aa4aa6369aacee049f5819d5ea4e9970144..090899436592e279cc831f60bc1ab3f933fe9927 100644 --- a/TASMAN-core/src/main/java/it/inaf/ia2/tsm/model/TableXMLModel.java +++ b/TASMAN-core/src/main/java/it/inaf/ia2/tsm/model/TableXMLModel.java @@ -35,7 +35,7 @@ public class TableXMLModel { private String name; private String description; - private List<PropertyXMLModel> add; + private List<PropertyModel> add; @XmlAttribute(name = "name") public String getName() { @@ -57,11 +57,11 @@ public class TableXMLModel { @XmlElementWrapper(name = "add") @XmlElement(name = "property") - public List<PropertyXMLModel> getAdd() { + public List<PropertyModel> getAdd() { return add; } - public void setAdd(List<PropertyXMLModel> add) { + public void setAdd(List<PropertyModel> add) { this.add = add; } } diff --git a/TASMAN-core/src/main/java/it/inaf/ia2/tsm/model/TapSchemaModels.java b/TASMAN-core/src/main/java/it/inaf/ia2/tsm/model/TapSchemaModels.java deleted file mode 100644 index 3f4f817eafc298161c99a82a1fdad25e374de508..0000000000000000000000000000000000000000 --- a/TASMAN-core/src/main/java/it/inaf/ia2/tsm/model/TapSchemaModels.java +++ /dev/null @@ -1,117 +0,0 @@ -/* - * _____________________________________________________________________________ - * - * 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.model; - -import java.io.IOException; -import java.io.InputStream; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.Iterator; -import java.util.List; -import java.util.Map; -import javax.xml.bind.JAXB; - -/** - * - * @author Sonia Zorba {@literal <zorba at oats.inaf.it>} - */ -public class TapSchemaModels { - - // Map keys are TS versions - private final static Map<String, TapSchemaModel> MODELS; - - private TapSchemaModels() { - } - - static { - - try { - Map<String, TapSchemaXMLModel> xmlModels = getXmlModels(); - - MODELS = new HashMap<>(); - - for (TapSchemaXMLModel xmlModel : xmlModels.values()) { - TapSchemaModel model = new TapSchemaModel(xmlModel.getVersion(), xmlModel.getDescription()); - loadTapSchemaModel(model, xmlModel, xmlModels); - MODELS.put(model.getVersion(), model); - } - } catch (IOException e) { - throw new ExceptionInInitializerError(e); - } - } - - private static Map<String, TapSchemaXMLModel> getXmlModels() throws IOException { - Map<String, TapSchemaXMLModel> xmlModels = new HashMap<>(); - - for (String modelFile : Tasman.XML_MODEL_FILES) { - try (InputStream in = TapSchemaModels.class.getClassLoader().getResourceAsStream(modelFile)) { - TapSchemaXMLModel model = JAXB.unmarshal(in, TapSchemaXMLModel.class); - xmlModels.put(model.getVersion(), model); - } - } - - return xmlModels; - } - - private static void loadTapSchemaModel(TapSchemaModel model, TapSchemaXMLModel xmlModel, Map<String, TapSchemaXMLModel> xmlModels) { - - for (TableXMLModel tableXmlModel : xmlModel.getTables()) { - String tableName = tableXmlModel.getName(); - TableModel tableModel = model.get(tableName); - if (tableModel == null) { - tableModel = new TableModel(tableName, tableXmlModel.getDescription()); - } - for (PropertyXMLModel property : tableXmlModel.getAdd()) { - tableModel.getProperties().put(property.getName(), new PropertyModel(property)); - } - model.getTables().put(tableName, tableModel); - } - - if (xmlModel.getExtendsFrom() != null) { - TapSchemaXMLModel parentModel = xmlModels.get(xmlModel.getExtendsFrom()); - loadTapSchemaModel(model, parentModel, xmlModels); - } - } - - public static TapSchemaModel getTapSchemaModel(String version) { - return MODELS.get(version); - } - - public static Iterator<TapSchemaModel> getIterator() { - return MODELS.values().iterator(); - } - - public static TableModel getTableModel(String tableName, String version) { - return getTapSchemaModel(version).getTables().get(tableName); - } - - public static List<String> getAvailableVersions() { - List<String> versions = new ArrayList<>(); - Iterator<TapSchemaModel> ite = getIterator(); - while (ite.hasNext()) { - TapSchemaModel tapSchemaModel = ite.next(); - versions.add(tapSchemaModel.getVersion()); - } - return versions; - } -} diff --git a/TASMAN-core/src/main/java/it/inaf/ia2/tsm/model/TypeMapping.java b/TASMAN-core/src/main/java/it/inaf/ia2/tsm/model/TypeMapping.java new file mode 100644 index 0000000000000000000000000000000000000000..e7d68f3b6f6470f97353f2a95f0bca5555b53122 --- /dev/null +++ b/TASMAN-core/src/main/java/it/inaf/ia2/tsm/model/TypeMapping.java @@ -0,0 +1,83 @@ +/* + * _____________________________________________________________________________ + * + * 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.model; + +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlTransient; + +/** + * + * @author Sonia Zorba {@literal <zorba at oats.inaf.it>} + */ +public class TypeMapping { + + private String adqlType; + private String mysqlType; + private String pgsqlType; + private String javaTypeString; + + @XmlElement(name = "adql") + public String getAdqlType() { + return adqlType; + } + + public void setAdqlType(String adqlType) { + this.adqlType = adqlType; + } + + @XmlElement(name = "mysql") + public String getMysqlType() { + return mysqlType; + } + + public void setMysqlType(String mysqlType) { + this.mysqlType = mysqlType; + } + + @XmlElement(name = "pgsql") + public String getPgsqlType() { + return pgsqlType; + } + + public void setPgsqlType(String pgsqlType) { + this.pgsqlType = pgsqlType; + } + + @XmlElement(name = "java") + public String getJavaTypeString() { + return javaTypeString; + } + + public void setJavaTypeString(String javaTypeString) { + this.javaTypeString = javaTypeString; + } + + @XmlTransient + public Class getJavaType() { + try { + return Class.forName(javaTypeString); + } catch (ClassNotFoundException ex) { + throw new RuntimeException(ex); + } + } +} diff --git a/TASMAN-core/src/main/java/it/inaf/ia2/tsm/model/TypesMapping.java b/TASMAN-core/src/main/java/it/inaf/ia2/tsm/model/TypesMapping.java new file mode 100644 index 0000000000000000000000000000000000000000..487c6472ce902cbffda75c0853600a6f2245ef6e --- /dev/null +++ b/TASMAN-core/src/main/java/it/inaf/ia2/tsm/model/TypesMapping.java @@ -0,0 +1,134 @@ +/* + * _____________________________________________________________________________ + * + * 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.model; + +import it.inaf.ia2.tsm.datalayer.ADQL; +import java.io.IOException; +import java.io.InputStream; +import java.util.List; +import javax.xml.bind.JAXB; +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlElementWrapper; +import javax.xml.bind.annotation.XmlElements; +import javax.xml.bind.annotation.XmlRootElement; + +/** + * + * @author Sonia Zorba {@literal <zorba at oats.inaf.it>} + */ +public class TypesMapping { + + @XmlRootElement(name = "sql_type_mapping") + static class TypesWrapper { + + private List<TypeMapping> types; + + @XmlElements({ + @XmlElement(name = "type") + }) + public List<TypeMapping> getTypes() { + return types; + } + + public void setTypes(List<TypeMapping> types) { + this.types = types; + } + } + + private static final List<TypeMapping> TYPES; + + static { + try (InputStream is = TypeMapping.class.getClassLoader().getResourceAsStream("sql_type_mapping.xml")) { + + TypesWrapper typesWrapper = JAXB.unmarshal(is, TypesWrapper.class); + TYPES = typesWrapper.types; + } catch (IOException e) { + throw new ExceptionInInitializerError(e); + } + } + + private static TypeMapping getTypeMappingFromADQLType(String adqlType) { + for (TypeMapping typeMapping : TYPES) { + if (adqlType.equals(typeMapping.getAdqlType())) { + return typeMapping; + } + } + throw new IllegalArgumentException("Unable to retrieve mapping for ADQL type " + adqlType); + } + + public static Class getClassFromAdqlType(String adqlType) { + return getTypeMappingFromADQLType(adqlType).getJavaType(); + } + + public static String getMySQLTypeFromADQLType(String adqlType) { + return getTypeMappingFromADQLType(adqlType).getMysqlType(); + } + + public static String getPostgresSQLTypeFromADQLType(String adqlType) { + return getTypeMappingFromADQLType(adqlType).getPgsqlType(); + } + + public static String getADQLTypeFromPostgresType(String pgsqlType) { + for (TypeMapping typeMapping : TYPES) { + if (typeMapping.getPgsqlType().startsWith(pgsqlType)) { + return typeMapping.getAdqlType(); + } + } + return ADQL.getDataType(pgsqlType); + } + + public static String getADQLTypeFromMySQLType(String mysqlType) { + for (TypeMapping typeMapping : TYPES) { + if (typeMapping.getPgsqlType().startsWith(mysqlType)) { + return typeMapping.getAdqlType(); + } + } + return ADQL.getDataType(mysqlType); + } + + public static Object parseDefaultValue(String defaultValue, String type) { + return parseDefaultValue(defaultValue, ADQL.valueOf(type)); + } + + public static Object parseDefaultValue(String defaultValue, ADQL type) { + + switch (type) { + case VARCHAR: + case CHAR: + return defaultValue; + case INTEGER: + case SMALLINT: + return Integer.parseInt(defaultValue); + case BIGINT: + return Long.parseLong(defaultValue); + case REAL: + return Float.parseFloat(defaultValue); + case DOUBLE: + return Double.parseDouble(defaultValue); + case BOOLEAN: + return Boolean.parseBoolean(defaultValue); + default: + throw new UnsupportedOperationException("Default value for type " + type + " not supported yet."); + } + } +} diff --git a/TASMAN-core/src/main/resources/core.properties b/TASMAN-core/src/main/resources/core.properties index c6d53afa6e9f6528c3a0821b276af2fb7885a10b..6defa839f02585e6060b3f543af51bce3549f8a7 100644 --- a/TASMAN-core/src/main/resources/core.properties +++ b/TASMAN-core/src/main/resources/core.properties @@ -1,7 +1,6 @@ ## Configuration properties ## -# List of supported TAP_SCHEMA models. These comma separated values are XML file -# suffixes for model configurations. +# Comma separated list of files inside schema_definition folder. # (It is necessary to put this list here because, unfortunately, there is no -# easy way to read an entire resource folder using the ClassLoader). -models = 1,1-IA2,1_1 +# easy way to read an entire resource folder using the ClassLoader API). +schema_definition_files = ivoa-1_1.xml,tap_schema-1-IA2.xml,tap_schema-1.xml,tap_schema-1_1.xml diff --git a/TASMAN-core/src/main/resources/schema_definition/ivoa-1_1.xml b/TASMAN-core/src/main/resources/schema_definition/ivoa-1_1.xml new file mode 100644 index 0000000000000000000000000000000000000000..9a5c96dcd4e8907d4ccff60894e38d78b47fcc8f --- /dev/null +++ b/TASMAN-core/src/main/resources/schema_definition/ivoa-1_1.xml @@ -0,0 +1,236 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- +_____________________________________________________________________________ + +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. +--> +<schema name="ivoa" version="1.1"> + <table name="obscore"> + <add> + <property> + <name>dataproduct_type</name> + <type>VARCHAR</type> + <size>255</size> + <updatable>true</updatable> + <description>Logical data product type (image etc.)</description> + </property> + <property> + <name>calib_level</name> + <type>INTEGER</type> + <updatable>true</updatable> + <nullable>false</nullable> + <description>Calibration level {0, 1, 2, 3, 4}</description> + </property> + <property> + <name>obs_collection</name> + <type>VARCHAR</type> + <size>255</size> + <updatable>true</updatable> + <nullable>false</nullable> + <description>Name of the data collection</description> + </property> + <property> + <name>obs_id</name> + <type>VARCHAR</type> + <size>255</size> + <updatable>true</updatable> + <nullable>false</nullable> + <description>Observation ID</description> + </property> + <property> + <name>obs_publisher_did</name> + <type>VARCHAR</type> + <size>255</size> + <updatable>true</updatable> + <nullable>false</nullable> + <description>Dataset identifier given by the publisher</description> + </property> + <property> + <name>access_url</name> + <type>VARCHAR</type> + <size>255</size> + <updatable>true</updatable> + <description>URL used to access (download) dataset</description> + </property> + <property> + <name>access_format</name> + <type>VARCHAR</type> + <size>255</size> + <updatable>true</updatable> + <description>File content format</description> + </property> + <property> + <name>access_estsize</name> + <type>INTEGER</type> + <updatable>true</updatable> + <description>Estimated size of dataset in kilo bytes</description> + <unit>kbyte</unit> + </property> + <property> + <name>target_name</name> + <type>VARCHAR</type> + <size>255</size> + <updatable>true</updatable> + <description>Astronomical object observed, if any</description> + </property> + <property> + <name>s_ra</name> + <type>DOUBLE</type> + <updatable>true</updatable> + <description>Central right ascension, ICRS</description> + <unit>deg</unit> + </property> + <property> + <name>s_dec</name> + <type>DOUBLE</type> + <updatable>true</updatable> + <description>Central declination, ICRS</description> + <unit>deg</unit> + </property> + <property> + <name>s_fov</name> + <type>DOUBLE</type> + <updatable>true</updatable> + <description>Diameter (bounds) of the covered region</description> + <unit>deg</unit> + </property> + <property> + <name>s_region</name> + <type>VARCHAR</type> + <size>255</size> + <updatable>true</updatable> + <description>Sky region covered by the data product (expressed in ICRS frame)</description> + </property> + <property> + <name>s_xel1</name> + <type>INTEGER</type> + <updatable>true</updatable> + <description>Number of elements along the first spatial axis</description> + </property> + <property> + <name>s_xel2</name> + <type>INTEGER</type> + <updatable>true</updatable> + <description>Number of elements along the second spatial axis</description> + </property> + <property> + <name>s_resolution</name> + <type>DOUBLE</type> + <updatable>true</updatable> + <description>Spatial resolution of data as FWHM</description> + <unit>arcsec</unit> + </property> + <property> + <name>t_min</name> + <type>DOUBLE</type> + <updatable>true</updatable> + <description>Start time in MJD</description> + <unit>d</unit> + </property> + <property> + <name>t_max</name> + <type>DOUBLE</type> + <updatable>true</updatable> + <description>Stop time in MJD</description> + <unit>d</unit> + </property> + <property> + <name>t_exptime</name> + <type>DOUBLE</type> + <updatable>true</updatable> + <description>Total exposure time</description> + <unit>s</unit> + </property> + <property> + <name>t_resolution</name> + <type>DOUBLE</type> + <updatable>true</updatable> + <description>Temporal resolution FWHM</description> + <unit>s</unit> + </property> + <property> + <name>t_xel</name> + <type>INTEGER</type> + <updatable>true</updatable> + <description>Number of elements along the time axis</description> + </property> + <property> + <name>em_min</name> + <type>DOUBLE</type> + <updatable>true</updatable> + <description>Start in spectral coordinates</description> + <unit>m</unit> + </property> + <property> + <name>em_max</name> + <type>DOUBLE</type> + <updatable>true</updatable> + <description>Stop in spectral coordinates</description> + <unit>m</unit> + </property> + <property> + <name>em_res_power</name> + <type>DOUBLE</type> + <updatable>true</updatable> + <description>Spectral resolving power</description> + </property> + <property> + <name>em_xel</name> + <type>INTEGER</type> + <updatable>true</updatable> + <description>Number of elements along the spectral axis</description> + </property> + <property> + <name>o_ucd</name> + <type>VARCHAR</type> + <size>255</size> + <updatable>true</updatable> + <description>UCD of observable (e.g. phot.flux.density, phot.count, etc.)</description> + </property> + <property> + <name>pol_states</name> + <type>VARCHAR</type> + <size>255</size> + <updatable>true</updatable> + <description>List of polarization states or NULL if not applicable</description> + </property> + <property> + <name>pol_xel</name> + <type>INTEGER</type> + <updatable>true</updatable> + <description>Number of polarization samples</description> + </property> + <property> + <name>facility_name</name> + <type>VARCHAR</type> + <size>255</size> + <updatable>true</updatable> + <description>Name of the facility used for this observation</description> + </property> + <property> + <name>instrument_name</name> + <type>VARCHAR</type> + <size>255</size> + <updatable>true</updatable> + <description>Name of the instrument used for this observation</description> + </property> + </add> + </table> +</schema> diff --git a/TASMAN-core/src/main/resources/tap_schema/tap_schema-1-IA2.xml b/TASMAN-core/src/main/resources/schema_definition/tap_schema-1-IA2.xml similarity index 79% rename from TASMAN-core/src/main/resources/tap_schema/tap_schema-1-IA2.xml rename to TASMAN-core/src/main/resources/schema_definition/tap_schema-1-IA2.xml index 40f909f1f3b2e9b69d90e2532bc8f97bc00fd72e..148c41ea875f60b2737bb9b25516dd104c74d68c 100644 --- a/TASMAN-core/src/main/resources/tap_schema/tap_schema-1-IA2.xml +++ b/TASMAN-core/src/main/resources/schema_definition/tap_schema-1-IA2.xml @@ -21,13 +21,14 @@ 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. --> -<tap_schema version="1.0-IA2" extends="1.0"> +<schema name="tap_schema" version="1.0-IA2" extends="1.0"> <table name="schemas"> <add> <property> <name>schemaID</name> - <type>java.lang.Long</type> + <type>BIGINT</type> <updatable>true</updatable> + <standard>false</standard> </property> </add> </table> @@ -35,8 +36,9 @@ Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. <add> <property> <name>tableID</name> - <type>java.lang.Long</type> + <type>BIGINT</type> <updatable>true</updatable> + <standard>false</standard> </property> </add> </table> @@ -44,13 +46,15 @@ Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. <add> <property> <name>columnID</name> - <type>java.lang.Long</type> + <type>BIGINT</type> <updatable>true</updatable> + <standard>false</standard> </property> <property> <name>id</name> - <type>java.lang.Integer</type> + <type>INTEGER</type> <updatable>true</updatable> + <standard>false</standard> </property> </add> </table> @@ -58,8 +62,9 @@ Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. <add> <property> <name>keyID</name> - <type>java.lang.Long</type> + <type>BIGINT</type> <updatable>true</updatable> + <standard>false</standard> </property> </add> </table> @@ -67,9 +72,10 @@ Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. <add> <property> <name>key_columnID</name> - <type>java.lang.Long</type> + <type>BIGINT</type> <updatable>true</updatable> + <standard>false</standard> </property> </add> </table> -</tap_schema> +</schema> diff --git a/TASMAN-core/src/main/resources/tap_schema/tap_schema-1.xml b/TASMAN-core/src/main/resources/schema_definition/tap_schema-1.xml similarity index 79% rename from TASMAN-core/src/main/resources/tap_schema/tap_schema-1.xml rename to TASMAN-core/src/main/resources/schema_definition/tap_schema-1.xml index 9fabe2eb7f1ea7c4ae293044537cb507c31c439c..e8594537ba8db3cbdb3806734d487a768f390d58 100644 --- a/TASMAN-core/src/main/resources/tap_schema/tap_schema-1.xml +++ b/TASMAN-core/src/main/resources/schema_definition/tap_schema-1.xml @@ -21,31 +21,34 @@ 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. --> -<tap_schema version="1.0" description="a special schema to describe a TAP tableset"> +<schema name="tap_schema" version="1.0" description="a special schema to describe a TAP tableset"> <table name="schemas" description="description of schemas in this tableset"> <add> <property> <name>schema_name</name> - <type>java.lang.String</type> + <type>VARCHAR</type> <size>64</size> <updatable>false</updatable> <nullable>false</nullable> <key>schema_name</key> <description>schema name for reference to TAP_SCHEMA.schemas</description> + <standard>true</standard> </property> <property> <name>utype</name> - <type>java.lang.String</type> + <type>VARCHAR</type> <size>255</size> <updatable>true</updatable> <description>lists the utypes of schemas in the tableset</description> + <standard>true</standard> </property> <property> <name>description</name> - <type>java.lang.String</type> + <type>VARCHAR</type> <size>255</size> <updatable>true</updatable> <description>describes schemas in the tableset</description> + <standard>true</standard> </property> </add> </table> @@ -53,43 +56,48 @@ Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. <add> <property> <name>schema_name</name> - <type>java.lang.String</type> + <type>VARCHAR</type> <size>64</size> <updatable>false</updatable> <nullable>false</nullable> <key>schema_name</key> <description>the schema this table belongs to</description> + <standard>true</standard> </property> <property> <name>table_name</name> - <type>java.lang.String</type> + <type>VARCHAR</type> <size>128</size> <updatable>false</updatable> <nullable>false</nullable> <key>table_name</key> <description>the fully qualified table name</description> + <standard>true</standard> </property> <property> <name>table_type</name> <size>8</size> - <type>java.lang.String</type> + <type>VARCHAR</type> <updatable>false</updatable> <key>table_type</key> <description>one of: table view</description> + <standard>true</standard> </property> <property> <name>utype</name> - <type>java.lang.String</type> + <type>VARCHAR</type> <size>255</size> <updatable>true</updatable> <description>lists the utype of tables in the tableset</description> + <standard>true</standard> </property> <property> <name>description</name> - <type>java.lang.String</type> + <type>VARCHAR</type> <size>255</size> <updatable>true</updatable> - <description>describes tables in the tableset</description> + <description>describes tables in the tableset</description> + <standard>true</standard> </property> </add> </table> @@ -98,90 +106,101 @@ Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. <property> <name>table_name</name> <required>true</required> - <type>java.lang.String</type> + <type>VARCHAR</type> <size>128</size> <updatable>false</updatable> <nullable>false</nullable> <key>table_name</key> <description>the table this column belongs to</description> + <standard>true</standard> </property> <property> <name>column_name</name> <required>true</required> - <type>java.lang.String</type> + <type>VARCHAR</type> <size>64</size> <updatable>false</updatable> <nullable>false</nullable> <key>column_name</key> <description>the column name</description> + <standard>true</standard> </property> <property> <name>datatype</name> - <type>java.lang.String</type> + <type>VARCHAR</type> <size>64</size> <updatable>false</updatable> <key>datatype</key> <description>lists the ADQL datatype of columns in the tableset</description> + <standard>true</standard> </property> <property> <name>size</name> - <type>java.lang.Integer</type> + <type>INTEGER</type> <updatable>false</updatable> <key>size</key> <description>lists the size of variable-length columns in the tableset</description> + <standard>true</standard> </property> <property> <name>description</name> - <type>java.lang.String</type> + <type>VARCHAR</type> <size>255</size> <updatable>true</updatable> - <description>describes the columns in the tableset</description> + <description>describes the columns in the tableset</description> + <standard>true</standard> </property> <property> <name>utype</name> - <type>java.lang.String</type> + <type>VARCHAR</type> <size>255</size> <updatable>true</updatable> <description>lists the utypes of columns in the tableset</description> + <standard>true</standard> </property> <property> <name>unit</name> - <type>java.lang.String</type> + <type>VARCHAR</type> <size>64</size> <updatable>true</updatable> <description>lists the unit used for column values in the tableset</description> + <standard>true</standard> </property> <property> <name>ucd</name> - <type>java.lang.String</type> + <type>VARCHAR</type> <size>64</size> <updatable>true</updatable> <description>lists the UCDs of columns in the tableset</description> + <standard>true</standard> </property> <property> <name>indexed</name> - <type>java.lang.Integer</type> + <type>INTEGER</type> <updatable>false</updatable> <nullable>false</nullable> <key>indexed</key> <default-value>0</default-value> <description>an indexed column; 1 means true, 0 means false</description> + <standard>true</standard> </property> <property> <name>principal</name> - <type>java.lang.Integer</type> + <type>INTEGER</type> <updatable>true</updatable> <nullable>false</nullable> <default-value>0</default-value> - <description>a principal column; 1 means true, 0 means false</description> + <description>a principal column; 1 means true, 0 means false</description> + <standard>true</standard> </property> <property> <name>std</name> - <type>java.lang.Integer</type> + <type>INTEGER</type> <updatable>true</updatable> <nullable>false</nullable> <default-value>0</default-value> - <description>a standard column; 1 means true, 0 means false</description> + <description>a standard column; 1 means true, 0 means false</description> + <standard>true</standard> </property> </add> </table> @@ -189,40 +208,45 @@ Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. <add> <property> <name>key_id</name> - <type>java.lang.String</type> + <type>VARCHAR</type> <size>64</size> <updatable>true</updatable> <description>unique key to join to TAP_SCHEMA.key_columns</description> + <standard>true</standard> </property> <property> <name>from_table</name> - <type>java.lang.String</type> + <type>VARCHAR</type> <size>128</size> <updatable>false</updatable> <key>from_table</key> <description>the table with the foreign key</description> + <standard>true</standard> </property> <property> <name>target_table</name> - <type>java.lang.String</type> + <type>VARCHAR</type> <size>128</size> <updatable>false</updatable> <key>target_table</key> <description>the table with the primary key</description> + <standard>true</standard> </property> <property> <name>description</name> - <type>java.lang.String</type> + <type>VARCHAR</type> <size>255</size> <updatable>true</updatable> <description>describes keys in the tableset</description> + <standard>true</standard> </property> <property> <name>utype</name> - <type>java.lang.String</type> + <type>VARCHAR</type> <size>255</size> <updatable>true</updatable> <description>lists the utype of keys in the tableset</description> + <standard>true</standard> </property> </add> </table> @@ -230,27 +254,30 @@ Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. <add> <property> <name>key_id</name> - <type>java.lang.String</type> + <type>VARCHAR</type> <size>64</size> <updatable>true</updatable> <description>key to join to TAP_SCHEMA.keys</description> + <standard>true</standard> </property> <property> <name>from_column</name> - <type>java.lang.String</type> + <type>VARCHAR</type> <size>64</size> <updatable>false</updatable> <key>from_column</key> <description>column in the from_table</description> + <standard>true</standard> </property> <property> <name>target_column</name> - <type>java.lang.String</type> + <type>VARCHAR</type> <size>64</size> <updatable>false</updatable> <key>target_column</key> <description>column in the target_table</description> + <standard>true</standard> </property> </add> </table> -</tap_schema> +</schema> diff --git a/TASMAN-core/src/main/resources/tap_schema/tap_schema-1_1.xml b/TASMAN-core/src/main/resources/schema_definition/tap_schema-1_1.xml similarity index 75% rename from TASMAN-core/src/main/resources/tap_schema/tap_schema-1_1.xml rename to TASMAN-core/src/main/resources/schema_definition/tap_schema-1_1.xml index 43efe31c1acf0935951ed40fa761c533a11694ae..bcb19f48e5458592b7832ed7774bacb9d2813546 100644 --- a/TASMAN-core/src/main/resources/tap_schema/tap_schema-1_1.xml +++ b/TASMAN-core/src/main/resources/schema_definition/tap_schema-1_1.xml @@ -21,13 +21,14 @@ 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. --> -<tap_schema version="1.1" extends="1.0"> +<schema name="tap_schema" version="1.1" extends="1.0"> <table name="tables"> <add> <property> <name>table_index</name> - <type>java.lang.Integer</type> - <updatable>true</updatable> + <type>INTEGER</type> + <updatable>true</updatable> + <standard>true</standard> </property> </add> </table> @@ -35,45 +36,51 @@ Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. <add> <property> <name>arraysize</name> - <type>java.lang.String</type> + <type>VARCHAR</type> <updatable>false</updatable> - <key>arraysize</key> + <key>arraysize</key> + <standard>true</standard> </property> <property> <name>xtype</name> - <type>java.lang.String</type> + <type>VARCHAR</type> <updatable>true</updatable> + <standard>true</standard> </property> <property> <name>column_index</name> - <type>java.lang.Integer</type> - <updatable>true</updatable> + <type>INTEGER</type> + <updatable>true</updatable> + <standard>true</standard> </property> <property> <name>indexed</name> - <type>java.lang.Boolean</type> + <type>BOOLEAN</type> <updatable>false</updatable> <nullable>false</nullable> <default-value>false</default-value> <key>indexed</key> <description>an indexed column</description> + <standard>true</standard> </property> <property> <name>principal</name> - <type>java.lang.Boolean</type> + <type>BOOLEAN</type> <updatable>true</updatable> <nullable>false</nullable> <default-value>false</default-value> <description>a principal column</description> + <standard>true</standard> </property> <property> <name>std</name> - <type>java.lang.Boolean</type> + <type>BOOLEAN</type> <updatable>true</updatable> <nullable>false</nullable> <default-value>false</default-value> - <description>a standard column</description> + <description>a standard column</description> + <standard>true</standard> </property> </add> </table> -</tap_schema> +</schema> diff --git a/TASMAN-core/src/main/resources/sql_type_mapping.xml b/TASMAN-core/src/main/resources/sql_type_mapping.xml new file mode 100644 index 0000000000000000000000000000000000000000..cb60fd4bebf81fcddcea57b52de4a5ef60d056b8 --- /dev/null +++ b/TASMAN-core/src/main/resources/sql_type_mapping.xml @@ -0,0 +1,75 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- +_____________________________________________________________________________ + +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. +--> +<sql_type_mapping> + + <type> + <adql>VARCHAR</adql> + <mysql>VARCHAR</mysql> + <pgsql>character varying</pgsql> + <java>java.lang.String</java> + </type> + <type> + <adql>CHAR</adql> + <mysql>CHAR</mysql> + <pgsql>character</pgsql> + <java>java.lang.String</java> + </type> + <type> + <adql>INTEGER</adql> + <mysql>INT</mysql> + <pgsql>integer</pgsql> + <java>java.lang.Integer</java> + </type> + <type> + <adql>BIGINT</adql> + <mysql>BIGINT</mysql> + <pgsql>bigint</pgsql> + <java>java.lang.Long</java> + </type> + <type> + <adql>REAL</adql> + <mysql>FLOAT</mysql> + <pgsql>integer</pgsql> + <java>java.lang.Float</java> + </type> + <type> + <adql>DOUBLE</adql> + <mysql>DOUBLE</mysql> + <pgsql>double precision</pgsql> + <java>java.lang.Double</java> + </type> + <type> + <adql>BOOLEAN</adql> + <mysql>BIT</mysql> + <pgsql>boolean</pgsql> + <java>java.lang.Boolean</java> + </type> + <type> + <adql>CLOB</adql> + <mysql>LONGTEXT</mysql> + <pgsql>text</pgsql> + <java>java.lang.String</java> + </type> + +</sql_type_mapping> diff --git a/TASMAN-core/src/test/java/it/inaf/ia2/tsm/TestAll.java b/TASMAN-core/src/test/java/it/inaf/ia2/tsm/TestAll.java index e68aa982c6a2d2b65ebb23cd67f7a6a800461f79..04b81fcee54a278a32264c428721cf83e874d9c8 100644 --- a/TASMAN-core/src/test/java/it/inaf/ia2/tsm/TestAll.java +++ b/TASMAN-core/src/test/java/it/inaf/ia2/tsm/TestAll.java @@ -60,6 +60,7 @@ public class TestAll { private static final int TABLES_COUNT = 3; private static List<DBWrapper> dbWrappers; + private static TapSchemaSettings settings; public TestAll() { } @@ -89,6 +90,12 @@ public class TestAll { dbWrapper.testConnections(); dbWrappers.add(dbWrapper); + settings = new TapSchemaSettings(); + settings.setTapSchemaName("test_tap_schema"); + settings.setTapSchemaVersion("1.0"); + settings.setHasObscore(true); + settings.setObscoreVersion("1.1"); + // Mix! //dbWrappers.add(new DBWrapper(mysqlCredentials, postgresCredentials)); //dbWrappers.add(new DBWrapper(postgresCredentials, mysqlCredentials)); @@ -293,7 +300,7 @@ public class TestAll { for (DBWrapper dbWrapper : dbWrappers) { // Initializing a not existing TAP_SCHEMA - TapSchema tapSchema = new TapSchema("1.0", dbWrapper, "test_tap_schema", false); + TapSchema tapSchema = new TapSchema(dbWrapper, settings, false); ///////////////////////////////////// // ADDING A SCHEMA // @@ -452,7 +459,7 @@ public class TestAll { // reloading LOG.debug("----- Reloading saved TAP_SCHEMA -----"); - tapSchema = new TapSchema("1.0", dbWrapper, "test_tap_schema", true); + tapSchema = new TapSchema(dbWrapper, settings, true); assertEquals("1.0", tapSchema.getTapSchemaDBBroker().detectVersion(tapSchema.getName())); LOG.debug(tapSchema.toString()); @@ -506,7 +513,7 @@ public class TestAll { // reloading LOG.debug("----- Reloading saved TAP_SCHEMA -----"); - tapSchema = new TapSchema("1.0", dbWrapper, "test_tap_schema", true); + tapSchema = new TapSchema(dbWrapper, settings, true); LOG.debug(tapSchema.toString()); assertNotNull(sch0 = tapSchema.getChild("sch0", Status.ADDED_PERSISTED)); @@ -572,7 +579,7 @@ public class TestAll { public void testTapSchemaSerialization() throws Exception { for (DBWrapper dbWrapper : dbWrappers) { if (dbWrapper.getTapSchemaDatabaseType() == DatabaseType.MYSQL) { // currently "tng_TAP_SCHEMA" exists in a MySQL instance - TapSchema tapSchema = new TapSchema("1.0", dbWrapper, "test_tap_schema", true); + TapSchema tapSchema = new TapSchema(dbWrapper, settings, true); File temp = File.createTempFile("test_tap_schema", ".ser"); diff --git a/TASMAN-webapp/src/main/java/it/inaf/ia2/tsm/webapp/CredentialsEditing.java b/TASMAN-webapp/src/main/java/it/inaf/ia2/tsm/webapp/CredentialsEditing.java index 3ce133c11ec3f6e3f115ef3156101627fd800990..a5e4e7154b5cb6016e767d283360217865dc3b01 100644 --- a/TASMAN-webapp/src/main/java/it/inaf/ia2/tsm/webapp/CredentialsEditing.java +++ b/TASMAN-webapp/src/main/java/it/inaf/ia2/tsm/webapp/CredentialsEditing.java @@ -24,7 +24,7 @@ package it.inaf.ia2.tsm.webapp; import it.inaf.ia2.tsm.webapp.xmlconfig.SeparatedCredentials; import it.inaf.ia2.tsm.datalayer.Credentials; -import it.inaf.ia2.tsm.model.TapSchemaModels; +import it.inaf.ia2.tsm.model.SchemaModels; import it.inaf.ia2.tsm.webapp.xmlconfig.JoinedCredentials; import it.inaf.ia2.tsm.webapp.xmlconfig.TapCredentials; import java.io.IOException; @@ -76,7 +76,7 @@ public class CredentialsEditing implements Serializable { @PostConstruct public void init() { - this.tapSchemaVersions = TapSchemaModels.getAvailableVersions(); + this.tapSchemaVersions = SchemaModels.getAvailableTapSchemaVersions(); } public List<TapCredentials> getSavedCredentials() {