changeset 35:f425150ea2b5

New filter option in LProbe GET request.
author Raimund Renkert <rrenkert@intevation.de>
date Thu, 23 May 2013 14:12:27 +0200
parents fa44e69fc2e2
children 8155d2ed5cb8
files src/main/java/de/intevation/lada/data/LProbeRepository.java src/main/java/de/intevation/lada/rest/LProbeRESTService.java
diffstat 2 files changed, 69 insertions(+), 27 deletions(-) [+]
line wrap: on
line diff
--- a/src/main/java/de/intevation/lada/data/LProbeRepository.java	Thu May 23 14:10:26 2013 +0200
+++ b/src/main/java/de/intevation/lada/data/LProbeRepository.java	Thu May 23 14:12:27 2013 +0200
@@ -16,6 +16,7 @@
  */
 package de.intevation.lada.data;
 
+import java.util.Date;
 import java.util.List;
 
 import javax.enterprise.context.ApplicationScoped;
@@ -25,6 +26,7 @@
 import javax.persistence.PersistenceContextType;
 import javax.persistence.criteria.CriteriaBuilder;
 import javax.persistence.criteria.CriteriaQuery;
+import javax.persistence.criteria.Predicate;
 import javax.persistence.criteria.Root;
 
 import de.intevation.lada.model.LProbe;
@@ -54,25 +56,45 @@
         }
     }
 
-    //public LProbe findByEmail(String email) {
-    //    CriteriaBuilder cb = em.getCriteriaBuilder();
-    //    CriteriaQuery<LProbe> criteria = cb.createQuery(LProbe.class);
-    //    Root<LProbe> member = criteria.from(LProbe.class);
-    //    // Swap criteria statements if you would like to try out type-safe criteria queries, a new
-    //    // feature in JPA 2.0
-    //    // criteria.select(member).where(cb.equal(member.get(LProbe_.name), email));
-    //    criteria.select(member).where(cb.equal(member.get("email"), email));
-    //    return em.createQuery(criteria).getSingleResult();
-    //}
+    public List<LProbe> filter(String mstId, String uwbId, Long begin) {
+        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));
+        }
+        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);
+        }
+        return em.createQuery(criteria).getResultList();
+    }
 
     public List<LProbe> findAll() {
         CriteriaBuilder cb = em.getCriteriaBuilder();
         CriteriaQuery<LProbe> criteria = cb.createQuery(LProbe.class);
         Root<LProbe> member = criteria.from(LProbe.class);
-        // Swap criteria statements if you would like to try out type-safe criteria queries, a new
-        // feature in JPA 2.0
-        // criteria.select(member).orderBy(cb.asc(member.get(LProbe_.name)));
-        //criteria.select(member).orderBy(cb.asc(member.get("name")));
         criteria.select(member);
         return em.createQuery(criteria).getResultList();
     }
--- a/src/main/java/de/intevation/lada/rest/LProbeRESTService.java	Thu May 23 14:10:26 2013 +0200
+++ b/src/main/java/de/intevation/lada/rest/LProbeRESTService.java	Thu May 23 14:12:27 2013 +0200
@@ -5,9 +5,15 @@
 
 import javax.enterprise.context.RequestScoped;
 import javax.inject.Inject;
+import javax.ws.rs.DefaultValue;
 import javax.ws.rs.GET;
 import javax.ws.rs.Path;
+import javax.ws.rs.PathParam;
 import javax.ws.rs.Produces;
+import javax.ws.rs.QueryParam;
+import javax.ws.rs.core.Context;
+import javax.ws.rs.core.MultivaluedMap;
+import javax.ws.rs.core.UriInfo;
 
 import de.intevation.lada.data.LProbeRepository;
 import de.intevation.lada.model.LProbe;
@@ -30,17 +36,10 @@
     private Logger log;
 
     @GET
-    @Produces("text/json")
-    public List<LProbe> listAllMembers() {
-       final List<LProbe> result = repository.findAll();
-       return result;
-    }
-
-    @GET
     @Path("/{id:[0-9][0-9]*}")
     @Produces("text/plain")
-    public String loadById() {
-       return "Eine Probe!";
+    public LProbe loadById(@PathParam("id") long id) {
+       return repository.findById(id);
     }
 
     @GET
@@ -54,10 +53,31 @@
     }
 
     @GET
-    @Path("/new")
-    @Produces("text/plain")
-    public String create() {
-       return "Neu";
+    @Produces("text/json")
+    public List<LProbe> filter(@Context UriInfo info) {
+        MultivaluedMap<String, String> params = info.getQueryParameters();
+        if (params.isEmpty()) {
+            return repository.findAll();
+        }
+        String mstId = "";
+        String uwbId = "";
+        Long begin = null;
+        if (params.containsKey("mst")) {
+            mstId = params.getFirst("mst");
+        }
+        if (params.containsKey("uwb")) {
+            uwbId = params.getFirst("uwb");
+        }
+        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);
     }
     //@GET
     //@Produces("text/xml")
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)