# HG changeset patch # User Mike Becker # Date 1589828798 -7200 # Node ID 51aa5e267c7f36b6d629e06f31f5ff936d6bfd8a # Parent 833e0385572aa2679da91adc43ffec4bf96bc146 adds utility function to find an entity by ID (reduces code duplication) diff -r 833e0385572a -r 51aa5e267c7f src/main/java/de/uapcore/lightpit/AbstractLightPITServlet.java --- a/src/main/java/de/uapcore/lightpit/AbstractLightPITServlet.java Mon May 18 21:05:57 2020 +0200 +++ b/src/main/java/de/uapcore/lightpit/AbstractLightPITServlet.java Mon May 18 21:06:38 2020 +0200 @@ -45,6 +45,7 @@ import java.sql.Connection; import java.sql.SQLException; import java.util.*; +import java.util.function.Function; /** * A special implementation of a HTTPServlet which is focused on implementing @@ -61,6 +62,25 @@ */ private LightPITModule.ELProxy moduleInfo = null; + @FunctionalInterface + protected interface SQLFindFunction { + T apply(K key) throws SQLException; + + default SQLFindFunction compose(Function before) throws SQLException { + Objects.requireNonNull(before); + return (v) -> this.apply(before.apply(v)); + } + + default SQLFindFunction andThen(Function after) throws SQLException { + Objects.requireNonNull(after); + return (t) -> after.apply(this.apply(t)); + } + + static Function identity() { + return (t) -> t; + } + } + /** * Invocation mapping gathered from the {@link RequestMapping} annotations. *

@@ -224,7 +244,7 @@ * @param fragmentName the name of the fragment * @see Constants#DYN_FRAGMENT_PATH_PREFIX */ - public void setDynamicFragment(HttpServletRequest req, String fragmentName) { + protected void setDynamicFragment(HttpServletRequest req, String fragmentName) { req.setAttribute(Constants.REQ_ATTR_FRAGMENT, Functions.dynFragmentPath(fragmentName)); } @@ -233,7 +253,7 @@ * @param location the location where to redirect * @see Constants#REQ_ATTR_REDIRECT_LOCATION */ - public void setRedirectLocation(HttpServletRequest req, String location) { + protected void setRedirectLocation(HttpServletRequest req, String location) { if (location.startsWith("./")) { location = location.replaceFirst("\\./", Functions.baseHref(req)); } @@ -267,7 +287,7 @@ * @param the expected type * @return the parameter value or an empty optional, if no parameter with the specified name was found */ - public Optional getParameter(HttpServletRequest req, Class clazz, String name) { + protected Optional getParameter(HttpServletRequest req, Class clazz, String name) { final String paramValue = req.getParameter(name); if (paramValue == null) return Optional.empty(); if (clazz.equals(String.class)) return Optional.of((T)paramValue); @@ -280,6 +300,27 @@ } + /** + * Tries to look up an entity with a key obtained from a request parameter. + * + * @param req the servlet request object + * @param clazz the class representing the type of the request parameter + * @param name the name of the request parameter + * @param find the find function (typically a DAO function) + * @param the type of the request parameter + * @param the type of the looked up entity + * @return the retrieved entity or an empty optional if there is no such entity or the request parameter was missing + * @throws SQLException if the find function throws an exception + */ + protected Optional findByParameter(HttpServletRequest req, Class clazz, String name, SQLFindFunction find) throws SQLException { + final var param = getParameter(req, clazz, name); + if (param.isPresent()) { + return Optional.ofNullable(find.apply(param.get())); + } else { + return Optional.empty(); + } + } + private void forwardToFullView(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {