Mon, 18 May 2020 21:06:38 +0200
adds utility function to find an entity by ID (reduces code duplication)
src/main/java/de/uapcore/lightpit/AbstractLightPITServlet.java | file | annotate | diff | comparison | revisions |
1.1 --- a/src/main/java/de/uapcore/lightpit/AbstractLightPITServlet.java Mon May 18 21:05:57 2020 +0200 1.2 +++ b/src/main/java/de/uapcore/lightpit/AbstractLightPITServlet.java Mon May 18 21:06:38 2020 +0200 1.3 @@ -45,6 +45,7 @@ 1.4 import java.sql.Connection; 1.5 import java.sql.SQLException; 1.6 import java.util.*; 1.7 +import java.util.function.Function; 1.8 1.9 /** 1.10 * A special implementation of a HTTPServlet which is focused on implementing 1.11 @@ -61,6 +62,25 @@ 1.12 */ 1.13 private LightPITModule.ELProxy moduleInfo = null; 1.14 1.15 + @FunctionalInterface 1.16 + protected interface SQLFindFunction<K, T> { 1.17 + T apply(K key) throws SQLException; 1.18 + 1.19 + default <V> SQLFindFunction<V, T> compose(Function<? super V, ? extends K> before) throws SQLException { 1.20 + Objects.requireNonNull(before); 1.21 + return (v) -> this.apply(before.apply(v)); 1.22 + } 1.23 + 1.24 + default <V> SQLFindFunction<K, V> andThen(Function<? super T, ? extends V> after) throws SQLException { 1.25 + Objects.requireNonNull(after); 1.26 + return (t) -> after.apply(this.apply(t)); 1.27 + } 1.28 + 1.29 + static <K> Function<K, K> identity() { 1.30 + return (t) -> t; 1.31 + } 1.32 + } 1.33 + 1.34 /** 1.35 * Invocation mapping gathered from the {@link RequestMapping} annotations. 1.36 * <p> 1.37 @@ -224,7 +244,7 @@ 1.38 * @param fragmentName the name of the fragment 1.39 * @see Constants#DYN_FRAGMENT_PATH_PREFIX 1.40 */ 1.41 - public void setDynamicFragment(HttpServletRequest req, String fragmentName) { 1.42 + protected void setDynamicFragment(HttpServletRequest req, String fragmentName) { 1.43 req.setAttribute(Constants.REQ_ATTR_FRAGMENT, Functions.dynFragmentPath(fragmentName)); 1.44 } 1.45 1.46 @@ -233,7 +253,7 @@ 1.47 * @param location the location where to redirect 1.48 * @see Constants#REQ_ATTR_REDIRECT_LOCATION 1.49 */ 1.50 - public void setRedirectLocation(HttpServletRequest req, String location) { 1.51 + protected void setRedirectLocation(HttpServletRequest req, String location) { 1.52 if (location.startsWith("./")) { 1.53 location = location.replaceFirst("\\./", Functions.baseHref(req)); 1.54 } 1.55 @@ -267,7 +287,7 @@ 1.56 * @param <T> the expected type 1.57 * @return the parameter value or an empty optional, if no parameter with the specified name was found 1.58 */ 1.59 - public<T> Optional<T> getParameter(HttpServletRequest req, Class<T> clazz, String name) { 1.60 + protected<T> Optional<T> getParameter(HttpServletRequest req, Class<T> clazz, String name) { 1.61 final String paramValue = req.getParameter(name); 1.62 if (paramValue == null) return Optional.empty(); 1.63 if (clazz.equals(String.class)) return Optional.of((T)paramValue); 1.64 @@ -280,6 +300,27 @@ 1.65 1.66 } 1.67 1.68 + /** 1.69 + * Tries to look up an entity with a key obtained from a request parameter. 1.70 + * 1.71 + * @param req the servlet request object 1.72 + * @param clazz the class representing the type of the request parameter 1.73 + * @param name the name of the request parameter 1.74 + * @param find the find function (typically a DAO function) 1.75 + * @param <T> the type of the request parameter 1.76 + * @param <R> the type of the looked up entity 1.77 + * @return the retrieved entity or an empty optional if there is no such entity or the request parameter was missing 1.78 + * @throws SQLException if the find function throws an exception 1.79 + */ 1.80 + protected<T,R> Optional<R> findByParameter(HttpServletRequest req, Class<T> clazz, String name, SQLFindFunction<? super T, ? extends R> find) throws SQLException { 1.81 + final var param = getParameter(req, clazz, name); 1.82 + if (param.isPresent()) { 1.83 + return Optional.ofNullable(find.apply(param.get())); 1.84 + } else { 1.85 + return Optional.empty(); 1.86 + } 1.87 + } 1.88 + 1.89 private void forwardToFullView(HttpServletRequest req, HttpServletResponse resp) 1.90 throws IOException, ServletException { 1.91