37 import javax.servlet.annotation.WebServlet; |
37 import javax.servlet.annotation.WebServlet; |
38 import javax.servlet.http.HttpServletRequest; |
38 import javax.servlet.http.HttpServletRequest; |
39 import java.sql.SQLException; |
39 import java.sql.SQLException; |
40 import java.util.Optional; |
40 import java.util.Optional; |
41 |
41 |
|
42 import static de.uapcore.lightpit.Functions.fqn; |
|
43 |
42 @LightPITModule( |
44 @LightPITModule( |
43 bundleBaseName = "localization.projects", |
45 bundleBaseName = "localization.projects", |
44 modulePath = "projects", |
46 modulePath = "projects", |
45 defaultPriority = 20 |
47 defaultPriority = 20 |
46 ) |
48 ) |
48 name = "ProjectsModule", |
50 name = "ProjectsModule", |
49 urlPatterns = "/projects/*" |
51 urlPatterns = "/projects/*" |
50 ) |
52 ) |
51 public final class ProjectsModule extends AbstractLightPITServlet { |
53 public final class ProjectsModule extends AbstractLightPITServlet { |
52 |
54 |
|
55 public static final String SESSION_ATTR_SELECTED_PROJECT = fqn(ProjectsModule.class, "selected-project"); |
|
56 |
53 @RequestMapping(method = HttpMethod.GET) |
57 @RequestMapping(method = HttpMethod.GET) |
54 public ResponseType index(HttpServletRequest req, DataAccessObjects dao) throws SQLException { |
58 public ResponseType index(HttpServletRequest req, DataAccessObjects dao) throws SQLException { |
55 req.setAttribute("projects", dao.getProjectDao().list()); |
59 final var projectList = dao.getProjectDao().list(); |
|
60 req.setAttribute("projects", projectList); |
56 setDynamicFragment(req, "projects"); |
61 setDynamicFragment(req, "projects"); |
|
62 setStylesheet(req, "projects"); |
|
63 |
|
64 final var session = req.getSession(); |
|
65 final var projectSelection = getParameter(req, Integer.class, "select"); |
|
66 if (projectSelection.isPresent()) { |
|
67 final var selectedId = projectSelection.get(); |
|
68 for (var proj : projectList) { |
|
69 if (proj.getId() == selectedId) { |
|
70 session.setAttribute(SESSION_ATTR_SELECTED_PROJECT, proj); |
|
71 break; |
|
72 } |
|
73 } |
|
74 } else { |
|
75 final var selectedProject = session.getAttribute(SESSION_ATTR_SELECTED_PROJECT); |
|
76 if (selectedProject == null) { |
|
77 projectList.stream().findFirst().ifPresent(proj -> session.setAttribute(SESSION_ATTR_SELECTED_PROJECT, proj)); |
|
78 } |
|
79 } |
57 |
80 |
58 return ResponseType.HTML; |
81 return ResponseType.HTML; |
59 } |
82 } |
60 |
83 |
61 @RequestMapping(requestPath = "edit", method = HttpMethod.GET) |
84 @RequestMapping(requestPath = "edit", method = HttpMethod.GET) |