Commit 47a0d660 authored by Sonia Zorba's avatar Sonia Zorba
Browse files

New version: removed Wicket and managed with JSP, JSON models and AJAX

parent 30de91f6
Loading
Loading
Loading
Loading

new/pom.xml

0 → 100644
+88 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>it.inaf.oats.ia2.tap</groupId>
    <artifactId>TSM</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>TSM</name>

    <properties>
        <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    
    <dependencies>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.37</version>
            <type>jar</type>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.5</version>
        </dependency>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-web-api</artifactId>
            <version>7.0</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                    <compilerArguments>
                        <endorseddirs>${endorsed.dir}</endorseddirs>
                    </compilerArguments>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.3</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.6</version>
                <executions>
                    <execution>
                        <phase>validate</phase>
                        <goals>
                            <goal>copy</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${endorsed.dir}</outputDirectory>
                            <silent>true</silent>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>javax</groupId>
                                    <artifactId>javaee-endorsed-api</artifactId>
                                    <version>7.0</version>
                                    <type>jar</type>
                                </artifactItem>
                            </artifactItems>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>
+34 −0
Original line number Diff line number Diff line
package it.inaf.oats.ia2.tap.tsm.datalayer;

import it.inaf.oats.ia2.tap.tsm.model.Credentials;
import java.sql.Connection;
import java.sql.SQLException;

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

    private final Credentials credentials;

    public ConnectionManager(Credentials credentials) {
        this.credentials = credentials;
    }

    public void connect() throws SQLException {
        Connection connection = null;
        try {
            connection = credentials.getConnection();
            use(connection);
        } catch (SQLException e) {
            throw e;
        } finally {
            if (connection != null) {
                connection.close();
            }
        }
    }

    public abstract void use(Connection connection) throws SQLException;
}
+42 −0
Original line number Diff line number Diff line
package it.inaf.oats.ia2.tap.tsm.datalayer;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

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

    private final String query;

    public ResultSetReader(String query) {
        this.query = query;
    }

    public abstract void manipulateItem(ResultSet resultSet) throws SQLException;

    public final void read(Connection connection) throws SQLException {
        Statement statement = null;
        ResultSet resultSet = null;
        try {
            statement = connection.createStatement();
            resultSet = statement.executeQuery(query);
            while (resultSet.next()) {
                manipulateItem(resultSet);
            }
        } catch (SQLException e) {
            throw e;
        } finally {
            if (resultSet != null) {
                resultSet.close();
            }
            if (statement != null) {
                statement.close();
            }
        }
    }
}
+94 −0
Original line number Diff line number Diff line
package it.inaf.oats.ia2.tap.tsm.datalayer;

import it.inaf.oats.ia2.tap.tsm.model.*;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

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

    public static ArrayList<String> getAllTablesNames(Connection connection, String schemaName) throws SQLException {
        final ArrayList<String> allTables = new ArrayList<String>();

        (new ResultSetReader("SHOW TABLES FROM " + schemaName + ";") {

            @Override
            public void manipulateItem(ResultSet resultSet) throws SQLException {
                allTables.add(resultSet.getString(1));
            }
        }).read(connection);

        return allTables;
    }

    public static void setKeysMap(final Connection connection, final String tapSchemaName, final String schemaName, final Map<String, Key> keysMap) throws SQLException {

        final HashMap<String, Key> keys = new HashMap<String, Key>();

        (new ResultSetReader("SELECT k.CONSTRAINT_NAME, 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 {
                        String constraintName = resultSet.getString("k.CONSTRAINT_NAME");
                        String fromTable = schemaName + "." + resultSet.getString("k.TABLE_NAME");
                        String targetTable = schemaName + "." + resultSet.getString("k.REFERENCED_TABLE_NAME");
                        String fromColumn = resultSet.getString("k.COLUMN_NAME");
                        String targetColumn = resultSet.getString("k.REFERENCED_COLUMN_NAME");

                        Key key = keys.get(constraintName);
                        if (key == null) {
                            String keyId = "" + keys.size() + 1;
                            key = new Key(keyId, fromTable, targetTable);
                            keys.put(constraintName, key);
                            keysMap.put(keyId, key);
                        }
                        key.addKeyColumn(fromColumn, targetColumn);
                    }
                }).read(connection);
    }

    public static Table loadTable(final Connection connection, final String tapSchemaName, final Schema schema, final String tableName) throws SQLException {
        Table table = new Table(connection, tapSchemaName, schema, tableName, true);
        for (IDbEntity column : table.getAllEntities()) {
            // Select all table columns by default
            column.setStatus(Status.SELECTED_TO_SAVE);
        }
        return table;
    }

    public static void loadSavedTables(final Connection connection, final String tapSchemaName, final Schema schema, final boolean toCreate) throws SQLException {

        (new ResultSetReader("SELECT * FROM " + tapSchemaName + ".tables "
                + "WHERE schema_name='" + schema.getName() + "';") {

                    @Override
                    public void manipulateItem(ResultSet resultSet) throws SQLException {

                        String tableName = resultSet.getString("table_name");
                        // Removing schema prefix from table name
                        if (tableName.startsWith(schema.getName() + ".")) {
                            tableName = tableName.substring(schema.getName().length() + 1);
                        }

                        Table table = new Table(connection, tapSchemaName, schema, tableName, toCreate);
                        table.setStatus(Status.SELECTED_SAVED);
                        schema.addEntity(tableName, table);
                    }
                }).read(connection);
    }

}
+152 −0
Original line number Diff line number Diff line
package it.inaf.oats.ia2.tap.tsm.datalayer;

