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

Thu, 05 Nov 2020 13:37:48 +0100

author
Mike Becker <universe@uap-core.de>
date
Thu, 05 Nov 2020 13:37:48 +0100
changeset 157
1e6f16fad3a5
parent 152
7761c37c5e61
child 158
4f912cd42876
permissions
-rw-r--r--

removes ResponseType enum

universe@41 1 /*
universe@41 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
universe@41 3 *
universe@41 4 * Copyright 2018 Mike Becker. All rights reserved.
universe@41 5 *
universe@41 6 * Redistribution and use in source and binary forms, with or without
universe@41 7 * modification, are permitted provided that the following conditions are met:
universe@41 8 *
universe@41 9 * 1. Redistributions of source code must retain the above copyright
universe@41 10 * notice, this list of conditions and the following disclaimer.
universe@41 11 *
universe@41 12 * 2. Redistributions in binary form must reproduce the above copyright
universe@41 13 * notice, this list of conditions and the following disclaimer in the
universe@41 14 * documentation and/or other materials provided with the distribution.
universe@41 15 *
universe@41 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
universe@41 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
universe@41 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
universe@41 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
universe@41 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
universe@41 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
universe@41 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
universe@41 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
universe@41 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
universe@41 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
universe@41 26 * POSSIBILITY OF SUCH DAMAGE.
universe@41 27 *
universe@41 28 */
universe@41 29 package de.uapcore.lightpit.modules;
universe@41 30
universe@41 31
universe@41 32 import de.uapcore.lightpit.*;
universe@41 33 import de.uapcore.lightpit.dao.DataAccessObjects;
universe@64 34 import de.uapcore.lightpit.entities.*;
universe@134 35 import de.uapcore.lightpit.types.WebColor;
universe@86 36 import de.uapcore.lightpit.viewmodel.*;
universe@121 37 import de.uapcore.lightpit.viewmodel.util.IssueSorter;
universe@59 38 import org.slf4j.Logger;
universe@59 39 import org.slf4j.LoggerFactory;
universe@41 40
universe@157 41 import javax.servlet.ServletException;
universe@41 42 import javax.servlet.annotation.WebServlet;
universe@41 43 import javax.servlet.http.HttpServletRequest;
universe@59 44 import javax.servlet.http.HttpServletResponse;
universe@59 45 import java.io.IOException;
universe@75 46 import java.sql.Date;
universe@47 47 import java.sql.SQLException;
universe@134 48 import java.util.List;
universe@86 49 import java.util.NoSuchElementException;
universe@136 50 import java.util.Optional;
universe@83 51 import java.util.stream.Collectors;
universe@83 52 import java.util.stream.Stream;
universe@41 53
universe@41 54 @WebServlet(
universe@41 55 name = "ProjectsModule",
universe@41 56 urlPatterns = "/projects/*"
universe@41 57 )
universe@41 58 public final class ProjectsModule extends AbstractLightPITServlet {
universe@41 59
universe@59 60 private static final Logger LOG = LoggerFactory.getLogger(ProjectsModule.class);
universe@59 61
universe@78 62 @Override
universe@78 63 protected String getResourceBundleName() {
universe@78 64 return "localization.projects";
universe@78 65 }
universe@71 66
universe@131 67 private void populate(ProjectView viewModel, PathParameters pathParameters, DataAccessObjects dao) throws SQLException {
universe@99 68 final var projectDao = dao.getProjectDao();
universe@99 69 final var versionDao = dao.getVersionDao();
universe@129 70 final var componentDao = dao.getComponentDao();
universe@99 71
universe@99 72 projectDao.list().stream().map(ProjectInfo::new).forEach(viewModel.getProjectList()::add);
universe@99 73
universe@131 74 if (pathParameters == null)
universe@131 75 return;
universe@131 76
universe@99 77 // Select Project
universe@138 78 final var project = projectDao.findByNode(pathParameters.get("project"));
universe@138 79 if (project == null)
universe@138 80 return;
universe@138 81
universe@138 82 final var info = new ProjectInfo(project);
universe@138 83 info.setVersions(versionDao.list(project));
universe@138 84 info.setComponents(componentDao.list(project));
universe@138 85 info.setIssueSummary(projectDao.getIssueSummary(project));
universe@138 86 viewModel.setProjectInfo(info);
universe@99 87
universe@99 88 // Select Version
universe@138 89 final var versionNode = pathParameters.get("version");
universe@138 90 if ("no-version".equals(versionNode)) {
universe@134 91 viewModel.setVersionFilter(ProjectView.NO_VERSION);
universe@138 92 } else if ("all-versions".equals(versionNode)) {
universe@134 93 viewModel.setVersionFilter(ProjectView.ALL_VERSIONS);
universe@134 94 } else {
universe@138 95 viewModel.setVersionFilter(versionDao.findByNode(project, versionNode));
universe@131 96 }
universe@131 97
universe@131 98 // Select Component
universe@138 99 final var componentNode = pathParameters.get("component");
universe@138 100 if ("no-component".equals(componentNode)) {
universe@134 101 viewModel.setComponentFilter(ProjectView.NO_COMPONENT);
universe@138 102 } else if ("all-components".equals(componentNode)) {
universe@134 103 viewModel.setComponentFilter(ProjectView.ALL_COMPONENTS);
universe@134 104 } else {
universe@138 105 viewModel.setComponentFilter(componentDao.findByNode(project, componentNode));
universe@138 106 }
universe@138 107 }
universe@138 108
universe@138 109 private static String sanitizeNode(String node, String defaultValue) {
universe@138 110 String result = node == null || node.isBlank() ? defaultValue : node;
universe@138 111 result = result.replace('/', '-');
universe@138 112 if (result.equals(".") || result.equals("..")) {
universe@138 113 return "_"+result;
universe@138 114 } else {
universe@138 115 return result;
universe@129 116 }
universe@99 117 }
universe@99 118
universe@157 119 private void forwardView(HttpServletRequest req, HttpServletResponse resp, ProjectView viewModel, String name) throws ServletException, IOException {
universe@99 120 setViewModel(req, viewModel);
universe@99 121 setContentPage(req, name);
universe@99 122 setStylesheet(req, "projects");
universe@109 123 setNavigationMenu(req, "project-navmenu");
universe@157 124 renderSite(req, resp);
universe@64 125 }
universe@64 126
universe@61 127 @RequestMapping(method = HttpMethod.GET)
universe@157 128 public void index(HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) throws SQLException, ServletException, IOException {
universe@99 129 final var viewModel = new ProjectView();
universe@131 130 populate(viewModel, null, dao);
universe@86 131
universe@86 132 final var projectDao = dao.getProjectDao();
universe@86 133 final var versionDao = dao.getVersionDao();
universe@86 134
universe@99 135 for (var info : viewModel.getProjectList()) {
universe@99 136 info.setVersions(versionDao.list(info.getProject()));
universe@99 137 info.setIssueSummary(projectDao.getIssueSummary(info.getProject()));
universe@86 138 }
universe@86 139
universe@157 140 forwardView(req, resp, viewModel, "projects");
universe@45 141 }
universe@45 142
universe@131 143 private void configureProjectEditor(ProjectEditView viewModel, Project project, DataAccessObjects dao) throws SQLException {
universe@99 144 viewModel.setProject(project);
universe@86 145 viewModel.setUsers(dao.getUserDao().list());
universe@71 146 }
universe@71 147
universe@131 148 @RequestMapping(requestPath = "$project/edit", method = HttpMethod.GET)
universe@157 149 public void edit(HttpServletRequest req, HttpServletResponse resp, PathParameters pathParams, DataAccessObjects dao) throws IOException, SQLException, ServletException {
universe@99 150 final var viewModel = new ProjectEditView();
universe@131 151 populate(viewModel, pathParams, dao);
universe@47 152
universe@134 153 if (!viewModel.isProjectInfoPresent()) {
universe@131 154 resp.sendError(HttpServletResponse.SC_NOT_FOUND);
universe@157 155 return;
universe@131 156 }
universe@47 157
universe@131 158 configureProjectEditor(viewModel, viewModel.getProjectInfo().getProject(), dao);
universe@157 159 forwardView(req, resp, viewModel, "project-form");
universe@131 160 }
universe@131 161
universe@131 162 @RequestMapping(requestPath = "create", method = HttpMethod.GET)
universe@157 163 public void create(HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) throws SQLException, ServletException, IOException {
universe@131 164 final var viewModel = new ProjectEditView();
universe@131 165 populate(viewModel, null, dao);
universe@131 166 configureProjectEditor(viewModel, new Project(-1), dao);
universe@157 167 forwardView(req, resp, viewModel, "project-form");
universe@47 168 }
universe@47 169
universe@47 170 @RequestMapping(requestPath = "commit", method = HttpMethod.POST)
universe@157 171 public void commit(HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) throws IOException, ServletException {
universe@47 172
universe@47 173 try {
universe@131 174 final var project = new Project(getParameter(req, Integer.class, "pid").orElseThrow());
universe@47 175 project.setName(getParameter(req, String.class, "name").orElseThrow());
universe@138 176
universe@138 177 final var node = getParameter(req, String.class, "node").orElse(null);
universe@138 178 project.setNode(sanitizeNode(node, project.getName()));
universe@138 179
universe@47 180 getParameter(req, String.class, "description").ifPresent(project::setDescription);
universe@47 181 getParameter(req, String.class, "repoUrl").ifPresent(project::setRepoUrl);
universe@47 182 getParameter(req, Integer.class, "owner").map(
universe@47 183 ownerId -> ownerId >= 0 ? new User(ownerId) : null
universe@47 184 ).ifPresent(project::setOwner);
universe@47 185
universe@47 186 dao.getProjectDao().saveOrUpdate(project);
universe@47 187
universe@118 188 setRedirectLocation(req, "./projects/");
universe@74 189 setContentPage(req, Constants.JSP_COMMIT_SUCCESSFUL);
universe@59 190 LOG.debug("Successfully updated project {}", project.getName());
universe@99 191
universe@157 192 renderSite(req, resp);
universe@75 193 } catch (NoSuchElementException | IllegalArgumentException | SQLException ex) {
universe@131 194 resp.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED);
universe@131 195 // TODO: implement - fix issue #21
universe@47 196 }
universe@47 197 }
universe@47 198
universe@134 199 @RequestMapping(requestPath = "$project/$component/$version/issues/", method = HttpMethod.GET)
universe@157 200 public void issues(HttpServletRequest req, HttpServletResponse resp, PathParameters pathParams, DataAccessObjects dao) throws SQLException, IOException, ServletException {
universe@99 201 final var viewModel = new ProjectDetailsView();
universe@131 202 populate(viewModel, pathParams, dao);
universe@86 203
universe@134 204 if (!viewModel.isEveryFilterValid()) {
universe@131 205 resp.sendError(HttpServletResponse.SC_NOT_FOUND);
universe@157 206 return;
universe@80 207 }
universe@47 208
universe@134 209 final var project = viewModel.getProjectInfo().getProject();
universe@134 210 final var version = viewModel.getVersionFilter();
universe@134 211 final var component = viewModel.getComponentFilter();
universe@134 212
universe@86 213 final var issueDao = dao.getIssueDao();
universe@70 214
universe@134 215 final List<Issue> issues;
universe@134 216 if (version.equals(ProjectView.NO_VERSION)) {
universe@134 217 if (component.equals(ProjectView.ALL_COMPONENTS)) {
universe@134 218 issues = issueDao.list(project, (Version) null);
universe@134 219 } else if (component.equals(ProjectView.NO_COMPONENT)) {
universe@134 220 issues = issueDao.list(project, null, null);
universe@134 221 } else {
universe@134 222 issues = issueDao.list(project, component, null);
universe@134 223 }
universe@134 224 } else if (version.equals(ProjectView.ALL_VERSIONS)) {
universe@134 225 if (component.equals(ProjectView.ALL_COMPONENTS)) {
universe@134 226 issues = issueDao.list(project);
universe@134 227 } else if (component.equals(ProjectView.NO_COMPONENT)) {
universe@134 228 issues = issueDao.list(project, (Component)null);
universe@134 229 } else {
universe@134 230 issues = issueDao.list(project, component);
universe@134 231 }
universe@134 232 } else {
universe@134 233 if (component.equals(ProjectView.ALL_COMPONENTS)) {
universe@134 234 issues = issueDao.list(project, version);
universe@134 235 } else if (component.equals(ProjectView.NO_COMPONENT)) {
universe@134 236 issues = issueDao.list(project, null, version);
universe@134 237 } else {
universe@134 238 issues = issueDao.list(project, component, version);
universe@134 239 }
universe@134 240 }
universe@134 241
universe@100 242 for (var issue : issues) issueDao.joinVersionInformation(issue);
universe@121 243 issues.sort(new IssueSorter(
universe@141 244 new IssueSorter.Criteria(IssueSorter.Field.DONE, true),
universe@121 245 new IssueSorter.Criteria(IssueSorter.Field.ETA, true),
universe@121 246 new IssueSorter.Criteria(IssueSorter.Field.UPDATED, false)
universe@121 247 ));
universe@134 248
universe@134 249
universe@134 250 viewModel.getProjectDetails().updateDetails(issues);
universe@134 251 if (version.getId() > 0)
universe@134 252 viewModel.getProjectDetails().updateVersionInfo(version);
universe@80 253
universe@157 254 forwardView(req, resp, viewModel, "project-details");
universe@71 255 }
universe@71 256
universe@131 257 @RequestMapping(requestPath = "$project/versions/", method = HttpMethod.GET)
universe@157 258 public void versions(HttpServletRequest req, HttpServletResponse resp, PathParameters pathParameters, DataAccessObjects dao) throws IOException, SQLException, ServletException {
universe@109 259 final var viewModel = new VersionsView();
universe@131 260 populate(viewModel, pathParameters, dao);
universe@109 261
universe@109 262 final var projectInfo = viewModel.getProjectInfo();
universe@109 263 if (projectInfo == null) {
universe@131 264 resp.sendError(HttpServletResponse.SC_NOT_FOUND);
universe@157 265 return;
universe@109 266 }
universe@109 267
universe@109 268 final var issueDao = dao.getIssueDao();
universe@109 269 final var issues = issueDao.list(projectInfo.getProject());
universe@109 270 for (var issue : issues) issueDao.joinVersionInformation(issue);
universe@109 271 viewModel.update(projectInfo.getVersions(), issues);
universe@109 272
universe@157 273 forwardView(req, resp, viewModel, "versions");
universe@109 274 }
universe@109 275
universe@131 276 @RequestMapping(requestPath = "$project/versions/$version/edit", method = HttpMethod.GET)
universe@157 277 public void editVersion(HttpServletRequest req, HttpServletResponse resp, PathParameters pathParameters, DataAccessObjects dao) throws IOException, SQLException, ServletException {
universe@99 278 final var viewModel = new VersionEditView();
universe@131 279 populate(viewModel, pathParameters, dao);
universe@99 280
universe@131 281 if (viewModel.getProjectInfo() == null || viewModel.getVersionFilter() == null) {
universe@131 282 resp.sendError(HttpServletResponse.SC_NOT_FOUND);
universe@157 283 return;
universe@110 284 }
universe@110 285
universe@131 286 viewModel.setVersion(viewModel.getVersionFilter());
universe@59 287
universe@157 288 forwardView(req, resp, viewModel, "version-form");
universe@59 289 }
universe@59 290
universe@131 291 @RequestMapping(requestPath = "$project/create-version", method = HttpMethod.GET)
universe@157 292 public void createVersion(HttpServletRequest req, HttpServletResponse resp, PathParameters pathParameters, DataAccessObjects dao) throws IOException, SQLException, ServletException {
universe@131 293 final var viewModel = new VersionEditView();
universe@131 294 populate(viewModel, pathParameters, dao);
universe@59 295
universe@131 296 if (viewModel.getProjectInfo() == null) {
universe@131 297 resp.sendError(HttpServletResponse.SC_NOT_FOUND);
universe@157 298 return;
universe@131 299 }
universe@131 300
universe@152 301 viewModel.setVersion(new Version(-1));
universe@131 302
universe@157 303 forwardView(req, resp, viewModel, "version-form");
universe@131 304 }
universe@131 305
universe@131 306 @RequestMapping(requestPath = "commit-version", method = HttpMethod.POST)
universe@157 307 public void commitVersion(HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) throws IOException, ServletException {
universe@131 308
universe@59 309 try {
universe@138 310 final var project = dao.getProjectDao().find(getParameter(req, Integer.class, "pid").orElseThrow());
universe@138 311 if (project == null) {
universe@138 312 // TODO: improve error handling, because not found is not correct for this POST request
universe@138 313 resp.sendError(HttpServletResponse.SC_NOT_FOUND);
universe@157 314 return;
universe@138 315 }
universe@131 316 final var version = new Version(getParameter(req, Integer.class, "id").orElseThrow());
universe@59 317 version.setName(getParameter(req, String.class, "name").orElseThrow());
universe@138 318
universe@138 319 final var node = getParameter(req, String.class, "node").orElse(null);
universe@138 320 version.setNode(sanitizeNode(node, version.getName()));
universe@138 321
universe@59 322 getParameter(req, Integer.class, "ordinal").ifPresent(version::setOrdinal);
universe@59 323 version.setStatus(VersionStatus.valueOf(getParameter(req, String.class, "status").orElseThrow()));
universe@128 324 dao.getVersionDao().saveOrUpdate(version, project);
universe@59 325
universe@138 326 setRedirectLocation(req, "./projects/" + project.getNode() + "/versions/");
universe@74 327 setContentPage(req, Constants.JSP_COMMIT_SUCCESSFUL);
universe@157 328
universe@157 329 renderSite(req, resp);
universe@75 330 } catch (NoSuchElementException | IllegalArgumentException | SQLException ex) {
universe@131 331 resp.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED);
universe@131 332 // TODO: implement - fix issue #21
universe@59 333 }
universe@41 334 }
universe@64 335
universe@134 336 @RequestMapping(requestPath = "$project/components/", method = HttpMethod.GET)
universe@157 337 public void components(HttpServletRequest req, HttpServletResponse resp, PathParameters pathParameters, DataAccessObjects dao) throws IOException, SQLException, ServletException {
universe@134 338 final var viewModel = new ComponentsView();
universe@134 339 populate(viewModel, pathParameters, dao);
universe@134 340
universe@134 341 final var projectInfo = viewModel.getProjectInfo();
universe@134 342 if (projectInfo == null) {
universe@134 343 resp.sendError(HttpServletResponse.SC_NOT_FOUND);
universe@157 344 return;
universe@134 345 }
universe@134 346
universe@134 347 final var issueDao = dao.getIssueDao();
universe@134 348 final var issues = issueDao.list(projectInfo.getProject());
universe@134 349 viewModel.update(projectInfo.getComponents(), issues);
universe@134 350
universe@157 351 forwardView(req, resp, viewModel, "components");
universe@134 352 }
universe@134 353
universe@134 354 @RequestMapping(requestPath = "$project/components/$component/edit", method = HttpMethod.GET)
universe@157 355 public void editComponent(HttpServletRequest req, HttpServletResponse resp, PathParameters pathParameters, DataAccessObjects dao) throws IOException, SQLException, ServletException {
universe@134 356 final var viewModel = new ComponentEditView();
universe@134 357 populate(viewModel, pathParameters, dao);
universe@134 358
universe@134 359 if (viewModel.getProjectInfo() == null || viewModel.getComponentFilter() == null) {
universe@134 360 resp.sendError(HttpServletResponse.SC_NOT_FOUND);
universe@157 361 return;
universe@134 362 }
universe@134 363
universe@134 364 viewModel.setComponent(viewModel.getComponentFilter());
universe@134 365 viewModel.setUsers(dao.getUserDao().list());
universe@134 366
universe@157 367 forwardView(req, resp, viewModel, "component-form");
universe@134 368 }
universe@134 369
universe@134 370 @RequestMapping(requestPath = "$project/create-component", method = HttpMethod.GET)
universe@157 371 public void createComponent(HttpServletRequest req, HttpServletResponse resp, PathParameters pathParameters, DataAccessObjects dao) throws IOException, SQLException, ServletException {
universe@134 372 final var viewModel = new ComponentEditView();
universe@134 373 populate(viewModel, pathParameters, dao);
universe@134 374
universe@134 375 if (viewModel.getProjectInfo() == null) {
universe@134 376 resp.sendError(HttpServletResponse.SC_NOT_FOUND);
universe@157 377 return;
universe@134 378 }
universe@134 379
universe@134 380 viewModel.setComponent(new Component(-1));
universe@134 381 viewModel.setUsers(dao.getUserDao().list());
universe@134 382
universe@157 383 forwardView(req, resp, viewModel, "component-form");
universe@134 384 }
universe@134 385
universe@134 386 @RequestMapping(requestPath = "commit-component", method = HttpMethod.POST)
universe@157 387 public void commitComponent(HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) throws IOException, ServletException {
universe@134 388
universe@134 389 try {
universe@138 390 final var project = dao.getProjectDao().find(getParameter(req, Integer.class, "pid").orElseThrow());
universe@138 391 if (project == null) {
universe@138 392 // TODO: improve error handling, because not found is not correct for this POST request
universe@138 393 resp.sendError(HttpServletResponse.SC_NOT_FOUND);
universe@157 394 return;
universe@138 395 }
universe@134 396 final var component = new Component(getParameter(req, Integer.class, "id").orElseThrow());
universe@134 397 component.setName(getParameter(req, String.class, "name").orElseThrow());
universe@138 398
universe@138 399 final var node = getParameter(req, String.class, "node").orElse(null);
universe@138 400 component.setNode(sanitizeNode(node, component.getName()));
universe@138 401
universe@134 402 component.setColor(getParameter(req, WebColor.class, "color").orElseThrow());
universe@134 403 getParameter(req, Integer.class, "ordinal").ifPresent(component::setOrdinal);
universe@134 404 getParameter(req, Integer.class, "lead").map(
universe@134 405 userid -> userid >= 0 ? new User(userid) : null
universe@134 406 ).ifPresent(component::setLead);
universe@134 407 getParameter(req, String.class, "description").ifPresent(component::setDescription);
universe@134 408
universe@134 409 dao.getComponentDao().saveOrUpdate(component, project);
universe@134 410
universe@138 411 setRedirectLocation(req, "./projects/" + project.getNode() + "/components/");
universe@134 412 setContentPage(req, Constants.JSP_COMMIT_SUCCESSFUL);
universe@157 413
universe@157 414 renderSite(req, resp);
universe@134 415 } catch (NoSuchElementException | IllegalArgumentException | SQLException ex) {
universe@134 416 resp.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED);
universe@134 417 // TODO: implement - fix issue #21
universe@134 418 }
universe@134 419 }
universe@134 420
universe@133 421 private void configureIssueEditor(IssueEditView viewModel, Issue issue, DataAccessObjects dao) throws SQLException {
universe@134 422 final var project = viewModel.getProjectInfo().getProject();
universe@146 423 issue.setProject(project); // automatically set current project for new issues
universe@99 424 viewModel.setIssue(issue);
universe@99 425 viewModel.configureVersionSelectors(viewModel.getProjectInfo().getVersions());
universe@86 426 viewModel.setUsers(dao.getUserDao().list());
universe@134 427 viewModel.setComponents(dao.getComponentDao().list(project));
universe@71 428 }
universe@71 429
universe@146 430 @RequestMapping(requestPath = "$project/issues/$issue/view", method = HttpMethod.GET)
universe@157 431 public void viewIssue(HttpServletRequest req, HttpServletResponse resp, PathParameters pathParameters, DataAccessObjects dao) throws IOException, SQLException, ServletException {
universe@146 432 final var viewModel = new IssueDetailView();
universe@146 433 populate(viewModel, pathParameters, dao);
universe@146 434
universe@146 435 final var projectInfo = viewModel.getProjectInfo();
universe@146 436 if (projectInfo == null) {
universe@146 437 resp.sendError(HttpServletResponse.SC_NOT_FOUND);
universe@157 438 return;
universe@146 439 }
universe@146 440
universe@146 441 final var issueDao = dao.getIssueDao();
universe@146 442 final var issue = issueDao.find(Functions.parseIntOrZero(pathParameters.get("issue")));
universe@146 443 if (issue == null) {
universe@146 444 resp.sendError(HttpServletResponse.SC_NOT_FOUND);
universe@157 445 return;
universe@146 446 }
universe@146 447
universe@146 448 issueDao.joinVersionInformation(issue);
universe@146 449 viewModel.setIssue(issue);
universe@146 450 viewModel.setComments(issueDao.listComments(issue));
universe@146 451
universe@157 452 forwardView(req, resp, viewModel, "issue-view");
universe@146 453 }
universe@146 454
universe@146 455 // TODO: why should the issue editor be child of $project?
universe@131 456 @RequestMapping(requestPath = "$project/issues/$issue/edit", method = HttpMethod.GET)
universe@157 457 public void editIssue(HttpServletRequest req, HttpServletResponse resp, PathParameters pathParameters, DataAccessObjects dao) throws IOException, SQLException, ServletException {
universe@99 458 final var viewModel = new IssueEditView();
universe@131 459 populate(viewModel, pathParameters, dao);
universe@99 460
universe@131 461 final var projectInfo = viewModel.getProjectInfo();
universe@131 462 if (projectInfo == null) {
universe@131 463 resp.sendError(HttpServletResponse.SC_NOT_FOUND);
universe@157 464 return;
universe@86 465 }
universe@64 466
universe@131 467 final var issueDao = dao.getIssueDao();
universe@131 468 final var issue = issueDao.find(Functions.parseIntOrZero(pathParameters.get("issue")));
universe@131 469 if (issue == null) {
universe@131 470 resp.sendError(HttpServletResponse.SC_NOT_FOUND);
universe@157 471 return;
universe@131 472 }
universe@131 473
universe@131 474 issueDao.joinVersionInformation(issue);
universe@133 475 configureIssueEditor(viewModel, issue, dao);
universe@131 476
universe@157 477 forwardView(req, resp, viewModel, "issue-form");
universe@64 478 }
universe@64 479
universe@131 480 @RequestMapping(requestPath = "$project/create-issue", method = HttpMethod.GET)
universe@157 481 public void createIssue(HttpServletRequest req, HttpServletResponse resp, PathParameters pathParameters, DataAccessObjects dao) throws IOException, SQLException, ServletException {
universe@131 482 final var viewModel = new IssueEditView();
universe@131 483 populate(viewModel, pathParameters, dao);
universe@131 484
universe@131 485 final var projectInfo = viewModel.getProjectInfo();
universe@131 486 if (projectInfo == null) {
universe@131 487 resp.sendError(HttpServletResponse.SC_NOT_FOUND);
universe@157 488 return;
universe@131 489 }
universe@131 490
universe@131 491 final var issue = new Issue(-1);
universe@131 492 issue.setProject(projectInfo.getProject());
universe@133 493 configureIssueEditor(viewModel, issue, dao);
universe@131 494
universe@157 495 forwardView(req, resp, viewModel, "issue-form");
universe@131 496 }
universe@131 497
universe@131 498 @RequestMapping(requestPath = "commit-issue", method = HttpMethod.POST)
universe@157 499 public void commitIssue(HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) throws IOException, ServletException {
universe@64 500 try {
universe@131 501 final var issue = new Issue(getParameter(req, Integer.class, "id").orElseThrow());
universe@136 502 final var componentId = getParameter(req, Integer.class, "component");
universe@136 503 final Component component;
universe@136 504 if (componentId.isPresent()) {
universe@136 505 component = dao.getComponentDao().find(componentId.get());
universe@136 506 } else {
universe@136 507 component = null;
universe@136 508 }
universe@138 509 final var project = dao.getProjectDao().find(getParameter(req, Integer.class, "pid").orElseThrow());
universe@138 510 if (project == null) {
universe@138 511 // TODO: improve error handling, because not found is not correct for this POST request
universe@138 512 resp.sendError(HttpServletResponse.SC_NOT_FOUND);
universe@157 513 return;
universe@138 514 }
universe@138 515 issue.setProject(project);
universe@75 516 getParameter(req, String.class, "category").map(IssueCategory::valueOf).ifPresent(issue::setCategory);
universe@75 517 getParameter(req, String.class, "status").map(IssueStatus::valueOf).ifPresent(issue::setStatus);
universe@75 518 issue.setSubject(getParameter(req, String.class, "subject").orElseThrow());
universe@136 519 issue.setComponent(component);
universe@136 520 getParameter(req, Integer.class, "assignee").map(userid -> {
universe@136 521 if (userid >= 0) {
universe@136 522 return new User(userid);
universe@136 523 } else if (userid == -2) {
universe@136 524 return Optional.ofNullable(component).map(Component::getLead).orElse(null);
universe@136 525 } else {
universe@136 526 return null;
universe@136 527 }
universe@136 528 }
universe@75 529 ).ifPresent(issue::setAssignee);
universe@75 530 getParameter(req, String.class, "description").ifPresent(issue::setDescription);
universe@75 531 getParameter(req, Date.class, "eta").ifPresent(issue::setEta);
universe@83 532
universe@83 533 getParameter(req, Integer[].class, "affected")
universe@83 534 .map(Stream::of)
universe@83 535 .map(stream ->
universe@96 536 stream.map(Version::new).collect(Collectors.toList())
universe@83 537 ).ifPresent(issue::setAffectedVersions);
universe@83 538 getParameter(req, Integer[].class, "resolved")
universe@83 539 .map(Stream::of)
universe@83 540 .map(stream ->
universe@86 541 stream.map(Version::new).collect(Collectors.toList())
universe@83 542 ).ifPresent(issue::setResolvedVersions);
universe@83 543
universe@128 544 dao.getIssueDao().saveOrUpdate(issue, issue.getProject());
universe@64 545
universe@146 546 // TODO: fix redirect location
universe@146 547 setRedirectLocation(req, "./projects/" + issue.getProject().getNode()+"/issues/"+issue.getId()+"/view");
universe@74 548 setContentPage(req, Constants.JSP_COMMIT_SUCCESSFUL);
universe@124 549
universe@157 550 renderSite(req, resp);
universe@75 551 } catch (NoSuchElementException | IllegalArgumentException | SQLException ex) {
universe@131 552 resp.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED);
universe@131 553 // TODO: implement - fix issue #21
universe@64 554 }
universe@124 555 }
universe@64 556
universe@131 557 @RequestMapping(requestPath = "commit-issue-comment", method = HttpMethod.POST)
universe@157 558 public void commentIssue(HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) throws SQLException, IOException, ServletException {
universe@124 559 final var issueIdParam = getParameter(req, Integer.class, "issueid");
universe@124 560 if (issueIdParam.isEmpty()) {
universe@124 561 resp.sendError(HttpServletResponse.SC_FORBIDDEN, "Detected manipulated form.");
universe@157 562 return;
universe@124 563 }
universe@137 564 final var issue = dao.getIssueDao().find(issueIdParam.get());
universe@137 565 if (issue == null) {
universe@137 566 resp.sendError(HttpServletResponse.SC_NOT_FOUND);
universe@157 567 return;
universe@137 568 }
universe@124 569 try {
universe@150 570 final var issueComment = new IssueComment(getParameter(req, Integer.class, "commentid").orElse(-1));
universe@124 571 issueComment.setComment(getParameter(req, String.class, "comment").orElse(""));
universe@124 572
universe@124 573 if (issueComment.getComment().isBlank()) {
universe@124 574 throw new IllegalArgumentException("comment.null");
universe@124 575 }
universe@124 576
universe@124 577 LOG.debug("User {} is commenting on issue #{}", req.getRemoteUser(), issue.getId());
universe@124 578 if (req.getRemoteUser() != null) {
universe@124 579 dao.getUserDao().findByUsername(req.getRemoteUser()).ifPresent(issueComment::setAuthor);
universe@124 580 }
universe@124 581
universe@150 582 dao.getIssueDao().saveComment(issue, issueComment);
universe@124 583
universe@146 584 // TODO: fix redirect location
universe@146 585 setRedirectLocation(req, "./projects/" + issue.getProject().getNode()+"/issues/"+issue.getId()+"/view");
universe@124 586 setContentPage(req, Constants.JSP_COMMIT_SUCCESSFUL);
universe@124 587
universe@157 588 renderSite(req, resp);
universe@124 589 } catch (NoSuchElementException | IllegalArgumentException | SQLException ex) {
universe@131 590 resp.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED);
universe@131 591 // TODO: implement - fix issue #21
universe@124 592 }
universe@64 593 }
universe@41 594 }

mercurial