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

changeset 159
86b5d8a1662f
parent 158
4f912cd42876
child 161
3d9218457b62
     1.1 --- a/src/main/java/de/uapcore/lightpit/modules/ProjectsModule.java	Fri Nov 06 10:50:32 2020 +0100
     1.2 +++ b/src/main/java/de/uapcore/lightpit/modules/ProjectsModule.java	Thu Nov 19 13:58:54 2020 +0100
     1.3 @@ -30,7 +30,7 @@
     1.4  
     1.5  
     1.6  import de.uapcore.lightpit.*;
     1.7 -import de.uapcore.lightpit.dao.DataAccessObjects;
     1.8 +import de.uapcore.lightpit.dao.DaoProvider;
     1.9  import de.uapcore.lightpit.entities.*;
    1.10  import de.uapcore.lightpit.types.WebColor;
    1.11  import de.uapcore.lightpit.viewmodel.*;
    1.12 @@ -72,7 +72,7 @@
    1.13          }
    1.14      }
    1.15  
    1.16 -    private void populate(ProjectView viewModel, PathParameters pathParameters, DataAccessObjects dao) throws SQLException {
    1.17 +    private void populate(ProjectView viewModel, PathParameters pathParameters, DaoProvider dao) throws SQLException {
    1.18          final var projectDao = dao.getProjectDao();
    1.19          final var versionDao = dao.getVersionDao();
    1.20          final var componentDao = dao.getComponentDao();
    1.21 @@ -97,7 +97,7 @@
    1.22          final var versionNode = pathParameters.get("version");
    1.23          if ("no-version".equals(versionNode)) {
    1.24              viewModel.setVersionFilter(ProjectView.NO_VERSION);
    1.25 -        } else if ("all-versions".equals(versionNode)) {
    1.26 +        } else if ("all-versions".equals(versionNode) || versionNode == null) {
    1.27              viewModel.setVersionFilter(ProjectView.ALL_VERSIONS);
    1.28          } else {
    1.29              viewModel.setVersionFilter(versionDao.findByNode(project, versionNode));
    1.30 @@ -107,7 +107,7 @@
    1.31          final var componentNode = pathParameters.get("component");
    1.32          if ("no-component".equals(componentNode)) {
    1.33              viewModel.setComponentFilter(ProjectView.NO_COMPONENT);
    1.34 -        } else if ("all-components".equals(componentNode)) {
    1.35 +        } else if ("all-components".equals(componentNode) || componentNode == null) {
    1.36              viewModel.setComponentFilter(ProjectView.ALL_COMPONENTS);
    1.37          } else {
    1.38              viewModel.setComponentFilter(componentDao.findByNode(project, componentNode));
    1.39 @@ -133,7 +133,7 @@
    1.40      }
    1.41  
    1.42      @RequestMapping(method = HttpMethod.GET)
    1.43 -    public void index(HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) throws SQLException, ServletException, IOException {
    1.44 +    public void index(HttpServletRequest req, HttpServletResponse resp, DaoProvider dao) throws SQLException, ServletException, IOException {
    1.45          final var viewModel = new ProjectView();
    1.46          populate(viewModel, null, dao);
    1.47  
    1.48 @@ -148,13 +148,13 @@
    1.49          forwardView(req, resp, viewModel, "projects");
    1.50      }
    1.51  
    1.52 -    private void configureProjectEditor(ProjectEditView viewModel, Project project, DataAccessObjects dao) throws SQLException {
    1.53 +    private void configureProjectEditor(ProjectEditView viewModel, Project project, DaoProvider dao) throws SQLException {
    1.54          viewModel.setProject(project);
    1.55          viewModel.setUsers(dao.getUserDao().list());
    1.56      }
    1.57  
    1.58      @RequestMapping(requestPath = "$project/edit", method = HttpMethod.GET)
    1.59 -    public void edit(HttpServletRequest req, HttpServletResponse resp, PathParameters pathParams, DataAccessObjects dao) throws IOException, SQLException, ServletException {
    1.60 +    public void edit(HttpServletRequest req, HttpServletResponse resp, PathParameters pathParams, DaoProvider dao) throws IOException, SQLException, ServletException {
    1.61          final var viewModel = new ProjectEditView();
    1.62          populate(viewModel, pathParams, dao);
    1.63  
    1.64 @@ -168,7 +168,7 @@
    1.65      }
    1.66  
    1.67      @RequestMapping(requestPath = "create", method = HttpMethod.GET)
    1.68 -    public void create(HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) throws SQLException, ServletException, IOException {
    1.69 +    public void create(HttpServletRequest req, HttpServletResponse resp, DaoProvider dao) throws SQLException, ServletException, IOException {
    1.70          final var viewModel = new ProjectEditView();
    1.71          populate(viewModel, null, dao);
    1.72          configureProjectEditor(viewModel, new Project(-1), dao);
    1.73 @@ -176,7 +176,7 @@
    1.74      }
    1.75  
    1.76      @RequestMapping(requestPath = "commit", method = HttpMethod.POST)
    1.77 -    public void commit(HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) throws IOException, ServletException {
    1.78 +    public void commit(HttpServletRequest req, HttpServletResponse resp, DaoProvider dao) throws IOException, ServletException {
    1.79  
    1.80          try {
    1.81              final var project = new Project(getParameter(req, Integer.class, "pid").orElseThrow());
    1.82 @@ -191,21 +191,27 @@
    1.83                      ownerId -> ownerId >= 0 ? new User(ownerId) : null
    1.84              ).ifPresent(project::setOwner);
    1.85  
    1.86 -            dao.getProjectDao().saveOrUpdate(project);
    1.87 +            final var projectDao = dao.getProjectDao();
    1.88 +            if (project.getId() > 0) {
    1.89 +                // TODO: unused return value
    1.90 +                projectDao.update(project);
    1.91 +            } else {
    1.92 +                projectDao.save(project);
    1.93 +            }
    1.94  
    1.95              setRedirectLocation(req, "./projects/");
    1.96              setContentPage(req, Constants.JSP_COMMIT_SUCCESSFUL);
    1.97              LOG.debug("Successfully updated project {}", project.getName());
    1.98  
    1.99              renderSite(req, resp);
   1.100 -        } catch (NoSuchElementException | IllegalArgumentException | SQLException ex) {
   1.101 +        } catch (NoSuchElementException | IllegalArgumentException ex) {
   1.102              resp.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED);
   1.103              // TODO: implement - fix issue #21
   1.104          }
   1.105      }
   1.106  
   1.107      @RequestMapping(requestPath = "$project/$component/$version/issues/", method = HttpMethod.GET)
   1.108 -    public void issues(HttpServletRequest req, HttpServletResponse resp, PathParameters pathParams, DataAccessObjects dao) throws SQLException, IOException, ServletException {
   1.109 +    public void issues(HttpServletRequest req, HttpServletResponse resp, PathParameters pathParams, DaoProvider dao) throws SQLException, IOException, ServletException {
   1.110          final var viewModel = new ProjectDetailsView();
   1.111          populate(viewModel, pathParams, dao);
   1.112  
   1.113 @@ -263,7 +269,7 @@
   1.114      }
   1.115  
   1.116      @RequestMapping(requestPath = "$project/versions/", method = HttpMethod.GET)
   1.117 -    public void versions(HttpServletRequest req, HttpServletResponse resp, PathParameters pathParameters, DataAccessObjects dao) throws IOException, SQLException, ServletException {
   1.118 +    public void versions(HttpServletRequest req, HttpServletResponse resp, PathParameters pathParameters, DaoProvider dao) throws IOException, SQLException, ServletException {
   1.119          final var viewModel = new VersionsView();
   1.120          populate(viewModel, pathParameters, dao);
   1.121  
   1.122 @@ -282,7 +288,7 @@
   1.123      }
   1.124  
   1.125      @RequestMapping(requestPath = "$project/versions/$version/edit", method = HttpMethod.GET)
   1.126 -    public void editVersion(HttpServletRequest req, HttpServletResponse resp, PathParameters pathParameters, DataAccessObjects dao) throws IOException, SQLException, ServletException {
   1.127 +    public void editVersion(HttpServletRequest req, HttpServletResponse resp, PathParameters pathParameters, DaoProvider dao) throws IOException, SQLException, ServletException {
   1.128          final var viewModel = new VersionEditView();
   1.129          populate(viewModel, pathParameters, dao);
   1.130  
   1.131 @@ -297,7 +303,7 @@
   1.132      }
   1.133  
   1.134      @RequestMapping(requestPath = "$project/create-version", method = HttpMethod.GET)
   1.135 -    public void createVersion(HttpServletRequest req, HttpServletResponse resp, PathParameters pathParameters, DataAccessObjects dao) throws IOException, SQLException, ServletException {
   1.136 +    public void createVersion(HttpServletRequest req, HttpServletResponse resp, PathParameters pathParameters, DaoProvider dao) throws IOException, SQLException, ServletException {
   1.137          final var viewModel = new VersionEditView();
   1.138          populate(viewModel, pathParameters, dao);
   1.139  
   1.140 @@ -312,7 +318,7 @@
   1.141      }
   1.142  
   1.143      @RequestMapping(requestPath = "commit-version", method = HttpMethod.POST)
   1.144 -    public void commitVersion(HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) throws IOException, ServletException {
   1.145 +    public void commitVersion(HttpServletRequest req, HttpServletResponse resp, DaoProvider dao) throws IOException, ServletException {
   1.146  
   1.147          try {
   1.148              final var project = dao.getProjectDao().find(getParameter(req, Integer.class, "pid").orElseThrow());
   1.149 @@ -329,20 +335,27 @@
   1.150  
   1.151              getParameter(req, Integer.class, "ordinal").ifPresent(version::setOrdinal);
   1.152              version.setStatus(VersionStatus.valueOf(getParameter(req, String.class, "status").orElseThrow()));
   1.153 -            dao.getVersionDao().saveOrUpdate(version, project);
   1.154 +
   1.155 +            final var versionDao = dao.getVersionDao();
   1.156 +            if (version.getId() > 0) {
   1.157 +                // TODO: use return value
   1.158 +                versionDao.update(version);
   1.159 +            } else {
   1.160 +                versionDao.save(version, project);
   1.161 +            }
   1.162  
   1.163              setRedirectLocation(req, "./projects/" + project.getNode() + "/versions/");
   1.164              setContentPage(req, Constants.JSP_COMMIT_SUCCESSFUL);
   1.165  
   1.166              renderSite(req, resp);
   1.167 -        } catch (NoSuchElementException | IllegalArgumentException | SQLException ex) {
   1.168 +        } catch (NoSuchElementException | IllegalArgumentException ex) {
   1.169              resp.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED);
   1.170              // TODO: implement - fix issue #21
   1.171          }
   1.172      }
   1.173  
   1.174      @RequestMapping(requestPath = "$project/components/", method = HttpMethod.GET)
   1.175 -    public void components(HttpServletRequest req, HttpServletResponse resp, PathParameters pathParameters, DataAccessObjects dao) throws IOException, SQLException, ServletException {
   1.176 +    public void components(HttpServletRequest req, HttpServletResponse resp, PathParameters pathParameters, DaoProvider dao) throws IOException, SQLException, ServletException {
   1.177          final var viewModel = new ComponentsView();
   1.178          populate(viewModel, pathParameters, dao);
   1.179  
   1.180 @@ -360,7 +373,7 @@
   1.181      }
   1.182  
   1.183      @RequestMapping(requestPath = "$project/components/$component/edit", method = HttpMethod.GET)
   1.184 -    public void editComponent(HttpServletRequest req, HttpServletResponse resp, PathParameters pathParameters, DataAccessObjects dao) throws IOException, SQLException, ServletException {
   1.185 +    public void editComponent(HttpServletRequest req, HttpServletResponse resp, PathParameters pathParameters, DaoProvider dao) throws IOException, SQLException, ServletException {
   1.186          final var viewModel = new ComponentEditView();
   1.187          populate(viewModel, pathParameters, dao);
   1.188  
   1.189 @@ -376,7 +389,7 @@
   1.190      }
   1.191  
   1.192      @RequestMapping(requestPath = "$project/create-component", method = HttpMethod.GET)
   1.193 -    public void createComponent(HttpServletRequest req, HttpServletResponse resp, PathParameters pathParameters, DataAccessObjects dao) throws IOException, SQLException, ServletException {
   1.194 +    public void createComponent(HttpServletRequest req, HttpServletResponse resp, PathParameters pathParameters, DaoProvider dao) throws IOException, SQLException, ServletException {
   1.195          final var viewModel = new ComponentEditView();
   1.196          populate(viewModel, pathParameters, dao);
   1.197  
   1.198 @@ -392,7 +405,7 @@
   1.199      }
   1.200  
   1.201      @RequestMapping(requestPath = "commit-component", method = HttpMethod.POST)
   1.202 -    public void commitComponent(HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) throws IOException, ServletException {
   1.203 +    public void commitComponent(HttpServletRequest req, HttpServletResponse resp, DaoProvider dao) throws IOException, ServletException {
   1.204  
   1.205          try {
   1.206              final var project = dao.getProjectDao().find(getParameter(req, Integer.class, "pid").orElseThrow());
   1.207 @@ -414,19 +427,25 @@
   1.208              ).ifPresent(component::setLead);
   1.209              getParameter(req, String.class, "description").ifPresent(component::setDescription);
   1.210  
   1.211 -            dao.getComponentDao().saveOrUpdate(component, project);
   1.212 +            final var componentDao = dao.getComponentDao();
   1.213 +            if (component.getId() > 0) {
   1.214 +                // TODO: use return value
   1.215 +                componentDao.update(component);
   1.216 +            } else {
   1.217 +                componentDao.save(component, project);
   1.218 +            }
   1.219  
   1.220              setRedirectLocation(req, "./projects/" + project.getNode() + "/components/");
   1.221              setContentPage(req, Constants.JSP_COMMIT_SUCCESSFUL);
   1.222  
   1.223              renderSite(req, resp);
   1.224 -        } catch (NoSuchElementException | IllegalArgumentException | SQLException ex) {
   1.225 +        } catch (NoSuchElementException | IllegalArgumentException ex) {
   1.226              resp.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED);
   1.227              // TODO: implement - fix issue #21
   1.228          }
   1.229      }
   1.230  
   1.231 -    private void configureIssueEditor(IssueEditView viewModel, Issue issue, DataAccessObjects dao) throws SQLException {
   1.232 +    private void configureIssueEditor(IssueEditView viewModel, Issue issue, DaoProvider dao) throws SQLException {
   1.233          final var project = viewModel.getProjectInfo().getProject();
   1.234          issue.setProject(project); // automatically set current project for new issues
   1.235          viewModel.setIssue(issue);
   1.236 @@ -436,7 +455,7 @@
   1.237      }
   1.238  
   1.239      @RequestMapping(requestPath = "$project/issues/$issue/view", method = HttpMethod.GET)
   1.240 -    public void viewIssue(HttpServletRequest req, HttpServletResponse resp, PathParameters pathParameters, DataAccessObjects dao) throws IOException, SQLException, ServletException {
   1.241 +    public void viewIssue(HttpServletRequest req, HttpServletResponse resp, PathParameters pathParameters, DaoProvider dao) throws IOException, SQLException, ServletException {
   1.242          final var viewModel = new IssueDetailView();
   1.243          populate(viewModel, pathParameters, dao);
   1.244  
   1.245 @@ -462,7 +481,7 @@
   1.246  
   1.247      // TODO: why should the issue editor be child of $project?
   1.248      @RequestMapping(requestPath = "$project/issues/$issue/edit", method = HttpMethod.GET)
   1.249 -    public void editIssue(HttpServletRequest req, HttpServletResponse resp, PathParameters pathParameters, DataAccessObjects dao) throws IOException, SQLException, ServletException {
   1.250 +    public void editIssue(HttpServletRequest req, HttpServletResponse resp, PathParameters pathParameters, DaoProvider dao) throws IOException, SQLException, ServletException {
   1.251          final var viewModel = new IssueEditView();
   1.252          populate(viewModel, pathParameters, dao);
   1.253  
   1.254 @@ -486,7 +505,7 @@
   1.255      }
   1.256  
   1.257      @RequestMapping(requestPath = "$project/create-issue", method = HttpMethod.GET)
   1.258 -    public void createIssue(HttpServletRequest req, HttpServletResponse resp, PathParameters pathParameters, DataAccessObjects dao) throws IOException, SQLException, ServletException {
   1.259 +    public void createIssue(HttpServletRequest req, HttpServletResponse resp, PathParameters pathParameters, DaoProvider dao) throws IOException, SQLException, ServletException {
   1.260          final var viewModel = new IssueEditView();
   1.261          populate(viewModel, pathParameters, dao);
   1.262  
   1.263 @@ -504,7 +523,7 @@
   1.264      }
   1.265  
   1.266      @RequestMapping(requestPath = "commit-issue", method = HttpMethod.POST)
   1.267 -    public void commitIssue(HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) throws IOException, ServletException {
   1.268 +    public void commitIssue(HttpServletRequest req, HttpServletResponse resp, DaoProvider dao) throws IOException, ServletException {
   1.269          try {
   1.270              final var issue = new Issue(getParameter(req, Integer.class, "id").orElseThrow());
   1.271              final var componentId = getParameter(req, Integer.class, "component");
   1.272 @@ -549,21 +568,27 @@
   1.273                              stream.map(Version::new).collect(Collectors.toList())
   1.274                      ).ifPresent(issue::setResolvedVersions);
   1.275  
   1.276 -            dao.getIssueDao().saveOrUpdate(issue, issue.getProject());
   1.277 +            final var issueDao = dao.getIssueDao();
   1.278 +            if (issue.getId() > 0) {
   1.279 +                // TODO: use return value
   1.280 +                issueDao.update(issue);
   1.281 +            } else {
   1.282 +                issueDao.save(issue, project);
   1.283 +            }
   1.284  
   1.285              // TODO: fix redirect location
   1.286              setRedirectLocation(req, "./projects/" + issue.getProject().getNode()+"/issues/"+issue.getId()+"/view");
   1.287              setContentPage(req, Constants.JSP_COMMIT_SUCCESSFUL);
   1.288  
   1.289              renderSite(req, resp);
   1.290 -        } catch (NoSuchElementException | IllegalArgumentException | SQLException ex) {
   1.291 +        } catch (NoSuchElementException | IllegalArgumentException ex) {
   1.292              resp.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED);
   1.293              // TODO: implement - fix issue #21
   1.294          }
   1.295      }
   1.296  
   1.297      @RequestMapping(requestPath = "commit-issue-comment", method = HttpMethod.POST)
   1.298 -    public void commentIssue(HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) throws SQLException, IOException, ServletException {
   1.299 +    public void commentIssue(HttpServletRequest req, HttpServletResponse resp, DaoProvider dao) throws IOException, ServletException {
   1.300          final var issueIdParam = getParameter(req, Integer.class, "issueid");
   1.301          if (issueIdParam.isEmpty()) {
   1.302              resp.sendError(HttpServletResponse.SC_FORBIDDEN, "Detected manipulated form.");
   1.303 @@ -584,7 +609,7 @@
   1.304  
   1.305              LOG.debug("User {} is commenting on issue #{}", req.getRemoteUser(), issue.getId());
   1.306              if (req.getRemoteUser() != null) {
   1.307 -                dao.getUserDao().findByUsername(req.getRemoteUser()).ifPresent(issueComment::setAuthor);
   1.308 +                Optional.ofNullable(dao.getUserDao().findByUsername(req.getRemoteUser())).ifPresent(issueComment::setAuthor);
   1.309              }
   1.310  
   1.311              dao.getIssueDao().saveComment(issue, issueComment);
   1.312 @@ -594,7 +619,7 @@
   1.313              setContentPage(req, Constants.JSP_COMMIT_SUCCESSFUL);
   1.314  
   1.315              renderSite(req, resp);
   1.316 -        } catch (NoSuchElementException | IllegalArgumentException | SQLException ex) {
   1.317 +        } catch (NoSuchElementException | IllegalArgumentException ex) {
   1.318              resp.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED);
   1.319              // TODO: implement - fix issue #21
   1.320          }

mercurial