[Lada-commits] [PATCH 8 of 9] Updated repositoy structure and added probe services

Wald Commits scm-commit at wald.intevation.org
Wed Feb 11 13:02:46 CET 2015


# HG changeset patch
# User Raimund Renkert <raimund.renkert at intevation.de>
# Date 1423656115 -3600
# Node ID 808ea2091c1d416ea424ad22e21f32f3d4983e4e
# Parent  ee5c7309e4b978a7ac717b5c6f555b55a97713f3
Updated repositoy structure and added probe services.

diff -r ee5c7309e4b9 -r 808ea2091c1d src/main/java/de/intevation/lada/rest/ProbeService.java
--- a/src/main/java/de/intevation/lada/rest/ProbeService.java	Wed Feb 11 12:56:59 2015 +0100
+++ b/src/main/java/de/intevation/lada/rest/ProbeService.java	Wed Feb 11 13:01:55 2015 +0100
@@ -7,9 +7,14 @@
  */
 package de.intevation.lada.rest;
 
+import java.util.List;
+
 import javax.enterprise.context.RequestScoped;
 import javax.inject.Inject;
+import javax.ws.rs.DELETE;
 import javax.ws.rs.GET;
+import javax.ws.rs.POST;
+import javax.ws.rs.PUT;
 import javax.ws.rs.Path;
 import javax.ws.rs.PathParam;
 import javax.ws.rs.Produces;
@@ -20,6 +25,7 @@
 import org.apache.log4j.Logger;
 
 import de.intevation.lada.model.land.LProbe;
+import de.intevation.lada.model.land.ProbeTranslation;
 import de.intevation.lada.util.annotation.AuthenticationConfig;
 import de.intevation.lada.util.annotation.AuthorizationConfig;
 import de.intevation.lada.util.annotation.RepositoryConfig;
@@ -27,6 +33,7 @@
 import de.intevation.lada.util.auth.AuthenticationType;
 import de.intevation.lada.util.auth.Authorization;
 import de.intevation.lada.util.auth.AuthorizationType;
+import de.intevation.lada.util.data.QueryBuilder;
 import de.intevation.lada.util.data.Repository;
 import de.intevation.lada.util.data.RepositoryType;
 import de.intevation.lada.util.rest.Response;
@@ -77,12 +84,79 @@
         return defaultRepo.getById(LProbe.class, Integer.valueOf(id));
     }
 
-    @GET
-    @Produces("application/json")
-    @Path("/land")
-    public Response getAllLand() {
-        //List<LProbe> res = dao.getAllLand();
-        //return new Response(true, 200, res);
-        return null;
+    @POST
+    @Path("/")
+    @Produces(MediaType.APPLICATION_JSON)
+    public Response create(@Context HttpHeaders headers, LProbe probe) {
+        if (!authentication.isAuthenticated(headers)) {
+            return new Response(false, 699, null);
+        }
+        /* Persist the new probe object*/
+        Response response = defaultRepo.create(probe, "land");
+        LProbe ret = (LProbe)response.getData();
+        /* Create and persist a new probe translation object*/
+        ProbeTranslation trans = new ProbeTranslation();
+        trans.setProbeId(ret);
+        defaultRepo.create(trans, "land");
+        /* Get and return the new probe object*/
+        Response created =
+            defaultRepo.getById(LProbe.class, ret.getId(), "land");
+        return new Response(true, 200, created.getData());
+    }
+
+    /**
+     * Update an existing probe object.
+     *
+     * @return Response object containing the updated probe object.
+     */
+    @PUT
+    @Path("/")
+    @Produces(MediaType.APPLICATION_JSON)
+    public Response update(@Context HttpHeaders headers, LProbe probe) {
+        if (!authentication.isAuthenticated(headers)) {
+            logger.debug("User is not authenticated!");
+            return new Response(false, 699, null);
+        }
+        Response response = defaultRepo.update(probe, "land");
+        Response updated = defaultRepo.getById(
+            LProbe.class,
+            ((LProbe)response.getData()).getId(), "land");
+        return updated;
+    }
+
+    /**
+     * Delete an existing probe object by id.
+     *
+     * @return Response object.
+     */
+    @DELETE
+    @Path("/{id}")
+    @Produces(MediaType.APPLICATION_JSON)
+    public Response delete(
+        @Context HttpHeaders headers,
+        @PathParam("id") String id
+    ) {
+        if (!authentication.isAuthenticated(headers)) {
+            logger.debug("User is not authenticated!");
+            return new Response(false, 699, null);
+        }
+        /* Get the probe object by id*/
+        Response probe =
+            defaultRepo.getById(LProbe.class, Integer.valueOf(id), "land");
+        LProbe probeObj = (LProbe)probe.getData();
+        /* Create a query and request the probetranslation object for the
+         * probe*/
+        QueryBuilder<ProbeTranslation> builder =
+            new QueryBuilder<ProbeTranslation>(
+                defaultRepo.entityManager("land"), ProbeTranslation.class);
+        builder.and("probe", probeObj.getId());
+        Response probeTrans = defaultRepo.filter(builder.getQuery(), "land");
+        @SuppressWarnings("unchecked")
+        ProbeTranslation probeTransObj = ((List<ProbeTranslation>)probeTrans.getData()).get(0);
+        /* Delete the probe translation object*/
+        defaultRepo.delete(probeTransObj, "land");
+        /* Delete the probe object*/
+        Response response = defaultRepo.delete(probeObj, "land");
+        return response;
     }
 }
