diff --git a/TapSchemaManager/src/main/java/it/inaf/oats/ia2/tapschemamanager/businesslayer/Column.java b/TapSchemaManager/src/main/java/it/inaf/oats/ia2/tapschemamanager/businesslayer/Column.java index 727014b47bd9536b04b6dded19fa86c0d3c30e08..021318cf6b2c3c8559e09d94d7a7ee549777e77a 100644 --- a/TapSchemaManager/src/main/java/it/inaf/oats/ia2/tapschemamanager/businesslayer/Column.java +++ b/TapSchemaManager/src/main/java/it/inaf/oats/ia2/tapschemamanager/businesslayer/Column.java @@ -1,6 +1,7 @@ package it.inaf.oats.ia2.tapschemamanager.businesslayer; import it.inaf.oats.ia2.tapschemamanager.datalayer.ColumnEntity; +import it.inaf.oats.ia2.tapschemamanager.datalayer.TapSchemaHandler; /** * @@ -20,12 +21,12 @@ public class Column extends EntityWrapper { private final boolean primaryKey; private final boolean indexed; - private boolean foreignKey; + private final String foreignKeyReference; private boolean hidden; - public Column(ColumnEntity columnEntity, boolean primaryKey) { - super(columnEntity, UTYPE, UCD, UNIT, DESCRIPTION, STD); + public Column(TapSchemaHandler tapSchemaHandler, ColumnEntity columnEntity, boolean primaryKey) { + super(columnEntity, UTYPE, UCD, UNIT, DESCRIPTION, STD, PRINCIPAL); hidden = true; this.primaryKey = primaryKey; this.datatype = columnEntity.getDatatype(); @@ -38,6 +39,10 @@ public class Column extends EntityWrapper { addValue(DESCRIPTION, columnEntity.getDescription()); addValue(STD, columnEntity.getStd() + ""); addValue(PRINCIPAL, columnEntity.getPrincipal() + ""); + + String tableName = columnEntity.getTableName(); + String schemaName = columnEntity.getTable().getSchemaName(); + foreignKeyReference = tapSchemaHandler.getForeignKeyReference(schemaName, tableName, columnEntity.getName()); } @Override @@ -53,6 +58,8 @@ public class Column extends EntityWrapper { columnEntity.setDescription(value); } else if (key.equals(STD)) { columnEntity.setStd(Integer.parseInt(value)); + } else if (key.equals(PRINCIPAL)) { + columnEntity.setPrincipal(Integer.parseInt(value)); } } @@ -83,8 +90,8 @@ public class Column extends EntityWrapper { return primaryKey; } - public boolean isForeignKey() { - return foreignKey; + public String getForeignKey() { + return foreignKeyReference; } public boolean isIndexed() { diff --git a/TapSchemaManager/src/main/java/it/inaf/oats/ia2/tapschemamanager/businesslayer/Schema.java b/TapSchemaManager/src/main/java/it/inaf/oats/ia2/tapschemamanager/businesslayer/Schema.java index ccc57636ec250c36887545b5ecb7381a785654b4..2f9037817e3a1a71cb02fe555190704da9ab364c 100644 --- a/TapSchemaManager/src/main/java/it/inaf/oats/ia2/tapschemamanager/businesslayer/Schema.java +++ b/TapSchemaManager/src/main/java/it/inaf/oats/ia2/tapschemamanager/businesslayer/Schema.java @@ -80,7 +80,7 @@ public class Schema extends EntityWrapper implements EntityWrapperContainer { } selectedTable = name; - getSchemaEntity().addTable((TableEntity) table.getEntity()); + tapSchemaHandler.addTable(getName(), table.getTableEntity()); } @Override @@ -93,7 +93,7 @@ public class Schema extends EntityWrapper implements EntityWrapperContainer { } selectedTable = tables.isEmpty() ? null : tables.keySet().iterator().next(); - getSchemaEntity().getTables().remove(table.getFullName()); + tapSchemaHandler.removeTable(getSchemaEntity(), table.getTableEntity()); } public Table getTable(String name) { @@ -138,7 +138,7 @@ public class Schema extends EntityWrapper implements EntityWrapperContainer { Table table = tables.get(entityName); List columns = table.getColumnsNames(); if (!columns.isEmpty()) { - table.selectEntity(columns.get(0)); + table.selectEntity(Util.getFirstInAlphabeticalOrder(columns)); } } diff --git a/TapSchemaManager/src/main/java/it/inaf/oats/ia2/tapschemamanager/businesslayer/Table.java b/TapSchemaManager/src/main/java/it/inaf/oats/ia2/tapschemamanager/businesslayer/Table.java index c33dfba91586b652a2693e7a1a8c29d89aabe6fc..79d466068287ba7ad839f68d64ed976b615a7b61 100644 --- a/TapSchemaManager/src/main/java/it/inaf/oats/ia2/tapschemamanager/businesslayer/Table.java +++ b/TapSchemaManager/src/main/java/it/inaf/oats/ia2/tapschemamanager/businesslayer/Table.java @@ -23,19 +23,17 @@ public class Table extends EntityWrapper implements EntityWrapperContainer { private String selectedColumn; private final Map columns; + private final TapSchemaHandler tapSchemaHandler; private final String schemaName; private final String tableName; -// public Table(TapSchemaHandler tapSchemaHandler, String schemaName, String tableName) throws SQLException { -// this(tapSchemaHandler, schemaName, tableName, new TableEntity(schemaName + "." + tableName)); -// setStatus(Status.ADDED_NOT_PERSISTED); -// } public Table(TapSchemaHandler tapSchemaHandler, String schemaName, String tableName, TableEntity tableEntity) throws SQLException { super(tableEntity, UTYPE, DESCRIPTION); addValue(UTYPE, tableEntity.getUtype()); addValue(DESCRIPTION, tableEntity.getDescription()); + this.tapSchemaHandler = tapSchemaHandler; this.schemaName = schemaName; this.tableName = tableName; @@ -44,7 +42,7 @@ public class Table extends EntityWrapper implements EntityWrapperContainer { Set alreadyLoadedColumns = tableEntity.getColumns().keySet(); for (ColumnInfo columnInfo : tapSchemaHandler.getColumnInfo(schemaName, tableName)) { ColumnEntity columnEntity = columnInfo.getColumnEntity(); - Column column = new Column(columnEntity, columnInfo.isPrimaryKey()); + Column column = new Column(tapSchemaHandler, columnEntity, columnInfo.isPrimaryKey()); columns.put(columnEntity.getName(), column); if (alreadyLoadedColumns.contains(columnEntity.getName())) { @@ -53,8 +51,6 @@ public class Table extends EntityWrapper implements EntityWrapperContainer { } } - // Load foreign keys constraints - // TODO ... setStatus(Status.ADDED_PERSISTED); } @@ -96,7 +92,7 @@ public class Table extends EntityWrapper implements EntityWrapperContainer { } selectedColumn = name; - getTableEntity().addColumn(column.getColumnEntity()); + tapSchemaHandler.addColumn(getTableEntity(), column.getColumnEntity()); } @Override @@ -111,8 +107,7 @@ public class Table extends EntityWrapper implements EntityWrapperContainer { List visibleColumns = getColumnsNames(); selectedColumn = visibleColumns.isEmpty() ? null : visibleColumns.get(0); - column.setStatus(Status.ADDED_NOT_PERSISTED); - getTableEntity().addColumn(column.getColumnEntity()); + tapSchemaHandler.removeColumn(getTableEntity(), name); } public Column getColumn(String name) { diff --git a/TapSchemaManager/src/main/java/it/inaf/oats/ia2/tapschemamanager/businesslayer/TapSchema.java b/TapSchemaManager/src/main/java/it/inaf/oats/ia2/tapschemamanager/businesslayer/TapSchema.java index 3783c7751cb6c98fbdb0f2a74e1c5c0ccb7201aa..993fdbed095e68baa12acb4cbfc77aa22efcd953 100644 --- a/TapSchemaManager/src/main/java/it/inaf/oats/ia2/tapschemamanager/businesslayer/TapSchema.java +++ b/TapSchemaManager/src/main/java/it/inaf/oats/ia2/tapschemamanager/businesslayer/TapSchema.java @@ -124,7 +124,7 @@ public class TapSchema implements EntityWrapperContainer { Schema schema = schemas.get(entityName); List tables = schema.getTablesNames(); if (!tables.isEmpty()) { - schema.selectEntity(tables.get(0)); + schema.selectEntity(Util.getFirstInAlphabeticalOrder(tables)); } } diff --git a/TapSchemaManager/src/main/java/it/inaf/oats/ia2/tapschemamanager/businesslayer/Util.java b/TapSchemaManager/src/main/java/it/inaf/oats/ia2/tapschemamanager/businesslayer/Util.java index 56b6cd5885e429b45f9e3655bb1616953e3843d9..480cdb3d39eb6fa131483ebb9a3be5888fe4de20 100644 --- a/TapSchemaManager/src/main/java/it/inaf/oats/ia2/tapschemamanager/businesslayer/Util.java +++ b/TapSchemaManager/src/main/java/it/inaf/oats/ia2/tapschemamanager/businesslayer/Util.java @@ -1,7 +1,6 @@ package it.inaf.oats.ia2.tapschemamanager.businesslayer; import java.util.ArrayList; -import java.util.Collection; import java.util.List; import java.util.Set; @@ -26,4 +25,17 @@ class Util { list.addAll(set); return list; } + + protected static String getFirstInAlphabeticalOrder(List list) { + String result = null; + for (String value : list) { + if (result == null) { + result = value; + } + if (result.compareTo(value) > 0) { + result = value; + } + } + return result; + } } diff --git a/TapSchemaManager/src/main/java/it/inaf/oats/ia2/tapschemamanager/webapp/EditTapSchemaPageModel.java b/TapSchemaManager/src/main/java/it/inaf/oats/ia2/tapschemamanager/webapp/EditTapSchemaPageModel.java index 9acf23c38aadc8385a2bf82a3fd3a7669f99dc62..d1e8ad3a7c63483d22319ba6339e24e7ee939289 100644 --- a/TapSchemaManager/src/main/java/it/inaf/oats/ia2/tapschemamanager/webapp/EditTapSchemaPageModel.java +++ b/TapSchemaManager/src/main/java/it/inaf/oats/ia2/tapschemamanager/webapp/EditTapSchemaPageModel.java @@ -8,6 +8,8 @@ import it.inaf.oats.ia2.tapschemamanager.businesslayer.Status; import it.inaf.oats.ia2.tapschemamanager.businesslayer.Table; import it.inaf.oats.ia2.tapschemamanager.businesslayer.TapSchema; import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -51,6 +53,24 @@ public class EditTapSchemaPageModel { this.toRemove = entityWrapper.getStatus() == Status.TO_REMOVE; this.active = active; } + + public String getTitle() { + return title; + } + } + + class ColumnEntityItem extends EntityItem { + + private final boolean isPrimaryKey; + private final boolean isIndexed; + private final String foreignKey; + + public ColumnEntityItem(Column column, boolean active) { + super(column, active); + isPrimaryKey = column.isPrimaryKey(); + isIndexed = column.isIndexed(); + foreignKey = column.getForeignKey(); + } } class TapSchemaModel { @@ -90,7 +110,8 @@ public class EditTapSchemaPageModel { class ColumnModel extends Entity { boolean isPrimaryKey; - boolean isForeignKey; + boolean isIndexed; + String foreignKey; String dataType; int size; @@ -98,7 +119,8 @@ public class EditTapSchemaPageModel { super(column); isPrimaryKey = column.isPrimaryKey(); - isForeignKey = column.isForeignKey(); + foreignKey = column.getForeignKey(); + isIndexed = column.isIndexed(); dataType = column.getDatatype(); size = column.getSize(); } @@ -145,8 +167,22 @@ public class EditTapSchemaPageModel { ArrayList entities = new ArrayList(); String selectedName = selectedEntityWrapper == null ? null : selectedEntityWrapper.getName(); for (EntityWrapper entityWrapper : container.getAllEntityWrappers()) { - entities.add(new EntityItem(entityWrapper, entityWrapper.getName().equals(selectedName))); + EntityItem item; + if (entityWrapper.getClass() == Column.class) { + item = new ColumnEntityItem((Column) entityWrapper, entityWrapper.getName().equals(selectedName)); + } else { + item = new EntityItem(entityWrapper, entityWrapper.getName().equals(selectedName)); + } + entities.add(item); } + + Collections.sort(entities, new Comparator() { + @Override + public int compare(EntityItem lhs, EntityItem rhs) { + return lhs.getTitle().compareTo(rhs.getTitle()); + } + }); + return entities; } diff --git a/TapSchemaManager/src/main/webapp/css/fontello.css b/TapSchemaManager/src/main/webapp/css/fontello.css index deed1683bac4703db1b081af02c6f3849060719e..2ca8b026436e4e06f4063d91b492664be148948d 100644 --- a/TapSchemaManager/src/main/webapp/css/fontello.css +++ b/TapSchemaManager/src/main/webapp/css/fontello.css @@ -1,10 +1,10 @@ @font-face { font-family: 'fontello'; - src: url('../fonts/fontello.eot?96985520'); - src: url('../fonts/fontello.eot?96985520#iefix') format('embedded-opentype'), - url('../fonts/fontello.woff?96985520') format('woff'), - url('../fonts/fontello.ttf?96985520') format('truetype'), - url('../fonts/fontello.svg?96985520#fontello') format('svg'); + src: url('../fonts/fontello.eot?15528887'); + src: url('../fonts/fontello.eot?15528887#iefix') format('embedded-opentype'), + url('../fonts/fontello.woff?15528887') format('woff'), + url('../fonts/fontello.ttf?15528887') format('truetype'), + url('../fonts/fontello.svg?15528887#fontello') format('svg'); font-weight: normal; font-style: normal; } @@ -14,7 +14,7 @@ @media screen and (-webkit-min-device-pixel-ratio:0) { @font-face { font-family: 'fontello'; - src: url('../fonts/fontello.svg?96985520#fontello') format('svg'); + src: url('../font/fontello.svg?15528887#fontello') format('svg'); } } */ @@ -64,6 +64,7 @@ .icon-warning:before { content: '\e807'; } /* '' */ .icon-error:before { content: '\e808'; } /* '' */ .icon-save:before { content: '\e809'; } /* '' */ +.icon-index:before { content: '\e80a'; } /* '' */ .icon-logout:before { content: '\e80b'; } /* '' */ .icon-left:before { content: '\e80c'; } /* '' */ -.icon-right:before { content: '\e80d'; } /* '' */ +.icon-right:before { content: '\e80d'; } /* '' */ \ No newline at end of file diff --git a/TapSchemaManager/src/main/webapp/editTapSchema.jsp b/TapSchemaManager/src/main/webapp/editTapSchema.jsp index 025e54665c7567b9b6ae0ad7602dc5dd5c597ae4..81cbbdf702d33aa7f001854162d7c5b195a69ea9 100644 --- a/TapSchemaManager/src/main/webapp/editTapSchema.jsp +++ b/TapSchemaManager/src/main/webapp/editTapSchema.jsp @@ -7,10 +7,13 @@ <%@page contentType="text/html" pageEncoding="UTF-8"%> <%@taglib prefix="t" tagdir="/WEB-INF/tags" %> - + + + +
-
+

