src/main/java/de/uapcore/lightpit/modules/ProjectsModule.java

changeset 131
67df332e3146
parent 129
a09d5c59351a
child 133
ef075cd7ce55
--- a/src/main/java/de/uapcore/lightpit/modules/ProjectsModule.java	Thu Oct 15 18:36:05 2020 +0200
+++ b/src/main/java/de/uapcore/lightpit/modules/ProjectsModule.java	Thu Oct 15 20:02:30 2020 +0200
@@ -44,12 +44,9 @@
 import java.sql.Date;
 import java.sql.SQLException;
 import java.util.NoSuchElementException;
-import java.util.Optional;
 import java.util.stream.Collectors;
 import java.util.stream.Stream;
 
-import static de.uapcore.lightpit.Functions.fqn;
-
 @WebServlet(
         name = "ProjectsModule",
         urlPatterns = "/projects/*"
@@ -58,45 +55,26 @@
 
     private static final Logger LOG = LoggerFactory.getLogger(ProjectsModule.class);
 
-    private static final String SESSION_ATTR_SELECTED_PROJECT = fqn(ProjectsModule.class, "selected_project");
-    private static final String SESSION_ATTR_SELECTED_VERSION = fqn(ProjectsModule.class, "selected_version");
-    private static final String SESSION_ATTR_SELECTED_COMPONENT = fqn(ProjectsModule.class, "selected_component");
-    private static final String PARAMETER_SELECTED_PROJECT = "pid";
-    private static final String PARAMETER_SELECTED_VERSION = "vid";
-    private static final String PARAMETER_SELECTED_COMPONENT = "cid";
-
     @Override
     protected String getResourceBundleName() {
         return "localization.projects";
     }
 
-    private int syncParamWithSession(HttpServletRequest req, String param, String attr) {
-        final var session = req.getSession();
-        final var idParam = getParameter(req, Integer.class, param);
-        final int id;
-        if (idParam.isPresent()) {
-            id = idParam.get();
-            session.setAttribute(attr, id);
-        } else {
-            id = Optional.ofNullable(session.getAttribute(attr)).map(x->(Integer)x).orElse(-1);
-        }
-        return id;
-    }
-
-    private void populate(ProjectView viewModel, HttpServletRequest req, DataAccessObjects dao) throws SQLException {
+    private void populate(ProjectView viewModel, PathParameters pathParameters, DataAccessObjects dao) throws SQLException {
         final var projectDao = dao.getProjectDao();
         final var versionDao = dao.getVersionDao();
         final var componentDao = dao.getComponentDao();
 
         projectDao.list().stream().map(ProjectInfo::new).forEach(viewModel.getProjectList()::add);
 
+        if (pathParameters == null)
+            return;
+
         // Select Project
-        final int pid = syncParamWithSession(req, PARAMETER_SELECTED_PROJECT, SESSION_ATTR_SELECTED_PROJECT);
-        if (pid >= 0) {
+        final int pid = Functions.parseIntOrZero(pathParameters.get("project"));
+        if (pid > 0) {
             final var project = projectDao.find(pid);
-            if (project == null) {
-                req.setAttribute(SESSION_ATTR_SELECTED_PROJECT, -1);
-            } else {
+            if (project != null) {
                 final var info = new ProjectInfo(project);
                 info.setVersions(versionDao.list(project));
                 info.setComponents(componentDao.list(project));
@@ -106,22 +84,19 @@
         }
 
         // Select Version
-        final int vid = syncParamWithSession(req, PARAMETER_SELECTED_VERSION, SESSION_ATTR_SELECTED_VERSION);
+        final int vid = Functions.parseIntOrZero(pathParameters.get("version"));
         if (vid > 0) {
             viewModel.setVersionFilter(versionDao.find(vid));
-        } else {
-            // NULL for version means: show all unassigned
-            viewModel.setVersionFilter(null);
+        }
+        // TODO: don't treat unknown == unassigned - send 404 for unknown and introduce special word for unassigned
+
+        // Select Component
+        final int cid = Functions.parseIntOrZero(pathParameters.get("component"));
+        if (cid > 0) {
+            viewModel.setComponentFilter(componentDao.find(cid));
         }
 
-        // Select Component
-        final int cid = syncParamWithSession(req, PARAMETER_SELECTED_COMPONENT, SESSION_ATTR_SELECTED_COMPONENT);
-        if (cid > 0) {
-            viewModel.setComponentFilter(componentDao.find(cid));
-        } else if (cid <= 0) {
-            // -1 means: filter for unassigned, null means: show all
-            viewModel.setComponentFilter(new Component(-1));
-        }
+        // TODO: distinguish all/unassigned for components
     }
 
     private ResponseType forwardView(HttpServletRequest req, ProjectView viewModel, String name) {
@@ -135,7 +110,7 @@
     @RequestMapping(method = HttpMethod.GET)
     public ResponseType index(HttpServletRequest req, DataAccessObjects dao) throws SQLException {
         final var viewModel = new ProjectView();
-        populate(viewModel, req, dao);
+        populate(viewModel, null, dao);
 
         final var projectDao = dao.getProjectDao();
         final var versionDao = dao.getVersionDao();
@@ -148,30 +123,38 @@
         return forwardView(req, viewModel, "projects");
     }
 
-    private void configure(ProjectEditView viewModel, Project project, DataAccessObjects dao) throws SQLException {
+    private void configureProjectEditor(ProjectEditView viewModel, Project project, DataAccessObjects dao) throws SQLException {
         viewModel.setProject(project);
         viewModel.setUsers(dao.getUserDao().list());
     }
 
-    @RequestMapping(requestPath = "edit", method = HttpMethod.GET)
-    public ResponseType edit(HttpServletRequest req, DataAccessObjects dao) throws SQLException {
+    @RequestMapping(requestPath = "$project/edit", method = HttpMethod.GET)
+    public ResponseType edit(HttpServletRequest req, HttpServletResponse resp, PathParameters pathParams, DataAccessObjects dao) throws IOException, SQLException {
         final var viewModel = new ProjectEditView();
-        populate(viewModel, req, dao);
+        populate(viewModel, pathParams, dao);
+
+        if (viewModel.getProjectInfo() == null) {
+            resp.sendError(HttpServletResponse.SC_NOT_FOUND);
+            return ResponseType.NONE;
+        }
 
-        final var project = Optional.ofNullable(viewModel.getProjectInfo())
-                .map(ProjectInfo::getProject)
-                .orElse(new Project(-1));
-        configure(viewModel, project, dao);
+        configureProjectEditor(viewModel, viewModel.getProjectInfo().getProject(), dao);
+        return forwardView(req, viewModel, "project-form");
+    }
 
+    @RequestMapping(requestPath = "create", method = HttpMethod.GET)
+    public ResponseType create(HttpServletRequest req, DataAccessObjects dao) throws SQLException {
+        final var viewModel = new ProjectEditView();
+        populate(viewModel, null, dao);
+        configureProjectEditor(viewModel, new Project(-1), dao);
         return forwardView(req, viewModel, "project-form");
     }
 
     @RequestMapping(requestPath = "commit", method = HttpMethod.POST)
-    public ResponseType commit(HttpServletRequest req, DataAccessObjects dao) throws SQLException {
+    public ResponseType commit(HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) throws IOException {
 
-        Project project = new Project(-1);
         try {
-            project = new Project(getParameter(req, Integer.class, "pid").orElseThrow());
+            final var project = new Project(getParameter(req, Integer.class, "pid").orElseThrow());
             project.setName(getParameter(req, String.class, "name").orElseThrow());
             getParameter(req, String.class, "description").ifPresent(project::setDescription);
             getParameter(req, String.class, "repoUrl").ifPresent(project::setRepoUrl);
@@ -187,30 +170,25 @@
 
             return ResponseType.HTML;
         } catch (NoSuchElementException | IllegalArgumentException | SQLException ex) {
-            LOG.warn("Form validation failure: {}", ex.getMessage());
-            LOG.debug("Details:", ex);
-            final var viewModel = new ProjectEditView();
-            populate(viewModel, req, dao);
-            configure(viewModel, project, dao);
-            // TODO: error text
-            return forwardView(req, viewModel, "project-form");
+            resp.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED);
+            // TODO: implement - fix issue #21
+            return ResponseType.NONE;
         }
     }
 
-    @RequestMapping(requestPath = "view", method = HttpMethod.GET)
-    public ResponseType view(HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) throws SQLException, IOException {
+    @RequestMapping(requestPath = "$project/versions/$version", method = HttpMethod.GET)
+    public ResponseType view(HttpServletRequest req, HttpServletResponse resp, PathParameters pathParams, DataAccessObjects dao) throws SQLException, IOException {
         final var viewModel = new ProjectDetailsView();
-        populate(viewModel, req, dao);
+        populate(viewModel, pathParams, dao);
+        final var version = viewModel.getVersionFilter();
 
-        if (viewModel.getProjectInfo() == null) {
-            resp.sendError(HttpServletResponse.SC_NOT_FOUND, "No project selected.");
+        if (viewModel.getProjectInfo() == null || version == null) {
+            resp.sendError(HttpServletResponse.SC_NOT_FOUND);
             return ResponseType.NONE;
         }
 
         final var issueDao = dao.getIssueDao();
 
-        final var version = viewModel.getVersionFilter();
-
         final var detailView = viewModel.getProjectDetails();
         final var issues = issueDao.list(version);
         for (var issue : issues) issueDao.joinVersionInformation(issue);
@@ -224,15 +202,14 @@
         return forwardView(req, viewModel, "project-details");
     }
 
-    @RequestMapping(requestPath = "versions", method = HttpMethod.GET)
-    public ResponseType versions(HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) throws IOException, SQLException {
+    @RequestMapping(requestPath = "$project/versions/", method = HttpMethod.GET)
+    public ResponseType versions(HttpServletRequest req, HttpServletResponse resp, PathParameters pathParameters, DataAccessObjects dao) throws IOException, SQLException {
         final var viewModel = new VersionsView();
-        populate(viewModel, req, dao);
-        viewModel.setVersionFilter(null);
+        populate(viewModel, pathParameters, dao);
 
         final var projectInfo = viewModel.getProjectInfo();
         if (projectInfo == null) {
-            resp.sendError(HttpServletResponse.SC_NOT_FOUND, "No project selected.");
+            resp.sendError(HttpServletResponse.SC_NOT_FOUND);
             return ResponseType.NONE;
         }
 
@@ -244,49 +221,60 @@
         return forwardView(req, viewModel, "versions");
     }
 
-    @RequestMapping(requestPath = "versions/edit", method = HttpMethod.GET)
-    public ResponseType editVersion(HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) throws IOException, SQLException {
+    @RequestMapping(requestPath = "$project/versions/$version/edit", method = HttpMethod.GET)
+    public ResponseType editVersion(HttpServletRequest req, HttpServletResponse resp, PathParameters pathParameters, DataAccessObjects dao) throws IOException, SQLException {
         final var viewModel = new VersionEditView();
-        populate(viewModel, req, dao);
+        populate(viewModel, pathParameters, dao);
 
-        if (viewModel.getProjectInfo() == null) {
-            resp.sendError(HttpServletResponse.SC_NOT_FOUND, "No project selected.");
+        if (viewModel.getProjectInfo() == null || viewModel.getVersionFilter() == null) {
+            resp.sendError(HttpServletResponse.SC_NOT_FOUND);
             return ResponseType.NONE;
         }
 
-        viewModel.setVersion(Optional.ofNullable(viewModel.getVersionFilter()).orElse(new Version(-1)));
+        viewModel.setVersion(viewModel.getVersionFilter());
 
         return forwardView(req, viewModel, "version-form");
     }
 
-    @RequestMapping(requestPath = "versions/commit", method = HttpMethod.POST)
-    public ResponseType commitVersion(HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) throws SQLException {
+    @RequestMapping(requestPath = "$project/create-version", method = HttpMethod.GET)
+    public ResponseType createVersion(HttpServletRequest req, HttpServletResponse resp, PathParameters pathParameters, DataAccessObjects dao) throws IOException, SQLException {
+        final var viewModel = new VersionEditView();
+        populate(viewModel, pathParameters, dao);
 
-        var version = new Version(-1);
+        if (viewModel.getProjectInfo() == null) {
+            resp.sendError(HttpServletResponse.SC_NOT_FOUND);
+            return ResponseType.NONE;
+        }
+
+        viewModel.setVersion(viewModel.getVersionFilter());
+
+        return forwardView(req, viewModel, "version-form");
+    }
+
+    @RequestMapping(requestPath = "commit-version", method = HttpMethod.POST)
+    public ResponseType commitVersion(HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) throws IOException {
+
         try {
             final var project = new Project(getParameter(req, Integer.class, "pid").orElseThrow());
-            version = new Version(getParameter(req, Integer.class, "id").orElseThrow());
+            final var version = new Version(getParameter(req, Integer.class, "id").orElseThrow());
             version.setName(getParameter(req, String.class, "name").orElseThrow());
             getParameter(req, Integer.class, "ordinal").ifPresent(version::setOrdinal);
             version.setStatus(VersionStatus.valueOf(getParameter(req, String.class, "status").orElseThrow()));
             dao.getVersionDao().saveOrUpdate(version, project);
 
-            setRedirectLocation(req, "./projects/versions?pid=" + project.getId());
+            // TODO: improve building the redirect location
+            setRedirectLocation(req, "./projects/" + project.getId() + "/versions/");
             setContentPage(req, Constants.JSP_COMMIT_SUCCESSFUL);
         } catch (NoSuchElementException | IllegalArgumentException | SQLException ex) {
-            LOG.warn("Form validation failure: {}", ex.getMessage());
-            LOG.debug("Details:", ex);
-            final var viewModel = new VersionEditView();
-            populate(viewModel, req, dao);
-            viewModel.setVersion(version);
-            // TODO: set Error Text
-            return forwardView(req, viewModel, "version-form");
+            resp.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED);
+            // TODO: implement - fix issue #21
+            return ResponseType.NONE;
         }
 
         return ResponseType.HTML;
     }
 
-    private void configure(IssueEditView viewModel, Issue issue, DataAccessObjects dao) throws SQLException {
+    private void configureProjectEditor(IssueEditView viewModel, Issue issue, DataAccessObjects dao) throws SQLException {
         issue.setProject(viewModel.getProjectInfo().getProject());
         viewModel.setIssue(issue);
         viewModel.configureVersionSelectors(viewModel.getProjectInfo().getVersions());
@@ -296,30 +284,52 @@
         }
     }
 
-    @RequestMapping(requestPath = "issues/edit", method = HttpMethod.GET)
-    public ResponseType editIssue(HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) throws SQLException {
+    @RequestMapping(requestPath = "$project/issues/$issue/edit", method = HttpMethod.GET)
+    public ResponseType editIssue(HttpServletRequest req, HttpServletResponse resp, PathParameters pathParameters, DataAccessObjects dao) throws IOException, SQLException {
         final var viewModel = new IssueEditView();
-        populate(viewModel, req, dao);
+        populate(viewModel, pathParameters, dao);
 
-        final var issueParam = getParameter(req, Integer.class, "issue");
-        if (issueParam.isPresent()) {
-            final var issueDao = dao.getIssueDao();
-            final var issue = issueDao.find(issueParam.get());
-            issueDao.joinVersionInformation(issue);
-            req.getSession().setAttribute(SESSION_ATTR_SELECTED_PROJECT, issue.getProject().getId());
-            configure(viewModel, issue, dao);
-        } else {
-            configure(viewModel, new Issue(-1), dao);
+        final var projectInfo = viewModel.getProjectInfo();
+        if (projectInfo == null) {
+            resp.sendError(HttpServletResponse.SC_NOT_FOUND);
+            return ResponseType.NONE;
         }
 
+        final var issueDao = dao.getIssueDao();
+        final var issue = issueDao.find(Functions.parseIntOrZero(pathParameters.get("issue")));
+        if (issue == null) {
+            resp.sendError(HttpServletResponse.SC_NOT_FOUND);
+            return ResponseType.NONE;
+        }
+
+        issueDao.joinVersionInformation(issue);
+        configureProjectEditor(viewModel, issue, dao);
+
         return forwardView(req, viewModel, "issue-form");
     }
 
-    @RequestMapping(requestPath = "issues/commit", method = HttpMethod.POST)
-    public ResponseType commitIssue(HttpServletRequest req, DataAccessObjects dao) throws SQLException {
-        Issue issue = new Issue(-1);
+    @RequestMapping(requestPath = "$project/create-issue", method = HttpMethod.GET)
+    public ResponseType createIssue(HttpServletRequest req, HttpServletResponse resp, PathParameters pathParameters, DataAccessObjects dao) throws IOException, SQLException {
+        final var viewModel = new IssueEditView();
+        populate(viewModel, pathParameters, dao);
+
+        final var projectInfo = viewModel.getProjectInfo();
+        if (projectInfo == null) {
+            resp.sendError(HttpServletResponse.SC_NOT_FOUND);
+            return ResponseType.NONE;
+        }
+
+        final var issue = new Issue(-1);
+        issue.setProject(projectInfo.getProject());
+        configureProjectEditor(viewModel, issue, dao);
+
+        return forwardView(req, viewModel, "issue-form");
+    }
+
+    @RequestMapping(requestPath = "commit-issue", method = HttpMethod.POST)
+    public ResponseType commitIssue(HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) throws IOException {
         try {
-            issue = new Issue(getParameter(req, Integer.class, "id").orElseThrow());
+            final var issue = new Issue(getParameter(req, Integer.class, "id").orElseThrow());
             issue.setProject(new Project(getParameter(req, Integer.class, "pid").orElseThrow()));
             getParameter(req, String.class, "category").map(IssueCategory::valueOf).ifPresent(issue::setCategory);
             getParameter(req, String.class, "status").map(IssueStatus::valueOf).ifPresent(issue::setStatus);
@@ -343,24 +353,19 @@
 
             dao.getIssueDao().saveOrUpdate(issue, issue.getProject());
 
-            // specifying the issue parameter keeps the edited issue as menu item
-            setRedirectLocation(req, "./projects/view?pid=" + issue.getProject().getId());
+            // TODO: fix issue #14
+            setRedirectLocation(req, "./projects/" + issue.getProject().getId() + "/versions/");
             setContentPage(req, Constants.JSP_COMMIT_SUCCESSFUL);
 
             return ResponseType.HTML;
         } catch (NoSuchElementException | IllegalArgumentException | SQLException ex) {
-            // TODO: set request attribute with error text
-            LOG.warn("Form validation failure: {}", ex.getMessage());
-            LOG.debug("Details:", ex);
-            final var viewModel = new IssueEditView();
-            populate(viewModel, req, dao);
-            configure(viewModel, issue, dao);
-            // TODO: set Error Text
-            return forwardView(req, viewModel, "issue-form");
+            resp.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED);
+            // TODO: implement - fix issue #21
+            return ResponseType.NONE;
         }
     }
 
-    @RequestMapping(requestPath = "issues/comment", method = HttpMethod.POST)
+    @RequestMapping(requestPath = "commit-issue-comment", method = HttpMethod.POST)
     public ResponseType commentIssue(HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) throws SQLException, IOException {
         final var issueIdParam = getParameter(req, Integer.class, "issueid");
         if (issueIdParam.isEmpty()) {
@@ -383,20 +388,15 @@
 
             dao.getIssueDao().saveComment(issueComment);
 
-            // specifying the issue parameter keeps the edited issue as menu item
-            setRedirectLocation(req, "./projects/issues/edit?issue=" + issue.getId());
+            // TODO: fix redirect location (e.g. after fixing #24)
+            setRedirectLocation(req, "./projects/" + issue.getProject().getId()+"/issues/"+issue.getId()+"/edit");
             setContentPage(req, Constants.JSP_COMMIT_SUCCESSFUL);
 
             return ResponseType.HTML;
         } catch (NoSuchElementException | IllegalArgumentException | SQLException ex) {
-            // TODO: set request attribute with error text
-            LOG.warn("Form validation failure: {}", ex.getMessage());
-            LOG.debug("Details:", ex);
-            final var viewModel = new IssueEditView();
-            populate(viewModel, req, dao);
-            configure(viewModel, issue, dao);
-            // TODO: set Error Text
-            return forwardView(req, viewModel, "issue-form");
+            resp.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED);
+            // TODO: implement - fix issue #21
+            return ResponseType.NONE;
         }
     }
 }

mercurial