changeset 131:4e181a4aaab0

Refactored the entity repositories. * Generic repository is now abstract. * Concrete repositories implement all abstract methods. * Services are using the generic repository.
author Raimund Renkert <rrenkert@intevation.de>
date Thu, 13 Jun 2013 16:17:11 +0200
parents 7ea3ea59e971
children 07e8324c04a4
files src/main/java/de/intevation/lada/data/LKommentarPRepository.java src/main/java/de/intevation/lada/data/LMessungRepository.java src/main/java/de/intevation/lada/data/LOrtRepository.java src/main/java/de/intevation/lada/data/LProbeRepository.java src/main/java/de/intevation/lada/data/Repository.java src/main/java/de/intevation/lada/rest/LKommentarService.java src/main/java/de/intevation/lada/rest/LMessungService.java src/main/java/de/intevation/lada/rest/LOrtService.java src/main/java/de/intevation/lada/rest/LProbeService.java
diffstat 9 files changed, 159 insertions(+), 134 deletions(-) [+]
line wrap: on
line diff
--- a/src/main/java/de/intevation/lada/data/LKommentarPRepository.java	Thu Jun 13 16:11:30 2013 +0200
+++ b/src/main/java/de/intevation/lada/data/LKommentarPRepository.java	Thu Jun 13 16:17:11 2013 +0200
@@ -2,11 +2,10 @@
 
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Map;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
-import javax.ejb.TransactionAttribute;
-import javax.ejb.TransactionAttributeType;
 import javax.inject.Inject;
 import javax.inject.Named;
 import javax.persistence.EntityExistsException;
@@ -37,20 +36,30 @@
     @Inject
     private Logger logger;
 
