changeset 107:5abec5413d65

Let all services and repositories return a response object that contains errors and warnings.
author Raimund Renkert <rrenkert@intevation.de>
date Wed, 12 Jun 2013 11:07:16 +0200 (2013-06-12)
parents 069cec27c9c6
children 03df43fdd359
files src/main/java/de/intevation/lada/data/LKommentarPRepository.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/LProbeService.java src/main/java/de/intevation/lada/rest/OrteService.java src/main/java/de/intevation/lada/rest/SDatenbasisService.java src/main/java/de/intevation/lada/rest/SMessstelleService.java src/main/java/de/intevation/lada/rest/SNetzBetreiberService.java src/main/java/de/intevation/lada/rest/SProbenartService.java src/main/java/de/intevation/lada/rest/SUmweltService.java
diffstat 11 files changed, 46 insertions(+), 143 deletions(-) [+]
line wrap: on
line diff
--- a/src/main/java/de/intevation/lada/data/LKommentarPRepository.java	Tue Jun 11 14:16:22 2013 +0200
+++ b/src/main/java/de/intevation/lada/data/LKommentarPRepository.java	Wed Jun 12 11:07:16 2013 +0200
@@ -18,6 +18,7 @@
 
 import de.intevation.lada.manage.LKommentarPManager;
 import de.intevation.lada.model.LKommentarP;
+import de.intevation.lada.rest.Response;
 
 
 @Named("lkommentarRepository")
@@ -36,32 +37,33 @@
     @Inject
     private Logger logger;
 
-    public List<LKommentarP> filter(String probeId) {
+    public Response filter(String probeId) {
         if (probeId.isEmpty()) {
-            return new ArrayList<LKommentarP>(0);
+            return new Response(false, 600, new ArrayList<LKommentarP>(0));
         }
         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));
 
-        return em.createQuery(criteria).getResultList();
+        List<LKommentarP> result = em.createQuery(criteria).getResultList();
+        return new Response(true, 200, result);
     }
 
-    public String create(LKommentarP kommentar) {
+    public Response create(LKommentarP kommentar) {
         try {
             manager.create(kommentar);
-            return "";
+            return new Response(true, 200, kommentar);
         }
         catch(EntityExistsException eee) {
-            return "Entity already exists.";
+            return new Response(false, 601, kommentar);
         }
         catch(IllegalArgumentException iae) {
-            return "Object is not an entity.";
+            return new Response(false, 602, kommentar);
         }
         catch(TransactionRequiredException tre) {
             logger.log(Level.INFO, "exception: " + tre);
-            return "Transaction failed.";
+            return new Response(false, 603, kommentar);
         }
     }
 }
--- a/src/main/java/de/intevation/lada/data/LProbeRepository.java	Tue Jun 11 14:16:22 2013 +0200
+++ b/src/main/java/de/intevation/lada/data/LProbeRepository.java	Wed Jun 12 11:07:16 2013 +0200
@@ -54,8 +54,7 @@
      * @param begin probeentnahmebegin
      * @return
      */
-    public List<LProbe> filter(String mstId, String uwbId, Long begin) {
-        this.reset();
+    public Response 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);
@@ -87,7 +86,8 @@
             Predicate beg = cb.equal(member.get("probeentnahmeBeginn"), new Date(begin));
             criteria.where(beg);
         }
-        return em.createQuery(criteria).getResultList();
+        List<LProbe> result = em.createQuery(criteria).getResultList();
+        return new Response(true, 200, result);
     }
 
     /**
--- a/src/main/java/de/intevation/lada/data/Repository.java	Tue Jun 11 14:16:22 2013 +0200
+++ b/src/main/java/de/intevation/lada/data/Repository.java	Wed Jun 12 11:07:16 2013 +0200
@@ -12,6 +12,8 @@
 import javax.persistence.criteria.Root;
 import javax.inject.Named;
 
+import de.intevation.lada.rest.Response;
+
 /**
  * This generic Container is an interface to request and select Data
  * obejcts from the connected database.
@@ -42,20 +44,13 @@
      * @param clazz The class type.
      * @return List of objects.
      */
