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

     1 /*
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  *
     4  * Copyright 2018 Mike Becker. All rights reserved.
     5  *
     6  * Redistribution and use in source and binary forms, with or without
     7  * modification, are permitted provided that the following conditions are met:
     8  *
     9  *   1. Redistributions of source code must retain the above copyright
    10  *      notice, this list of conditions and the following disclaimer.
    11  *
    12  *   2. Redistributions in binary form must reproduce the above copyright
    13  *      notice, this list of conditions and the following disclaimer in the
    14  *      documentation and/or other materials provided with the distribution.
    15  *
    16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    26  * POSSIBILITY OF SUCH DAMAGE.
    27  *
    28  */
    29 package de.uapcore.lightpit.modules;
    32 import de.uapcore.lightpit.*;
    33 import de.uapcore.lightpit.dao.DataAccessObjects;
    34 import de.uapcore.lightpit.entities.Project;
    35 import de.uapcore.lightpit.entities.User;
    37 import javax.servlet.annotation.WebServlet;
    38 import javax.servlet.http.HttpServletRequest;
    39 import java.sql.SQLException;
    40 import java.util.Optional;
    42 @LightPITModule(
    43         bundleBaseName = "localization.projects",
    44         modulePath = "projects",
    45         defaultPriority = 20
    46 )
    47 @WebServlet(
    48         name = "ProjectsModule",
    49         urlPatterns = "/projects/*"
    50 )
    51 public final class ProjectsModule extends AbstractLightPITServlet {
    53     @RequestMapping(method = HttpMethod.GET)
    54     public ResponseType index(HttpServletRequest req, DataAccessObjects dao) throws SQLException {
    55         final var projectDao = dao.getProjectDao();
    57         req.setAttribute("projects", projectDao.list());
    58         setDynamicFragment(req, "projects");
    60         return ResponseType.HTML;
    61     }
    63     @RequestMapping(requestPath = "edit", method = HttpMethod.GET)
    64     public ResponseType displayCreateForm(HttpServletRequest req, DataAccessObjects dao) throws SQLException {
    65         final var projectDao = dao.getProjectDao();
    67         Optional<Integer> id = getParameter(req, Integer.class, "id");
    68         if (id.isPresent()) {
    69             req.setAttribute("project", Optional.ofNullable(projectDao.find(id.get())).orElse(new Project(-1)));
    70         } else {
    71             req.setAttribute("project", new Project(-1));
    72         }
    74         setDynamicFragment(req, "project-form");
    76         return ResponseType.HTML;
    77     }
    79     @RequestMapping(requestPath = "commit", method = HttpMethod.POST)
    80     public ResponseType commit(HttpServletRequest req, DataAccessObjects dao) {
    82         Project project = new Project(-1);
    83         try {
    84             project = new Project(getParameter(req, Integer.class, "id").orElseThrow());
    85             project.setName(getParameter(req, String.class, "name").orElseThrow());
    86             getParameter(req, String.class, "description").ifPresent(project::setDescription);
    87             getParameter(req, String.class, "repoUrl").ifPresent(project::setRepoUrl);
    88             getParameter(req, Integer.class, "owner").map(
    89                     ownerId -> ownerId >= 0 ? new User(ownerId) : null
    90             ).ifPresent(project::setOwner);
    92             dao.getProjectDao().saveOrUpdate(project);
    94             setRedirectLocation(req, "./projects/");
    95             setDynamicFragment(req, Constants.DYN_FRAGMENT_COMMIT_SUCCESSFUL);
    96         } catch (NullPointerException | NumberFormatException | SQLException ex) {
    97             // TODO: set request attribute with error text
    98             req.setAttribute("project", project);
    99             setDynamicFragment(req, "project-form");
   100         }
   102         return ResponseType.HTML;
   103     }
   106     @RequestMapping(requestPath = "versions", method = HttpMethod.GET, menuKey = "menu.versions")
   107     public ResponseType versions(HttpServletRequest req, DataAccessObjects dao) {
   109         return ResponseType.HTML;
   110     }
   111 }

mercurial