src/main/java/de/uapcore/lightpit/AbstractLightPITServlet.java

changeset 167
3f30adba1c63
parent 163
a5b9632729b6
child 168
1c3694ae224c
--- a/src/main/java/de/uapcore/lightpit/AbstractLightPITServlet.java	Sun Dec 20 11:06:25 2020 +0100
+++ b/src/main/java/de/uapcore/lightpit/AbstractLightPITServlet.java	Mon Dec 21 18:29:34 2020 +0100
@@ -28,8 +28,8 @@
  */
 package de.uapcore.lightpit;
 
-import de.uapcore.lightpit.dao.DaoProvider;
-import de.uapcore.lightpit.dao.postgres.PGDaoProvider;
+import de.uapcore.lightpit.dao.DataAccessObject;
+import de.uapcore.lightpit.dao.PostgresDataAccessObject;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -56,26 +56,6 @@
 
     private static final String SITE_JSP = jspPath("site");
 
-
-    @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>
@@ -101,15 +81,15 @@
      * @param connection the SQL connection
      * @return a set of data access objects
      */
-    private DaoProvider createDataAccessObjects(Connection connection) throws SQLException {
+    private DataAccessObject createDataAccessObjects(Connection connection) {
         final var df = (DataSourceProvider) getServletContext().getAttribute(DataSourceProvider.Companion.getSC_ATTR_NAME());
         if (df.getDialect() == DataSourceProvider.Dialect.Postgres) {
-            return new PGDaoProvider(connection);
+            return new PostgresDataAccessObject(connection);
         }
         throw new UnsupportedOperationException("Non-exhaustive if-else - this is a bug.");
     }
 
-    private void invokeMapping(Map.Entry<PathPattern, Method> mapping, HttpServletRequest req, HttpServletResponse resp, DaoProvider dao) throws IOException {
+    private void invokeMapping(Map.Entry<PathPattern, Method> mapping, HttpServletRequest req, HttpServletResponse resp, DataAccessObject dao) throws IOException {
         final var pathPattern = mapping.getKey();
         final var method = mapping.getValue();
         try {
@@ -122,7 +102,7 @@
                 } else if (paramTypes[i].isAssignableFrom(HttpServletResponse.class)) {
                     paramValues[i] = resp;
                 }
-                if (paramTypes[i].isAssignableFrom(DaoProvider.class)) {
+                if (paramTypes[i].isAssignableFrom(DataAccessObject.class)) {
                     paramValues[i] = dao;
                 }
                 if (paramTypes[i].isAssignableFrom(PathParameters.class)) {
@@ -179,9 +159,9 @@
                     boolean paramsInjectible = true;
                     for (var param : method.getParameterTypes()) {
                         paramsInjectible &= HttpServletRequest.class.isAssignableFrom(param)
-                                || HttpServletResponse.class.isAssignableFrom(param)
-                                || PathParameters.class.isAssignableFrom(param)
-                                || DaoProvider.class.isAssignableFrom(param);
+                                            || HttpServletResponse.class.isAssignableFrom(param)
+                                            || PathParameters.class.isAssignableFrom(param)
+                                            || DataAccessObject.class.isAssignableFrom(param);
                     }
                     if (paramsInjectible) {
                         try {
@@ -360,7 +340,7 @@
      * @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 {
+    protected <T, R> Optional<R> findByParameter(HttpServletRequest req, Class<T> clazz, String name, Function<? super T, ? extends R> find) {
         final var param = getParameter(req, clazz, name);
         if (param.isPresent()) {
             return Optional.ofNullable(find.apply(param.get()));

mercurial