Skip to content
SchemaSelectionBean.java 6.42 KiB
Newer Older
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.DataProvider;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.enterprise.context.Conversation;
import javax.enterprise.context.ConversationScoped;
Sonia Zorba's avatar
Sonia Zorba committed
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
Sonia Zorba's avatar
Sonia Zorba committed
import javax.faces.validator.ValidatorException;
import javax.inject.Inject;
import javax.inject.Named;

/**
 *
 * @author Sonia Zorba <zorba at oats.inaf.it>
 */
@Named("schemaSelection")
@ConversationScoped
public class SchemaSelectionBean implements Serializable {
Sonia Zorba's avatar
Sonia Zorba committed

    private static final long serialVersionUID = -5745720427701334323L;

    @Inject
    private Conversation conversation;

    @Inject
    TapSchemaEditingBean tapSchemaEditingBean;

    private Credentials credentials;

    private String selectedRadioOption;

    // For editing
    private List<String> allTAPSchemas;
    private String selectedTAPSchema;
    private String exposedSchemas;

    // For creation
    private String tapSchemaName;
    private List<String> allSchemas;
    private List<String> selectedSchemas;

    @PostConstruct
    public void init() {
        selectedRadioOption = "edit";
        exposedSchemas = "";
    }

Sonia Zorba's avatar
Sonia Zorba committed
    public void onPageLoad() {
        FacesContext fc = FacesContext.getCurrentInstance();
        final boolean ajaxRequest = fc.getPartialViewContext().isAjaxRequest();
        final boolean validationFailed = fc.isValidationFailed();

        if (!ajaxRequest && !validationFailed) {
            Connection connection = null;
            try {
                connection = credentials.getConnection();

                allSchemas = DataProvider.getAllSchemasNames(connection);
                setSelectedSchemas(new ArrayList<String>());

                allTAPSchemas = DataProvider.getAllTAPSchemasNames(connection, allSchemas);

                if (!allTAPSchemas.isEmpty()) {
                    this.selectedTAPSchema = allTAPSchemas.get(0);
                    loadExposedSchemas(connection);
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            } finally {
                try {
                    if (connection != null) {
                        connection.close();
                    }
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }

    public void loadExposedSchemas(Connection connection) throws SQLException {
        List<String> schemas = DataProvider.getExposedSchemas(connection, selectedTAPSchema);
        exposedSchemas = "";
        for (int i = 0; i < schemas.size(); i++) {
            exposedSchemas += schemas.get(i);
            if (i < schemas.size() - 1) {
                exposedSchemas += ", ";
            }
        }
    }

    public List<String> getAllTAPSchemas() {
        return allTAPSchemas;
    }

    public List<String> getAllSchemas() {
        return allSchemas;
    }

    public String getExposedSchemas() {
        return exposedSchemas;
    }

    public String getSelectedRadioOption() {
        return selectedRadioOption;
    }

    public void setSelectedRadioOption(String selectedRadioOption) {
        this.selectedRadioOption = selectedRadioOption;
    }

    public String getSelectedTAPSchema() {
        return selectedTAPSchema;
    }

    public void setSelectedTAPSchema(String selectedTAPSchema) {
        this.selectedTAPSchema = selectedTAPSchema;

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

    public List<String> getSelectedSchemas() {
        return selectedSchemas;
    }

    public void setSelectedSchemas(List<String> selectedSchemas) {
        this.selectedSchemas = selectedSchemas;
    }

    private String loadTapSchema(TapSchema tapSchema) {
        tapSchemaEditingBean.setTapSchema(tapSchema);
        return "tapSchemaEditing.xhtml?faces-redirect=true";
    }

    public String edit() {
        try {
            return loadTapSchema(new TapSchema(credentials, selectedTAPSchema, true));
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public String create() {
        try {
            TapSchema tapSchema = new TapSchema(credentials, tapSchemaName, false);
            for (String selectedSchema : selectedSchemas) {
                tapSchema.addEntityWrapper(selectedSchema);
            }
            return loadTapSchema(tapSchema);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public String getTapSchemaName() {
        return tapSchemaName;
    }

    public void setTapSchemaName(String tapSchemaName) {
        this.tapSchemaName = tapSchemaName;
    }

    public Credentials getCredentials() {
        return credentials;
    }

    public void setCredentials(Credentials credentials) {
        this.credentials = credentials;
    }

    public String logout() {
        conversation.end();
        return "index.xhtml?faces-redirect=true";
    }
Sonia Zorba's avatar
Sonia Zorba committed

    public void validateTapSchemaName(FacesContext context, UIComponent inputComponent, Object value) {
        String textValue = (String) value;

        String validatorMessage = null;
        if (textValue == null || textValue.isEmpty()) {
            validatorMessage = "TAP_SCHEMA name is required";
        } else if (!textValue.matches("^[a-zA-Z0-9_[-]]*$")) {
            validatorMessage = "TAP_SCHEMA name has to be a valid table name";
        } else if (allSchemas.contains(textValue)) {
            validatorMessage = "Database already contains a schema with this name. Please choose another name";
        }

        if (validatorMessage != null) {
            throw new ValidatorException(new FacesMessage(validatorMessage));
        }
    }