Thu, 19 Nov 2020 13:58:54 +0100
migrates DAO classes
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.*; |
mike@159 | 33 | import de.uapcore.lightpit.dao.DaoProvider; |
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@158 | 67 | private static int parseIntOrZero(String str) { |
universe@158 | 68 | try { |
universe@158 | 69 | return Integer.parseInt(str); |
universe@158 | 70 | } catch (NumberFormatException ex) { |
universe@158 | 71 | return 0; |
universe@158 | 72 | } |
universe@158 | 73 | } |
universe@158 | 74 | |
mike@159 | 75 | private void populate(ProjectView viewModel, PathParameters pathParameters, DaoProvider dao) throws SQLException { |
universe@99 | 76 | final var projectDao = dao.getProjectDao(); |
universe@99 | 77 | final var versionDao = dao.getVersionDao(); |
universe@129 | 78 | final var componentDao = dao.getComponentDao(); |
universe@99 | 79 | |
universe@99 | 80 | projectDao.list().stream().map(ProjectInfo::new).forEach(viewModel.getProjectList()::add); |
universe@99 | 81 | |
universe@131 | 82 | if (pathParameters == null) |
universe@131 | 83 | return; |
universe@131 | 84 | |
universe@99 | 85 | // Select Project |
universe@138 | 86 | final var project = projectDao.findByNode(pathParameters.get("project")); |
universe@138 | 87 | if (project == null) |
universe@138 | 88 | return; |
universe@138 | 89 | |
universe@138 | 90 | final var info = new ProjectInfo(project); |
universe@138 | 91 | info.setVersions(versionDao.list(project)); |
universe@138 | 92 | info.setComponents(componentDao.list(project)); |
universe@138 | 93 | info.setIssueSummary(projectDao.getIssueSummary(project)); |
universe@138 | 94 | viewModel.setProjectInfo(info); |
universe@99 | 95 | |
universe@99 | 96 | // Select Version |
universe@138 | 97 | final var versionNode = pathParameters.get("version"); |
universe@138 | 98 | if ("no-version".equals(versionNode)) { |
universe@134 | 99 | viewModel.setVersionFilter(ProjectView.NO_VERSION); |
mike@159 | 100 | } else if ("all-versions".equals(versionNode) || versionNode == null) { |
universe@134 | 101 | viewModel.setVersionFilter(ProjectView.ALL_VERSIONS); |
universe@134 | 102 | } else { |
universe@138 | 103 | viewModel.setVersionFilter(versionDao.findByNode(project, versionNode)); |
universe@131 | 104 | } |
universe@131 | 105 | |
universe@131 | 106 | // Select Component |
universe@138 | 107 | final var componentNode = pathParameters.get("component"); |
universe@138 | 108 | if ("no-component".equals(componentNode)) { |
universe@134 | 109 | viewModel.setComponentFilter(ProjectView.NO_COMPONENT); |
mike@159 | 110 | } else if ("all-components".equals(componentNode) || componentNode == null) { |
universe@134 | 111 | viewModel.setComponentFilter(ProjectView.ALL_COMPONENTS); |
universe@134 | 112 | } else { |
universe@138 | 113 | viewModel.setComponentFilter(componentDao.findByNode(project, componentNode)); |
universe@138 | 114 | } |
universe@138 | 115 | } |
universe@138 | 116 | |
universe@138 | 117 | private static String sanitizeNode(String node, String defaultValue) { |
universe@138 | 118 | String result = node == null || node.isBlank() ? defaultValue : node; |
universe@138 | 119 | result = result.replace('/', '-'); |
universe@138 | 120 | if (result.equals(".") || result.equals("..")) { |
universe@138 | 121 | return "_"+result; |
universe@138 | 122 | } else { |
universe@138 | 123 | return result; |
universe@129 | 124 | } |
universe@99 | 125 | } |
universe@99 | 126 | |
universe@157 | 127 | private void forwardView(HttpServletRequest req, HttpServletResponse resp, ProjectView viewModel, String name) throws ServletException, IOException { |
universe@99 | 128 | setViewModel(req, viewModel); |
universe@99 | 129 | setContentPage(req, name); |
universe@99 | 130 | setStylesheet(req, "projects"); |
universe@109 | 131 | setNavigationMenu(req, "project-navmenu"); |
universe@157 | 132 | renderSite(req, resp); |
universe@64 | 133 | } |
universe@64 | 134 | |
universe@61 | 135 | @RequestMapping(method = HttpMethod.GET) |
mike@159 | 136 | public void index(HttpServletRequest req, HttpServletResponse resp, DaoProvider dao) throws SQLException, ServletException, IOException { |
universe@99 | 137 | final var viewModel = new ProjectView(); |
universe@131 | 138 | populate(viewModel, null, dao); |
universe@86 | 139 | |
universe@86 | 140 | final var projectDao = dao.getProjectDao(); |
universe@86 | 141 | final var versionDao = dao.getVersionDao(); |
universe@86 | 142 | |
universe@99 | 143 | for (var info : viewModel.getProjectList()) { |
universe@99 | 144 | info.setVersions(versionDao.list(info.getProject())); |
universe@99 | 145 | info.setIssueSummary(projectDao.getIssueSummary(info.getProject())); |
universe@86 | 146 | } |
universe@86 | 147 | |
universe@157 | 148 | forwardView(req, resp, viewModel, "projects"); |
universe@45 | 149 | } |
universe@45 | 150 | |
mike@159 | 151 | private void configureProjectEditor(ProjectEditView viewModel, Project project, DaoProvider dao) throws SQLException { |
universe@99 | 152 | viewModel.setProject(project); |
universe@86 | 153 | viewModel.setUsers(dao.getUserDao().list()); |
universe@71 | 154 | } |
universe@71 | 155 | |
universe@131 | 156 | @RequestMapping(requestPath = "$project/edit", method = HttpMethod.GET) |
mike@159 | 157 | public void edit(HttpServletRequest req, HttpServletResponse resp, PathParameters pathParams, DaoProvider dao) throws IOException, SQLException, ServletException { |
universe@99 | 158 | final var viewModel = new ProjectEditView(); |
universe@131 | 159 | populate(viewModel, pathParams, dao); |
universe@47 | 160 | |
universe@134 | 161 | if (!viewModel.isProjectInfoPresent()) { |
universe@131 | 162 | resp.sendError(HttpServletResponse.SC_NOT_FOUND); |
universe@157 | 163 | return; |
universe@131 | 164 | } |
universe@47 | 165 | |
universe@131 | 166 | configureProjectEditor(viewModel, viewModel.getProjectInfo().getProject(), dao); |
universe@157 | 167 | forwardView(req, resp, viewModel, "project-form"); |
universe@131 | 168 | } |
universe@131 | 169 | |
universe@131 | 170 | @RequestMapping(requestPath = "create", method = HttpMethod.GET) |
mike@159 | 171 | public void create(HttpServletRequest req, HttpServletResponse resp, DaoProvider dao) throws SQLException, ServletException, IOException { |
universe@131 | 172 | final var viewModel = new ProjectEditView(); |
universe@131 | 173 | populate(viewModel, null, dao); |
universe@131 | 174 | configureProjectEditor(viewModel, new Project(-1), dao); |
universe@157 | 175 | forwardView(req, resp, viewModel, "project-form"); |
universe@47 | 176 | } |
universe@47 | 177 | |
universe@47 | 178 | @RequestMapping(requestPath = "commit", method = HttpMethod.POST) |
mike@159 | 179 | public void commit(HttpServletRequest req, HttpServletResponse resp, DaoProvider dao) throws IOException, ServletException { |
universe@47 | 180 | |
universe@47 | 181 | try { |
universe@131 | 182 | final var project = new Project(getParameter(req, Integer.class, "pid").orElseThrow()); |
universe@47 | 183 | project.setName(getParameter(req, String.class, "name").orElseThrow()); |
universe@138 | 184 | |
universe@138 | 185 | final var node = getParameter(req, String.class, "node").orElse(null); |
universe@138 | 186 | project.setNode(sanitizeNode(node, project.getName())); |
universe@138 | 187 | |
universe@47 | 188 | getParameter(req, String.class, "description").ifPresent(project::setDescription); |
universe@47 | 189 | getParameter(req, String.class, "repoUrl").ifPresent(project::setRepoUrl); |
universe@47 | 190 | getParameter(req, Integer.class, "owner").map( |
universe@47 | 191 | ownerId -> ownerId >= 0 ? new User(ownerId) : null |
universe@47 | 192 | ).ifPresent(project::setOwner); |
universe@47 | 193 | |
mike@159 | 194 | final var projectDao = dao.getProjectDao(); |
mike@159 | 195 | if (project.getId() > 0) { |
mike@159 | 196 | // TODO: unused return value |
mike@159 | 197 | projectDao.update(project); |
mike@159 | 198 | } else { |
mike@159 | 199 | projectDao.save(project); |
mike@159 | 200 | } |
universe@47 | 201 | |
universe@118 | 202 | setRedirectLocation(req, "./projects/"); |
universe@74 | 203 | setContentPage(req, Constants.JSP_COMMIT_SUCCESSFUL); |
universe@59 | 204 | LOG.debug("Successfully updated project {}", project.getName()); |
universe@99 | 205 | |
universe@157 | 206 | renderSite(req, resp); |
mike@159 | 207 | } catch (NoSuchElementException | IllegalArgumentException ex) { |
universe@131 | 208 | resp.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED); |
universe@131 | 209 | // TODO: implement - fix issue #21 |
universe@47 | 210 | } |
universe@47 | 211 | } |
universe@47 | 212 | |
universe@134 | 213 | @RequestMapping(requestPath = "$project/$component/$version/issues/", method = HttpMethod.GET) |
mike@159 | 214 | public void issues(HttpServletRequest req, HttpServletResponse resp, PathParameters pathParams, DaoProvider dao) throws SQLException, IOException, ServletException { |
universe@99 | 215 | final var viewModel = new ProjectDetailsView(); |
universe@131 | 216 | populate(viewModel, pathParams, dao); |
universe@86 | 217 | |
universe@134 | 218 | if (!viewModel.isEveryFilterValid()) { |
universe@131 | 219 | resp.sendError(HttpServletResponse.SC_NOT_FOUND); |
universe@157 | 220 | return; |
universe@80 | 221 | } |
universe@47 | 222 | |
universe@134 | 223 | final var project = viewModel.getProjectInfo().getProject(); |
universe@134 | 224 | final var version = viewModel.getVersionFilter(); |
universe@134 | 225 | final var component = viewModel.getComponentFilter(); |
universe@134 | 226 | |
universe@86 | 227 | final var issueDao = dao.getIssueDao(); |
universe@70 | 228 | |
universe@134 | 229 | final List<Issue> issues; |
universe@134 | 230 | if (version.equals(ProjectView.NO_VERSION)) { |
universe@134 | 231 | if (component.equals(ProjectView.ALL_COMPONENTS)) { |
universe@134 | 232 | issues = issueDao.list(project, (Version) null); |
universe@134 | 233 | } else if (component.equals(ProjectView.NO_COMPONENT)) { |
universe@134 | 234 | issues = issueDao.list(project, null, null); |
universe@134 | 235 | } else { |
universe@134 | 236 | issues = issueDao.list(project, component, null); |
universe@134 | 237 | } |
universe@134 | 238 | } else if (version.equals(ProjectView.ALL_VERSIONS)) { |
universe@134 | 239 | if (component.equals(ProjectView.ALL_COMPONENTS)) { |
universe@134 | 240 | issues = issueDao.list(project); |
universe@134 | 241 | } else if (component.equals(ProjectView.NO_COMPONENT)) { |
universe@134 | 242 | issues = issueDao.list(project, (Component)null); |
universe@134 | 243 | } else { |
universe@134 | 244 | issues = issueDao.list(project, component); |
universe@134 | 245 | } |
universe@134 | 246 | } else { |
universe@134 | 247 | if (component.equals(ProjectView.ALL_COMPONENTS)) { |
universe@134 | 248 | issues = issueDao.list(project, version); |
universe@134 | 249 | } else if (component.equals(ProjectView.NO_COMPONENT)) { |
universe@134 | 250 | issues = issueDao.list(project, null, version); |
universe@134 | 251 | } else { |
universe@134 | 252 | issues = issueDao.list(project, component, version); |
universe@134 | 253 | } |
universe@134 | 254 | } |
universe@134 | 255 | |
universe@100 | 256 | for (var issue : issues) issueDao.joinVersionInformation(issue); |
universe@121 | 257 | issues.sort(new IssueSorter( |
universe@141 | 258 | new IssueSorter.Criteria(IssueSorter.Field.DONE, true), |
universe@121 | 259 | new IssueSorter.Criteria(IssueSorter.Field.ETA, true), |
universe@121 | 260 | new IssueSorter.Criteria(IssueSorter.Field.UPDATED, false) |
universe@121 | 261 | )); |
universe@134 | 262 | |
universe@134 | 263 | |
universe@134 | 264 | viewModel.getProjectDetails().updateDetails(issues); |
universe@134 | 265 | if (version.getId() > 0) |
universe@134 | 266 | viewModel.getProjectDetails().updateVersionInfo(version); |
universe@80 | 267 | |
universe@157 | 268 | forwardView(req, resp, viewModel, "project-details"); |
universe@71 | 269 | } |
universe@71 | 270 | |
universe@131 | 271 | @RequestMapping(requestPath = "$project/versions/", method = HttpMethod.GET) |
mike@159 | 272 | public void versions(HttpServletRequest req, HttpServletResponse resp, PathParameters pathParameters, DaoProvider dao) throws IOException, SQLException, ServletException { |
universe@109 | 273 | final var viewModel = new VersionsView(); |
universe@131 | 274 | populate(viewModel, pathParameters, dao); |
universe@109 | 275 | |
universe@109 | 276 | final var projectInfo = viewModel.getProjectInfo(); |
universe@109 | 277 | if (projectInfo == null) { |
universe@131 | 278 | resp.sendError(HttpServletResponse.SC_NOT_FOUND); |
universe@157 | 279 | return; |
universe@109 | 280 | } |
universe@109 | 281 | |
universe@109 | 282 | final var issueDao = dao.getIssueDao(); |
universe@109 | 283 | final var issues = issueDao.list(projectInfo.getProject()); |
universe@109 | 284 | for (var issue : issues) issueDao.joinVersionInformation(issue); |
universe@109 | 285 | viewModel.update(projectInfo.getVersions(), issues); |
universe@109 | 286 | |
universe@157 | 287 | forwardView(req, resp, viewModel, "versions"); |
universe@109 | 288 | } |
universe@109 | 289 | |
universe@131 | 290 | @RequestMapping(requestPath = "$project/versions/$version/edit", method = HttpMethod.GET) |
mike@159 | 291 | public void editVersion(HttpServletRequest req, HttpServletResponse resp, PathParameters pathParameters, DaoProvider dao) throws IOException, SQLException, ServletException { |
universe@99 | 292 | final var viewModel = new VersionEditView(); |
universe@131 | 293 | populate(viewModel, pathParameters, dao); |
universe@99 | 294 | |
universe@131 | 295 | if (viewModel.getProjectInfo() == null || viewModel.getVersionFilter() == null) { |
universe@131 | 296 | resp.sendError(HttpServletResponse.SC_NOT_FOUND); |
universe@157 | 297 | return; |
universe@110 | 298 | } |
universe@110 | 299 | |
universe@131 | 300 | viewModel.setVersion(viewModel.getVersionFilter()); |
universe@59 | 301 | |
universe@157 | 302 | forwardView(req, resp, viewModel, "version-form"); |
universe@59 | 303 | } |
universe@59 | 304 | |
universe@131 | 305 | @RequestMapping(requestPath = "$project/create-version", method = HttpMethod.GET) |
mike@159 | 306 | public void createVersion(HttpServletRequest req, HttpServletResponse resp, PathParameters pathParameters, DaoProvider dao) throws IOException, SQLException, ServletException { |
universe@131 | 307 | final var viewModel = new VersionEditView(); |
universe@131 | 308 | populate(viewModel, pathParameters, dao); |
universe@59 | 309 | |
universe@131 | 310 | if (viewModel.getProjectInfo() == null) { |
universe@131 | 311 | resp.sendError(HttpServletResponse.SC_NOT_FOUND); |
universe@157 | 312 | return; |
universe@131 | 313 | } |
universe@131 | 314 | |
universe@152 | 315 | viewModel.setVersion(new Version(-1)); |
universe@131 | 316 | |
universe@157 | 317 | forwardView(req, resp, viewModel, "version-form"); |
universe@131 | 318 | } |
universe@131 | 319 | |
universe@131 | 320 | @RequestMapping(requestPath = "commit-version", method = HttpMethod.POST) |
mike@159 | 321 | public void commitVersion(HttpServletRequest req, HttpServletResponse resp, DaoProvider dao) throws IOException, ServletException { |
universe@131 | 322 | |
universe@59 | 323 | try { |
universe@138 | 324 | final var project = dao.getProjectDao().find(getParameter(req, Integer.class, "pid").orElseThrow()); |
universe@138 | 325 | if (project == null) { |
universe@138 | 326 | // TODO: improve error handling, because not found is not correct for this POST request |
universe@138 | 327 | resp.sendError(HttpServletResponse.SC_NOT_FOUND); |
universe@157 | 328 | return; |
universe@138 | 329 | } |
universe@131 | 330 | final var version = new Version(getParameter(req, Integer.class, "id").orElseThrow()); |
universe@59 | 331 | version.setName(getParameter(req, String.class, "name").orElseThrow()); |
universe@138 | 332 | |
universe@138 | 333 | final var node = getParameter(req, String.class, "node").orElse(null); |
universe@138 | 334 | version.setNode(sanitizeNode(node, version.getName())); |
universe@138 | 335 | |
universe@59 | 336 | getParameter(req, Integer.class, "ordinal").ifPresent(version::setOrdinal); |
universe@59 | 337 | version.setStatus(VersionStatus.valueOf(getParameter(req, String.class, "status").orElseThrow())); |
mike@159 | 338 | |
mike@159 | 339 | final var versionDao = dao.getVersionDao(); |
mike@159 | 340 | if (version.getId() > 0) { |
mike@159 | 341 | // TODO: use return value |
mike@159 | 342 | versionDao.update(version); |
mike@159 | 343 | } else { |
mike@159 | 344 | versionDao.save(version, project); |
mike@159 | 345 | } |
universe@59 | 346 | |
universe@138 | 347 | setRedirectLocation(req, "./projects/" + project.getNode() + "/versions/"); |
universe@74 | 348 | setContentPage(req, Constants.JSP_COMMIT_SUCCESSFUL); |
universe@157 | 349 | |
universe@157 | 350 | renderSite(req, resp); |
mike@159 | 351 | } catch (NoSuchElementException | IllegalArgumentException ex) { |
universe@131 | 352 | resp.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED); |
universe@131 | 353 | // TODO: implement - fix issue #21 |
universe@59 | 354 | } |
universe@41 | 355 | } |
universe@64 | 356 | |
universe@134 | 357 | @RequestMapping(requestPath = "$project/components/", method = HttpMethod.GET) |
mike@159 | 358 | public void components(HttpServletRequest req, HttpServletResponse resp, PathParameters pathParameters, DaoProvider dao) throws IOException, SQLException, ServletException { |
universe@134 | 359 | final var viewModel = new ComponentsView(); |
universe@134 | 360 | populate(viewModel, pathParameters, dao); |
universe@134 | 361 | |
universe@134 | 362 | final var projectInfo = viewModel.getProjectInfo(); |
universe@134 | 363 | if (projectInfo == null) { |
universe@134 | 364 | resp.sendError(HttpServletResponse.SC_NOT_FOUND); |
universe@157 | 365 | return; |
universe@134 | 366 | } |
universe@134 | 367 | |
universe@134 | 368 | final var issueDao = dao.getIssueDao(); |
universe@134 | 369 | final var issues = issueDao.list(projectInfo.getProject()); |
universe@134 | 370 | viewModel.update(projectInfo.getComponents(), issues); |
universe@134 | 371 | |
universe@157 | 372 | forwardView(req, resp, viewModel, "components"); |
universe@134 | 373 | } |
universe@134 | 374 | |
universe@134 | 375 | @RequestMapping(requestPath = "$project/components/$component/edit", method = HttpMethod.GET) |
mike@159 | 376 | public void editComponent(HttpServletRequest req, HttpServletResponse resp, PathParameters pathParameters, DaoProvider dao) throws IOException, SQLException, ServletException { |
universe@134 | 377 | final var viewModel = new ComponentEditView(); |
universe@134 | 378 | populate(viewModel, pathParameters, dao); |
universe@134 | 379 | |
universe@134 | 380 | if (viewModel.getProjectInfo() == null || viewModel.getComponentFilter() == null) { |
universe@134 | 381 | resp.sendError(HttpServletResponse.SC_NOT_FOUND); |
universe@157 | 382 | return; |
universe@134 | 383 | } |
universe@134 | 384 | |
universe@134 | 385 | viewModel.setComponent(viewModel.getComponentFilter()); |
universe@134 | 386 | viewModel.setUsers(dao.getUserDao().list()); |
universe@134 | 387 | |
universe@157 | 388 | forwardView(req, resp, viewModel, "component-form"); |
universe@134 | 389 | } |
universe@134 | 390 | |
universe@134 | 391 | @RequestMapping(requestPath = "$project/create-component", method = HttpMethod.GET) |
mike@159 | 392 | public void createComponent(HttpServletRequest req, HttpServletResponse resp, PathParameters pathParameters, DaoProvider dao) throws IOException, SQLException, ServletException { |
universe@134 | 393 | final var viewModel = new ComponentEditView(); |
universe@134 | 394 | populate(viewModel, pathParameters, dao); |
universe@134 | 395 | |
universe@134 | 396 | if (viewModel.getProjectInfo() == null) { |
universe@134 | 397 | resp.sendError(HttpServletResponse.SC_NOT_FOUND); |
universe@157 | 398 | return; |
universe@134 | 399 | } |
universe@134 | 400 | |
universe@134 | 401 | viewModel.setComponent(new Component(-1)); |
universe@134 | 402 | viewModel.setUsers(dao.getUserDao().list()); |
universe@134 | 403 | |
universe@157 | 404 | forwardView(req, resp, viewModel, "component-form"); |
universe@134 | 405 | } |
universe@134 | 406 | |
universe@134 | 407 | @RequestMapping(requestPath = "commit-component", method = HttpMethod.POST) |
mike@159 | 408 | public void commitComponent(HttpServletRequest req, HttpServletResponse resp, DaoProvider dao) throws IOException, ServletException { |
universe@134 | 409 | |
universe@134 | 410 | try { |
universe@138 | 411 | final var project = dao.getProjectDao().find(getParameter(req, Integer.class, "pid").orElseThrow()); |
universe@138 | 412 | if (project == null) { |
universe@138 | 413 | // TODO: improve error handling, because not found is not correct for this POST request |
universe@138 | 414 | resp.sendError(HttpServletResponse.SC_NOT_FOUND); |
universe@157 | 415 | return; |
universe@138 | 416 | } |
universe@134 | 417 | final var component = new Component(getParameter(req, Integer.class, "id").orElseThrow()); |
universe@134 | 418 | component.setName(getParameter(req, String.class, "name").orElseThrow()); |
universe@138 | 419 | |
universe@138 | 420 | final var node = getParameter(req, String.class, "node").orElse(null); |
universe@138 | 421 | component.setNode(sanitizeNode(node, component.getName())); |
universe@138 | 422 | |
universe@134 | 423 | component.setColor(getParameter(req, WebColor.class, "color").orElseThrow()); |
universe@134 | 424 | getParameter(req, Integer.class, "ordinal").ifPresent(component::setOrdinal); |
universe@134 | 425 | getParameter(req, Integer.class, "lead").map( |
universe@134 | 426 | userid -> userid >= 0 ? new User(userid) : null |
universe@134 | 427 | ).ifPresent(component::setLead); |
universe@134 | 428 | getParameter(req, String.class, "description").ifPresent(component::setDescription); |
universe@134 | 429 | |
mike@159 | 430 | final var componentDao = dao.getComponentDao(); |
mike@159 | 431 | if (component.getId() > 0) { |
mike@159 | 432 | // TODO: use return value |
mike@159 | 433 | componentDao.update(component); |
mike@159 | 434 | } else { |
mike@159 | 435 | componentDao.save(component, project); |
mike@159 | 436 | } |
universe@134 | 437 | |
universe@138 | 438 | setRedirectLocation(req, "./projects/" + project.getNode() + "/components/"); |
universe@134 | 439 | setContentPage(req, Constants.JSP_COMMIT_SUCCESSFUL); |
universe@157 | 440 | |
universe@157 | 441 | renderSite(req, resp); |
mike@159 | 442 | } catch (NoSuchElementException | IllegalArgumentException ex) { |
universe@134 | 443 | resp.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED); |
universe@134 | 444 | // TODO: implement - fix issue #21 |
universe@134 | 445 | } |
universe@134 | 446 | } |
universe@134 | 447 | |
mike@159 | 448 | private void configureIssueEditor(IssueEditView viewModel, Issue issue, DaoProvider dao) throws SQLException { |
universe@134 | 449 | final var project = viewModel.getProjectInfo().getProject(); |
universe@146 | 450 | issue.setProject(project); // automatically set current project for new issues |
universe@99 | 451 | viewModel.setIssue(issue); |
universe@99 | 452 | viewModel.configureVersionSelectors(viewModel.getProjectInfo().getVersions()); |
universe@86 | 453 | viewModel.setUsers(dao.getUserDao().list()); |
universe@134 | 454 | viewModel.setComponents(dao.getComponentDao().list(project)); |
universe@71 | 455 | } |
universe@71 | 456 | |
universe@146 | 457 | @RequestMapping(requestPath = "$project/issues/$issue/view", method = HttpMethod.GET) |
mike@159 | 458 | public void viewIssue(HttpServletRequest req, HttpServletResponse resp, PathParameters pathParameters, DaoProvider dao) throws IOException, SQLException, ServletException { |
universe@146 | 459 | final var viewModel = new IssueDetailView(); |
universe@146 | 460 | populate(viewModel, pathParameters, dao); |
universe@146 | 461 | |
universe@146 | 462 | final var projectInfo = viewModel.getProjectInfo(); |
universe@146 | 463 | if (projectInfo == null) { |
universe@146 | 464 | resp.sendError(HttpServletResponse.SC_NOT_FOUND); |
universe@157 | 465 | return; |
universe@146 | 466 | } |
universe@146 | 467 | |
universe@146 | 468 | final var issueDao = dao.getIssueDao(); |
universe@158 | 469 | final var issue = issueDao.find(parseIntOrZero(pathParameters.get("issue"))); |
universe@146 | 470 | if (issue == null) { |
universe@146 | 471 | resp.sendError(HttpServletResponse.SC_NOT_FOUND); |
universe@157 | 472 | return; |
universe@146 | 473 | } |
universe@146 | 474 | |
universe@146 | 475 | issueDao.joinVersionInformation(issue); |
universe@146 | 476 | viewModel.setIssue(issue); |
universe@146 | 477 | viewModel.setComments(issueDao.listComments(issue)); |
universe@146 | 478 | |
universe@157 | 479 | forwardView(req, resp, viewModel, "issue-view"); |
universe@146 | 480 | } |
universe@146 | 481 | |
universe@146 | 482 | // TODO: why should the issue editor be child of $project? |
universe@131 | 483 | @RequestMapping(requestPath = "$project/issues/$issue/edit", method = HttpMethod.GET) |
mike@159 | 484 | public void editIssue(HttpServletRequest req, HttpServletResponse resp, PathParameters pathParameters, DaoProvider dao) throws IOException, SQLException, ServletException { |
universe@99 | 485 | final var viewModel = new IssueEditView(); |
universe@131 | 486 | populate(viewModel, pathParameters, dao); |
universe@99 | 487 | |
universe@131 | 488 | final var projectInfo = viewModel.getProjectInfo(); |
universe@131 | 489 | if (projectInfo == null) { |
universe@131 | 490 | resp.sendError(HttpServletResponse.SC_NOT_FOUND); |
universe@157 | 491 | return; |
universe@86 | 492 | } |
universe@64 | 493 | |
universe@131 | 494 | final var issueDao = dao.getIssueDao(); |
universe@158 | 495 | final var issue = issueDao.find(parseIntOrZero(pathParameters.get("issue"))); |
universe@131 | 496 | if (issue == null) { |
universe@131 | 497 | resp.sendError(HttpServletResponse.SC_NOT_FOUND); |
universe@157 | 498 | return; |
universe@131 | 499 | } |
universe@131 | 500 | |
universe@131 | 501 | issueDao.joinVersionInformation(issue); |
universe@133 | 502 | configureIssueEditor(viewModel, issue, dao); |
universe@131 | 503 | |
universe@157 | 504 | forwardView(req, resp, viewModel, "issue-form"); |
universe@64 | 505 | } |
universe@64 | 506 | |
universe@131 | 507 | @RequestMapping(requestPath = "$project/create-issue", method = HttpMethod.GET) |
mike@159 | 508 | public void createIssue(HttpServletRequest req, HttpServletResponse resp, PathParameters pathParameters, DaoProvider dao) throws IOException, SQLException, ServletException { |
universe@131 | 509 | final var viewModel = new IssueEditView(); |
universe@131 | 510 | populate(viewModel, pathParameters, dao); |
universe@131 | 511 | |
universe@131 | 512 | final var projectInfo = viewModel.getProjectInfo(); |
universe@131 | 513 | if (projectInfo == null) { |
universe@131 | 514 | resp.sendError(HttpServletResponse.SC_NOT_FOUND); |
universe@157 | 515 | return; |
universe@131 | 516 | } |
universe@131 | 517 | |
universe@131 | 518 | final var issue = new Issue(-1); |
universe@131 | 519 | issue.setProject(projectInfo.getProject()); |
universe@133 | 520 | configureIssueEditor(viewModel, issue, dao); |
universe@131 | 521 | |
universe@157 | 522 | forwardView(req, resp, viewModel, "issue-form"); |
universe@131 | 523 | } |
universe@131 | 524 | |
universe@131 | 525 | @RequestMapping(requestPath = "commit-issue", method = HttpMethod.POST) |
mike@159 | 526 | public void commitIssue(HttpServletRequest req, HttpServletResponse resp, DaoProvider dao) throws IOException, ServletException { |
universe@64 | 527 | try { |
universe@131 | 528 | final var issue = new Issue(getParameter(req, Integer.class, "id").orElseThrow()); |
universe@136 | 529 | final var componentId = getParameter(req, Integer.class, "component"); |
universe@136 | 530 | final Component component; |
universe@136 | 531 | if (componentId.isPresent()) { |
universe@136 | 532 | component = dao.getComponentDao().find(componentId.get()); |
universe@136 | 533 | } else { |
universe@136 | 534 | component = null; |
universe@136 | 535 | } |
universe@138 | 536 | final var project = dao.getProjectDao().find(getParameter(req, Integer.class, "pid").orElseThrow()); |
universe@138 | 537 | if (project == null) { |
universe@138 | 538 | // TODO: improve error handling, because not found is not correct for this POST request |
universe@138 | 539 | resp.sendError(HttpServletResponse.SC_NOT_FOUND); |
universe@157 | 540 | return; |
universe@138 | 541 | } |
universe@138 | 542 | issue.setProject(project); |
universe@75 | 543 | getParameter(req, String.class, "category").map(IssueCategory::valueOf).ifPresent(issue::setCategory); |
universe@75 | 544 | getParameter(req, String.class, "status").map(IssueStatus::valueOf).ifPresent(issue::setStatus); |
universe@75 | 545 | issue.setSubject(getParameter(req, String.class, "subject").orElseThrow()); |
universe@136 | 546 | issue.setComponent(component); |
universe@136 | 547 | getParameter(req, Integer.class, "assignee").map(userid -> { |
universe@136 | 548 | if (userid >= 0) { |
universe@136 | 549 | return new User(userid); |
universe@136 | 550 | } else if (userid == -2) { |
universe@136 | 551 | return Optional.ofNullable(component).map(Component::getLead).orElse(null); |
universe@136 | 552 | } else { |
universe@136 | 553 | return null; |
universe@136 | 554 | } |
universe@136 | 555 | } |
universe@75 | 556 | ).ifPresent(issue::setAssignee); |
universe@75 | 557 | getParameter(req, String.class, "description").ifPresent(issue::setDescription); |
universe@75 | 558 | getParameter(req, Date.class, "eta").ifPresent(issue::setEta); |
universe@83 | 559 | |
universe@83 | 560 | getParameter(req, Integer[].class, "affected") |
universe@83 | 561 | .map(Stream::of) |
universe@83 | 562 | .map(stream -> |
universe@96 | 563 | stream.map(Version::new).collect(Collectors.toList()) |
universe@83 | 564 | ).ifPresent(issue::setAffectedVersions); |
universe@83 | 565 | getParameter(req, Integer[].class, "resolved") |
universe@83 | 566 | .map(Stream::of) |
universe@83 | 567 | .map(stream -> |
universe@86 | 568 | stream.map(Version::new).collect(Collectors.toList()) |
universe@83 | 569 | ).ifPresent(issue::setResolvedVersions); |
universe@83 | 570 | |
mike@159 | 571 | final var issueDao = dao.getIssueDao(); |
mike@159 | 572 | if (issue.getId() > 0) { |
mike@159 | 573 | // TODO: use return value |
mike@159 | 574 | issueDao.update(issue); |
mike@159 | 575 | } else { |
mike@159 | 576 | issueDao.save(issue, project); |
mike@159 | 577 | } |
universe@64 | 578 | |
universe@146 | 579 | // TODO: fix redirect location |
universe@146 | 580 | setRedirectLocation(req, "./projects/" + issue.getProject().getNode()+"/issues/"+issue.getId()+"/view"); |
universe@74 | 581 | setContentPage(req, Constants.JSP_COMMIT_SUCCESSFUL); |
universe@124 | 582 | |
universe@157 | 583 | renderSite(req, resp); |
mike@159 | 584 | } catch (NoSuchElementException | IllegalArgumentException ex) { |
universe@131 | 585 | resp.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED); |
universe@131 | 586 | // TODO: implement - fix issue #21 |
universe@64 | 587 | } |
universe@124 | 588 | } |
universe@64 | 589 | |
universe@131 | 590 | @RequestMapping(requestPath = "commit-issue-comment", method = HttpMethod.POST) |
mike@159 | 591 | public void commentIssue(HttpServletRequest req, HttpServletResponse resp, DaoProvider dao) throws IOException, ServletException { |
universe@124 | 592 | final var issueIdParam = getParameter(req, Integer.class, "issueid"); |
universe@124 | 593 | if (issueIdParam.isEmpty()) { |
universe@124 | 594 | resp.sendError(HttpServletResponse.SC_FORBIDDEN, "Detected manipulated form."); |
universe@157 | 595 | return; |
universe@124 | 596 | } |
universe@137 | 597 | final var issue = dao.getIssueDao().find(issueIdParam.get()); |
universe@137 | 598 | if (issue == null) { |
universe@137 | 599 | resp.sendError(HttpServletResponse.SC_NOT_FOUND); |
universe@157 | 600 | return; |
universe@137 | 601 | } |
universe@124 | 602 | try { |
universe@150 | 603 | final var issueComment = new IssueComment(getParameter(req, Integer.class, "commentid").orElse(-1)); |
universe@124 | 604 | issueComment.setComment(getParameter(req, String.class, "comment").orElse("")); |
universe@124 | 605 | |
universe@124 | 606 | if (issueComment.getComment().isBlank()) { |
universe@124 | 607 | throw new IllegalArgumentException("comment.null"); |
universe@124 | 608 | } |
universe@124 | 609 | |
universe@124 | 610 | LOG.debug("User {} is commenting on issue #{}", req.getRemoteUser(), issue.getId()); |
universe@124 | 611 | if (req.getRemoteUser() != null) { |
mike@159 | 612 | Optional.ofNullable(dao.getUserDao().findByUsername(req.getRemoteUser())).ifPresent(issueComment::setAuthor); |
universe@124 | 613 | } |
universe@124 | 614 | |
universe@150 | 615 | dao.getIssueDao().saveComment(issue, issueComment); |
universe@124 | 616 | |
universe@146 | 617 | // TODO: fix redirect location |
universe@146 | 618 | setRedirectLocation(req, "./projects/" + issue.getProject().getNode()+"/issues/"+issue.getId()+"/view"); |
universe@124 | 619 | setContentPage(req, Constants.JSP_COMMIT_SUCCESSFUL); |
universe@124 | 620 | |
universe@157 | 621 | renderSite(req, resp); |
mike@159 | 622 | } catch (NoSuchElementException | IllegalArgumentException ex) { |
universe@131 | 623 | resp.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED); |
universe@131 | 624 | // TODO: implement - fix issue #21 |
universe@124 | 625 | } |
universe@64 | 626 | } |
universe@41 | 627 | } |