diff -r ee5c7309e4b9 -r 808ea2091c1d src/main/java/de/intevation/lada/util/data/AbstractRepository.java
--- a/src/main/java/de/intevation/lada/util/data/AbstractRepository.java	Wed Feb 11 12:56:59 2015 +0100
+++ b/src/main/java/de/intevation/lada/util/data/AbstractRepository.java	Wed Feb 11 13:01:55 2015 +0100
@@ -11,20 +11,24 @@
 import javax.ejb.Stateless;
 import javax.ejb.TransactionAttribute;
 import javax.ejb.TransactionAttributeType;
+import javax.inject.Inject;
 import javax.persistence.EntityExistsException;
 import javax.persistence.EntityManager;
 import javax.persistence.Query;
 import javax.persistence.TransactionRequiredException;
 
-
+/**
+ * Abstract class implementing low level data operations.
+ *
+ * @author <a href="mailto:rrenkert at intevation.de">Raimund Renkert</a>
+ */
 @Stateless
 public abstract class AbstractRepository
-implements Repository 
+implements Repository
 {
+    @Inject
     protected EntityManagerProducer emp;
 
-    protected String dataSource;
-
     protected String jndiPath;
 
     /**
@@ -39,7 +43,17 @@
      * @throws TransactionRequiredException
      */
     @TransactionAttribute(TransactionAttributeType.REQUIRED)
-    protected void persistInDatabase(Object object)
+    protected void persistInDatabase(Object object, String dataSource)
+    throws EntityExistsException,
+        IllegalArgumentException,
+        EJBTransactionRolledbackException,
+        TransactionRequiredException
+    {
+        emp.entityManager(dataSource).persist(object);
+    }
+
+    @TransactionAttribute(TransactionAttributeType.REQUIRED)
+    protected void updateInDatabase(Object object, String dataSource)
     throws EntityExistsException,
         IllegalArgumentException,
         EJBTransactionRolledbackException,
@@ -58,7 +72,7 @@
      * @throws TransactionRequiredException
      */
     @TransactionAttribute(TransactionAttributeType.REQUIRED)
-    protected void removeFromDatabase(Object object)
+    protected void removeFromDatabase(Object object, String dataSource)
     throws IllegalArgumentException,
         TransactionRequiredException
     {
@@ -68,22 +82,12 @@
                 object : em.merge(object));
     }
 
