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

Sat, 16 May 2020 15:11:07 +0200

author
Mike Becker <universe@uap-core.de>
date
Sat, 16 May 2020 15:11:07 +0200
changeset 52
67a02e79b7a1
parent 51
dd0a45ae25d7
child 59
c759c60507a2
permissions
-rw-r--r--

adds project selection

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@47 34 import de.uapcore.lightpit.entities.Project;
universe@47 35 import de.uapcore.lightpit.entities.User;
universe@41 36
universe@41 37 import javax.servlet.annotation.WebServlet;
universe@41 38 import javax.servlet.http.HttpServletRequest;
universe@47 39 import java.sql.SQLException;
universe@47 40 import java.util.Optional;
universe@41 41
universe@52 42 import static de.uapcore.lightpit.Functions.fqn;
universe@52 43
universe@41 44 @LightPITModule(
universe@41 45 bundleBaseName = "localization.projects",
universe@41 46 modulePath = "projects",
universe@41 47 defaultPriority = 20
universe@41 48 )
universe@41 49 @WebServlet(
universe@41 50 name = "ProjectsModule",
universe@41 51 urlPatterns = "/projects/*"
universe@41 52 )
universe@41 53 public final class ProjectsModule extends AbstractLightPITServlet {
universe@41 54
universe@52 55 public static final String SESSION_ATTR_SELECTED_PROJECT = fqn(ProjectsModule.class, "selected-project");
universe@52 56
universe@41 57 @RequestMapping(method = HttpMethod.GET)
universe@47 58 public ResponseType index(HttpServletRequest req, DataAccessObjects dao) throws SQLException {
universe@52 59 final var projectList = dao.getProjectDao().list();
universe@52 60 req.setAttribute("projects", projectList);
universe@47 61 setDynamicFragment(req, "projects");
universe@52 62 setStylesheet(req, "projects");
universe@52 63
universe@52 64 final var session = req.getSession();
universe@52 65 final var projectSelection = getParameter(req, Integer.class, "select");
universe@52 66 if (projectSelection.isPresent()) {
universe@52 67 final var selectedId = projectSelection.get();
universe@52 68 for (var proj : projectList) {
universe@52 69 if (proj.getId() == selectedId) {
universe@52 70 session.setAttribute(SESSION_ATTR_SELECTED_PROJECT, proj);
universe@52 71 break;
universe@52 72 }
universe@52 73 }
universe@52 74 } else {
universe@52 75 final var selectedProject = session.getAttribute(SESSION_ATTR_SELECTED_PROJECT);
universe@52 76 if (selectedProject == null) {
universe@52 77 projectList.stream().findFirst().ifPresent(proj -> session.setAttribute(SESSION_ATTR_SELECTED_PROJECT, proj));
universe@52 78 }
universe@52 79 }
universe@45 80
universe@45 81 return ResponseType.HTML;
universe@45 82 }
universe@45 83
universe@47 84 @RequestMapping(requestPath = "edit", method = HttpMethod.GET)
universe@51 85 public ResponseType edit(HttpServletRequest req, DataAccessObjects dao) throws SQLException {
universe@47 86 final var projectDao = dao.getProjectDao();
universe@47 87
universe@47 88 Optional<Integer> id = getParameter(req, Integer.class, "id");
universe@47 89 if (id.isPresent()) {
universe@47 90 req.setAttribute("project", Optional.ofNullable(projectDao.find(id.get())).orElse(new Project(-1)));
universe@47 91 } else {
universe@47 92 req.setAttribute("project", new Project(-1));
universe@47 93 }
universe@51 94 req.setAttribute("users", dao.getUserDao().list());
universe@47 95
universe@47 96 setDynamicFragment(req, "project-form");
universe@47 97
universe@47 98 return ResponseType.HTML;
universe@47 99 }
universe@47 100
universe@47 101 @RequestMapping(requestPath = "commit", method = HttpMethod.POST)
universe@47 102 public ResponseType commit(HttpServletRequest req, DataAccessObjects dao) {
universe@47 103
universe@47 104 Project project = new Project(-1);
universe@47 105 try {
universe@47 106 project = new Project(getParameter(req, Integer.class, "id").orElseThrow());
universe@47 107 project.setName(getParameter(req, String.class, "name").orElseThrow());
universe@47 108 getParameter(req, String.class, "description").ifPresent(project::setDescription);
universe@47 109 getParameter(req, String.class, "repoUrl").ifPresent(project::setRepoUrl);
universe@47 110 getParameter(req, Integer.class, "owner").map(
universe@47 111 ownerId -> ownerId >= 0 ? new User(ownerId) : null
universe@47 112 ).ifPresent(project::setOwner);
universe@47 113
universe@47 114 dao.getProjectDao().saveOrUpdate(project);
universe@47 115
universe@47 116 setRedirectLocation(req, "./projects/");
universe@47 117 setDynamicFragment(req, Constants.DYN_FRAGMENT_COMMIT_SUCCESSFUL);
universe@47 118 } catch (NullPointerException | NumberFormatException | SQLException ex) {
universe@47 119 // TODO: set request attribute with error text
universe@47 120 req.setAttribute("project", project);
universe@47 121 setDynamicFragment(req, "project-form");
universe@47 122 }
universe@47 123
universe@47 124 return ResponseType.HTML;
universe@47 125 }
universe@47 126
universe@47 127
universe@47 128 @RequestMapping(requestPath = "versions", method = HttpMethod.GET, menuKey = "menu.versions")
universe@45 129 public ResponseType versions(HttpServletRequest req, DataAccessObjects dao) {
universe@41 130
universe@43 131 return ResponseType.HTML;
universe@41 132 }
universe@41 133 }

mercurial