adds backend methods for issues (TODO: implement commitIssue())

Mon, 18 May 2020 21:08:14 +0200

author
Mike Becker <universe@uap-core.de>
date
Mon, 18 May 2020 21:08:14 +0200
changeset 64
0f1746c6abfb
parent 63
51aa5e267c7f
child 65
9861a68a4612

adds backend methods for issues (TODO: implement commitIssue())

src/main/java/de/uapcore/lightpit/modules/ProjectsModule.java file | annotate | diff | comparison | revisions
src/main/resources/localization/projects.properties file | annotate | diff | comparison | revisions
src/main/resources/localization/projects_de.properties file | annotate | diff | comparison | revisions
src/main/webapp/WEB-INF/dynamic_fragments/projects.jsp file | annotate | diff | comparison | revisions
--- a/src/main/java/de/uapcore/lightpit/modules/ProjectsModule.java	Mon May 18 21:06:38 2020 +0200
+++ b/src/main/java/de/uapcore/lightpit/modules/ProjectsModule.java	Mon May 18 21:08:14 2020 +0200
@@ -31,10 +31,7 @@
 
 import de.uapcore.lightpit.*;
 import de.uapcore.lightpit.dao.DataAccessObjects;
-import de.uapcore.lightpit.entities.Project;
-import de.uapcore.lightpit.entities.User;
-import de.uapcore.lightpit.entities.Version;
-import de.uapcore.lightpit.entities.VersionStatus;
+import de.uapcore.lightpit.entities.*;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -44,7 +41,6 @@
 import java.io.IOException;
 import java.sql.SQLException;
 import java.util.NoSuchElementException;
-import java.util.Optional;
 
 import static de.uapcore.lightpit.Functions.fqn;
 
@@ -63,6 +59,20 @@
 
     public static final String SESSION_ATTR_SELECTED_PROJECT = fqn(ProjectsModule.class, "selected-project");
 
