Commit 5df3d36f authored by Sonia Zorba's avatar Sonia Zorba
Browse files

JavaScript and models changes

parent 1f42c1a6
Loading
Loading
Loading
Loading
+11 −0
Original line number Diff line number Diff line
@@ -33,6 +33,17 @@
            <version>7.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>com.flipkart.zjsonpatch</groupId>
            <artifactId>zjsonpatch</artifactId>
            <version>0.2.1</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.10</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
+159 −91
Original line number Diff line number Diff line
@@ -13,6 +13,7 @@ import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashSet;

@@ -25,7 +26,6 @@ public class UpdateManager {
    private final Credentials credentials;
    private final TapSchema tapSchema;

    private final ArrayList<Key> keysReaded;
    private final ArrayList<Integer> keysToRemove;

    private final ArrayList<Schema> schemasToAdd;
@@ -44,7 +44,6 @@ public class UpdateManager {
        this.credentials = credentials;
        this.tapSchema = tapSchema;

        keysReaded = new ArrayList<Key>();
        keysToRemove = new ArrayList<Integer>();

        schemasToAdd = new ArrayList<Schema>();
@@ -60,20 +59,85 @@ public class UpdateManager {
        columnsToUpdate = new ArrayList<Column>();
    }

    private void addKey(String fromTable, String targetTable, String fromColumn, String targetColumn) {
        Key key = null;
        for (Key k : keysReaded) {
            if (k.getFromTable().equals(fromTable) && k.getTargetTable().equals(targetTable)) {
                key = k;
            }
        }
    private void createTapSchema(Connection connection) throws SQLException {
        Statement statement = null;

        if (key == null) {
            key = new Key(fromTable, targetTable);
            keysReaded.add(key);
        try {
            statement = connection.createStatement();

            String tapschemaName = tapSchema.getName();

            //creates database
            String queryString = "CREATE DATABASE IF NOT EXISTS " + tapschemaName + ";";
            statement.executeUpdate(queryString);

            //creates schemas table
            queryString = "CREATE TABLE IF NOT EXISTS " + tapschemaName + ".`schemas` ("
                    + "schema_name   varchar(64), "
                    + "utype         varchar(512) NULL, "
                    + "description   varchar(512) NULL, "
                    + "schemaID      bigint, "
                    + "PRIMARY KEY (schema_name));";
            statement.executeUpdate(queryString);

            //creates tables table
            queryString = "CREATE TABLE IF NOT EXISTS " + tapschemaName + ".tables ("
                    + "schema_name   varchar(64), "
                    + "table_name    varchar(128), "
                    + "table_type    varchar(8), "
                    + "utype         varchar(512) NULL, "
                    + "description   varchar(512) NULL, "
                    + "tableID       bigint, "
                    + "PRIMARY KEY (table_name), "
                    + "FOREIGN KEY (schema_name) REFERENCES " + tapschemaName + ".`schemas` (schema_name));";
            statement.executeUpdate(queryString);

            //creates columns table
            queryString = "CREATE TABLE IF NOT EXISTS " + tapschemaName + ".columns ("
                    + "table_name    varchar(128), "
                    + "column_name   varchar(64), "
                    + "utype         varchar(512) NULL, "
                    + "ucd           varchar(64)  NULL, "
                    + "unit          varchar(64)  NULL, "
                    + "description   varchar(512) NULL, "
                    + "datatype      varchar(64)  NOT NULL, "
                    + "size          integer      NULL, "
                    + "principal     integer      NOT NULL, "
                    + "indexed       integer      NOT NULL, "
                    + "std           integer      NOT NULL, "
                    + "columnID      bigint, "
                    + "PRIMARY KEY (table_name, column_name), "
                    + "FOREIGN KEY (table_name) REFERENCES " + tapschemaName + ".tables (table_name));";
            statement.executeUpdate(queryString);

            //creates keys table
            queryString = "CREATE TABLE IF NOT EXISTS " + tapschemaName + ".keys ("
                    + "key_id        varchar(64), "
                    + "from_table    varchar(128) NOT NULL, "
                    + "target_table  varchar(128) NOT NULL, "
                    + "utype         varchar(512) NULL, "
                    + "description   varchar(512) NULL, "
                    + "keyID         bigint, "
                    + "PRIMARY KEY (key_id), "
                    + "FOREIGN KEY (from_table) REFERENCES " + tapschemaName + ".tables (table_name), "
                    + "FOREIGN KEY (target_table) REFERENCES " + tapschemaName + ".tables (table_name));";
            statement.executeUpdate(queryString);

            //creates key_columns table
            queryString = "CREATE TABLE IF NOT EXISTS " + tapschemaName + ".key_columns ("
                    + "key_id          varchar(64), "
                    + "from_column     varchar(64)   NOT NULL, "
                    + "target_column   varchar(64) NOT NULL, "
                    + "key_columnID    bigint, "
                    + "FOREIGN KEY (key_id) REFERENCES " + tapschemaName + ".keys (key_id));";
            statement.executeUpdate(queryString);
        } catch (SQLException e) {
            throw e;
        } finally {
            if (statement != null) {
                statement.close();
            }
        }

        key.addKeyColumn(fromColumn, targetColumn);
    }

    public void update() {
@@ -165,34 +229,17 @@ public class UpdateManager {

            /* ---------- Find keys to add ---------- */
            HashSet<String> schemasToCheck = new HashSet<String>(); // schema in which could be keys to add.
            ArrayList<Key> keysToCheck = new ArrayList<Key>();
            for (Table table : tablesToAdd) {
                schemasToCheck.add(table.getSchemaName());
            }

            for (final String schemaName : schemasToCheck) {
                (new ResultSetReader("SELECT k.TABLE_NAME, k.COLUMN_NAME, "
                        + "k.REFERENCED_TABLE_NAME, k.REFERENCED_COLUMN_NAME "
                        + "FROM information_schema.TABLE_CONSTRAINTS i "
                        + "LEFT JOIN information_schema.KEY_COLUMN_USAGE k "
                        + "ON i.CONSTRAINT_NAME = k.CONSTRAINT_NAME "
                        + "WHERE i.CONSTRAINT_TYPE = 'FOREIGN KEY' "
                        + "AND i.TABLE_SCHEMA = '" + schemaName + "' "
                        + "AND k.TABLE_SCHEMA = '" + schemaName + "'") {

                            @Override
                            public void manipulateItem(ResultSet resultSet) throws SQLException {
                                addKey(
                                        schemaName + "." + resultSet.getString("k.TABLE_NAME"),
                                        schemaName + "." + resultSet.getString("k.REFERENCED_TABLE_NAME"),
                                        resultSet.getString("k.COLUMN_NAME"),
                                        resultSet.getString("k.REFERENCED_COLUMN_NAME")
                                );
                String schemaName = table.getSchemaName();
                if (!schemasToCheck.contains(schemaName)) {
                    schemasToCheck.add(schemaName);
                    keysToCheck.addAll(tapSchema.getSchema(schemaName).getKeys());
                }
                        }).read(connection);
            }

            ArrayList<Key> keysToAdd = new ArrayList<Key>();
            for (Key key : keysReaded) {
            for (Key key : keysToCheck) {
                for (Table table : tablesToAdd) {
                    String tableName = table.getFullName();
                    if (key.getFromTable().equals(tableName) || key.getTargetTable().equals(tableName)) {
@@ -201,6 +248,10 @@ public class UpdateManager {
                }
            }

            if (tapSchema.toCreate()) {
                createTapSchema(connection);
            }

            // Use tapSchema database
            connection.setCatalog(tapSchema.getName());

@@ -226,8 +277,11 @@ public class UpdateManager {

            }

            int newKeyId = 1; // key id is varchar and needs to be incremented manually. This is the start value.

            /* ---------- Find max key id after keys removal ---------- */
            final ArrayList<Integer> maxKey = new ArrayList<Integer>();
            // if some keys are already saved in the database we need to find their max value
            if (!tapSchema.toCreate()) {
                String query = "SELECT MAX(CAST(key_id AS UNSIGNED)) max_key from `keys`";

                if (keysToRemove.size() > 0) {
@@ -242,20 +296,31 @@ public class UpdateManager {
                    query += " WHERE key_id NOT IN (" + keysToRemoveStr + ")";
                }

            (new ResultSetReader(query) {

                @Override
                public void manipulateItem(ResultSet resultSet) throws SQLException {
                    maxKey.add(resultSet.getInt("max_key"));
                Statement s = null;
                ResultSet rs = null;
                try {
                    s = connection.createStatement();
                    rs = s.executeQuery(query);
                    while (rs.next()) {
                        newKeyId = rs.getInt("max_key") + 1;
                    }
                } catch (SQLException e) {
                    throw e;
                } finally {
                    if (rs != null) {
                        rs.close();
                    }
                    if (s != null) {
                        s.close();
                    }
                }
            }
            }).read(connection);

            int newKeyId = maxKey.isEmpty() ? 1 : maxKey.get(0) + 1; // key id start offset

            // Start update
            connection.setAutoCommit(false); // start transaction

            // REMOVE ELEMENTS
            if (!tapSchema.toCreate()) {
                for (int keyId : keysToRemove) {
                    statement = connection.prepareStatement("DELETE FROM key_columns WHERE key_id = ?");
                    statement.setString(1, keyId + "");
@@ -283,6 +348,7 @@ public class UpdateManager {
                    statement.setString(1, schemaName);
                    statement.executeUpdate();
                }
            }

            // INSERT ELEMENTS
            for (Schema schema : schemasToAdd) {
@@ -322,6 +388,7 @@ public class UpdateManager {
            }

            //UPDATE ELEMENTS
            if (!tapSchema.toCreate()) {
                for (Schema schema : schemasToUpdate) {
                    schema.update(connection);
                }
@@ -333,6 +400,7 @@ public class UpdateManager {
                for (Column column : columnsToUpdate) {
                    column.update(connection);
                }
            }

            connection.commit();
        } catch (SQLException e) {
+5 −0
Original line number Diff line number Diff line
@@ -10,12 +10,17 @@ import java.sql.SQLException;
 */
public class Column extends DbEntity {

    // editable properties
    public static final String UTYPE = "utype";
    public static final String UCD = "ucd";
    public static final String UNIT = "unit";
    public static final String DESCRIPTION = "description";
    public static final String STD = "std";
    
    // uneditable properties
    //public static final String DESCRIPTION = "description";
    
    
    private String tableName;
    private String name;

+30 −28
Original line number Diff line number Diff line
@@ -7,12 +7,8 @@ import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author Sonia Zorba <zorba at oats.inaf.it>
 * Object that keeps track of changes in the value that it wraps.
 */
public abstract class DbEntity {

    /**  Object that keeps track of changes in the value that it wraps. */
class EditableValue {

    String originalValue;
@@ -43,6 +39,12 @@ public abstract class DbEntity {
    }
}

/**
 *
 * @author Sonia Zorba <zorba at oats.inaf.it>
 */
public abstract class DbEntity {

    private Status status;

    private final HashMap<String, EditableValue> values = new HashMap<String, EditableValue>();
+50 −4
Original line number Diff line number Diff line
package it.inaf.oats.ia2.tap.tsm.model;

import it.inaf.oats.ia2.tap.tsm.datalayer.ResultSetReader;
import it.inaf.oats.ia2.tap.tsm.datalayer.SchemaDL;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
@@ -27,7 +29,8 @@ public class Schema extends DbEntity implements IDbEntitiesContainer, Serializab
    //private String utype;
    //private String description;
    private final Map<String, Table> tablesMap;
    private final Map<String, Key> keysMap;
    //private final Map<String, Key> keysMap;
    private final List<Key> keysInfo;

    private String selectedTable;

@@ -43,9 +46,29 @@ public class Schema extends DbEntity implements IDbEntitiesContainer, Serializab
            tablesMap.put(tableName, null);
        }

        keysMap = new HashMap<String, Key>();
        SchemaDL.setKeysMap(connection, tapSchemaName, schemaName, keysMap);
        keysInfo = new ArrayList<Key>();
        (new ResultSetReader("SELECT k.TABLE_NAME, k.COLUMN_NAME, "
                + "k.REFERENCED_TABLE_NAME, k.REFERENCED_COLUMN_NAME "
                + "FROM information_schema.TABLE_CONSTRAINTS i "
                + "LEFT JOIN information_schema.KEY_COLUMN_USAGE k "
                + "ON i.CONSTRAINT_NAME = k.CONSTRAINT_NAME "
                + "WHERE i.CONSTRAINT_TYPE = 'FOREIGN KEY' "
                + "AND i.TABLE_SCHEMA = '" + schemaName + "' "
                + "AND k.TABLE_SCHEMA = '" + schemaName + "'") {

                    @Override
                    public void manipulateItem(ResultSet resultSet) throws SQLException {
                        addKey(
                                resultSet.getString("k.TABLE_NAME"),
                                resultSet.getString("k.REFERENCED_TABLE_NAME"),
                                resultSet.getString("k.COLUMN_NAME"),
                                resultSet.getString("k.REFERENCED_COLUMN_NAME")
                        );
                    }
                }).read(connection);

        //keysMap = new HashMap<String, Key>();
        //SchemaDL.setKeysMap(connection, tapSchemaName, schemaName, keysMap);
        if (!toCreate) {
            SchemaDL.loadSavedTables(connection, tapSchemaName, (this), toCreate);
        }
@@ -54,6 +77,25 @@ public class Schema extends DbEntity implements IDbEntitiesContainer, Serializab
//        description = new EditableValue<String>();
    }

    private void addKey(String fromTable, String targetTable, String fromColumn, String targetColumn) {
        fromTable = getName() + "." + fromTable;
        targetTable = getName() + "." + targetTable;

        Key key = null;
        for (Key k : keysInfo) {
            if (k.getFromTable().equals(fromTable) && k.getTargetTable().equals(targetTable)) {
                key = k;
            }
        }

        if (key == null) {
            key = new Key(fromTable, targetTable);
            keysInfo.add(key);
        }

        key.addKeyColumn(fromColumn, targetColumn);
    }

    @Override
    public void loadEntities(Connection connection, List<String> selectedTables) throws SQLException {
        for (String tableName : selectedTables) {
@@ -142,7 +184,7 @@ public class Schema extends DbEntity implements IDbEntitiesContainer, Serializab
    }

    public boolean isForeignKey(String tableName, String columnName) {
        for (Key key : keysMap.values()) {
        for (Key key : keysInfo) {
            if (key.getFromTable().equals(tableName) && key.hasFromKeyColumn(columnName)) {
                return true;
            }
@@ -214,4 +256,8 @@ public class Schema extends DbEntity implements IDbEntitiesContainer, Serializab
            }
        }
    }

    public List<Key> getKeys() {
        return keysInfo;
    }
}
Loading