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