Commit 400c1b43 authored by Sonia Zorba's avatar Sonia Zorba
Browse files

Changes on pages navigation and session

parent be2fcde4
Loading
Loading
Loading
Loading
+33 −7
Original line number Original line Diff line number Diff line
@@ -14,7 +14,25 @@
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>    
    </properties>    
    
    
    <dependencyManagement>
        <dependencies>
        <dependencies>
            <dependency>
                <groupId>org.glassfish.jersey</groupId>
                <artifactId>jersey-bom</artifactId>
                <version>2.6</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    
    <dependencies>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-web-api</artifactId>
            <version>6.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
        <dependency>
            <groupId>mysql</groupId>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <artifactId>mysql-connector-java</artifactId>
@@ -22,17 +40,25 @@
            <type>jar</type>
            <type>jar</type>
            <scope>runtime</scope>
            <scope>runtime</scope>
        </dependency>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.ext</groupId>
            <artifactId>jersey-mvc-jsp</artifactId>
        </dependency>
        <!-- JSON support -->
        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-moxy</artifactId>
        </dependency>
        <dependency>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <artifactId>gson</artifactId>
            <version>2.5</version>
            <version>2.5</version>
        </dependency>
        </dependency>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-web-api</artifactId>
            <version>7.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
        <dependency>
            <groupId>com.flipkart.zjsonpatch</groupId>
            <groupId>com.flipkart.zjsonpatch</groupId>
            <artifactId>zjsonpatch</artifactId>
            <artifactId>zjsonpatch</artifactId>
+12 −0
Original line number Original line Diff line number Diff line
package it.inaf.oats.ia2.tap.tsm;

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

    String CREDENTIALS = "credentials";
    String SELECT_SCHEMA = "selectTapSchema";
    String EDIT_SCHEMA = "editTapSchema";
}
+160 −0
Original line number Original line Diff line number Diff line
package it.inaf.oats.ia2.tap.tsm;

import it.inaf.oats.ia2.tap.tsm.ejb.ApplicationManagerLocal;
import it.inaf.oats.ia2.tap.tsm.model.Credentials;
import it.inaf.oats.ia2.tap.tsm.model.InsertCredentialsModel;
import it.inaf.oats.ia2.tap.tsm.model.SelectTapSchemaModel;
import it.inaf.oats.ia2.tap.tsm.model.TapSchema;
import it.inaf.oats.ia2.tap.tsm.rest.EditTapSchemaPageState;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Sonia Zorba <zorba at oats.inaf.it>
 */
@WebServlet(name = "TSMServlet", urlPatterns = {"/" + TSMPages.CREDENTIALS, "/" + TSMPages.SELECT_SCHEMA, "/" + TSMPages.EDIT_SCHEMA})
public class TSMServlet extends HttpServlet {

    @EJB
    private ApplicationManagerLocal applicationManager;

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String path = request.getRequestURI().substring(request.getContextPath().length());

        if (!applicationManager.isLoggedIn() && !path.startsWith("/" + TSMPages.CREDENTIALS)) {
            response.sendRedirect(TSMPages.CREDENTIALS);
        } else {
            if (path.startsWith("/" + TSMPages.CREDENTIALS)) {
                applicationManager.setCurrentPage(TSMPages.CREDENTIALS);
                showCredentialForm(request, response, new InsertCredentialsModel(applicationManager.getCredentials()));
            } else if (path.startsWith("/" + TSMPages.SELECT_SCHEMA)) {
                applicationManager.setCurrentPage(TSMPages.SELECT_SCHEMA);
                request.setAttribute("model", applicationManager.getSelectTapSchema());
                request.getRequestDispatcher("/" + TSMPages.SELECT_SCHEMA + ".jsp").forward(request, response);
            } else if (path.startsWith("/" + TSMPages.EDIT_SCHEMA)) {
                applicationManager.setCurrentPage(TSMPages.EDIT_SCHEMA);
                request.setAttribute("tapSchemaName", applicationManager.getEditTapSchemaPageState().getTapSchema().getName());
                request.getRequestDispatcher("/" + TSMPages.EDIT_SCHEMA + ".jsp").forward(request, response);
            }
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String path = request.getRequestURI().substring(request.getContextPath().length());

        if (path.startsWith("/" + TSMPages.CREDENTIALS)) {
            processInsertCredentials(request, response);
        } else if (path.startsWith("/" + TSMPages.SELECT_SCHEMA)) {
            Connection connection = null;
            try {
                connection = applicationManager.getCredentials().getConnection();

                TapSchema tapSchema;
                if (request.getParameter("tapschemaRadioGroup").equals("create")) {
                    String tapSchemaName = request.getParameter("tapschemaName");
                    ArrayList<String> selectedSchemas = new ArrayList<String>();
                    String[] selectedSchemasParam = request.getParameterValues("selectedSchemas");
                    if (selectedSchemasParam != null) {
                        selectedSchemas.addAll(Arrays.asList(selectedSchemasParam));
                    }
                    tapSchema = new TapSchema(connection, tapSchemaName, selectedSchemas);
                } else {
                    String tapSchemaName = request.getParameter("tapschemaDropDownChoice");
                    tapSchema = new TapSchema(connection, tapSchemaName);
                }

                applicationManager.setEditTapSchemaPageState(new EditTapSchemaPageState(tapSchema));
                response.sendRedirect(TSMPages.EDIT_SCHEMA);
            } catch (SQLException e) {
                e.printStackTrace(System.err);
            } finally {
                try {
                    if (connection != null) {
                        connection.close();
                    }
                } catch (SQLException e) {
                    e.printStackTrace(System.err);
                }
            }
        }
    }

