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