adds utility function to find an entity by ID (reduces code duplication)

Mon, 18 May 2020 21:06:38 +0200

author
Mike Becker <universe@uap-core.de>
date
Mon, 18 May 2020 21:06:38 +0200
changeset 63
51aa5e267c7f
parent 62
833e0385572a
child 64
0f1746c6abfb

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
--- 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<K, T> {
+        T apply(K key) throws SQLException;
+
+        default <V> SQLFindFunction<V, T> compose(Function<? super V, ? extends K> before) throws SQLException {
+            Objects.requireNonNull(before);
+            return (v) -> this.apply(before.apply(v));
+        }
+
+        default <V> SQLFindFunction<K, V> andThen(Function<? super T, ? extends V> after) throws SQLException {
+            Objects.requireNonNull(after);
+            return (t) -> after.apply(this.apply(t));
+        }
+
+        static <K> Function<K, K> identity() {
+            return (t) -> t;
+        }
+    }
+
     /**
      * Invocation mapping gathered from the {@link RequestMapping} annotations.
      * <p>
@@ -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 <T> the expected type
      * @return the parameter value or an empty optional, if no parameter with the specified name was found
      */
-    public<T> Optional<T> getParameter(HttpServletRequest req, Class<T> clazz, String name) {
+    protected<T> Optional<T> getParameter(HttpServletRequest req, Class<T> 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 <T> the type of the request parameter
+     * @param <R> 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<T,R> Optional<R> findByParameter(HttpServletRequest req, Class<T> clazz, String name, SQLFindFunction<? super T, ? extends R> 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 {
 

mercurial