-    public Query queryFromString(String sql) {
+    public Query queryFromString(String sql, String dataSource) {
         EntityManager em = emp.entityManager(dataSource);
         return em.createNativeQuery(sql);
     }
 
-    @Override
-    public void setDataSource(String dataSource) {
-        this.dataSource = dataSource;
-    }
-
-    @Override
-    public String getDataSource() {
-        return this.dataSource;
-    }
-
-    public void setEntityManagerProducer(EntityManagerProducer emp) {
-        this.emp = emp;
+    public EntityManager entityManager(String dataSource) {
+        return emp.entityManager(dataSource);
     }
 }
diff -r ee5c7309e4b9 -r 808ea2091c1d src/main/java/de/intevation/lada/util/data/DefaultRepository.java
--- a/src/main/java/de/intevation/lada/util/data/DefaultRepository.java	Wed Feb 11 12:56:59 2015 +0100
+++ b/src/main/java/de/intevation/lada/util/data/DefaultRepository.java	Wed Feb 11 13:01:55 2015 +0100
@@ -8,25 +8,71 @@
 package de.intevation.lada.util.data;
 
 import javax.ejb.EJBTransactionRolledbackException;
+import javax.ejb.Stateless;
+import javax.inject.Inject;
 import javax.persistence.EntityExistsException;
 import javax.persistence.TransactionRequiredException;
 
+import org.apache.log4j.Logger;
+
 import de.intevation.lada.util.rest.Response;
 
 
 /**
  * @author rrenkert
  */
+ at Stateless
 public class DefaultRepository extends ReadOnlyRepository {
 
-    public DefaultRepository() {
+    @Inject
+    private Logger logger;
+
+    /**
+     * Create and persist a new object in the database.
+     *
+     * @param object The new object.
+     * @param dataSource The datasource.
+     *
+     * @return Response object containing the new object.
+     */
+    @Override
+    public Response create(Object object, String dataSource) {
+        try {
+            this.persistInDatabase(object, dataSource);
+        }
+        catch (EntityExistsException eee) {
+            logger.error("Could not persist " + object.getClass().getName() +
+                ". Reason: " + eee.getClass().getName() + " - " +
+                eee.getMessage());
+            return new Response(false, 601, object);
+        }
+        catch (IllegalArgumentException iae) {
+            logger.error("Could not persist " + object.getClass().getName() +
+                ". Reason: " + iae.getClass().getName() + " - " +
+                iae.getMessage());
+            return new Response(false, 602, object);
+        }
+        catch (TransactionRequiredException tre) {
+            logger.error("Could not persist " + object.getClass().getName() +
+                ". Reason: " + tre.getClass().getName() + " - " +
+                tre.getMessage());
+            return new Response(false, 603, object);
+        }
+        catch (EJBTransactionRolledbackException ete) {
+            logger.error("Could not persist " + object.getClass().getName() +
+                ". Reason: " + ete.getClass().getName() + " - " +
+                ete.getMessage());
+            return new Response(false, 604, object);
+        }
+        Response response = new Response(true, 200, object);
+        return response;
     }
 
     @Override
-    public Response create(Object object) {
+    public Response update(Object object, String dataSource) {
         Response response = new Response(true, 200, object);
         try {
-            this.persistInDatabase(object);
+            this.updateInDatabase(object, dataSource);
         }
         catch (EntityExistsException eee) {
             return new Response(false, 601, object);
@@ -44,31 +90,10 @@
     }
 
     @Override
-    public Response update(Object object) {
-        Response response = new Response(true, 200, object);
-        try {
-            this.persistInDatabase(object);
-        }
-        catch (EntityExistsException eee) {
-            return new Response(false, 601, object);
-        }
-        catch (IllegalArgumentException iae) {
-            return new Response(false, 602, object);
-        }
-        catch (TransactionRequiredException tre) {
-            return new Response(false, 603, object);
-        }
-        catch (EJBTransactionRolledbackException ete) {
-            return new Response(false, 604, object);
-        }
-        return response;
-    }
-
-    @Override
-    public Response delete(Object object) {
+    public Response delete(Object object, String dataSource) {
         Response response = new Response(true, 200, null);
         try {
-            this.removeFromDatabase(object);
+            this.removeFromDatabase(object, dataSource);
         }
         catch (IllegalArgumentException iae) {
             return new Response(false, 602, object);
diff -r ee5c7309e4b9 -r 808ea2091c1d src/main/java/de/intevation/lada/util/data/ReadOnlyRepository.java
--- a/src/main/java/de/intevation/lada/util/data/ReadOnlyRepository.java	Wed Feb 11 12:56:59 2015 +0100
+++ b/src/main/java/de/intevation/lada/util/data/ReadOnlyRepository.java	Wed Feb 11 13:01:55 2015 +0100
@@ -9,43 +9,57 @@
 
 import java.util.List;
 
+import javax.ejb.Stateless;
+import javax.inject.Inject;
 import javax.persistence.EntityManager;
 import javax.persistence.criteria.CriteriaQuery;
 
+import org.apache.log4j.Logger;
+
 import de.intevation.lada.util.rest.Response;
 
+
 /**
  * @author rrenkert
  */
+ at Stateless
 public class ReadOnlyRepository extends AbstractRepository {
 
+    @Inject
+    private Logger logger;
+
     public ReadOnlyRepository() {
     }
 
     @Override
-    public Response create(Object object) {
+    public Response create(Object object, String dataSource) {
         return null;
     }
 
     @Override
-    public Response update(Object object) {
+    public Response update(Object object, String dataSource) {
         return null;
     }
 
     @Override
-    public Response delete(Object object) {
+    public Response delete(Object object, String dataSource) {
         return null;
     }
 
     @Override
-    public <T> Response filter(CriteriaQuery<T> filter) {
+    public <T> Response filter(CriteriaQuery<T> filter, String dataSource) {
         List<T> result =
             emp.entityManager(dataSource).createQuery(filter).getResultList();
         return new Response(true, 200, result);
     }
 
     @Override
-    public <T> Response filter(CriteriaQuery<T> filter, int size, int start) {
+    public <T> Response filter(
+        CriteriaQuery<T> filter,
+        int size,
+        int start,
+        String dataSource
+    ) {
         List<T> result =
             emp.entityManager(dataSource).createQuery(filter).getResultList();
         if (size > 0 && start > -1) {
@@ -55,7 +69,7 @@
         return new Response(true, 200, result);
     }
 
-    public <T> Response getAll(Class<T> clazz) {
+    public <T> Response getAll(Class<T> clazz, String dataSource) {
         EntityManager manager = emp.entityManager(dataSource);
         QueryBuilder<T> builder =
             new QueryBuilder<T>(manager, clazz);
@@ -65,7 +79,7 @@
     }
 
     @Override
-    public <T> Response getById(Class<T> clazz, Object id) {
+    public <T> Response getById(Class<T> clazz, Object id, String dataSource) {
         T item = emp.entityManager(dataSource).find(clazz, id);
         if (item == null) {
             return new Response(false, 600, null);
diff -r ee5c7309e4b9 -r 808ea2091c1d src/main/java/de/intevation/lada/util/data/Repository.java
--- a/src/main/java/de/intevation/lada/util/data/Repository.java	Wed Feb 11 12:56:59 2015 +0100
+++ b/src/main/java/de/intevation/lada/util/data/Repository.java	Wed Feb 11 13:01:55 2015 +0100
@@ -7,6 +7,7 @@
  */
 package de.intevation.lada.util.data;
 
+import javax.persistence.EntityManager;
 import javax.persistence.Query;
 import javax.persistence.criteria.CriteriaQuery;
 
@@ -20,25 +21,25 @@
  */
 public interface Repository {
 
-    public Response create(Object object);
+    public Response create(Object object, String dataSource);
 
-    public Response update(Object object);
+    public Response update(Object object, String dataSource);
 
-    public Response delete(Object object);
+    public Response delete(Object object, String dataSource);
 
-    public <T> Response filter(CriteriaQuery<T> filter);
+    public <T> Response filter(CriteriaQuery<T> filter, String dataSource);
 
-    public <T> Response filter(CriteriaQuery<T> filter, int size, int start);
+    public <T> Response filter(
+        CriteriaQuery<T> filter,
+        int size,
+        int start,
+        String dataSource);
 
-    public <T> Response getAll(Class<T> clazz);
+    public <T> Response getAll(Class<T> clazz, String dataSource);
 
-    public <T> Response getById(Class<T> clazz, Object id);
+    public <T> Response getById(Class<T> clazz, Object id, String dataSource);
 
-    public Query queryFromString(String sql); 
+    public Query queryFromString(String sql, String dataSource);
 
-    public void setDataSource(String dataSource);
-
-    public String getDataSource();
-
-    public void setEntityManagerProducer(EntityManagerProducer emp);
+    public EntityManager entityManager(String dataSource);
 }
diff -r ee5c7309e4b9 -r 808ea2091c1d src/main/java/de/intevation/lada/util/factory/RepositoryFactory.java
--- a/src/main/java/de/intevation/lada/util/factory/RepositoryFactory.java	Wed Feb 11 12:56:59 2015 +0100
+++ b/src/main/java/de/intevation/lada/util/factory/RepositoryFactory.java	Wed Feb 11 13:01:55 2015 +0100
@@ -7,42 +7,39 @@
  */
 package de.intevation.lada.util.factory;
 
-import javax.enterprise.context.ApplicationScoped;
+import javax.enterprise.context.RequestScoped;
+import javax.enterprise.inject.New;
 import javax.enterprise.inject.Produces;
 import javax.enterprise.inject.spi.Annotated;
 import javax.enterprise.inject.spi.InjectionPoint;
-import javax.inject.Inject;
 
 import de.intevation.lada.util.annotation.RepositoryConfig;
 import de.intevation.lada.util.data.DefaultRepository;
-import de.intevation.lada.util.data.EntityManagerProducer;
 import de.intevation.lada.util.data.ReadOnlyRepository;
 import de.intevation.lada.util.data.Repository;
 import de.intevation.lada.util.data.RepositoryType;
 
 
- at ApplicationScoped
+ at RequestScoped
 public class RepositoryFactory {
 
-    @Inject
-    private EntityManagerProducer emp;
-
     @Produces
-    Repository createRepository(InjectionPoint injectionPoint) {
+    Repository createRepository(InjectionPoint injectionPoint,
+        @New ReadOnlyRepository readOnlyRepo,
+        @New DefaultRepository defaultRepo) {
         Annotated annotated = injectionPoint.getAnnotated();
-        RepositoryConfig config = annotated.getAnnotation(RepositoryConfig.class);
+        RepositoryConfig config =
+            annotated.getAnnotation(RepositoryConfig.class);
         if (config == null) {
-            return new ReadOnlyRepository();
+            return readOnlyRepo;
         }
         Repository repository;
         if (config.type() == RepositoryType.RW) {
-            repository = new DefaultRepository();
+            repository = defaultRepo;
         }
         else {
-            repository = new ReadOnlyRepository();
+            repository = readOnlyRepo;
         }
-        repository.setEntityManagerProducer(emp);
-        repository.setDataSource(config.dataSource());
         return repository;
     }
 }


More information about the Lada-commits mailing list