-    public <T> List<T> findAll(Class<T> clazz) {
-        this.reset();
+    public <T> Response findAll(Class<T> clazz) {
         CriteriaBuilder builder = em.getCriteriaBuilder();
         CriteriaQuery<T> criteria = builder.createQuery(clazz);
         Root<T> member = criteria.from(clazz);
         criteria.select(member);
-        return em.createQuery(criteria).getResultList();
-    }
-
-    public void reset() {
-        this.setSuccess(true);
-        this.setGeneralError(200);
-        this.setErrors(new HashMap<String, Integer>());
-        this.setWarnings(new HashMap<String, Integer>());
+        List<T> result = em.createQuery(criteria).getResultList();
+        return new Response(true, 200, result);
     }
 
     /**
@@ -65,86 +60,11 @@
      * @param id The object id.
      * @return The requested object of type clazz
      */
-    public <T> T findById(Class<T> clazz, String id) {
-        this.reset();
+    public <T> Response findById(Class<T> clazz, String id) {
         T item = em.find(clazz, id);
         if (item == null) {
-            this.setGeneralError(600);
-            this.setSuccess(false);
+            return new Response(false, 600, null);
         }
-        return item;
-    }
-
-    /**
-     * Getter for the success boolean which indicates whether the request
-     * succeeds.
-     *
-     * @return The true or false.
-     */
-    public boolean getSuccess() {
-        return this.success;
-    }
-
-    /**
-     * Protected setter for the success boolean which indicates whether the
-     * request succeeds.
-     *
-     */
-    public void setSuccess(boolean success) {
-        this.success = success;
-    }
-
-    /**
-     * Getter for the error code returned by the validator.
-     *
-     * @return The error code returned by the validator.
-     */
-    public int getGeneralError() {
-        return generalError;
-    }
-
-    /**
-     * Protected setter for the general error code.
-     *
-     * @param generalError
-     */
-    protected void setGeneralError(int generalError) {
-        this.generalError = generalError;
-    }
-
-    /**
-     * Getter for all errors occured while validating a LProbe object.
-     *
-     * @return Map of field - error code pairs.
-     */
-    public Map<String, Integer> getErrors() {
-        return errors;
-    }
-
-    /**
-     * Protected setter for validation errors.
-     * 
-     * @param errors
-     */
-    protected void setErrors(Map<String, Integer> errors) {
-        this.errors = errors;
-    }
-
-    /**
-     * Getter for all warnings occured while validating a LProbe object.
-     *
-     * @return Map of field - error code pairs.
-     */
-    public Map<String, Integer> getWarnings() {
-        return warnings;
-    }
-
-    /**
-     * Protected setter for validation warnings.
-     *
-     * @param warnings
-     */
-    protected void setWarnings(Map<String, Integer> warnings) {
-        this.warnings = warnings;
+        return new Response(true, 200, item);
     }
 }
--- a/src/main/java/de/intevation/lada/rest/LKommentarService.java	Tue Jun 11 14:16:22 2013 +0200
+++ b/src/main/java/de/intevation/lada/rest/LKommentarService.java	Wed Jun 12 11:07:16 2013 +0200
@@ -53,7 +53,7 @@
     @GET
     @Path("/{id}")
     @Produces("text/json")
-    public LKommentarP findById(@PathParam("id") String id) {
+    public Response findById(@PathParam("id") String id) {
         return repository.findById(LKommentarP.class, id);
     }
 
@@ -95,7 +95,7 @@
      */
     @GET
     @Produces("text/json")
-    public List<LKommentarP> filter(@Context UriInfo info) {
+    public Response filter(@Context UriInfo info) {
         MultivaluedMap<String, String> params = info.getQueryParameters();
         if (params.containsKey("probe")) {
             String probe = params.getFirst("probe");
@@ -108,14 +108,7 @@
 
     @POST
     @Consumes("application/json")
-    public String create(LKommentarP kommentar) {
-        String response = repository.create(kommentar);
-        if (response.isEmpty()) {
-            return "[{success: true}]";
-        }
-        else {
-            return "[{success: false," +
-                " error: " + response + "}]";
-        }
+    public Response create(LKommentarP kommentar) {
+        return repository.create(kommentar);
     }
 }
--- a/src/main/java/de/intevation/lada/rest/LProbeService.java	Tue Jun 11 14:16:22 2013 +0200
+++ b/src/main/java/de/intevation/lada/rest/LProbeService.java	Wed Jun 12 11:07:16 2013 +0200
@@ -51,11 +51,7 @@
     @Path("/{id}")
     @Produces("text/json")
     public Response findById(@PathParam("id") String id) {
-        LProbe item = repository.findById(LProbe.class, id);
-        Response response = new Response(repository.getSuccess(), repository.getGeneralError(), item);
-        response.setWarnings(repository.getWarnings());
-        response.setErrors(repository.getErrors());
-        return response;
+        return repository.findById(LProbe.class, id);
     }
 
     /**
@@ -75,8 +71,7 @@
     public Response filter(@Context UriInfo info) {
         MultivaluedMap<String, String> params = info.getQueryParameters();
         if (params.isEmpty()) {
-            List<LProbe> items = repository.findAll(LProbe.class);
-            return new Response(true, repository.getGeneralError(), items);
+            return repository.findAll(LProbe.class);
         }
         String mstId = "";
         String uwbId = "";
@@ -96,8 +91,7 @@
                 begin = null;
             }
         }
-        List<LProbe> items = repository.filter(mstId, uwbId, begin);
-        return new Response(true, repository.getGeneralError(), items);
+        return repository.filter(mstId, uwbId, begin);
     }
 
     @PUT
--- a/src/main/java/de/intevation/lada/rest/OrteService.java	Tue Jun 11 14:16:22 2013 +0200
+++ b/src/main/java/de/intevation/lada/rest/OrteService.java	Wed Jun 12 11:07:16 2013 +0200
@@ -42,9 +42,8 @@
      */
     @GET
     @Produces("text/json")
-    public List<Ort> findAll() {
-        List<Ort> result = repository.findAll(Ort.class);
-        return result;
+    public Response findAll() {
+        return repository.findAll(Ort.class);
     }
 
     /**
@@ -56,7 +55,7 @@
     @GET
     @Path("/{id}")
     @Produces("text/json")
-    public Ort findById(@PathParam("id") String id) {
+    public Response findById(@PathParam("id") String id) {
         return repository.findById(Ort.class, id);
     }
 }
--- a/src/main/java/de/intevation/lada/rest/SDatenbasisService.java	Tue Jun 11 14:16:22 2013 +0200
+++ b/src/main/java/de/intevation/lada/rest/SDatenbasisService.java	Wed Jun 12 11:07:16 2013 +0200
@@ -42,9 +42,8 @@
      */
     @GET
     @Produces("text/json")
-    public List<SDatenbasis> findAll() {
-        List<SDatenbasis> result = repository.findAll(SDatenbasis.class);
-        return result;
+    public Response findAll() {
+        return repository.findAll(SDatenbasis.class);
     }
 
     /**
@@ -56,7 +55,7 @@
     @GET
     @Path("/{id}")
     @Produces("text/json")
-    public SDatenbasis findById(@PathParam("id") String id) {
+    public Response findById(@PathParam("id") String id) {
         return repository.findById(SDatenbasis.class, id);
     }
 }
--- a/src/main/java/de/intevation/lada/rest/SMessstelleService.java	Tue Jun 11 14:16:22 2013 +0200
+++ b/src/main/java/de/intevation/lada/rest/SMessstelleService.java	Wed Jun 12 11:07:16 2013 +0200
@@ -43,9 +43,8 @@
      */
     @GET
     @Produces("text/json")
-    public List<SMessStelle> findAll() {
-        List<SMessStelle> result = repository.findAll(SMessStelle.class);
-        return result;
+    public Response findAll() {
+        return repository.findAll(SMessStelle.class);
     }
 
     /**
@@ -57,7 +56,7 @@
     @GET
     @Path("/{id}")
     @Produces("text/json")
-    public SMessStelle findById(@PathParam("id") String id) {
+    public Response findById(@PathParam("id") String id) {
         return repository.findById(SMessStelle.class, id);
     }
 }
--- a/src/main/java/de/intevation/lada/rest/SNetzBetreiberService.java	Tue Jun 11 14:16:22 2013 +0200
+++ b/src/main/java/de/intevation/lada/rest/SNetzBetreiberService.java	Wed Jun 12 11:07:16 2013 +0200
@@ -42,9 +42,8 @@
      */
     @GET
     @Produces("text/json")
-    public List<SNetzBetreiber> findAll() {
-        List<SNetzBetreiber> result = repository.findAll(SNetzBetreiber.class);
-        return result;
+    public Response findAll() {
+        return repository.findAll(SNetzBetreiber.class);
     }
 
     /**
@@ -56,7 +55,7 @@
     @GET
     @Path("/{id}")
     @Produces("text/json")
-    public SNetzBetreiber findById(@PathParam("id") String id) {
+    public Response findById(@PathParam("id") String id) {
         return repository.findById(SNetzBetreiber.class, id);
     }
 }
--- a/src/main/java/de/intevation/lada/rest/SProbenartService.java	Tue Jun 11 14:16:22 2013 +0200
+++ b/src/main/java/de/intevation/lada/rest/SProbenartService.java	Wed Jun 12 11:07:16 2013 +0200
@@ -42,9 +42,8 @@
      */
     @GET
     @Produces("text/json")
-    public List<SProbenart> findAll() {
-        List<SProbenart> result = repository.findAll(SProbenart.class);
-        return result;
+    public Response findAll() {
+        return repository.findAll(SProbenart.class);
     }
 
     /**
@@ -56,7 +55,7 @@
     @GET
     @Path("/{id}")
     @Produces("text/json")
-    public SProbenart findById(@PathParam("id") String id) {
+    public Response findById(@PathParam("id") String id) {
         return repository.findById(SProbenart.class, id);
     }
 }
--- a/src/main/java/de/intevation/lada/rest/SUmweltService.java	Tue Jun 11 14:16:22 2013 +0200
+++ b/src/main/java/de/intevation/lada/rest/SUmweltService.java	Wed Jun 12 11:07:16 2013 +0200
@@ -42,9 +42,8 @@
      */
     @GET
     @Produces("text/json")
-    public List<SUmwelt> findAll() {
-        List<SUmwelt> result = repository.findAll(SUmwelt.class);
-        return result;
+    public Response findAll() {
+        return repository.findAll(SUmwelt.class);
     }
 
     /**
@@ -56,7 +55,7 @@
     @GET
     @Path("/{id:[0-9][0-9]*}")
     @Produces("text/json")
-    public SUmwelt loadById(@PathParam("id") String id) {
+    public Response findById(@PathParam("id") String id) {
         return repository.findById(SUmwelt.class, id);
     }
 }
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)