Commit 0527414f authored by Marco Molinaro's avatar Marco Molinaro
Browse files

Merge branch 'master' of ia2-git.oats.inaf.it:molinaro/tap_schema_manager

parents dd49adb1 183b59bf
Loading
Loading
Loading
Loading
+54 −0
Original line number Diff line number Diff line
package it.inaf.oats.ia2.tapschemamanager.businesslayer;

import it.inaf.oats.ia2.tapschemamanager.datalayer.Credentials;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElements;
import javax.xml.bind.annotation.XmlRootElement;

/**
 *
 * @author Sonia Zorba <zorba at oats.inaf.it>
 */
@XmlRootElement(name = "credentials-config")
public class CredentialsConfiguration {

    private String password;

    private List<Object> credentialsInfo;

    public CredentialsConfiguration() {
        credentialsInfo = new ArrayList<Object>();
    }

    public void addCredentials(Credentials credentials) {
        credentialsInfo.add(credentials);
    }

    public void addSeparateCredentials(SeparateCredentials separateCredentials) {
        credentialsInfo.add(separateCredentials);
    }

    @XmlElements({
        @XmlElement(name = "credentials", type = Credentials.class),
        @XmlElement(name = "separate-credentials", type = SeparateCredentials.class)
    })
    public List<Object> getCredentialsInfo() {
        return credentialsInfo;
    }

    private void setCredentialsInfo(List<Object> credentialsInfo) {
        this.credentialsInfo = credentialsInfo;
    }

    @XmlAttribute(name = "password")
    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}
+14 −0
Original line number Diff line number Diff line
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package it.inaf.oats.ia2.tapschemamanager.businesslayer;

/**
 *
 * @author Sonia Zorba <zorba at oats.inaf.it>
 */
public class CredentialsConfigurationBean {
    
}
+41 −0
Original line number Diff line number Diff line
package it.inaf.oats.ia2.tapschemamanager.businesslayer;

