Commit 51e701e8 authored by Sonia Zorba's avatar Sonia Zorba
Browse files

Postgres support implementation started

parent fddbc63a
Loading
Loading
Loading
Loading
+3 −2
Original line number Diff line number Diff line
package it.inaf.oats.ia2.tapschemamanager.businesslayer;

import it.inaf.oats.ia2.tapschemamanager.datalayer.Credentials;
import it.inaf.oats.ia2.tapschemamanager.datalayer.DBWrapper;
import it.inaf.oats.ia2.tapschemamanager.datalayer.SchemaEntity;
import it.inaf.oats.ia2.tapschemamanager.datalayer.TapSchemaHandler;
import java.io.Serializable;
@@ -26,11 +27,11 @@ public class TapSchema implements EntityWrapperContainer<Schema>, Serializable {
    private String selectedSchema;
    private final Map<String, Schema> schemas;

    public TapSchema(Credentials sourceCredentials, Credentials tapSchemaCredentials, String tapSchemaName, boolean exists) throws SQLException {
    public TapSchema(DBWrapper dbWrapper, String tapSchemaName, boolean exists) throws SQLException {
        this.name = tapSchemaName;
        schemas = new TreeMap<String, Schema>(String.CASE_INSENSITIVE_ORDER);

        this.tapSchemaHandler = new TapSchemaHandler(sourceCredentials, tapSchemaCredentials, tapSchemaName, exists);
        this.tapSchemaHandler = new TapSchemaHandler(dbWrapper, tapSchemaName, exists);

        if (exists) {
            for (SchemaEntity schemaEntity : tapSchemaHandler.getSchemas()) {
+8 −12
Original line number Diff line number Diff line
package it.inaf.oats.ia2.tapschemamanager.webapp;

import it.inaf.oats.ia2.tapschemamanager.datalayer.Credentials;
import it.inaf.oats.ia2.tapschemamanager.datalayer.DBWrapper;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.SQLException;
import javax.annotation.PostConstruct;
import javax.enterprise.context.Conversation;
@@ -56,22 +56,18 @@ public class CredentialsBean implements Serializable {

        loginError = null;

        if (!separateCredentials) {
            tapSchemaCredentials.setHostname(sourceCredentials.getHostname());
            tapSchemaCredentials.setPort(sourceCredentials.getPort());
            tapSchemaCredentials.setUsername(sourceCredentials.getUsername());
            tapSchemaCredentials.setPassword(sourceCredentials.getPassword());
        DBWrapper dbWrapper;
        if (separateCredentials) {
            dbWrapper = new DBWrapper(sourceCredentials, tapSchemaCredentials);
        } else {
            dbWrapper = new DBWrapper(sourceCredentials);
        }

        try {
            Connection connection = sourceCredentials.getConnection();
            connection.close();
            connection = tapSchemaCredentials.getConnection();
            connection.close();
            dbWrapper.testConnections();
            conversation.setTimeout(30 * 60000L); // 30 minutes
            conversation.begin();
            schemaSelectionBean.setSourceCredentials(sourceCredentials);
            schemaSelectionBean.setTapSchemaCredentials(tapSchemaCredentials);
            schemaSelectionBean.setDbWrapper(dbWrapper);
            return "schemaSelection.xhtml?faces-redirect=true";
        } catch (SQLException e) {
            log.error("Exception caught", e);
+33 −57
Original line number Diff line number Diff line
package it.inaf.oats.ia2.tapschemamanager.webapp;

import it.inaf.oats.ia2.tapschemamanager.businesslayer.TapSchema;
import it.inaf.oats.ia2.tapschemamanager.datalayer.Credentials;
import it.inaf.oats.ia2.tapschemamanager.datalayer.DBWrapper;
import it.inaf.oats.ia2.tapschemamanager.datalayer.DataProvider;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
@@ -37,8 +36,9 @@ public class SchemaSelectionBean implements Serializable {
    @Inject
    TapSchemaEditingBean tapSchemaEditingBean;

    private Credentials sourceCredentials;
    private Credentials tapSchemaCredentials;
//    private Credentials sourceCredentials;
//    private Credentials tapSchemaCredentials;
    private DBWrapper dbWrapper;

    private String selectedRadioOption;

@@ -66,51 +66,30 @@ public class SchemaSelectionBean implements Serializable {
        if (!ajaxRequest && !validationFailed) {

            // Loading all schemas of the source database
            Connection sourceConnection = null;
            try {
                sourceConnection = sourceCredentials.getConnection();
                allSchemas = DataProvider.getAllSchemasNames(sourceConnection);
                allSchemas = DataProvider.getAllSchemasNames(dbWrapper);
                setSelectedSchemas(new ArrayList<String>());
            } catch (SQLException e) {
                throw new RuntimeException(e);
            } finally {
                try {
                    if (sourceConnection != null) {
                        sourceConnection.close();
                    }
                } catch (SQLException e) {
                    log.error("Exception caught", e);
                }
            }

            // Loading all schemas of the TAP_SCHEMA database
            Connection tapSchemaConnection = null;
            try {
                tapSchemaConnection = tapSchemaCredentials.getConnection();

                allTAPSchemas = DataProvider.getAllTAPSchemasNames(
                        tapSchemaConnection, DataProvider.getAllSchemasNames(tapSchemaConnection));
                        dbWrapper, DataProvider.getAllSchemasNames(dbWrapper));

                if (!allTAPSchemas.isEmpty()) {
                    this.selectedTAPSchema = allTAPSchemas.get(0);
                    loadExposedSchemas(tapSchemaConnection);
                    loadExposedSchemas();
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            } finally {
                try {
                    if (tapSchemaConnection != null) {
                        tapSchemaConnection.close();
                    }
                } catch (SQLException e) {
                    log.error("Exception caught", e);
                }
            }
        }
    }

    private void loadExposedSchemas(Connection tapSchemaConnection) throws SQLException {
        List<String> schemas = DataProvider.getExposedSchemas(tapSchemaConnection, selectedTAPSchema);
    private void loadExposedSchemas() throws SQLException {
        List<String> schemas = DataProvider.getExposedSchemas(dbWrapper, selectedTAPSchema);
        exposedSchemas = "";
        for (int i = 0; i < schemas.size(); i++) {
            exposedSchemas += schemas.get(i);
@@ -147,22 +126,12 @@ public class SchemaSelectionBean implements Serializable {
    public void setSelectedTAPSchema(String selectedTAPSchema) {
        this.selectedTAPSchema = selectedTAPSchema;

        Connection connection = null;
        try {
            connection = tapSchemaCredentials.getConnection();
            loadExposedSchemas(connection);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
        try {
                if (connection != null) {
                    connection.close();
                }
            loadExposedSchemas();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    }

    public List<String> getSelectedSchemas() {
        return selectedSchemas;
@@ -179,7 +148,7 @@ public class SchemaSelectionBean implements Serializable {

    public String edit() {
        try {
            return loadTapSchema(new TapSchema(sourceCredentials, tapSchemaCredentials, selectedTAPSchema, true));
            return loadTapSchema(new TapSchema(dbWrapper, selectedTAPSchema, true));
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
@@ -187,7 +156,7 @@ public class SchemaSelectionBean implements Serializable {

    public String create() {
        try {
            TapSchema tapSchema = new TapSchema(sourceCredentials, tapSchemaCredentials, tapSchemaName, false);
            TapSchema tapSchema = new TapSchema(dbWrapper, tapSchemaName, false);
            for (String selectedSchema : selectedSchemas) {
                tapSchema.addEntityWrapper(selectedSchema);
            }
@@ -205,20 +174,27 @@ public class SchemaSelectionBean implements Serializable {
        this.tapSchemaName = tapSchemaName;
    }

    public Credentials getSourceCredentials() {
        return sourceCredentials;
    }

    public void setSourceCredentials(Credentials sourceCredentials) {
        this.sourceCredentials = sourceCredentials;
    }

    public Credentials getTapSchemaCredentials() {
        return tapSchemaCredentials;
    }

    public void setTapSchemaCredentials(Credentials tapSchemaCredentials) {
        this.tapSchemaCredentials = tapSchemaCredentials;
//    public Credentials getSourceCredentials() {
//        return sourceCredentials;
//    }
//
//    public void setSourceCredentials(Credentials sourceCredentials) {
//        this.sourceCredentials = sourceCredentials;
//    }
//
//    public Credentials getTapSchemaCredentials() {
//        return tapSchemaCredentials;
//    }
//
//    public void setTapSchemaCredentials(Credentials tapSchemaCredentials) {
//        this.tapSchemaCredentials = tapSchemaCredentials;
//    }
    public DBWrapper getDbWrapper() {
        return dbWrapper;
    }

    public void setDbWrapper(DBWrapper dbWrapper) {
        this.dbWrapper = dbWrapper;
    }

    public String logout() {
+24 −3
Original line number Diff line number Diff line
@@ -7,8 +7,8 @@
    <packaging>jar</packaging>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.6</maven.compiler.source>
        <maven.compiler.target>1.6</maven.compiler.target>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
@@ -21,7 +21,11 @@
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.37</version>
            <type>jar</type>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>9.3-1104-jdbc41</version>
        </dependency>
        <dependency> 
            <groupId>org.slf4j</groupId>
@@ -35,4 +39,21 @@
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <!--<plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.5.1</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.6</target>
                    <compilerVersion>1.7</compilerVersion>
                    <fork>true</fork>
                    <compilerArguments>
                        <bootclasspath>/usr/lib/jvm/java-6-openjdk-amd64/jre/lib/rt.jar</bootclasspath>
                    </compilerArguments>
                </configuration>
            </plugin>-->
        </plugins>
    </build>
</project>
 No newline at end of file
+153 −0
Original line number Diff line number Diff line
package it.inaf.oats.ia2.tapschemamanager.datalayer;

import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.SQLException;
import javax.sql.DataSource;
import org.postgresql.ds.PGPoolingDataSource;

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

    private static final long serialVersionUID = 1721030677924066695L;

    // Same credentials
    private Credentials credentials;

    // Separated credentials
    private Credentials sourceCredentials;
    private Credentials tapSchemaCredentials;

    private final DataSourcesWrapper dataSources;

    private DBWrapper() {
        dataSources = new DataSourcesWrapper();
    }

    /**
     * Constructor to use if the source schema credentials and the TAP_SCHEMA
     * credentials are the same.
     */
    public DBWrapper(Credentials credentials) {
        this();
        this.credentials = credentials;
    }

    /**
     * Constructor to use if the source schema credentials are different from
     * the TAP_SCHEMA credentials.
     */
    public DBWrapper(Credentials sourceCredentials, Credentials tapSchemaCredentials) {
        this();
        this.sourceCredentials = sourceCredentials;
        this.tapSchemaCredentials = tapSchemaCredentials;
    }

    public Connection getSourceConnection() throws SQLException {
        return dataSources.getSourceDataSource().getConnection();
    }

    public Connection getTapSchemaConnection() throws SQLException {
        return dataSources.getTapSchemaDataSource().getConnection();
    }

    public Credentials getSourceCredentials() {
        if (credentials != null) {
            return credentials;
        }
        return sourceCredentials;
    }

    public Credentials getTapSchemaCredentials() {
        if (credentials != null) {
            return credentials;
        }
        return tapSchemaCredentials;
    }

    public DatabaseType getSourceDatabaseType() {
        return getSourceCredentials().getDatabaseType();
    }

    public DatabaseType getTapSchemaDatabaseType() {
        return getTapSchemaCredentials().getDatabaseType();
    }

    public void testConnections() throws SQLException {
        Connection connection;
        if (credentials != null) {
            connection = dataSources.getSourceDataSource().getConnection();
            connection.close();
        } else {
            connection = dataSources.getSourceDataSource().getConnection();
            connection.close();
            connection = dataSources.getTapSchemaDataSource().getConnection();
            connection.close();
        }
    }

    private class DataSourcesWrapper implements Serializable {

        private static final long serialVersionUID = -7025255003212206748L;

        private transient DataSource dataSource;
        private transient DataSource sourceDataSource;
        private transient DataSource tapSchemaDataSource;

        public DataSource getTapSchemaDataSource() {
            if (credentials != null) {
                if (dataSource == null) {
                    dataSource = createDataSource(credentials);
                }
                return dataSource;
            }
            if (tapSchemaDataSource == null) {
                tapSchemaDataSource = createDataSource(tapSchemaCredentials);
            }
            return tapSchemaDataSource;
        }

        public DataSource getSourceDataSource() {
            if (credentials != null) {
                if (dataSource == null) {
                    dataSource = createDataSource(credentials);
                }
                return dataSource;
            }
            if (sourceDataSource == null) {
                sourceDataSource = createDataSource(sourceCredentials);
            }
            return sourceDataSource;
        }

        private DataSource createDataSource(Credentials credentials) {
            if (credentials.getDatabaseType() == DatabaseType.MYSQL) {

                MysqlDataSource ds = new MysqlDataSource();

                ds.setServerName(credentials.getHostname());
                ds.setPortNumber(credentials.getPort());
                ds.setUser(credentials.getUsername());
                ds.setPassword(credentials.getPassword());

                return ds;
            } else if (credentials.getDatabaseType() == DatabaseType.POSTGRES) {

                PGPoolingDataSource ds = new PGPoolingDataSource();

                ds.setServerName(credentials.getHostname());
                ds.setPortNumber(credentials.getPort());
                ds.setUser(credentials.getUsername());
                ds.setPassword(credentials.getPassword());

                return ds;
            }

            throw new UnsupportedOperationException(credentials.getDatabaseType() + " not supported yet.");
        }
    }
}
Loading