+    private Project getSelectedProject(HttpServletRequest req, DataAccessObjects dao) throws SQLException {
+        final var projectDao = dao.getProjectDao();
+        final var session = req.getSession();
+        final var projectSelection = getParameter(req, Integer.class, "pid");
+        if (projectSelection.isPresent()) {
+            final var selectedId = projectSelection.get();
+            final var selectedProject = projectDao.find(selectedId);
+            session.setAttribute(SESSION_ATTR_SELECTED_PROJECT, selectedProject);
+            return selectedProject;
+        } else {
+            return (Project) session.getAttribute(SESSION_ATTR_SELECTED_PROJECT);
+        }
+    }
+
     @RequestMapping(method = HttpMethod.GET)
     public ResponseType indexRedirect(HttpServletResponse resp) throws IOException {
         resp.sendRedirect("index/");
@@ -71,26 +81,14 @@
 
     @RequestMapping(requestPath = "index", method = HttpMethod.GET, menuKey = "menu.index")
     public ResponseType index(HttpServletRequest req, DataAccessObjects dao) throws SQLException {
+
         final var projectList = dao.getProjectDao().list();
         req.setAttribute("projects", projectList);
         setDynamicFragment(req, "projects");
         setStylesheet(req, "projects");
 
-        final var session = req.getSession();
-        final var projectSelection = getParameter(req, Integer.class, "select");
-        if (projectSelection.isPresent()) {
-            final var selectedId = projectSelection.get();
-            for (var proj : projectList) {
-                if (proj.getId() == selectedId) {
-                    session.setAttribute(SESSION_ATTR_SELECTED_PROJECT, proj);
-                    break;
-                }
-            }
-        } else {
-            final var selectedProject = session.getAttribute(SESSION_ATTR_SELECTED_PROJECT);
-            if (selectedProject == null) {
-                projectList.stream().findFirst().ifPresent(proj -> session.setAttribute(SESSION_ATTR_SELECTED_PROJECT, proj));
-            }
+        if (getSelectedProject(req, dao) == null) {
+            projectList.stream().findFirst().ifPresent(proj -> req.getSession().setAttribute(SESSION_ATTR_SELECTED_PROJECT, proj));
         }
 
         return ResponseType.HTML;
@@ -100,14 +98,7 @@
     public ResponseType edit(HttpServletRequest req, DataAccessObjects dao) throws SQLException {
         final var projectDao = dao.getProjectDao();
 
-        Optional<Integer> id = getParameter(req, Integer.class, "id");
-        if (id.isPresent()) {
-            req.setAttribute("project", Optional.ofNullable(projectDao.find(id.get())).orElse(new Project(-1)));
-        } else {
-            req.setAttribute("project", new Project(-1));
-        }
-        req.setAttribute("users", dao.getUserDao().list());
-
+        req.setAttribute("project", findByParameter(req, Integer.class, "id", projectDao::find));
         setDynamicFragment(req, "project-form");
 
         return ResponseType.HTML;
@@ -144,7 +135,7 @@
 
     @RequestMapping(requestPath = "versions", method = HttpMethod.GET, menuKey = "menu.versions")
     public ResponseType versions(HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) throws IOException, SQLException {
-        final var selectedProject = (Project)req.getSession().getAttribute(SESSION_ATTR_SELECTED_PROJECT);
+        final var selectedProject = getSelectedProject(req, dao);
         if (selectedProject == null) {
             resp.sendError(HttpServletResponse.SC_FORBIDDEN);
             return ResponseType.NONE;
@@ -158,18 +149,13 @@
 
     @RequestMapping(requestPath = "versions/edit", method = HttpMethod.GET)
     public ResponseType editVersion(HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) throws IOException, SQLException {
-        final var selectedProject = (Project)req.getSession().getAttribute(SESSION_ATTR_SELECTED_PROJECT);
+        final var selectedProject = getSelectedProject(req, dao);
         if (selectedProject == null) {
             resp.sendError(HttpServletResponse.SC_FORBIDDEN);
             return ResponseType.NONE;
         }
 
-        Optional<Integer> id = getParameter(req, Integer.class, "id");
-        if (id.isPresent()) {
-            req.setAttribute("version", Optional.ofNullable(dao.getVersionDao().find(id.get())).orElse(new Version(-1, selectedProject)));
-        } else {
-            req.setAttribute("version", new Version(-1, selectedProject));
-        }
+        req.setAttribute("version", findByParameter(req, Integer.class, "id", dao.getVersionDao()::find));
         req.setAttribute("versionStatusEnum", VersionStatus.values());
 
         setDynamicFragment(req, "version-form");
@@ -178,8 +164,8 @@
     }
 
     @RequestMapping(requestPath = "versions/commit", method = HttpMethod.POST)
-    public ResponseType commitVersion(HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) throws IOException {
-        final var selectedProject = (Project)req.getSession().getAttribute(SESSION_ATTR_SELECTED_PROJECT);
+    public ResponseType commitVersion(HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) throws IOException, SQLException {
+        final var selectedProject = getSelectedProject(req, dao);
         if (selectedProject == null) {
             resp.sendError(HttpServletResponse.SC_FORBIDDEN);
             return ResponseType.NONE;
@@ -207,4 +193,68 @@
 
         return ResponseType.HTML;
     }
+
+
+    @RequestMapping(requestPath = "issues", method = HttpMethod.GET, menuKey = "menu.issues")
+    public ResponseType issues(HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) throws IOException, SQLException {
+        final var selectedProject = getSelectedProject(req, dao);
+        if (selectedProject == null) {
+            resp.sendError(HttpServletResponse.SC_FORBIDDEN);
+            return ResponseType.NONE;
+        }
+
+        req.setAttribute("issues", dao.getVersionDao().list(selectedProject));
+        setDynamicFragment(req, "issues");
+
+        return ResponseType.HTML;
+    }
+
+    @RequestMapping(requestPath = "issues/edit", method = HttpMethod.GET)
+    public ResponseType editIssue(HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) throws IOException, SQLException {
+        final var selectedProject = getSelectedProject(req, dao);
+        if (selectedProject == null) {
+            resp.sendError(HttpServletResponse.SC_FORBIDDEN);
+            return ResponseType.NONE;
+        }
+
+        req.setAttribute("issue", findByParameter(req, Integer.class, "id", dao.getIssueDao()::find));
+        req.setAttribute("issueStatusEnum", IssueStatus.values());
+        req.setAttribute("issueCategoryEnum", IssueCategory.values());
+
+        setDynamicFragment(req, "issue-form");
+
+        return ResponseType.HTML;
+    }
+
+    @RequestMapping(requestPath = "issues/commit", method = HttpMethod.POST)
+    public ResponseType commitIssue(HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) throws IOException, SQLException {
+        final var selectedProject = getSelectedProject(req, dao);
+        if (selectedProject == null) {
+            resp.sendError(HttpServletResponse.SC_FORBIDDEN);
+            return ResponseType.NONE;
+        }
+
+        Issue issue = new Issue(-1, selectedProject);
+        try {
+            issue = new Issue(getParameter(req, Integer.class, "id").orElseThrow(), selectedProject);
+
+            // TODO: implement
+
+            dao.getIssueDao().saveOrUpdate(issue);
+
+            setRedirectLocation(req, "./projects/issues/");
+            setDynamicFragment(req, Constants.DYN_FRAGMENT_COMMIT_SUCCESSFUL);
+            LOG.debug("Successfully updated issue {} for project {}", issue.getId(), selectedProject.getName());
+        } catch (NoSuchElementException | NumberFormatException | SQLException ex) {
+            // TODO: set request attribute with error text
+            req.setAttribute("issue", issue);
+            req.setAttribute("issueStatusEnum", IssueStatus.values());
+            req.setAttribute("issueCategoryEnum", IssueCategory.values());
+            setDynamicFragment(req, "issue-form");
+            LOG.warn("Form validation failure: {}", ex.getMessage());
+            LOG.debug("Details:", ex);
+        }
+
+        return ResponseType.HTML;
+    }
 }
--- a/src/main/resources/localization/projects.properties	Mon May 18 21:06:38 2020 +0200
+++ b/src/main/resources/localization/projects.properties	Mon May 18 21:08:14 2020 +0200
@@ -25,6 +25,7 @@
 
 menu.index=Index
 menu.versions=Versions
+menu.issues=Issues
 
 button.create=New Project
 button.version.create=New Version
--- a/src/main/resources/localization/projects_de.properties	Mon May 18 21:06:38 2020 +0200
+++ b/src/main/resources/localization/projects_de.properties	Mon May 18 21:08:14 2020 +0200
@@ -25,6 +25,7 @@
 
 menu.index=Index
 menu.versions=Versionen
+menu.issues=Vorg\u00e4nge
 
 button.create=Neues Projekt
 button.version.create=Neue Version
--- a/src/main/webapp/WEB-INF/dynamic_fragments/projects.jsp	Mon May 18 21:06:38 2020 +0200
+++ b/src/main/webapp/WEB-INF/dynamic_fragments/projects.jsp	Mon May 18 21:08:14 2020 +0200
@@ -67,7 +67,7 @@
     <c:forEach var="project" items="${projects}">
         <tr class="nowrap" <c:if test="${project eq selectedProject}">data-selected</c:if> >
             <td style="width: 2em;"><a href="./${moduleInfo.modulePath}/edit?id=${project.id}">&#x270e;</a></td>
-            <td><a href="./${moduleInfo.modulePath}/index/?select=${project.id}"><c:out value="${project.name}"/></a></td>
+            <td><a href="./${moduleInfo.modulePath}/index/?pid=${project.id}"><c:out value="${project.name}"/></a></td>
             <td><c:out value="${project.description}"/></td>
             <td>
                 <c:if test="${not empty project.repoUrl}">

mercurial