    private void showCredentialForm(HttpServletRequest request, HttpServletResponse response, InsertCredentialsModel insertCredentialsModel)
            throws ServletException, IOException {
        request.setAttribute("insertCredentials", insertCredentialsModel);
        request.getRequestDispatcher("/" + TSMPages.CREDENTIALS + ".jsp").forward(request, response);
    }

    private void processInsertCredentials(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String hostname = request.getParameter("hostname");
        String portStr = request.getParameter("port");
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        Credentials credentials = applicationManager.getCredentials();
        InsertCredentialsModel insertCredentialsModel = new InsertCredentialsModel(credentials);

        if (hostname != null || portStr != null || username != null) {
            if (hostname == null || hostname.equals("")) {
                insertCredentialsModel.addError("hostname can't be empty");
            } else {
                credentials.setHostname(hostname);
            }

            if (portStr == null || portStr.equals("")) {
                insertCredentialsModel.addError("port can't be empty");
            }
            int port;
            try {
                port = Integer.parseInt(portStr);
                credentials.setPort(port);
            } catch (NumberFormatException e) {
                insertCredentialsModel.addError("invalid port value");
            }

            if (username == null || username.equals("")) {
                insertCredentialsModel.addError("username can't be empty!");
            } else {
                credentials.setUsername(username);
            }

            if (password == null) {
                password = "";
            }
            credentials.setPassword(password);
        }

        if (insertCredentialsModel.getFormErrors().isEmpty()) {
            try {
                SelectTapSchemaModel selectTapSchema = new SelectTapSchemaModel(credentials.getConnection());
                applicationManager.setSelectTapSchemaModel(selectTapSchema);
                request.getSession().setAttribute("applicationManager", applicationManager);
                response.sendRedirect(TSMPages.SELECT_SCHEMA);
            } catch (SQLException e) {
                insertCredentialsModel.addError("Connection error: " + e.getMessage());
            }
        }

        if (!applicationManager.isLoggedIn()) {
            showCredentialForm(request, response, insertCredentialsModel);
        }
    }
}
+1 −0
Original line number Original line Diff line number Diff line
package it.inaf.oats.ia2.tap.tsm.datalayer;
package it.inaf.oats.ia2.tap.tsm.datalayer;


import it.inaf.oats.ia2.tap.tsm.model.Credentials;
import it.inaf.oats.ia2.tap.tsm.model.*;
import it.inaf.oats.ia2.tap.tsm.model.*;
import java.sql.Connection;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSet;
+80 −0
Original line number Original line Diff line number Diff line
package it.inaf.oats.ia2.tap.tsm.ejb;

import it.inaf.oats.ia2.tap.tsm.TSMPages;
import it.inaf.oats.ia2.tap.tsm.model.Credentials;
import it.inaf.oats.ia2.tap.tsm.model.SelectTapSchemaModel;
import it.inaf.oats.ia2.tap.tsm.rest.EditTapSchemaPageState;
import javax.annotation.PostConstruct;
import javax.ejb.Stateful;

/**
 *
 * @author Sonia Zorba <zorba at oats.inaf.it>
 */
@Stateful
public class ApplicationManager implements ApplicationManagerLocal {

    String currentPage;

    private boolean isLoggedIn;

    private Credentials credentials;
    private SelectTapSchemaModel selectTapSchemaModel;
    private EditTapSchemaPageState editTapSchemaPageState;

    @PostConstruct
    public void init() {
        credentials = new Credentials();
        currentPage = TSMPages.CREDENTIALS;
        isLoggedIn = false;
        selectTapSchemaModel = null;
        editTapSchemaPageState = null;
    }

    @Override
    public boolean isLoggedIn() {
        return isLoggedIn;
    }

    @Override
    public Credentials getCredentials() {
        return this.credentials;
    }

    @Override
    public SelectTapSchemaModel getSelectTapSchema() {
        return this.selectTapSchemaModel;
    }

    @Override
    public void setSelectTapSchemaModel(SelectTapSchemaModel selectTapSchemaModel) {
        this.selectTapSchemaModel = selectTapSchemaModel;
        this.isLoggedIn = true;
    }

    @Override
    public EditTapSchemaPageState getEditTapSchemaPageState() {
        return this.editTapSchemaPageState;
    }

    @Override
    public void setEditTapSchemaPageState(EditTapSchemaPageState editTapSchemaPageState) {
        this.editTapSchemaPageState = editTapSchemaPageState;
    }

    @Override
    public String getCurrentPage() {
        return this.currentPage;
    }

    @Override
    public void setCurrentPage(String pageName) {
        this.currentPage = pageName;

        if (currentPage.equals(TSMPages.CREDENTIALS)) {
            init();
        } else if (currentPage.equals(TSMPages.SELECT_SCHEMA)) {
            editTapSchemaPageState = null;
        }
    }
}
Loading