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

Thu, 14 May 2020 22:48:01 +0200

author
Mike Becker <universe@uap-core.de>
date
Thu, 14 May 2020 22:48:01 +0200
changeset 47
57cfb94ab99f
parent 45
cc7f082c5ef3
child 51
dd0a45ae25d7
permissions
-rw-r--r--

projects can now be added and updated

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@41 42 @LightPITModule(
universe@41 43 bundleBaseName = "localization.projects",
universe@41 44 modulePath = "projects",
universe@41 45 defaultPriority = 20
universe@41 46 )
universe@41 47 @WebServlet(
universe@41 48 name = "ProjectsModule",
universe@41 49 urlPatterns = "/projects/*"
universe@41 50 )
universe@41 51 public final class ProjectsModule extends AbstractLightPITServlet {
universe@41 52
universe@41 53 @RequestMapping(method = HttpMethod.GET)
universe@47 54 public ResponseType index(HttpServletRequest req, DataAccessObjects dao) throws SQLException {
universe@47 55 final var projectDao = dao.getProjectDao();
universe@47 56
universe@47 57 req.setAttribute("projects", projectDao.list());
universe@47 58 setDynamicFragment(req, "projects");
universe@45 59
universe@45 60 return ResponseType.HTML;
universe@45 61 }
universe@45 62
universe@47 63 @RequestMapping(requestPath = "edit", method = HttpMethod.GET)
universe@47 64 public ResponseType displayCreateForm(HttpServletRequest req, DataAccessObjects dao) throws SQLException {
universe@47 65 final var projectDao = dao.getProjectDao();
universe@47 66
universe@47 67 Optional<Integer> id = getParameter(req, Integer.class, "id");
universe@47 68 if (id.isPresent()) {
universe@47 69 req.setAttribute("project", Optional.ofNullable(projectDao.find(id.get())).orElse(new Project(-1)));
universe@47 70 } else {
universe@47 71 req.setAttribute("project", new Project(-1));
universe@47 72 }
universe@47 73
universe@47 74 setDynamicFragment(req, "project-form");
universe@47 75
universe@47 76 return ResponseType.HTML;
universe@47 77 }
universe@47 78
universe@47 79 @RequestMapping(requestPath = "commit", method = HttpMethod.POST)
universe@47 80 public ResponseType commit(HttpServletRequest req, DataAccessObjects dao) {
universe@47 81
universe@47 82 Project project = new Project(-1);
universe@47 83 try {
universe@47 84 project = new Project(getParameter(req, Integer.class, "id").orElseThrow());
universe@47 85 project.setName(getParameter(req, String.class, "name").orElseThrow());
universe@47 86 getParameter(req, String.class, "description").ifPresent(project::setDescription);
universe@47 87 getParameter(req, String.class, "repoUrl").ifPresent(project::setRepoUrl);
universe@47 88 getParameter(req, Integer.class, "owner").map(
universe@47 89 ownerId -> ownerId >= 0 ? new User(ownerId) : null
universe@47 90 ).ifPresent(project::setOwner);
universe@47 91
universe@47 92 dao.getProjectDao().saveOrUpdate(project);
universe@47 93
universe@47 94 setRedirectLocation(req, "./projects/");
universe@47 95 setDynamicFragment(req, Constants.DYN_FRAGMENT_COMMIT_SUCCESSFUL);
universe@47 96 } catch (NullPointerException | NumberFormatException | SQLException ex) {
universe@47 97 // TODO: set request attribute with error text
universe@47 98 req.setAttribute("project", project);
universe@47 99 setDynamicFragment(req, "project-form");
universe@47 100 }
universe@47 101
universe@47 102 return ResponseType.HTML;
universe@47 103 }
universe@47 104
universe@47 105
universe@47 106 @RequestMapping(requestPath = "versions", method = HttpMethod.GET, menuKey = "menu.versions")
universe@45 107 public ResponseType versions(HttpServletRequest req, DataAccessObjects dao) {
universe@41 108
universe@43 109 return ResponseType.HTML;
universe@41 110 }
universe@41 111 }

mercurial