import it.inaf.oats.ia2.tap.tsm.model.*;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;

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

    private static boolean equalsOneOf(String string, String... values) {
        for (String value : values) {
            if (string.equals(value)) {
                return true;
            }
        }
        return false;
    }

    // updates variables of current column structure
    public static void loadAllColumns(Connection connection, final String tapSchemaName, final String schemaName, final Table table) throws SQLException {

        (new ResultSetReader("SHOW COLUMNS FROM " + schemaName + "." + table.getName() + ";") {

            @Override
            public void manipulateItem(ResultSet resultSet) throws SQLException {

                Column column = new Column();
                column.setTableName(table.getName());
                column.setStatus(Status.NOT_SELECTED);

                // Key
                String key = resultSet.getString("Key");
                column.setPrincipal(key.equals("PRI"));
                column.setIsPrimaryKey(key.equals("PRI"));
                column.setIndexed(equalsOneOf(key, "PRI", "UNI", "MUL"));

                // Datatype and Size
                String type = resultSet.getString("Type").toLowerCase();

                int size = 0;
                String datatype;
                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:" + type.toUpperCase();
                }
                column.setDatatype(datatype);
                column.setSize(size);

                // Column name
                String columnName = resultSet.getString("Field");
                column.setName(columnName);

                // STD
                boolean std = false;
                if ((schemaName.equals(tapSchemaName))
                        && !equalsOneOf(columnName, "schemaID", "tableID", "columnID", "keyID", "key_columnID")) {
                    std = true;
                }
                column.setStd(std);

                column.setIsForeignKey(table.isForeignKey(columnName));
                table.addEntity(columnName, column);
            }
        }).read(connection);
    }

    public static void loadSavedColumns(Connection connection, final String tapSchemaName, final String schemaName, final Table table) throws SQLException {

        (new ResultSetReader("SELECT * FROM " + tapSchemaName + ".columns "
                + "WHERE table_name='" + schemaName + "." + table.getName() + "';") {

                    @Override
                    public void manipulateItem(ResultSet columnsResultSet) throws SQLException {

                        Column column = table.getColumn(columnsResultSet.getString("column_name"));
                        column.setStatus(Status.SELECTED_SAVED);

                        // updates variables of current column structure
                        column.setTableName(columnsResultSet.getString("table_name"));
                        column.setUtype(columnsResultSet.getString("utype"));
                        column.setUcd(columnsResultSet.getString("ucd"));
                        column.setUnit(columnsResultSet.getString("unit"));
                        column.setDescription(columnsResultSet.getString("description"));
                        column.setDatatype(columnsResultSet.getString("datatype"));
                        column.setSize(columnsResultSet.getInt("size"));
                        column.setPrincipal(columnsResultSet.getInt("principal") == 1);
                        column.setIndexed(columnsResultSet.getInt("indexed") == 1);
                        column.setStd(columnsResultSet.getInt("std") == 1);
                        
                        //column.checked = true;
                        //search if foreign key
//                        statement = connection.createStatement();
//                        queryString = "SELECT * FROM " + tapschemaName + ".keys "
//                        + "INNER JOIN " + tapschemaName + ".key_columns "
//                        + "USING (key_id) "
//                        + "WHERE " + tapschemaName + ".keys.from_table='" + column.getTableName() + "' "
//                        + "AND " + tapschemaName + ".key_columns.from_column='" + column.getColumnName() + "';";
//                        keysResultSet = statement.executeQuery(queryString);
//
//                        statement = connection.createStatement();
//                        queryString = "SELECT * FROM " + tapschemaName + ".keys "
//                        + "INNER JOIN " + tapschemaName + ".key_columns "
//                        + "USING (key_id) "
//                        + "WHERE " + tapschemaName + ".keys.target_table='" + column.getTableName() + "' "
//                        + "AND " + tapschemaName + ".key_columns.target_column='" + column.getColumnName() + "';";
//                        foreignKeysResultSet = statement.executeQuery(queryString);
//                        while (keysResultSet.next()) {
//                            while (foreignKeysResultSet.next()) {
//                                for (Key key : schema.keysMap.values()) {
//                                    for (KeyColumn key_column : schema.key_columnsMap.values()) {
//                                        if (key.from_table.equals(keysResultSet.getString("from_table"))
//                                        && key_column.from_column.equals(foreignKeysResultSet.getString("from_column"))
//                                        && keysResultSet.getString("key_id").equals(foreignKeysResultSet.getString("key_id"))) {
//                                            // updates variables of current key structure
//                                            key.key_id = keysResultSet.getString("key_id");
//                                            key.utype = keysResultSet.getString("utype");
//                                            key.description = keysResultSet.getString("description");
//
//                                            // updates variables of current key_column structure
//                                            key_column.key_id = foreignKeysResultSet.getString("key_id");
//                                        }
//                                    }
//                                }
//                            }
//                        }
                    }
                }).read(connection);
    }
}
Loading