import it.inaf.oats.ia2.tapschemamanager.datalayer.Credentials;
import javax.xml.bind.annotation.XmlElement;

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

    private Credentials sourceCredentials;
    private Credentials tapSchemaCredentials;

    public SeparateCredentials() {
    }

    public SeparateCredentials(Credentials sourceCredentials, Credentials tapSchemaCredentials) {
        this.sourceCredentials = sourceCredentials;
        this.tapSchemaCredentials = tapSchemaCredentials;
    }

    @XmlElement(name = "source-credentials")
    public Credentials getSourceCredentials() {
        return sourceCredentials;
    }

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

    @XmlElement(name = "tap-schema-credentials")
    public Credentials getTapSchemaCredentials() {
        return tapSchemaCredentials;
    }

    public void setTapSchemaCredentials(Credentials tapSchemaCredentials) {
        this.tapSchemaCredentials = tapSchemaCredentials;
    }

}
+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()) {
+109 −21
Original line number Diff line number Diff line
package it.inaf.oats.ia2.tapschemamanager.webapp;

import it.inaf.oats.ia2.tapschemamanager.businesslayer.SeparateCredentials;
import it.inaf.oats.ia2.tapschemamanager.datalayer.Credentials;
import it.inaf.oats.ia2.tapschemamanager.datalayer.DBWrapper;
import java.io.IOException;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.enterprise.context.Conversation;
import javax.faces.view.ViewScoped;
import javax.enterprise.context.SessionScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;
import org.apache.log4j.Logger;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author Sonia Zorba <zorba at oats.inaf.it>
 */
@Named("credentialsInsertion")
@ViewScoped
@SessionScoped
public class CredentialsBean implements Serializable {

    private static final long serialVersionUID = -2688980249773483198L;
    private static final Logger log = Logger.getLogger(CredentialsBean.class);
    private static final Logger log = LoggerFactory.getLogger(CredentialsBean.class);

    @Inject
    CredentialsConfigurationBean ccBean;

    @Inject
    Conversation conversation;
@@ -28,11 +38,14 @@ public class CredentialsBean implements Serializable {
    @Inject
    SchemaSelectionBean schemaSelectionBean;

    private boolean loggedIn;
    private String adminPassword;
    private String loginError;

    private Credentials sourceCredentials;
    private Credentials tapSchemaCredentials;
    private boolean separateCredentials;
    private int currentEditingRow;

    @PostConstruct
    public void init() {
@@ -46,32 +59,60 @@ public class CredentialsBean implements Serializable {
        tapSchemaCredentials = new Credentials();
    }

    public void login() {
        if (adminPassword != null && adminPassword.equals(ccBean.getConfig().getPassword())) {
            loggedIn = true;
        } else {
            FacesContext.getCurrentInstance().addMessage("main:password", new FacesMessage("Invalid credentials"));
        }
    }

    public String getLoginError() {
        return loginError;
    }

    public String login() {
        System.out.println("login called");
        log.info("login called");
    public List getSavedCredentials() {
        return ccBean.getConfig().getCredentialsInfo();
    }

        loginError = null;
    public void editCredentials(Credentials credentials, int index) {
        this.sourceCredentials = credentials;
        separateCredentials = false;
        currentEditingRow = index;
    }

    public void editSeparateCredentials(SeparateCredentials sc, int index) {
        this.sourceCredentials = sc.getSourceCredentials();
        this.tapSchemaCredentials = sc.getTapSchemaCredentials();
        currentEditingRow = index;
        separateCredentials = true;
    }

        if (!separateCredentials) {
            tapSchemaCredentials.setHostname(sourceCredentials.getHostname());
            tapSchemaCredentials.setPort(sourceCredentials.getPort());
            tapSchemaCredentials.setUsername(sourceCredentials.getUsername());
            tapSchemaCredentials.setPassword(sourceCredentials.getPassword());
    public void addNewCredentials() {
        separateCredentials = false;
        this.sourceCredentials = new Credentials();
        this.tapSchemaCredentials = new Credentials();
        currentEditingRow = ccBean.getConfig().getCredentialsInfo().size();
    }

    public String loginWithSingleCredentials(Credentials credentials) {
        log.debug("Login with single credentials");
        return loginWithDBWrapper(new DBWrapper(credentials));
    }

    public String loginWithSeparatedCredentials(Credentials sourceCredentials, Credentials tapSchemaCredentials) {
        log.debug("Login with separated credentials");
        return loginWithDBWrapper(new DBWrapper(sourceCredentials, tapSchemaCredentials));
    }

    private String loginWithDBWrapper(DBWrapper dbWrapper) {
        loginError = null;

        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);
@@ -80,6 +121,29 @@ public class CredentialsBean implements Serializable {
        }
    }

    public void removeCredentials(int index) throws IOException {
        ccBean.getConfig().getCredentialsInfo().remove(index);
        ccBean.updateConfigurationFile();
    }

    public void saveCredentialsEdited() throws IOException {

        List credentialsList = ccBean.getConfig().getCredentialsInfo();

        if (currentEditingRow < credentialsList.size()) {
            credentialsList.remove(currentEditingRow);
        }

        if (separateCredentials) {
            SeparateCredentials sc = new SeparateCredentials(sourceCredentials, tapSchemaCredentials);
            credentialsList.add(currentEditingRow, sc);
        } else {
            credentialsList.add(currentEditingRow, sourceCredentials);
        }

        ccBean.updateConfigurationFile();
    }

    public boolean isSeparateCredentials() {
        return separateCredentials;
    }
@@ -103,4 +167,28 @@ public class CredentialsBean implements Serializable {
    public void setTapSchemaCredentials(Credentials tapSchemaCredentials) {
        this.tapSchemaCredentials = tapSchemaCredentials;
    }

    public String getAdminPassword() {
        return adminPassword;
    }

    public void setAdminPassword(String adminPassword) {
        this.adminPassword = adminPassword;
    }

    public boolean isLoggedIn() {
        return loggedIn;
    }

    public void setLoggedIn(boolean loggedIn) {
        this.loggedIn = loggedIn;
    }

    public String logout() {
        HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
        if (request.isRequestedSessionIdValid()) {
            request.getSession().invalidate();
        }
        return "index.xhtml?faces-redirect=true";
    }
}
Loading