Sat, 23 May 2020 13:52:04 +0200
bloat removal 2/3 - moduleInfo
/* * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. * * Copyright 2018 Mike Becker. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * */ package de.uapcore.lightpit.modules; import de.uapcore.lightpit.*; import de.uapcore.lightpit.dao.DataAccessObjects; import de.uapcore.lightpit.entities.*; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import java.io.IOException; import java.sql.Date; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import java.util.NoSuchElementException; import java.util.Objects; import static de.uapcore.lightpit.Functions.fqn; @LightPITModule( bundleBaseName = "localization.projects", modulePath = "projects", defaultPriority = 20 ) @WebServlet( name = "ProjectsModule", urlPatterns = "/projects/*" ) public final class ProjectsModule extends AbstractLightPITServlet { private static final Logger LOG = LoggerFactory.getLogger(ProjectsModule.class); public static final String SESSION_ATTR_SELECTED_PROJECT = fqn(ProjectsModule.class, "selected-project"); public static final String SESSION_ATTR_SELECTED_ISSUE = fqn(ProjectsModule.class, "selected-issue"); public static final String SESSION_ATTR_SELECTED_VERSION = fqn(ProjectsModule.class, "selected-version"); private class SessionSelection { final HttpSession session; Project project; Version version; Issue issue; SessionSelection(HttpServletRequest req, Project project) { this.session = req.getSession(); this.project = project; version = null; issue = null; updateAttributes(); } SessionSelection(HttpServletRequest req, DataAccessObjects dao) throws SQLException { this.session = req.getSession(); final var issueDao = dao.getIssueDao(); final var projectDao = dao.getProjectDao(); final var issueSelection = getParameter(req, Integer.class, "issue"); if (issueSelection.isPresent()) { issue = issueDao.find(issueSelection.get()); } else { final var issue = (Issue) session.getAttribute(SESSION_ATTR_SELECTED_ISSUE); this.issue = issue == null ? null : issueDao.find(issue.getId()); } if (issue != null) { version = null; // show the issue globally project = projectDao.find(issue.getProject().getId()); } final var projectSelection = getParameter(req, Integer.class, "pid"); if (projectSelection.isPresent()) { final var selectedProject = projectDao.find(projectSelection.get()); if (!Objects.equals(selectedProject, project)) { // reset version and issue if project changed version = null; issue = null; } project = selectedProject; } else { final var sessionProject = (Project) session.getAttribute(SESSION_ATTR_SELECTED_PROJECT); project = sessionProject == null ? null : projectDao.find(sessionProject.getId()); } updateAttributes(); } void selectVersion(Version version) { if (!Objects.equals(project, version.getProject())) throw new AssertionError("Nice, you implemented a bug!"); this.version = version; this.issue = null; updateAttributes(); } void selectIssue(Issue issue) { if (!Objects.equals(issue.getProject(), project)) throw new AssertionError("Nice, you implemented a bug!"); this.issue = issue; this.version = null; updateAttributes(); } void updateAttributes() { session.setAttribute(SESSION_ATTR_SELECTED_PROJECT, project); session.setAttribute(SESSION_ATTR_SELECTED_VERSION, version); session.setAttribute(SESSION_ATTR_SELECTED_ISSUE, issue); } } @Override protected String getResourceBundleName() { return "localization.projects"; } /** * Creates the breadcrumb menu. * * @param level the current active level (0: root, 1: project, 2: version, 3: issue) * @param sessionSelection the currently selected objects * @return a dynamic breadcrumb menu trying to display as many levels as possible */ private List<MenuEntry> getBreadcrumbs(int level, SessionSelection sessionSelection) { MenuEntry entry; final var breadcrumbs = new ArrayList<MenuEntry>(); entry = new MenuEntry(new ResourceKey("localization.projects", "menuLabel"), "projects/", 0); breadcrumbs.add(entry); if (level == 0) entry.setActive(true); if (sessionSelection.project != null) { if (sessionSelection.project.getId() < 0) { entry = new MenuEntry(new ResourceKey("localization.projects", "button.create"), "projects/edit", 1); } else { entry = new MenuEntry(sessionSelection.project.getName(), "projects/view?pid=" + sessionSelection.project.getId(), 1); } if (level == 1) entry.setActive(true); breadcrumbs.add(entry); } if (sessionSelection.version != null) { if (sessionSelection.version.getId() < 0) { entry = new MenuEntry(new ResourceKey("localization.projects", "button.version.create"), "projects/versions/edit", 2); } else { entry = new MenuEntry(sessionSelection.version.getName(), // TODO: change link to issue overview for that version "projects/versions/edit?id=" + sessionSelection.version.getId(), 2); } if (level == 2) entry.setActive(true); breadcrumbs.add(entry); } if (sessionSelection.issue != null) { entry = new MenuEntry(new ResourceKey("localization.projects", "menu.issues"), // TODO: change link to a separate issue view (maybe depending on the selected version) "projects/view?pid=" + sessionSelection.issue.getProject().getId(), 3); breadcrumbs.add(entry); if (sessionSelection.issue.getId() < 0) { entry = new MenuEntry(new ResourceKey("localization.projects", "button.issue.create"), "projects/issues/edit", 2); } else { entry = new MenuEntry("#" + sessionSelection.issue.getId(), // TODO: maybe change link to a view rather than directly opening the editor "projects/issues/edit?id=" + sessionSelection.issue.getId(), 4); } if (level == 3) entry.setActive(true); breadcrumbs.add(entry); } return breadcrumbs; } @RequestMapping(method = HttpMethod.GET) public ResponseType index(HttpServletRequest req, DataAccessObjects dao) throws SQLException { final var sessionSelection = new SessionSelection(req, dao); final var projectList = dao.getProjectDao().list(); req.setAttribute("projects", projectList); setContentPage(req, "projects"); setStylesheet(req, "projects"); setBreadcrumbs(req, getBreadcrumbs(0, sessionSelection)); return ResponseType.HTML; } private void configureEditForm(HttpServletRequest req, DataAccessObjects dao, SessionSelection selection) throws SQLException { req.setAttribute("project", selection.project); req.setAttribute("users", dao.getUserDao().list()); setContentPage(req, "project-form"); setBreadcrumbs(req, getBreadcrumbs(1, selection)); } @RequestMapping(requestPath = "edit", method = HttpMethod.GET) public ResponseType edit(HttpServletRequest req, DataAccessObjects dao) throws SQLException { final var selection = new SessionSelection(req, findByParameter(req, Integer.class, "id", dao.getProjectDao()::find).orElse(new Project(-1))); configureEditForm(req, dao, selection); return ResponseType.HTML; } @RequestMapping(requestPath = "commit", method = HttpMethod.POST) public ResponseType commit(HttpServletRequest req, DataAccessObjects dao) throws SQLException { Project project = new Project(-1); try { project = new Project(getParameter(req, Integer.class, "id").orElseThrow()); project.setName(getParameter(req, String.class, "name").orElseThrow()); getParameter(req, String.class, "description").ifPresent(project::setDescription); getParameter(req, String.class, "repoUrl").ifPresent(project::setRepoUrl); getParameter(req, Integer.class, "owner").map( ownerId -> ownerId >= 0 ? new User(ownerId) : null ).ifPresent(project::setOwner); dao.getProjectDao().saveOrUpdate(project); setRedirectLocation(req, "./projects/"); setContentPage(req, Constants.JSP_COMMIT_SUCCESSFUL); LOG.debug("Successfully updated project {}", project.getName()); } catch (NoSuchElementException | IllegalArgumentException | SQLException ex) { // TODO: set request attribute with error text LOG.warn("Form validation failure: {}", ex.getMessage()); LOG.debug("Details:", ex); configureEditForm(req, dao, new SessionSelection(req, project)); } return ResponseType.HTML; } @RequestMapping(requestPath = "view", method = HttpMethod.GET) public ResponseType view(HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) throws IOException, SQLException { final var sessionSelection = new SessionSelection(req, dao); req.setAttribute("versions", dao.getVersionDao().list(sessionSelection.project)); req.setAttribute("issues", dao.getIssueDao().list(sessionSelection.project)); setBreadcrumbs(req, getBreadcrumbs(1, sessionSelection)); setContentPage(req, "project-details"); return ResponseType.HTML; } private void configureEditVersionForm(HttpServletRequest req, DataAccessObjects dao, SessionSelection selection) throws SQLException { req.setAttribute("projects", dao.getProjectDao().list()); req.setAttribute("version", selection.version); req.setAttribute("versionStatusEnum", VersionStatus.values()); setContentPage(req, "version-form"); setBreadcrumbs(req, getBreadcrumbs(2, selection)); } @RequestMapping(requestPath = "versions/edit", method = HttpMethod.GET) public ResponseType editVersion(HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) throws IOException, SQLException { final var sessionSelection = new SessionSelection(req, dao); sessionSelection.selectVersion(findByParameter(req, Integer.class, "id", dao.getVersionDao()::find) .orElse(new Version(-1, sessionSelection.project))); configureEditVersionForm(req, dao, sessionSelection); return ResponseType.HTML; } @RequestMapping(requestPath = "versions/commit", method = HttpMethod.POST) public ResponseType commitVersion(HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) throws IOException, SQLException { final var sessionSelection = new SessionSelection(req, dao); var version = new Version(-1, sessionSelection.project); try { version = new Version(getParameter(req, Integer.class, "id").orElseThrow(), sessionSelection.project); version.setName(getParameter(req, String.class, "name").orElseThrow()); getParameter(req, Integer.class, "ordinal").ifPresent(version::setOrdinal); version.setStatus(VersionStatus.valueOf(getParameter(req, String.class, "status").orElseThrow())); dao.getVersionDao().saveOrUpdate(version); // specifying the pid parameter will purposely reset the session selected version! setRedirectLocation(req, "./projects/view?pid="+sessionSelection.project.getId()); setContentPage(req, Constants.JSP_COMMIT_SUCCESSFUL); LOG.debug("Successfully updated version {} for project {}", version.getName(), sessionSelection.project.getName()); } catch (NoSuchElementException | IllegalArgumentException | SQLException ex) { // TODO: set request attribute with error text LOG.warn("Form validation failure: {}", ex.getMessage()); LOG.debug("Details:", ex); sessionSelection.selectVersion(version); configureEditVersionForm(req, dao, sessionSelection); } return ResponseType.HTML; } private void configureEditIssueForm(HttpServletRequest req, DataAccessObjects dao, SessionSelection selection) throws SQLException { req.setAttribute("projects", dao.getProjectDao().list()); req.setAttribute("issue", selection.issue); req.setAttribute("issueStatusEnum", IssueStatus.values()); req.setAttribute("issueCategoryEnum", IssueCategory.values()); req.setAttribute("users", dao.getUserDao().list()); setContentPage(req, "issue-form"); setBreadcrumbs(req, getBreadcrumbs(3, selection)); } @RequestMapping(requestPath = "issues/edit", method = HttpMethod.GET) public ResponseType editIssue(HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) throws IOException, SQLException { final var sessionSelection = new SessionSelection(req, dao); sessionSelection.selectIssue(findByParameter(req, Integer.class, "id", dao.getIssueDao()::find).orElse(new Issue(-1, sessionSelection.project))); configureEditIssueForm(req, dao, sessionSelection); return ResponseType.HTML; } @RequestMapping(requestPath = "issues/commit", method = HttpMethod.POST) public ResponseType commitIssue(HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) throws IOException, SQLException { final var sessionSelection = new SessionSelection(req, dao); Issue issue = new Issue(-1, sessionSelection.project); try { issue = new Issue(getParameter(req, Integer.class, "id").orElseThrow(), sessionSelection.project); getParameter(req, String.class, "category").map(IssueCategory::valueOf).ifPresent(issue::setCategory); getParameter(req, String.class, "status").map(IssueStatus::valueOf).ifPresent(issue::setStatus); issue.setSubject(getParameter(req, String.class, "subject").orElseThrow()); getParameter(req, Integer.class, "assignee").map( userid -> userid >= 0 ? new User(userid) : null ).ifPresent(issue::setAssignee); getParameter(req, String.class, "description").ifPresent(issue::setDescription); getParameter(req, Date.class, "eta").ifPresent(issue::setEta); dao.getIssueDao().saveOrUpdate(issue); // TODO: redirect to issue overview // specifying the issue parameter keeps the edited issue as breadcrumb setRedirectLocation(req, "./projects/view?issue="+issue.getId()); setContentPage(req, Constants.JSP_COMMIT_SUCCESSFUL); LOG.debug("Successfully updated issue {} for project {}", issue.getId(), sessionSelection.project.getName()); } catch (NoSuchElementException | IllegalArgumentException | SQLException ex) { // TODO: set request attribute with error text LOG.warn("Form validation failure: {}", ex.getMessage()); LOG.debug("Details:", ex); sessionSelection.selectIssue(issue); configureEditIssueForm(req, dao, sessionSelection); } return ResponseType.HTML; } }