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

changeset 159
86b5d8a1662f
parent 158
4f912cd42876
child 161
3d9218457b62
--- a/src/main/java/de/uapcore/lightpit/modules/ProjectsModule.java	Fri Nov 06 10:50:32 2020 +0100
+++ b/src/main/java/de/uapcore/lightpit/modules/ProjectsModule.java	Thu Nov 19 13:58:54 2020 +0100
@@ -30,7 +30,7 @@
 
 
 import de.uapcore.lightpit.*;
-import de.uapcore.lightpit.dao.DataAccessObjects;
+import de.uapcore.lightpit.dao.DaoProvider;
 import de.uapcore.lightpit.entities.*;
 import de.uapcore.lightpit.types.WebColor;
 import de.uapcore.lightpit.viewmodel.*;
@@ -72,7 +72,7 @@
         }
     }
 
-    private void populate(ProjectView viewModel, PathParameters pathParameters, DataAccessObjects dao) throws SQLException {
+    private void populate(ProjectView viewModel, PathParameters pathParameters, DaoProvider dao) throws SQLException {
         final var projectDao = dao.getProjectDao();
         final var versionDao = dao.getVersionDao();
         final var componentDao = dao.getComponentDao();
@@ -97,7 +97,7 @@
         final var versionNode = pathParameters.get("version");
         if ("no-version".equals(versionNode)) {
             viewModel.setVersionFilter(ProjectView.NO_VERSION);
-        } else if ("all-versions".equals(versionNode)) {
+        } else if ("all-versions".equals(versionNode) || versionNode == null) {
             viewModel.setVersionFilter(ProjectView.ALL_VERSIONS);
         } else {
             viewModel.setVersionFilter(versionDao.findByNode(project, versionNode));
@@ -107,7 +107,7 @@
         final var componentNode = pathParameters.get("component");
         if ("no-component".equals(componentNode)) {
             viewModel.setComponentFilter(ProjectView.NO_COMPONENT);
-        } else if ("all-components".equals(componentNode)) {
+        } else if ("all-components".equals(componentNode) || componentNode == null) {
             viewModel.setComponentFilter(ProjectView.ALL_COMPONENTS);
         } else {
             viewModel.setComponentFilter(componentDao.findByNode(project, componentNode));
@@ -133,7 +133,7 @@
     }
 
     @RequestMapping(method = HttpMethod.GET)
-    public void index(HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) throws SQLException, ServletException, IOException {
+    public void index(HttpServletRequest req, HttpServletResponse resp, DaoProvider dao) throws SQLException, ServletException, IOException {
         final var viewModel = new ProjectView();
         populate(viewModel, null, dao);
 
@@ -148,13 +148,13 @@
         forwardView(req, resp, viewModel, "projects");
     }
 
-    private void configureProjectEditor(ProjectEditView viewModel, Project project, DataAccessObjects dao) throws SQLException {
+    private void configureProjectEditor(ProjectEditView viewModel, Project project, DaoProvider dao) throws SQLException {
         viewModel.setProject(project);
         viewModel.setUsers(dao.getUserDao().list());
     }
 
     @RequestMapping(requestPath = "$project/edit", method = HttpMethod.GET)
-    public void edit(HttpServletRequest req, HttpServletResponse resp, PathParameters pathParams, DataAccessObjects dao) throws IOException, SQLException, ServletException {
+    public void edit(HttpServletRequest req, HttpServletResponse resp, PathParameters pathParams, DaoProvider dao) throws IOException, SQLException, ServletException {
         final var viewModel = new ProjectEditView();
         populate(viewModel, pathParams, dao);
 
@@ -168,7 +168,7 @@
     }
 
     @RequestMapping(requestPath = "create", method = HttpMethod.GET)
-    public void create(HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) throws SQLException, ServletException, IOException {
+    public void create(HttpServletRequest req, HttpServletResponse resp, DaoProvider dao) throws SQLException, ServletException, IOException {
         final var viewModel = new ProjectEditView();
         populate(viewModel, null, dao);
         configureProjectEditor(viewModel, new Project(-1), dao);
@@ -176,7 +176,7 @@
     }
 
     @RequestMapping(requestPath = "commit", method = HttpMethod.POST)
-    public void commit(HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) throws IOException, ServletException {
+    public void commit(HttpServletRequest req, HttpServletResponse resp, DaoProvider dao) throws IOException, ServletException {
 
         try {
             final var project = new Project(getParameter(req, Integer.class, "pid").orElseThrow());
@@ -191,21 +191,27 @@
                     ownerId -> ownerId >= 0 ? new User(ownerId) : null
             ).ifPresent(project::setOwner);
 
-            dao.getProjectDao().saveOrUpdate(project);
+            final var projectDao = dao.getProjectDao();
+            if (project.getId() > 0) {
+                // TODO: unused return value
+                projectDao.update(project);
+            } else {
+                projectDao.save(project);
+            }
 
             setRedirectLocation(req, "./projects/");
             setContentPage(req, Constants.JSP_COMMIT_SUCCESSFUL);
             LOG.debug("Successfully updated project {}", project.getName());
 
             renderSite(req, resp);
-        } catch (NoSuchElementException | IllegalArgumentException | SQLException ex) {
+        } catch (NoSuchElementException | IllegalArgumentException ex) {
             resp.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED);
             // TODO: implement - fix issue #21
         }
     }
 
     @RequestMapping(requestPath = "$project/$component/$version/issues/", method = HttpMethod.GET)
-    public void issues(HttpServletRequest req, HttpServletResponse resp, PathParameters pathParams, DataAccessObjects dao) throws SQLException, IOException, ServletException {
+    public void issues(HttpServletRequest req, HttpServletResponse resp, PathParameters pathParams, DaoProvider dao) throws SQLException, IOException, ServletException {
         final var viewModel = new ProjectDetailsView();
         populate(viewModel, pathParams, dao);
 
@@ -263,7 +269,7 @@
     }
 
     @RequestMapping(requestPath = "$project/versions/", method = HttpMethod.GET)
-    public void versions(HttpServletRequest req, HttpServletResponse resp, PathParameters pathParameters, DataAccessObjects dao) throws IOException, SQLException, ServletException {
+    public void versions(HttpServletRequest req, HttpServletResponse resp, PathParameters pathParameters, DaoProvider dao) throws IOException, SQLException, ServletException {
         final var viewModel = new VersionsView();
         populate(viewModel, pathParameters, dao);
 
@@ -282,7 +288,7 @@
     }
 
     @RequestMapping(requestPath = "$project/versions/$version/edit", method = HttpMethod.GET)
-    public void editVersion(HttpServletRequest req, HttpServletResponse resp, PathParameters pathParameters, DataAccessObjects dao) throws IOException, SQLException, ServletException {
+    public void editVersion(HttpServletRequest req, HttpServletResponse resp, PathParameters pathParameters, DaoProvider dao) throws IOException, SQLException, ServletException {
         final var viewModel = new VersionEditView();
         populate(viewModel, pathParameters, dao);
 
@@ -297,7 +303,7 @@
     }
 
     @RequestMapping(requestPath = "$project/create-version", method = HttpMethod.GET)
-    public void createVersion(HttpServletRequest req, HttpServletResponse resp, PathParameters pathParameters, DataAccessObjects dao) throws IOException, SQLException, ServletException {
+    public void createVersion(HttpServletRequest req, HttpServletResponse resp, PathParameters pathParameters, DaoProvider dao) throws IOException, SQLException, ServletException {
         final var viewModel = new VersionEditView();
         populate(viewModel, pathParameters, dao);
 
@@ -312,7 +318,7 @@
     }
 
     @RequestMapping(requestPath = "commit-version", method = HttpMethod.POST)
-    public void commitVersion(HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) throws IOException, ServletException {
+    public void commitVersion(HttpServletRequest req, HttpServletResponse resp, DaoProvider dao) throws IOException, ServletException {
 
         try {
             final var project = dao.getProjectDao().find(getParameter(req, Integer.class, "pid").orElseThrow());
@@ -329,20 +335,27 @@
 
             getParameter(req, Integer.class, "ordinal").ifPresent(version::setOrdinal);
             version.setStatus(VersionStatus.valueOf(getParameter(req, String.class, "status").orElseThrow()));
-            dao.getVersionDao().saveOrUpdate(version, project);
+
+            final var versionDao = dao.getVersionDao();
+            if (version.getId() > 0) {
+                // TODO: use return value
+                versionDao.update(version);
+            } else {
+                versionDao.save(version, project);
+            }
 
             setRedirectLocation(req, "./projects/" + project.getNode() + "/versions/");
             setContentPage(req, Constants.JSP_COMMIT_SUCCESSFUL);
 
             renderSite(req, resp);
-        } catch (NoSuchElementException | IllegalArgumentException | SQLException ex) {
+        } catch (NoSuchElementException | IllegalArgumentException ex) {
             resp.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED);
             // TODO: implement - fix issue #21
         }
     }
 
     @RequestMapping(requestPath = "$project/components/", method = HttpMethod.GET)
-    public void components(HttpServletRequest req, HttpServletResponse resp, PathParameters pathParameters, DataAccessObjects dao) throws IOException, SQLException, ServletException {
+    public void components(HttpServletRequest req, HttpServletResponse resp, PathParameters pathParameters, DaoProvider dao) throws IOException, SQLException, ServletException {
         final var viewModel = new ComponentsView();
         populate(viewModel, pathParameters, dao);
 
@@ -360,7 +373,7 @@
     }
 
     @RequestMapping(requestPath = "$project/components/$component/edit", method = HttpMethod.GET)
-    public void editComponent(HttpServletRequest req, HttpServletResponse resp, PathParameters pathParameters, DataAccessObjects dao) throws IOException, SQLException, ServletException {
+    public void editComponent(HttpServletRequest req, HttpServletResponse resp, PathParameters pathParameters, DaoProvider dao) throws IOException, SQLException, ServletException {
         final var viewModel = new ComponentEditView();
         populate(viewModel, pathParameters, dao);
 
@@ -376,7 +389,7 @@
     }
 
     @RequestMapping(requestPath = "$project/create-component", method = HttpMethod.GET)
-    public void createComponent(HttpServletRequest req, HttpServletResponse resp, PathParameters pathParameters, DataAccessObjects dao) throws IOException, SQLException, ServletException {
+    public void createComponent(HttpServletRequest req, HttpServletResponse resp, PathParameters pathParameters, DaoProvider dao) throws IOException, SQLException, ServletException {
         final var viewModel = new ComponentEditView();
         populate(viewModel, pathParameters, dao);
 
@@ -392,7 +405,7 @@
     }
 
     @RequestMapping(requestPath = "commit-component", method = HttpMethod.POST)
-    public void commitComponent(HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) throws IOException, ServletException {
+    public void commitComponent(HttpServletRequest req, HttpServletResponse resp, DaoProvider dao) throws IOException, ServletException {
 
         try {
             final var project = dao.getProjectDao().find(getParameter(req, Integer.class, "pid").orElseThrow());
@@ -414,19 +427,25 @@
             ).ifPresent(component::setLead);
             getParameter(req, String.class, "description").ifPresent(component::setDescription);
 
-            dao.getComponentDao().saveOrUpdate(component, project);
+            final var componentDao = dao.getComponentDao();
+            if (component.getId() > 0) {
+                // TODO: use return value
+                componentDao.update(component);
+            } else {
+                componentDao.save(component, project);
+            }
 
             setRedirectLocation(req, "./projects/" + project.getNode() + "/components/");
             setContentPage(req, Constants.JSP_COMMIT_SUCCESSFUL);
 
             renderSite(req, resp);
-        } catch (NoSuchElementException | IllegalArgumentException | SQLException ex) {
+        } catch (NoSuchElementException | IllegalArgumentException ex) {
             resp.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED);
             // TODO: implement - fix issue #21
         }
     }
 
-    private void configureIssueEditor(IssueEditView viewModel, Issue issue, DataAccessObjects dao) throws SQLException {
+    private void configureIssueEditor(IssueEditView viewModel, Issue issue, DaoProvider dao) throws SQLException {
         final var project = viewModel.getProjectInfo().getProject();
         issue.setProject(project); // automatically set current project for new issues
         viewModel.setIssue(issue);
@@ -436,7 +455,7 @@
     }
 
     @RequestMapping(requestPath = "$project/issues/$issue/view", method = HttpMethod.GET)
-    public void viewIssue(HttpServletRequest req, HttpServletResponse resp, PathParameters pathParameters, DataAccessObjects dao) throws IOException, SQLException, ServletException {
+    public void viewIssue(HttpServletRequest req, HttpServletResponse resp, PathParameters pathParameters, DaoProvider dao) throws IOException, SQLException, ServletException {
         final var viewModel = new IssueDetailView();
         populate(viewModel, pathParameters, dao);
 
@@ -462,7 +481,7 @@
 
     // TODO: why should the issue editor be child of $project?
     @RequestMapping(requestPath = "$project/issues/$issue/edit", method = HttpMethod.GET)
-    public void editIssue(HttpServletRequest req, HttpServletResponse resp, PathParameters pathParameters, DataAccessObjects dao) throws IOException, SQLException, ServletException {
+    public void editIssue(HttpServletRequest req, HttpServletResponse resp, PathParameters pathParameters, DaoProvider dao) throws IOException, SQLException, ServletException {
         final var viewModel = new IssueEditView();
         populate(viewModel, pathParameters, dao);
 
@@ -486,7 +505,7 @@
     }
 
     @RequestMapping(requestPath = "$project/create-issue", method = HttpMethod.GET)
-    public void createIssue(HttpServletRequest req, HttpServletResponse resp, PathParameters pathParameters, DataAccessObjects dao) throws IOException, SQLException, ServletException {
+    public void createIssue(HttpServletRequest req, HttpServletResponse resp, PathParameters pathParameters, DaoProvider dao) throws IOException, SQLException, ServletException {
         final var viewModel = new IssueEditView();
         populate(viewModel, pathParameters, dao);
 
@@ -504,7 +523,7 @@
     }
 
     @RequestMapping(requestPath = "commit-issue", method = HttpMethod.POST)
-    public void commitIssue(HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) throws IOException, ServletException {
+    public void commitIssue(HttpServletRequest req, HttpServletResponse resp, DaoProvider dao) throws IOException, ServletException {
         try {
             final var issue = new Issue(getParameter(req, Integer.class, "id").orElseThrow());
             final var componentId = getParameter(req, Integer.class, "component");
@@ -549,21 +568,27 @@
                             stream.map(Version::new).collect(Collectors.toList())
                     ).ifPresent(issue::setResolvedVersions);
 
-            dao.getIssueDao().saveOrUpdate(issue, issue.getProject());
+            final var issueDao = dao.getIssueDao();
+            if (issue.getId() > 0) {
+                // TODO: use return value
+                issueDao.update(issue);
+            } else {
+                issueDao.save(issue, project);
+            }
 
             // TODO: fix redirect location
             setRedirectLocation(req, "./projects/" + issue.getProject().getNode()+"/issues/"+issue.getId()+"/view");
             setContentPage(req, Constants.JSP_COMMIT_SUCCESSFUL);
 
             renderSite(req, resp);
-        } catch (NoSuchElementException | IllegalArgumentException | SQLException ex) {
+        } catch (NoSuchElementException | IllegalArgumentException ex) {
             resp.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED);
             // TODO: implement - fix issue #21
         }
     }
 
     @RequestMapping(requestPath = "commit-issue-comment", method = HttpMethod.POST)
-    public void commentIssue(HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) throws SQLException, IOException, ServletException {
+    public void commentIssue(HttpServletRequest req, HttpServletResponse resp, DaoProvider dao) throws IOException, ServletException {
         final var issueIdParam = getParameter(req, Integer.class, "issueid");
         if (issueIdParam.isEmpty()) {
             resp.sendError(HttpServletResponse.SC_FORBIDDEN, "Detected manipulated form.");
@@ -584,7 +609,7 @@
 
             LOG.debug("User {} is commenting on issue #{}", req.getRemoteUser(), issue.getId());
             if (req.getRemoteUser() != null) {
-                dao.getUserDao().findByUsername(req.getRemoteUser()).ifPresent(issueComment::setAuthor);
+                Optional.ofNullable(dao.getUserDao().findByUsername(req.getRemoteUser())).ifPresent(issueComment::setAuthor);
             }
 
             dao.getIssueDao().saveComment(issue, issueComment);
@@ -594,7 +619,7 @@
             setContentPage(req, Constants.JSP_COMMIT_SUCCESSFUL);
 
             renderSite(req, resp);
-        } catch (NoSuchElementException | IllegalArgumentException | SQLException ex) {
+        } catch (NoSuchElementException | IllegalArgumentException ex) {
             resp.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED);
             // TODO: implement - fix issue #21
         }

mercurial