-    public Response filter(String probeId) {
-        if (probeId.isEmpty()) {
-            return new Response(false, 600, new ArrayList<LKommentarP>(0));
+    public Response filter(Map<String, String> filter) {
+        if (filter.isEmpty()) {
+            return findAll(LKommentarP.class);
         }
         CriteriaBuilder cb = em.getCriteriaBuilder();
         CriteriaQuery<LKommentarP> criteria = cb.createQuery(LKommentarP.class);
         Root<LKommentarP> member = criteria.from(LKommentarP.class);
-        criteria.where(cb.equal(member.get("probeId"), probeId));
+        if (filter.containsKey("probe")) {
+            criteria.where(
+                cb.equal(member.get("probeId"), filter.get("probe")));
+        }
+        else {
+            return new Response(false, 600, new ArrayList<LKommentarP>());
+        }
 
-        List<LKommentarP> result = em.createQuery(criteria).getResultList();
+        List<LKommentarP> result = filter(criteria);
         return new Response(true, 200, result);
     }
 
-    public Response create(LKommentarP kommentar) {
+    public Response create(Object object) {
+        if (!(object instanceof LKommentarP)) {
+            return new Response(false, 602, object);
+        }
+        LKommentarP kommentar = (LKommentarP)object;
         try {
             manager.create(kommentar);
             return new Response(true, 200, kommentar);
@@ -66,4 +75,9 @@
             return new Response(false, 603, kommentar);
         }
     }
+
+    @Override
+    public Response update(Object object) {
+        return new Response(false, 698, new ArrayList<LKommentarP>());
+    }
 }
--- a/src/main/java/de/intevation/lada/data/LMessungRepository.java	Thu Jun 13 16:11:30 2013 +0200
+++ b/src/main/java/de/intevation/lada/data/LMessungRepository.java	Thu Jun 13 16:17:11 2013 +0200
@@ -1,5 +1,6 @@
 package de.intevation.lada.data;
 
+import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
 
@@ -55,33 +56,35 @@
      * @param begin probeentnahmebegin
      * @return
      */
-    public Response filter(String probeId) {
+    public Response filter(Map<String, String> filter) {
+        if (filter.isEmpty()) {
+            return findAll(LMessung.class);
+        }
         CriteriaBuilder cb = em.getCriteriaBuilder();
         CriteriaQuery<LMessung> criteria = cb.createQuery(LMessung.class);
         Root<LMessung> member = criteria.from(LMessung.class);
-        Predicate probe = cb.equal(member.get("LProbeId"), probeId);
-        criteria.where(probe);
+        if (filter.containsKey("probe")) {
+            criteria.where(
+                cb.equal(member.get("LProbeId"), filter.get("probe")));
+        }
+        else {
+            return new Response(false, 600, new ArrayList<LMessung>());
+        }
         List<LMessung> result = filter(criteria);
         return new Response(true, 200, result);
     }
 
     /**
-     * Filter LProbe object list by the given criteria.
-     *
-     * @param criteria
-     * @return List of LProbe objects.
-     */
-    public List<LMessung> filter(CriteriaQuery<LMessung> criteria) {
-        return em.createQuery(criteria).getResultList();
-    }
-
-    /**
      * Validate and persist a new LProbe object.
      *
      * @param probe The new LProbe object
      * @return Response.
      */
-    public Response create(LMessung messung) {
+    public Response create(Object object) {
+        if (!(object instanceof LMessung)) {
+            return new Response(false, 602, object);
+        }
+        LMessung messung = (LMessung)object;
         Response response = new Response(true, 200, messung);
         // Try to save the new LMessung.
         try {
@@ -115,7 +118,11 @@
         return response;
     }
 
-    public Response update(LMessung messung) {
+    public Response update(Object object) {
+        if (!(object instanceof LMessung)) {
+            return new Response(false, 602, object);
+        }
+        LMessung messung = (LMessung)object;
         Response response = new Response(true, 200, messung);
         // Try to save the new LProbe.
         try {
--- a/src/main/java/de/intevation/lada/data/LOrtRepository.java	Thu Jun 13 16:11:30 2013 +0200
+++ b/src/main/java/de/intevation/lada/data/LOrtRepository.java	Thu Jun 13 16:17:11 2013 +0200
@@ -1,5 +1,6 @@
 package de.intevation.lada.data;
 
+import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
 
@@ -20,7 +21,7 @@
 import de.intevation.lada.validation.ValidationException;
 import de.intevation.lada.validation.Validator;
 
-
+@Named("lortrepository")
 public class LOrtRepository
 extends Repository
 {
@@ -43,25 +44,24 @@
      *
      * @return Response object containing LOrt objects.
      */
-    public Response filter(String probeId) {
+    public Response filter(Map<String, String> filter) {
+        if (filter.isEmpty()) {
+            return findAll(LOrt.class);
+        }
         CriteriaBuilder cb = em.getCriteriaBuilder();
         CriteriaQuery<LOrt> criteria = cb.createQuery(LOrt.class);
         Root<LOrt> member = criteria.from(LOrt.class);
-        Predicate pid = cb.equal(member.get("probeId"), probeId);
-        criteria.where(pid);
+        if (filter.containsKey("probe")) {
+            criteria.where(
+                cb.equal(member.get("probeId"), filter.get("probe")));
+        }
+        else {
+            return new Response(false, 600, new ArrayList<LOrt>());
+        }
         List<LOrt> result = filter(criteria);
 
         return new Response(true, 200, result);
     }
-    /**
-     * Filter LProbe object list by the given criteria.
-     *
-     * @param criteria
-     * @return List of LProbe objects.
-     */
-    public List<LOrt> filter(CriteriaQuery<LOrt> criteria) {
-        return em.createQuery(criteria).getResultList();
-    }
 
     /**
      * Validate and persist a new LProbe object.
@@ -69,7 +69,11 @@
      * @param probe The new LProbe object
      * @return Response.
      */
-    public Response create(LOrt ort) {
+    public Response create(Object object) {
+        if (!(object instanceof LOrt)) {
+            return new Response(false, 600, object);
+        }
+        LOrt ort = (LOrt)object;
         Response response = new Response(true, 200, ort);
         // Try to save the new LOrt.
         try {
@@ -103,7 +107,11 @@
         return response;
     }
 
-    public Response update(LOrt ort) {
+    public Response update(Object object) {
+        if (!(object instanceof LOrt)) {
+            return new Response(false, 600, object);
+        }
+        LOrt ort = (LOrt)object;
         Response response = new Response(true, 200, ort);
         // Try to update a LOrt object.
         try {
--- a/src/main/java/de/intevation/lada/data/LProbeRepository.java	Thu Jun 13 16:11:30 2013 +0200
+++ b/src/main/java/de/intevation/lada/data/LProbeRepository.java	Thu Jun 13 16:17:11 2013 +0200
@@ -1,6 +1,6 @@
 package de.intevation.lada.data;
 
-import java.util.Date;
+import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
 
@@ -18,6 +18,7 @@
 
 import de.intevation.lada.manage.LProbeManager;
 import de.intevation.lada.model.LProbe;
+import de.intevation.lada.model.LProbeInfo;
 import de.intevation.lada.rest.Response;
 import de.intevation.lada.validation.ValidationException;
 import de.intevation.lada.validation.Validator;
@@ -29,6 +30,7 @@
  * @author <a href="mailto:rrenkert@intevation.de">Raimund Renkert</a>
  */
 @ApplicationScoped
+@Named("lproberepository")
 public class LProbeRepository extends Repository{
 
     /**
@@ -55,59 +57,43 @@
      * @param begin probeentnahmebegin
      * @return
      */
-    public Response filter(String mstId, String uwbId, Long begin) {
+    public Response filter(Map<String, String> filter) {
         CriteriaBuilder cb = em.getCriteriaBuilder();
-        CriteriaQuery<LProbe> criteria = cb.createQuery(LProbe.class);
-        Root<LProbe> member = criteria.from(LProbe.class);
-        Predicate mst = cb.equal(member.get("mstId"), mstId);
-        Predicate uwb = cb.equal(member.get("umwId"), uwbId);
-
-        if (!mstId.isEmpty() && !uwbId.isEmpty() && begin != null) {
-            Predicate beg = cb.equal(member.get("probeentnahmeBeginn"), new Date(begin));
-            criteria.where(cb.and(mst, uwb, beg));
-        }
-        else if (!mstId.isEmpty() && !uwbId.isEmpty() && begin == null) {
-            criteria.where(cb.and(mst, uwb));
-        }
-        else if (!mstId.isEmpty() && uwbId.isEmpty() && begin != null) {
-            Predicate beg = cb.equal(member.get("probeentnahmeBeginn"), new Date(begin));
-            criteria.where(cb.and(mst, beg));
-        }
-        else if (mstId.isEmpty() && !uwbId.isEmpty() && begin != null) {
-            Predicate beg = cb.equal(member.get("probeentnahmeBeginn"), new Date(begin));
-            criteria.where(cb.and(uwb, beg));
+        CriteriaQuery<LProbeInfo> criteria = cb.createQuery(LProbeInfo.class);
+        Root<LProbeInfo> member = criteria.from(LProbeInfo.class);
+        List<Predicate> andFilter = new ArrayList<Predicate>();
+        if (filter.containsKey("mst")) {
+            andFilter.add(cb.equal(member.get("mstId"), filter.get("mst")));
         }
-        else if (!mstId.isEmpty() && uwbId.isEmpty() && begin == null) {
-            criteria.where(mst);
-        }
-        else if (mstId.isEmpty() && !uwbId.isEmpty() && begin == null) {
-            criteria.where(uwb);
-        }
-        else if (mstId.isEmpty() && uwbId.isEmpty() && begin != null) {
-            Predicate beg = cb.equal(member.get("probeentnahmeBeginn"), new Date(begin));
-            criteria.where(beg);
+        if (filter.containsKey("uwb")) {
+            andFilter.add(cb.equal(member.get("umwId"), filter.get("uwb")));
         }
-        List<LProbe> result = filter(criteria);
+        if (filter.containsKey("begin")) {
+            try {
+                Long date = Long.getLong(filter.get("begin"));
+                andFilter.add(
+                    cb.equal(member.get("probeentnahmeBeginn"), date));
+            }
+            catch(NumberFormatException nfe) {
+                //ignore filter parameter.
+            }
+        }
+        criteria.where(andFilter.toArray(new Predicate[andFilter.size()]));
+        List<LProbeInfo> result = filter(criteria);
         return new Response(true, 200, result);
     }
 
     /**
-     * Filter LProbe object list by the given criteria.
-     *
-     * @param criteria
-     * @return List of LProbe objects.
-     */
-    public List<LProbe> filter(CriteriaQuery<LProbe> criteria) {
-        return em.createQuery(criteria).getResultList();
-    }
-
-    /**
      * Validate and persist a new LProbe object.
      *
      * @param probe The new LProbe object
      * @return Response.
      */
-    public Response create(LProbe probe) {
+    public Response create(Object object) {
+        if (!(object instanceof LProbe)) {
+            return new Response(false, 602, object);
+        }
+        LProbe probe = (LProbe)object;
         Response response = new Response(true, 200, probe);
         // Try to save the new LProbe.
         try {
@@ -141,7 +127,11 @@
         return response;
     }
 
-    public Response update(LProbe probe) {
+    public Response update(Object object) {
+        if (!(object instanceof LProbe)) {
+            return new Response(false, 602, object);
+        }
+        LProbe probe = (LProbe)object;
         Response response = new Response(true, 200, probe);
         // Try to save the new LProbe.
         try {
--- a/src/main/java/de/intevation/lada/data/Repository.java	Thu Jun 13 16:11:30 2013 +0200
+++ b/src/main/java/de/intevation/lada/data/Repository.java	Thu Jun 13 16:17:11 2013 +0200
@@ -22,7 +22,7 @@
  */
 @Named
 @ApplicationScoped
-public class Repository
+public abstract class Repository
 {
     /**
      * The entitymanager managing the data.
@@ -30,13 +30,11 @@
     @Inject
     private EntityManager em;
 
-    /**
-     * Errors/Warnings occured in repository operations.
-     */
-    private boolean success;
-    private int generalError;
-    private Map<String, Integer> errors;
-    private Map<String, Integer> warnings;
+    public abstract Response create(Object object);
+
+    public abstract Response update(Object object);
+
+    public abstract Response filter(Map<String, String> keys);
 
     /**
      * Get all objects of type <link>clazz</link>from database.
@@ -68,6 +66,16 @@
         return new Response(true, 200, item);
     }
 
+    /**
+     * Filter object list by the given criteria.
+     *
+     * @param criteria
+     * @return List of LProbe objects.
+     */
+    public <T> List<T> filter(CriteriaQuery<T> criteria) {
+        return em.createQuery(criteria).getResultList();
+    }
+
     public CriteriaBuilder getCriteriaBuilder() {
         return em.getCriteriaBuilder();
     }
--- a/src/main/java/de/intevation/lada/rest/LKommentarService.java	Thu Jun 13 16:11:30 2013 +0200
+++ b/src/main/java/de/intevation/lada/rest/LKommentarService.java	Thu Jun 13 16:17:11 2013 +0200
@@ -1,17 +1,16 @@
 package de.intevation.lada.rest;
 
-import java.util.ArrayList;
-import java.util.List;
-import java.util.logging.Level;
+import java.util.HashMap;
+import java.util.Map;
 import java.util.logging.Logger;
 
 import javax.enterprise.context.RequestScoped;
 import javax.inject.Inject;
 import javax.inject.Named;
 import javax.ws.rs.Consumes;
+import javax.ws.rs.DELETE;
 import javax.ws.rs.GET;
 import javax.ws.rs.POST;
-import javax.ws.rs.DELETE;
 import javax.ws.rs.PUT;
 import javax.ws.rs.Path;
 import javax.ws.rs.PathParam;
@@ -20,7 +19,7 @@
 import javax.ws.rs.core.MultivaluedMap;
 import javax.ws.rs.core.UriInfo;
 
-import de.intevation.lada.data.LKommentarPRepository;
+import de.intevation.lada.data.Repository;
 import de.intevation.lada.model.LKommentarP;
 
 /**
@@ -35,8 +34,9 @@
     /**
      * The Repository.
      */
-    @Inject @Named("lkommentarRepository")
-    private LKommentarPRepository repository;
+    @Inject
+    @Named("lkommentarRepository")
+    private Repository repository;
 
     /**
      * The logger for this class
@@ -97,13 +97,14 @@
     @Produces("text/json")
     public Response filter(@Context UriInfo info) {
         MultivaluedMap<String, String> params = info.getQueryParameters();
-        if (params.containsKey("probe")) {
-            String probe = params.getFirst("probe");
-            return repository.filter(probe);
+        if (params.isEmpty()) {
+            repository.findAll(LKommentarP.class);
         }
-        else {
-            return repository.findAll(LKommentarP.class);
+        Map<String, String> filter = new HashMap<String, String>();
+        for (String key: params.keySet()) {
+            filter.put(key, params.getFirst(key));
         }
+        return repository.filter(filter);
     }
 
     @POST
--- a/src/main/java/de/intevation/lada/rest/LMessungService.java	Thu Jun 13 16:11:30 2013 +0200
+++ b/src/main/java/de/intevation/lada/rest/LMessungService.java	Thu Jun 13 16:17:11 2013 +0200
@@ -1,5 +1,8 @@
 package de.intevation.lada.rest;
 
+import java.util.HashMap;
+import java.util.Map;
+
 import javax.enterprise.context.RequestScoped;
 import javax.inject.Inject;
 import javax.ws.rs.Consumes;
@@ -59,13 +62,13 @@
     public Response filter(@Context UriInfo info) {
         MultivaluedMap<String, String> params = info.getQueryParameters();
         if (params.isEmpty()) {
-            return repository.findAll(LMessung.class);
+            repository.findAll(LMessung.class);
         }
-        String probeId = "";
-        if (params.containsKey("probe")) {
-            probeId = params.getFirst("probe");
+        Map<String, String> filter = new HashMap<String, String>();
+        for (String key: params.keySet()) {
+            filter.put(key, params.getFirst(key));
         }
-        return repository.filter(probeId);
+        return repository.filter(filter);
     }
 
     @PUT
--- a/src/main/java/de/intevation/lada/rest/LOrtService.java	Thu Jun 13 16:11:30 2013 +0200
+++ b/src/main/java/de/intevation/lada/rest/LOrtService.java	Thu Jun 13 16:17:11 2013 +0200
@@ -1,7 +1,11 @@
 package de.intevation.lada.rest;
 
+import java.util.HashMap;
+import java.util.Map;
+
 import javax.enterprise.context.RequestScoped;
 import javax.inject.Inject;
+import javax.inject.Named;
 import javax.ws.rs.Consumes;
 import javax.ws.rs.GET;
 import javax.ws.rs.POST;
@@ -13,7 +17,7 @@
 import javax.ws.rs.core.MultivaluedMap;
 import javax.ws.rs.core.UriInfo;
 
-import de.intevation.lada.data.LOrtRepository;
+import de.intevation.lada.data.Repository;
 import de.intevation.lada.model.LOrt;
 
 @Path("ort")
@@ -25,7 +29,8 @@
      * The repository for LOrt.
      */
     @Inject
-    private LOrtRepository repository;
+    @Named("lortrepository")
+    private Repository repository;
 
     @GET
     @Path("/{id}")
@@ -39,13 +44,13 @@
     public Response filter(@Context UriInfo info) {
         MultivaluedMap<String, String> params = info.getQueryParameters();
         if (params.isEmpty()) {
-            return repository.findAll(LOrt.class);
+            repository.findAll(LOrt.class);
         }
-        String probeId = "";
-        if (params.containsKey("probeid")) {
-            probeId = params.getFirst("probeid");
+        Map<String, String> filter = new HashMap<String, String>();
+        for (String key: params.keySet()) {
+            filter.put(key, params.getFirst(key));
         }
-        return repository.filter(probeId);
+        return repository.filter(filter);
     }
 
     @PUT
--- a/src/main/java/de/intevation/lada/rest/LProbeService.java	Thu Jun 13 16:11:30 2013 +0200
+++ b/src/main/java/de/intevation/lada/rest/LProbeService.java	Thu Jun 13 16:17:11 2013 +0200
@@ -1,11 +1,12 @@
 package de.intevation.lada.rest;
 
-import java.util.List;
+import java.util.HashMap;
 import java.util.Map;
 import java.util.logging.Logger;
 
 import javax.enterprise.context.RequestScoped;
 import javax.inject.Inject;
+import javax.inject.Named;
 import javax.ws.rs.Consumes;
 import javax.ws.rs.GET;
 import javax.ws.rs.POST;
@@ -17,7 +18,7 @@
 import javax.ws.rs.core.MultivaluedMap;
 import javax.ws.rs.core.UriInfo;
 
-import de.intevation.lada.data.LProbeRepository;
+import de.intevation.lada.data.Repository;
 import de.intevation.lada.model.LProbe;
 
 /**
@@ -33,7 +34,8 @@
      * The Repository for LProbe.
      */
     @Inject
-    private LProbeRepository repository;
+    @Named("lproberepository")
+    private Repository repository;
 
     /**
      * The logger for this class.
@@ -73,25 +75,12 @@
         if (params.isEmpty()) {
             return repository.findAll(LProbe.class);
         }
-        String mstId = "";
-        String uwbId = "";
-        Long begin = null;
-        if (params.containsKey("mst")) {
-            mstId = params.getFirst("mst");
-        }
-        if (params.containsKey("uwb")) {
-            uwbId = params.getFirst("uwb");
+        Map<String, String> filter = new HashMap<String, String>();
+        for (String key: params.keySet()) {
+            filter.put(key, params.getFirst(key));
         }
-        if (params.containsKey("begin")) {
-            String tmp = params.getFirst("begin");
-            try {
-                begin = Long.valueOf(tmp);
-            }
-            catch (NumberFormatException nfe) {
-                begin = null;
-            }
-        }
-        return repository.filter(mstId, uwbId, begin);
+
+        return repository.filter(filter);
     }
 
     @PUT
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)