Fri, 23 Oct 2020 18:40:50 +0200
migrate WebColor
/* * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. * * Copyright 2018 Mike Becker. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * */ package de.uapcore.lightpit.modules; import de.uapcore.lightpit.*; import de.uapcore.lightpit.dao.DataAccessObjects; import de.uapcore.lightpit.entities.*; import de.uapcore.lightpit.types.WebColor; import de.uapcore.lightpit.viewmodel.*; import de.uapcore.lightpit.viewmodel.util.IssueSorter; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.sql.Date; import java.sql.SQLException; import java.util.List; import java.util.NoSuchElementException; import java.util.Optional; import java.util.stream.Collectors; import java.util.stream.Stream; @WebServlet( name = "ProjectsModule", urlPatterns = "/projects/*" ) public final class ProjectsModule extends AbstractLightPITServlet { private static final Logger LOG = LoggerFactory.getLogger(ProjectsModule.class); @Override protected String getResourceBundleName() { return "localization.projects"; } 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 var project = projectDao.findByNode(pathParameters.get("project")); if (project == null) return; final var info = new ProjectInfo(project); info.setVersions(versionDao.list(project)); info.setComponents(componentDao.list(project)); info.setIssueSummary(projectDao.getIssueSummary(project)); viewModel.setProjectInfo(info); // Select Version final var versionNode = pathParameters.get("version"); if ("no-version".equals(versionNode)) { viewModel.setVersionFilter(ProjectView.NO_VERSION); } else if ("all-versions".equals(versionNode)) { viewModel.setVersionFilter(ProjectView.ALL_VERSIONS); } else { viewModel.setVersionFilter(versionDao.findByNode(project, versionNode)); } // Select Component final var componentNode = pathParameters.get("component"); if ("no-component".equals(componentNode)) { viewModel.setComponentFilter(ProjectView.NO_COMPONENT); } else if ("all-components".equals(componentNode)) { viewModel.setComponentFilter(ProjectView.ALL_COMPONENTS); } else { viewModel.setComponentFilter(componentDao.findByNode(project, componentNode)); } } private static String sanitizeNode(String node, String defaultValue) { String result = node == null || node.isBlank() ? defaultValue : node; result = result.replace('/', '-'); if (result.equals(".") || result.equals("..")) { return "_"+result; } else { return result; } } private ResponseType forwardView(HttpServletRequest req, ProjectView viewModel, String name) { setViewModel(req, viewModel); setContentPage(req, name); setStylesheet(req, "projects"); setNavigationMenu(req, "project-navmenu"); return ResponseType.HTML; } @RequestMapping(method = HttpMethod.GET) public ResponseType index(HttpServletRequest req, DataAccessObjects dao) throws SQLException { final var viewModel = new ProjectView(); populate(viewModel, null, dao); final var projectDao = dao.getProjectDao(); final var versionDao = dao.getVersionDao(); for (var info : viewModel.getProjectList()) { info.setVersions(versionDao.list(info.getProject())); info.setIssueSummary(projectDao.getIssueSummary(info.getProject())); } return forwardView(req, viewModel, "projects"); } private void configureProjectEditor(ProjectEditView viewModel, Project project, DataAccessObjects dao) throws SQLException { viewModel.setProject(project); viewModel.setUsers(dao.getUserDao().list()); } @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, pathParams, dao); if (!viewModel.isProjectInfoPresent()) { resp.sendError(HttpServletResponse.SC_NOT_FOUND); return ResponseType.NONE; } 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, HttpServletResponse resp, DataAccessObjects dao) throws IOException { try { final var project = new Project(getParameter(req, Integer.class, "pid").orElseThrow()); project.setName(getParameter(req, String.class, "name").orElseThrow()); final var node = getParameter(req, String.class, "node").orElse(null); project.setNode(sanitizeNode(node, project.getName())); getParameter(req, String.class, "description").ifPresent(project::setDescription); getParameter(req, String.class, "repoUrl").ifPresent(project::setRepoUrl); getParameter(req, Integer.class, "owner").map( ownerId -> ownerId >= 0 ? new User(ownerId) : null ).ifPresent(project::setOwner); dao.getProjectDao().saveOrUpdate(project); setRedirectLocation(req, "./projects/"); setContentPage(req, Constants.JSP_COMMIT_SUCCESSFUL); LOG.debug("Successfully updated project {}", project.getName()); return ResponseType.HTML; } catch (NoSuchElementException | IllegalArgumentException | SQLException ex) { resp.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED); // TODO: implement - fix issue #21 return ResponseType.NONE; } } @RequestMapping(requestPath = "$project/$component/$version/issues/", method = HttpMethod.GET) public ResponseType issues(HttpServletRequest req, HttpServletResponse resp, PathParameters pathParams, DataAccessObjects dao) throws SQLException, IOException { final var viewModel = new ProjectDetailsView(); populate(viewModel, pathParams, dao); if (!viewModel.isEveryFilterValid()) { resp.sendError(HttpServletResponse.SC_NOT_FOUND); return ResponseType.NONE; } final var project = viewModel.getProjectInfo().getProject(); final var version = viewModel.getVersionFilter(); final var component = viewModel.getComponentFilter(); final var issueDao = dao.getIssueDao(); final List<Issue> issues; if (version.equals(ProjectView.NO_VERSION)) { if (component.equals(ProjectView.ALL_COMPONENTS)) { issues = issueDao.list(project, (Version) null); } else if (component.equals(ProjectView.NO_COMPONENT)) { issues = issueDao.list(project, null, null); } else { issues = issueDao.list(project, component, null); } } else if (version.equals(ProjectView.ALL_VERSIONS)) { if (component.equals(ProjectView.ALL_COMPONENTS)) { issues = issueDao.list(project); } else if (component.equals(ProjectView.NO_COMPONENT)) { issues = issueDao.list(project, (Component)null); } else { issues = issueDao.list(project, component); } } else { if (component.equals(ProjectView.ALL_COMPONENTS)) { issues = issueDao.list(project, version); } else if (component.equals(ProjectView.NO_COMPONENT)) { issues = issueDao.list(project, null, version); } else { issues = issueDao.list(project, component, version); } } for (var issue : issues) issueDao.joinVersionInformation(issue); issues.sort(new IssueSorter( new IssueSorter.Criteria(IssueSorter.Field.DONE, true), new IssueSorter.Criteria(IssueSorter.Field.ETA, true), new IssueSorter.Criteria(IssueSorter.Field.UPDATED, false) )); viewModel.getProjectDetails().updateDetails(issues); if (version.getId() > 0) viewModel.getProjectDetails().updateVersionInfo(version); return forwardView(req, viewModel, "project-details"); } @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, pathParameters, 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 issues = issueDao.list(projectInfo.getProject()); for (var issue : issues) issueDao.joinVersionInformation(issue); viewModel.update(projectInfo.getVersions(), issues); return forwardView(req, viewModel, "versions"); } @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, pathParameters, dao); if (viewModel.getProjectInfo() == null || viewModel.getVersionFilter() == null) { resp.sendError(HttpServletResponse.SC_NOT_FOUND); return ResponseType.NONE; } viewModel.setVersion(viewModel.getVersionFilter()); return forwardView(req, viewModel, "version-form"); } @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); 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 = dao.getProjectDao().find(getParameter(req, Integer.class, "pid").orElseThrow()); if (project == null) { // TODO: improve error handling, because not found is not correct for this POST request resp.sendError(HttpServletResponse.SC_NOT_FOUND); return ResponseType.NONE; } final var version = new Version(getParameter(req, Integer.class, "id").orElseThrow()); version.setName(getParameter(req, String.class, "name").orElseThrow()); final var node = getParameter(req, String.class, "node").orElse(null); version.setNode(sanitizeNode(node, version.getName())); 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/" + project.getNode() + "/versions/"); setContentPage(req, Constants.JSP_COMMIT_SUCCESSFUL); } catch (NoSuchElementException | IllegalArgumentException | SQLException ex) { resp.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED); // TODO: implement - fix issue #21 return ResponseType.NONE; } return ResponseType.HTML; } @RequestMapping(requestPath = "$project/components/", method = HttpMethod.GET) public ResponseType components(HttpServletRequest req, HttpServletResponse resp, PathParameters pathParameters, DataAccessObjects dao) throws IOException, SQLException { final var viewModel = new ComponentsView(); populate(viewModel, pathParameters, 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 issues = issueDao.list(projectInfo.getProject()); viewModel.update(projectInfo.getComponents(), issues); return forwardView(req, viewModel, "components"); } @RequestMapping(requestPath = "$project/components/$component/edit", method = HttpMethod.GET) public ResponseType editComponent(HttpServletRequest req, HttpServletResponse resp, PathParameters pathParameters, DataAccessObjects dao) throws IOException, SQLException { final var viewModel = new ComponentEditView(); populate(viewModel, pathParameters, dao); if (viewModel.getProjectInfo() == null || viewModel.getComponentFilter() == null) { resp.sendError(HttpServletResponse.SC_NOT_FOUND); return ResponseType.NONE; } viewModel.setComponent(viewModel.getComponentFilter()); viewModel.setUsers(dao.getUserDao().list()); return forwardView(req, viewModel, "component-form"); } @RequestMapping(requestPath = "$project/create-component", method = HttpMethod.GET) public ResponseType createComponent(HttpServletRequest req, HttpServletResponse resp, PathParameters pathParameters, DataAccessObjects dao) throws IOException, SQLException { final var viewModel = new ComponentEditView(); populate(viewModel, pathParameters, dao); if (viewModel.getProjectInfo() == null) { resp.sendError(HttpServletResponse.SC_NOT_FOUND); return ResponseType.NONE; } viewModel.setComponent(new Component(-1)); viewModel.setUsers(dao.getUserDao().list()); return forwardView(req, viewModel, "component-form"); } @RequestMapping(requestPath = "commit-component", method = HttpMethod.POST) public ResponseType commitComponent(HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) throws IOException { try { final var project = dao.getProjectDao().find(getParameter(req, Integer.class, "pid").orElseThrow()); if (project == null) { // TODO: improve error handling, because not found is not correct for this POST request resp.sendError(HttpServletResponse.SC_NOT_FOUND); return ResponseType.NONE; } final var component = new Component(getParameter(req, Integer.class, "id").orElseThrow()); component.setName(getParameter(req, String.class, "name").orElseThrow()); final var node = getParameter(req, String.class, "node").orElse(null); component.setNode(sanitizeNode(node, component.getName())); component.setColor(getParameter(req, WebColor.class, "color").orElseThrow()); getParameter(req, Integer.class, "ordinal").ifPresent(component::setOrdinal); getParameter(req, Integer.class, "lead").map( userid -> userid >= 0 ? new User(userid) : null ).ifPresent(component::setLead); getParameter(req, String.class, "description").ifPresent(component::setDescription); dao.getComponentDao().saveOrUpdate(component, project); setRedirectLocation(req, "./projects/" + project.getNode() + "/components/"); setContentPage(req, Constants.JSP_COMMIT_SUCCESSFUL); } catch (NoSuchElementException | IllegalArgumentException | SQLException ex) { resp.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED); // TODO: implement - fix issue #21 return ResponseType.NONE; } return ResponseType.HTML; } private void configureIssueEditor(IssueEditView viewModel, Issue issue, DataAccessObjects dao) throws SQLException { final var project = viewModel.getProjectInfo().getProject(); issue.setProject(project); // automatically set current project for new issues viewModel.setIssue(issue); viewModel.configureVersionSelectors(viewModel.getProjectInfo().getVersions()); viewModel.setUsers(dao.getUserDao().list()); viewModel.setComponents(dao.getComponentDao().list(project)); } @RequestMapping(requestPath = "$project/issues/$issue/view", method = HttpMethod.GET) public ResponseType viewIssue(HttpServletRequest req, HttpServletResponse resp, PathParameters pathParameters, DataAccessObjects dao) throws IOException, SQLException { final var viewModel = new IssueDetailView(); populate(viewModel, pathParameters, 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); viewModel.setIssue(issue); viewModel.setComments(issueDao.listComments(issue)); return forwardView(req, viewModel, "issue-view"); } // TODO: why should the issue editor be child of $project? @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, pathParameters, 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); configureIssueEditor(viewModel, issue, dao); return forwardView(req, viewModel, "issue-form"); } @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()); configureIssueEditor(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 { final var issue = new Issue(getParameter(req, Integer.class, "id").orElseThrow()); final var componentId = getParameter(req, Integer.class, "component"); final Component component; if (componentId.isPresent()) { component = dao.getComponentDao().find(componentId.get()); } else { component = null; } final var project = dao.getProjectDao().find(getParameter(req, Integer.class, "pid").orElseThrow()); if (project == null) { // TODO: improve error handling, because not found is not correct for this POST request resp.sendError(HttpServletResponse.SC_NOT_FOUND); return ResponseType.NONE; } issue.setProject(project); getParameter(req, String.class, "category").map(IssueCategory::valueOf).ifPresent(issue::setCategory); getParameter(req, String.class, "status").map(IssueStatus::valueOf).ifPresent(issue::setStatus); issue.setSubject(getParameter(req, String.class, "subject").orElseThrow()); issue.setComponent(component); getParameter(req, Integer.class, "assignee").map(userid -> { if (userid >= 0) { return new User(userid); } else if (userid == -2) { return Optional.ofNullable(component).map(Component::getLead).orElse(null); } else { return null; } } ).ifPresent(issue::setAssignee); getParameter(req, String.class, "description").ifPresent(issue::setDescription); getParameter(req, Date.class, "eta").ifPresent(issue::setEta); getParameter(req, Integer[].class, "affected") .map(Stream::of) .map(stream -> stream.map(Version::new).collect(Collectors.toList()) ).ifPresent(issue::setAffectedVersions); getParameter(req, Integer[].class, "resolved") .map(Stream::of) .map(stream -> stream.map(Version::new).collect(Collectors.toList()) ).ifPresent(issue::setResolvedVersions); dao.getIssueDao().saveOrUpdate(issue, issue.getProject()); // TODO: fix redirect location setRedirectLocation(req, "./projects/" + issue.getProject().getNode()+"/issues/"+issue.getId()+"/view"); setContentPage(req, Constants.JSP_COMMIT_SUCCESSFUL); return ResponseType.HTML; } catch (NoSuchElementException | IllegalArgumentException | SQLException ex) { resp.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED); // TODO: implement - fix issue #21 return ResponseType.NONE; } } @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()) { resp.sendError(HttpServletResponse.SC_FORBIDDEN, "Detected manipulated form."); return ResponseType.NONE; } final var issue = dao.getIssueDao().find(issueIdParam.get()); if (issue == null) { resp.sendError(HttpServletResponse.SC_NOT_FOUND); return ResponseType.NONE; } try { final var issueComment = new IssueComment(getParameter(req, Integer.class, "commentid").orElse(-1), issue); issueComment.setComment(getParameter(req, String.class, "comment").orElse("")); if (issueComment.getComment().isBlank()) { throw new IllegalArgumentException("comment.null"); } LOG.debug("User {} is commenting on issue #{}", req.getRemoteUser(), issue.getId()); if (req.getRemoteUser() != null) { dao.getUserDao().findByUsername(req.getRemoteUser()).ifPresent(issueComment::setAuthor); } dao.getIssueDao().saveComment(issueComment); // TODO: fix redirect location setRedirectLocation(req, "./projects/" + issue.getProject().getNode()+"/issues/"+issue.getId()+"/view"); setContentPage(req, Constants.JSP_COMMIT_SUCCESSFUL); return ResponseType.HTML; } catch (NoSuchElementException | IllegalArgumentException | SQLException ex) { resp.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED); // TODO: implement - fix issue #21 return ResponseType.NONE; } } }