Editing ${tapSchemaName}

@@ -132,7 +135,8 @@
Column properties - + +
@@ -143,6 +147,9 @@ + + + {{column.title}} @@ -211,9 +218,7 @@
-
- @@ -321,7 +326,5 @@ - -
diff --git a/TapSchemaManager/src/main/webapp/fonts/fontello.eot b/TapSchemaManager/src/main/webapp/fonts/fontello.eot index d2fa61923d66a24a6dcbc7dd5234e6cc8625b5c1..fea0b1006c4825a6e303f821525b5d7a4fa502e9 100644 Binary files a/TapSchemaManager/src/main/webapp/fonts/fontello.eot and b/TapSchemaManager/src/main/webapp/fonts/fontello.eot differ diff --git a/TapSchemaManager/src/main/webapp/fonts/fontello.svg b/TapSchemaManager/src/main/webapp/fonts/fontello.svg index f8960d1b8a463ba705b2a7e2ae8377588b51bc6c..69f17b01c589fc2bc7734534796086bea8be20ed 100644 --- a/TapSchemaManager/src/main/webapp/fonts/fontello.svg +++ b/TapSchemaManager/src/main/webapp/fonts/fontello.svg @@ -1,7 +1,7 @@ -Copyright (C) 2015 by original authors @ fontello.com +Copyright (C) 2016 by original authors @ fontello.com @@ -16,6 +16,7 @@ + diff --git a/TapSchemaManager/src/main/webapp/fonts/fontello.ttf b/TapSchemaManager/src/main/webapp/fonts/fontello.ttf index 6940b1c042627eab8990054be2794e3783810c80..9401f38dffe39aa260fe7f24aa6566f4c9cd3b63 100644 Binary files a/TapSchemaManager/src/main/webapp/fonts/fontello.ttf and b/TapSchemaManager/src/main/webapp/fonts/fontello.ttf differ diff --git a/TapSchemaManager/src/main/webapp/fonts/fontello.woff b/TapSchemaManager/src/main/webapp/fonts/fontello.woff index e1cd519f4c8c6ac97b45c83755ff7907163998d7..e3e7f008bccbf3699a79b19df22e0b546003aac8 100644 Binary files a/TapSchemaManager/src/main/webapp/fonts/fontello.woff and b/TapSchemaManager/src/main/webapp/fonts/fontello.woff differ diff --git a/TapSchemaManager/src/main/webapp/js/edit-tapschema.js b/TapSchemaManager/src/main/webapp/js/edit-tapschema.js index a9d334b0a7822d5fa1edf149300eb124604b5cc2..2853d7cdfd1f8ac094f0806420c0a7c2587376c2 100644 --- a/TapSchemaManager/src/main/webapp/js/edit-tapschema.js +++ b/TapSchemaManager/src/main/webapp/js/edit-tapschema.js @@ -123,7 +123,7 @@ // Search UCD $scope.openUCDDialog = function () { - formPost('searchUCD', {description: $scope.model.column.values.description}, function (response) { + formPost('searchUCD', {description: $scope.model.column.values.description.value}, function (response) { $scope.ucdModel = response.data; $('#searchUCDModal').modal('show'); }); diff --git a/TapSchemaManagerDL/src/main/java/it/inaf/oats/ia2/tapschemamanager/datalayer/DataProvider.java b/TapSchemaManagerDL/src/main/java/it/inaf/oats/ia2/tapschemamanager/datalayer/DataProvider.java index 936d567601abae5e792818650a4db68dcd33f2b5..9bd9a808b3f1baa9d0fa8c62c752e2d7a165f16e 100644 --- a/TapSchemaManagerDL/src/main/java/it/inaf/oats/ia2/tapschemamanager/datalayer/DataProvider.java +++ b/TapSchemaManagerDL/src/main/java/it/inaf/oats/ia2/tapschemamanager/datalayer/DataProvider.java @@ -18,6 +18,19 @@ import javax.persistence.Persistence; */ public class DataProvider { + public static List getAllSchemasNames(Connection connection) throws SQLException { + final List allSchemas = new ArrayList(); + (new ResultSetReader("SHOW DATABASES;") { + + @Override + public void manipulateItem(ResultSet resultSet) throws SQLException { + allSchemas.add(resultSet.getString(1)); + } + + }).read(connection); + return allSchemas; + } + public static List getAllTAPSchemasNames(Connection connection, final List allSchemas) throws SQLException { class TAPSchemaChecker { @@ -87,7 +100,7 @@ public class DataProvider { return exposedSchemas; } - public static EntityManager getEntityManager(Credentials credentials, String tapSchemaName) { + protected static EntityManager getEntityManager(Credentials credentials, String tapSchemaName) { Map persistenceMap = new HashMap(); persistenceMap.put("javax.persistence.jdbc.url", "jdbc:mysql://" + credentials.getHostname() + ":" + credentials.getPort() + "/" + tapSchemaName); @@ -99,26 +112,13 @@ public class DataProvider { return managerFactory.createEntityManager(); } - public static void createDatabase(Connection connection, String tapSchemaName) throws SQLException { + protected static void createDatabase(Connection connection, String tapSchemaName) throws SQLException { Statement statement = connection.createStatement(); statement.executeUpdate("CREATE DATABASE IF NOT EXISTS " + tapSchemaName); statement.close(); } - public static List getAllSchemasNames(Connection connection) throws SQLException { - final List allSchemas = new ArrayList(); - (new ResultSetReader("SHOW DATABASES;") { - - @Override - public void manipulateItem(ResultSet resultSet) throws SQLException { - allSchemas.add(resultSet.getString(1)); - } - - }).read(connection); - return allSchemas; - } - - public static ArrayList getAllTablesNames(Connection connection, String schemaName) throws SQLException { + protected static ArrayList getAllTablesNames(Connection connection, String schemaName) throws SQLException { final ArrayList allTables = new ArrayList(); (new ResultSetReader("SHOW TABLES FROM " + schemaName + ";") { @@ -141,7 +141,7 @@ public class DataProvider { * @return * @throws SQLException */ - public static List getSchemaKeys(final Connection connection, final String schemaName) throws SQLException { + protected static List getSchemaKeys(final Connection connection, final String schemaName) throws SQLException { final Map schemaKeysMap = new HashMap(); @@ -188,7 +188,7 @@ public class DataProvider { return false; } - public static List getAllColumns(final Connection connection, final String schemaName, final String tableName, final Map alreadyLoadedColumns) throws SQLException { + protected static List getAllColumns(final Connection connection, final String schemaName, final String tableName, final Map alreadyLoadedColumns) throws SQLException { final List allColumns = new ArrayList(); (new ResultSetReader("SHOW COLUMNS FROM `" + schemaName + "`.`" + tableName + "`;") { diff --git a/TapSchemaManagerDL/src/main/java/it/inaf/oats/ia2/tapschemamanager/datalayer/KeyEntity.java b/TapSchemaManagerDL/src/main/java/it/inaf/oats/ia2/tapschemamanager/datalayer/KeyEntity.java index ff470b4d8abb3758a531e6fda36b4782c060c257..c67259104ea4cdd9f30b2846cf17ae16a52ae6b9 100644 --- a/TapSchemaManagerDL/src/main/java/it/inaf/oats/ia2/tapschemamanager/datalayer/KeyEntity.java +++ b/TapSchemaManagerDL/src/main/java/it/inaf/oats/ia2/tapschemamanager/datalayer/KeyEntity.java @@ -113,6 +113,14 @@ public class KeyEntity implements Serializable { return fromTable; } + public void setFromTable(TableEntity fromTable) { + this.fromTable = fromTable; + } + + public void setTargetTable(TableEntity targetTable) { + this.targetTable = targetTable; + } + public TableEntity getTargetTable() { return targetTable; } diff --git a/TapSchemaManagerDL/src/main/java/it/inaf/oats/ia2/tapschemamanager/datalayer/SchemaEntity.java b/TapSchemaManagerDL/src/main/java/it/inaf/oats/ia2/tapschemamanager/datalayer/SchemaEntity.java index 4a846eade22087ab08ad825fd21921d2cbceac99..3b58636192a65cd72bb4fad5a699b59a9dd8278c 100644 --- a/TapSchemaManagerDL/src/main/java/it/inaf/oats/ia2/tapschemamanager/datalayer/SchemaEntity.java +++ b/TapSchemaManagerDL/src/main/java/it/inaf/oats/ia2/tapschemamanager/datalayer/SchemaEntity.java @@ -2,6 +2,8 @@ package it.inaf.oats.ia2.tapschemamanager.datalayer; import java.io.Serializable; import java.math.BigInteger; +import java.util.Collection; +import java.util.Collections; import java.util.HashMap; import java.util.Map; import javax.persistence.CascadeType; @@ -80,11 +82,19 @@ public class SchemaEntity implements Serializable, TapSchemaEntity { } public Map getTables() { - return tables; + return Collections.unmodifiableMap(tables); } - public void addTable(TableEntity table) { + public TableEntity getTable(String tableName) { + return tables.get(tableName); + } + + protected void addTable(TableEntity table) { tables.put(table.getName(), table); table.setSchemaName(name); } + + protected void removeTable(String tableName) { + tables.remove(tableName); + } } diff --git a/TapSchemaManagerDL/src/main/java/it/inaf/oats/ia2/tapschemamanager/datalayer/TableEntity.java b/TapSchemaManagerDL/src/main/java/it/inaf/oats/ia2/tapschemamanager/datalayer/TableEntity.java index b9745257f3397b60d381ecce8fe4d94e99a3345c..24d279dc9e284eb0b6f329deb120bf5ff90fdfbe 100644 --- a/TapSchemaManagerDL/src/main/java/it/inaf/oats/ia2/tapschemamanager/datalayer/TableEntity.java +++ b/TapSchemaManagerDL/src/main/java/it/inaf/oats/ia2/tapschemamanager/datalayer/TableEntity.java @@ -3,7 +3,9 @@ package it.inaf.oats.ia2.tapschemamanager.datalayer; import java.io.Serializable; import java.math.BigInteger; 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 java.util.regex.Pattern; @@ -134,19 +136,43 @@ public class TableEntity implements Serializable, TapSchemaEntity { } public Map getColumns() { - return columns; + return Collections.unmodifiableMap(columns); } public List getFromKeys() { - return fromKeys; + return Collections.unmodifiableList(fromKeys); } public List getTargetKeys() { - return targetKeys; + return Collections.unmodifiableList(targetKeys); } - public void addColumn(ColumnEntity column) { + protected void addColumn(ColumnEntity column) { columns.put(column.getName(), column); column.setTableName(name); } + + protected void removeColumn(String columnName) { + columns.remove(columnName); + } + + public void addFromKey(KeyEntity key) { + if (!fromKeys.contains(key)) { + fromKeys.add(key); + } + } + + public void removeFromKey(KeyEntity key) { + fromKeys.remove(key); + } + + public void addTargetKey(KeyEntity key) { + if (!targetKeys.contains(key)) { + targetKeys.add(key); + } + } + + public void removeTargetKey(KeyEntity key) { + targetKeys.remove(key); + } } diff --git a/TapSchemaManagerDL/src/main/java/it/inaf/oats/ia2/tapschemamanager/datalayer/TapSchemaHandler.java b/TapSchemaManagerDL/src/main/java/it/inaf/oats/ia2/tapschemamanager/datalayer/TapSchemaHandler.java index a6fed9be47635812a934a6e4000714cbb32487b8..9aee364e5a9fa775d5d143880af51512e3b46cb3 100644 --- a/TapSchemaManagerDL/src/main/java/it/inaf/oats/ia2/tapschemamanager/datalayer/TapSchemaHandler.java +++ b/TapSchemaManagerDL/src/main/java/it/inaf/oats/ia2/tapschemamanager/datalayer/TapSchemaHandler.java @@ -7,6 +7,7 @@ import java.util.Collection; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.regex.Pattern; import javax.persistence.EntityManager; import javax.persistence.criteria.CriteriaBuilder; import javax.persistence.criteria.CriteriaQuery; @@ -168,52 +169,89 @@ public class TapSchemaHandler { } public TableEntity getNewTable(String schemaName, String tableName) throws SQLException { + TableEntity table = new TableEntity(schemaName + "." + tableName); + Connection conn = getConnection(); + try { + // Add columns info + // For performance reasons all possible column entities are loaded together + columnsInfo.addColumnsInfo(conn, schemaName, tableName, null); + } catch (SQLException e) { + throw e; + } finally { + closeConnection(); + } + + return table; + } + + public void addTable(String schemaName, TableEntity tableEntity) { SchemaEntity schema = schemas.get(schemaName); - TableEntity table = new TableEntity(schemaName + "." + tableName); + schema.addTable(tableEntity); // Add table keys int maxKey = getMaxKey(); for (KeyEntity keyEntity : schemaKeys.get(schemaName)) { - String fromTable = keyEntity.getFromTableName(); - String targetTable = keyEntity.getTargetTableName(); - if (schema.getTables().containsKey(fromTable) - && schema.getTables().containsKey(targetTable)) { + TableEntity fromTable = schema.getTable(keyEntity.getFromTableName()); + TableEntity targetTable = schema.getTable(keyEntity.getTargetTableName()); + if (fromTable != null && targetTable != null) { String key = maxKey + ""; keyEntity.setKeyId(key); + keyEntity.setFromTable(fromTable); + keyEntity.setTargetTable(targetTable); for (KeyColumnEntity keyColumn : keyEntity.getKeyColumns()) { keyColumn.setKeyId(key); } - schema.getTables().get(fromTable).getFromKeys().add(keyEntity); - schema.getTables().get(targetTable).getTargetKeys().add(keyEntity); + fromTable.addFromKey(keyEntity); + targetTable.addTargetKey(keyEntity); maxKey++; } } + } - Connection conn = getConnection(); - try { - // Add columns info - // For performance reasons all possible column entities are loaded together - columnsInfo.addColumnsInfo(conn, schemaName, tableName, null); - } catch (SQLException e) { - throw e; - } finally { - closeConnection(); + public void removeTable(SchemaEntity schemaEntity, TableEntity tableEntity) { + for (KeyEntity keyEntity : tableEntity.getFromKeys()) { + keyEntity.getTargetTable().removeTargetKey(keyEntity); + } + for (KeyEntity keyEntity : tableEntity.getTargetKeys()) { + keyEntity.getFromTable().removeFromKey(keyEntity); } - return table; + schemaEntity.removeTable(tableEntity.getName()); + } + + public void addColumn(TableEntity tableEntity, ColumnEntity columnEntity) { + tableEntity.addColumn(columnEntity); + } + + public void removeColumn(TableEntity tableEntity, String columnName) { + tableEntity.removeColumn(columnName); + } + + public String getForeignKeyReference(String schemaName, String tableName, String columnName) { + for (KeyEntity key : schemaKeys.get(schemaName)) { + if (key.getFromTableName().equals(tableName)) { + for (KeyColumnEntity keyColumn : key.getKeyColumns()) { + if (keyColumn.getFromColumn().equals(columnName)) { + return key.getTargetTableName().split(Pattern.quote("."))[1] + "." + keyColumn.getTargetColumn(); + } + } + } + } + + return null; } - public final Connection getConnection() throws SQLException { + private Connection getConnection() throws SQLException { if (connection == null) { connection = credentials.getConnection(); } return connection; } - public final void closeConnection() throws SQLException { + private void closeConnection() throws SQLException { if (connection != null) { try { connection.close(); @@ -225,6 +263,10 @@ public class TapSchemaHandler { } } + public List getAllSchemaKeys(String schemaName) { + return schemaKeys.get(schemaName); + } + public void save() throws SQLException { if (!exists) { try { diff --git a/TapSchemaManagerDL/src/test/java/TestQuery.java b/TapSchemaManagerDL/src/test/java/TestQuery.java index c1f35cf90f7c0cfd46438f85791c5d08696ba6ec..3082caba40301e4f8da4047574bc70129ca45318 100644 --- a/TapSchemaManagerDL/src/test/java/TestQuery.java +++ b/TapSchemaManagerDL/src/test/java/TestQuery.java @@ -1,6 +1,9 @@ import it.inaf.oats.ia2.tapschemamanager.datalayer.ColumnEntity; +import it.inaf.oats.ia2.tapschemamanager.datalayer.ColumnInfo; import it.inaf.oats.ia2.tapschemamanager.datalayer.Credentials; +import it.inaf.oats.ia2.tapschemamanager.datalayer.KeyColumnEntity; +import it.inaf.oats.ia2.tapschemamanager.datalayer.KeyEntity; import it.inaf.oats.ia2.tapschemamanager.datalayer.SchemaEntity; import it.inaf.oats.ia2.tapschemamanager.datalayer.TableEntity; import it.inaf.oats.ia2.tapschemamanager.datalayer.TapSchemaHandler; @@ -63,31 +66,47 @@ public class TestQuery { private void setUpTestingDatabases(Credentials credentials) throws SQLException { removeTestingDatabases(credentials); - Connection connection = credentials.getConnection(); - - Statement statement = connection.createStatement(); - for (int i = 0; i < DATABASES_COUNT; i++) { - statement.executeUpdate("CREATE DATABASE db" + i); - for (int j = 0; j < TABLES_COUNT; j++) { - statement.executeUpdate("CREATE TABLE db" + i + ".table" + j + " (\n" - + "id INT PRIMARY KEY AUTO_INCREMENT,\n" - + "value1 VARCHAR(255),\n" - + "value2 FLOAT\n" - + ");"); + Connection connection = null; + Statement statement = null; + try { + connection = credentials.getConnection(); + statement = connection.createStatement(); + for (int i = 0; i < DATABASES_COUNT; i++) { + statement.executeUpdate("CREATE DATABASE db" + i); + for (int j = 0; j < TABLES_COUNT; j++) { + statement.executeUpdate("CREATE TABLE db" + i + ".table" + j + " (\n" + + "id INT PRIMARY KEY AUTO_INCREMENT,\n" + + "value1 VARCHAR(255),\n" + + "value2 FLOAT\n" + + ");"); + } } - } - statement.close(); + statement.close(); - connection.setCatalog("db0"); + System.out.println("dbs created"); - statement = connection.createStatement(); - for (int j = 0; j < TABLES_COUNT - 1; j++) { - statement.executeUpdate("ALTER TABLE table" + (j + 1) + " ADD COLUMN table" + j + "_id INT"); - statement.executeUpdate("ALTER TABLE table" + (j + 1) + " ADD CONSTRAINT id_constraint FOREIGN KEY(table" + j + "_id) REFERENCES table" + j + "(id)"); - } - statement.close(); + connection.setCatalog("db0"); - connection.close(); + statement = connection.createStatement(); + for (int j = 0; j < TABLES_COUNT - 1; j++) { + String update1 = "ALTER TABLE table" + (j + 1) + " ADD COLUMN table" + j + "_id INT"; + statement.executeUpdate(update1); + //System.out.println(update1); + String update2 = "ALTER TABLE table" + (j + 1) + " ADD CONSTRAINT id_constraint_" + (j + 1) + " FOREIGN KEY(table" + j + "_id) REFERENCES table" + j + "(id)"; + //System.out.println(update2); + statement.executeUpdate(update2); + } + } catch (SQLException e) { + e.printStackTrace(System.err); + throw e; + } finally { + if (statement != null) { + statement.close(); + } + if (connection != null) { + connection.close(); + } + } } @Test @@ -96,11 +115,14 @@ public class TestQuery { assertTrue(!tapSchemaHandler.getSchemas().isEmpty()); } + @Ignore @Test public void createNewAndUpdate() throws SQLException { System.out.println("TEST createNewAndUpdate STARTED"); try { + removeTestingDatabases(credentials); + setUpTestingDatabases(credentials); // TEST first TapSchema creation @@ -109,24 +131,80 @@ public class TestQuery { SchemaEntity db0 = tsh.getNewSchema("db0"); tsh.addSchema(db0); + List db0Keys = tsh.getAllSchemaKeys("db0"); + assertEquals(db0Keys.size(), TABLES_COUNT - 1); + // Print keys: + for (KeyEntity schemaKey : db0Keys) { + System.out.println(schemaKey.getFromTableName() + " -> " + schemaKey.getTargetTableName()); + for (KeyColumnEntity keyColumn : schemaKey.getKeyColumns()) { + System.out.println("\t" + schemaKey.getFromTableName() + "." + keyColumn.getFromColumn() + " -> " + schemaKey.getTargetTableName() + "." + keyColumn.getTargetColumn()); + } + } + // Test add, remove and re-add of a table - List db0Tables = tsh.getAllTables(db0.getName()); + List db0Tables = tsh.getAllTables("db0"); - TableEntity db0table0 = tsh.getNewTable(db0.getName(), "table0"); + TableEntity db0table0 = tsh.getNewTable("db0", "table0"); assertEquals(db0table0.getName(), "db0.table0"); - db0.addTable(db0table0); - TableEntity db0table1 = tsh.getNewTable(db0.getName(), "table1"); - - //db0table1.getFromKeys(); - - + tsh.addTable("db0", db0table0); + TableEntity db0table1 = tsh.getNewTable("db0", "table1"); + tsh.addTable("db0", db0table1); + + // Test key adding + assertTrue(db0table0.getFromKeys().isEmpty()); + assertEquals(db0table0.getTargetKeys().size(), 1); + + assertTrue(db0table1.getTargetKeys().isEmpty()); + assertEquals(db0table1.getFromKeys().size(), 1); + + KeyEntity keyEntity = db0table1.getFromKeys().get(0); + assertEquals(keyEntity.getFromTableName(), "db0.table1"); + assertEquals(keyEntity.getKeyColumns().get(0).getFromColumn(), "table0_id"); + assertEquals(keyEntity.getTargetTableName(), "db0.table0"); + assertEquals(keyEntity.getKeyColumns().get(0).getTargetColumn(), "id"); + + TableEntity db0table2 = tsh.getNewTable("db0", "table2"); + tsh.addTable("db0", db0table2); + assertEquals(db0table1.getFromKeys().size(), 1); + assertEquals(db0table1.getTargetKeys().size(), 1); + assertEquals(db0table2.getFromKeys().size(), 1); + + // Test key removing + assertEquals(db0.getTables().size(), 3); + tsh.removeTable(db0, db0table2); + assertEquals(db0.getTables().size(), 2); + assertEquals(db0table1.getFromKeys().size(), 1); + assertTrue(db0table1.getTargetKeys().isEmpty()); + + tsh.addTable("db0", db0table2); + assertEquals(db0table1.getFromKeys().size(), 1); + assertEquals(db0table1.getTargetKeys().size(), 1); + assertEquals(db0table2.getFromKeys().size(), 1); + + assertEquals(tsh.getForeignKeyReference("db0", "db0.table1", "table0_id"), "table0.id"); + assertNull(tsh.getForeignKeyReference("db0", "db0.table1", "id")); + + tsh.removeTable(db0, db0table1); + assertTrue(db0table0.getFromKeys().isEmpty()); + assertTrue(db0table0.getTargetKeys().isEmpty()); + assertTrue(db0table2.getFromKeys().isEmpty()); + assertTrue(db0table2.getTargetKeys().isEmpty()); + + // Test add column + ColumnInfo ci = tsh.getColumnInfo("db0", "db0.table1").get(0); + ColumnEntity columnEntity = ci.getColumnEntity(); + tsh.addColumn(db0table1, columnEntity); + assertNotNull(columnEntity.getTable()); + + // Test save tsh.save(); assertNotNull(db0.getTables().get("db0.table0")); + // Test load saved tsh = new TapSchemaHandler(credentials, "test_tap_schema", true); db0 = tsh.getSchemas().iterator().next(); - db0.getTables().remove("db0.table0"); - assertNull(db0.getTables().get("db0.table0")); + tsh.removeTable(db0, db0.getTable("db0.table0")); + assertNull(db0.getTable("db0.table0")); tsh.save(); tsh = new TapSchemaHandler(credentials, "test_tap_schema", true); @@ -134,9 +212,8 @@ public class TestQuery { assertNull(db0.getTables().get("db0.table0")); // Test key constraints - db0.addTable(db0table0); - - + tsh.addTable("db0", db0table0); + // assertEquals(db0Tables.size(), TABLES_COUNT); // for (String tableName : db0Tables) { // tapSchemaHandler.get @@ -177,7 +254,7 @@ public class TestQuery { } catch (SQLException e) { throw e; } finally { - removeTestingDatabases(credentials); + //removeTestingDatabases(credentials); } } @@ -197,7 +274,6 @@ public class TestQuery { // // ColumnEntity column = new ColumnEntity("table1_column1"); // table.addColumn(column); - //TapSchemaHandler tapSchemaHandler = new TapSchemaHandler(credentials, "TestJPA", false); //statement = connection.createStatement(); // String tapschemaName = tapSchema.getName();