Mercurial > lada > lada-server
changeset 457:51784d74a85b
Refactored data access. CDI is much cleaner now.
author | Raimund Renkert <raimund.renkert@intevation.de> |
---|---|
date | Mon, 16 Feb 2015 15:14:48 +0100 |
parents | dbeb56e913fd |
children | 2f42128de67b |
files | src/main/java/de/intevation/lada/util/annotation/RepositoryConfig.java src/main/java/de/intevation/lada/util/data/AbstractRepository.java src/main/java/de/intevation/lada/util/data/DataTransaction.java src/main/java/de/intevation/lada/util/data/DefaultRepository.java src/main/java/de/intevation/lada/util/data/ReadOnlyRepository.java src/main/java/de/intevation/lada/util/factory/RepositoryFactory.java |
diffstat | 6 files changed, 225 insertions(+), 174 deletions(-) [+] |
line wrap: on
line diff
--- a/src/main/java/de/intevation/lada/util/annotation/RepositoryConfig.java Mon Feb 16 15:13:08 2015 +0100 +++ b/src/main/java/de/intevation/lada/util/annotation/RepositoryConfig.java Mon Feb 16 15:14:48 2015 +0100 @@ -1,20 +1,21 @@ -/* Copyright (C) 2013 by Bundesamt fuer Strahlenschutz - * Software engineering by Intevation GmbH - * - * This file is Free Software under the GNU GPL (v>=3) - * and comes with ABSOLUTELY NO WARRANTY! Check out - * the documentation coming with IMIS-Labordaten-Application for details. - */ package de.intevation.lada.util.annotation; +import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import javax.inject.Qualifier; import de.intevation.lada.util.data.RepositoryType; - +@Qualifier @Retention(RetentionPolicy.RUNTIME) +@Target({ + ElementType.TYPE, + ElementType.FIELD, + ElementType.METHOD, + ElementType.PARAMETER}) public @interface RepositoryConfig { RepositoryType type() default RepositoryType.RO; - String dataSource() default ""; }
--- a/src/main/java/de/intevation/lada/util/data/AbstractRepository.java Mon Feb 16 15:13:08 2015 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,104 +0,0 @@ -/* Copyright (C) 2013 by Bundesamt fuer Strahlenschutz - * Software engineering by Intevation GmbH - * - * This file is Free Software under the GNU GPL (v>=3) - * and comes with ABSOLUTELY NO WARRANTY! Check out - * the documentation coming with IMIS-Labordaten-Application for details. - */ -package de.intevation.lada.util.data; - -import javax.ejb.EJBTransactionRolledbackException; -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@intevation.de">Raimund Renkert</a> - */ -@Stateless -public abstract class AbstractRepository -implements Repository -{ - @Inject - protected EntityManagerProducer emp; - - protected String jndiPath; - - /** - * Create object in the database. - * This operation can not be undone. - * - * @param object The object to create - * - * @throws EntityExistsException - * @throws IllegalArgumentException - * @throws EJBTransactionRolledbackException - * @throws TransactionRequiredException - */ - @TransactionAttribute(TransactionAttributeType.REQUIRED) - protected void persistInDatabase(Object object, String dataSource) - throws EntityExistsException, - IllegalArgumentException, - EJBTransactionRolledbackException, - TransactionRequiredException - { - emp.entityManager(dataSource).persist(object); - } - - /** - * Create object in the database. - * This operation can not be undone. - * - * @param object The object to create - * - * @throws EntityExistsException - * @throws IllegalArgumentException - * @throws EJBTransactionRolledbackException - * @throws TransactionRequiredException - */ - @TransactionAttribute(TransactionAttributeType.REQUIRED) - protected void updateInDatabase(Object object, String dataSource) - throws EntityExistsException, - IllegalArgumentException, - EJBTransactionRolledbackException, - TransactionRequiredException - { - emp.entityManager(dataSource).merge(object); - } - - /** - * Remove an object from the datebase. - * This operation can not be undone. - * - * @param object The object to remove - * - * @throws IllegalArgumentException - * @throws TransactionRequiredException - */ - @TransactionAttribute(TransactionAttributeType.REQUIRED) - protected void removeFromDatabase(Object object, String dataSource) - throws IllegalArgumentException, - TransactionRequiredException - { - EntityManager em = emp.entityManager(dataSource); - em.remove( - em.contains(object) ? - object : em.merge(object)); - } - - public Query queryFromString(String sql, String dataSource) { - EntityManager em = emp.entityManager(dataSource); - return em.createNativeQuery(sql); - } - - public EntityManager entityManager(String dataSource) { - return emp.entityManager(dataSource); - } -}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/de/intevation/lada/util/data/DataTransaction.java Mon Feb 16 15:14:48 2015 +0100 @@ -0,0 +1,103 @@ +/* Copyright (C) 2013 by Bundesamt fuer Strahlenschutz + * Software engineering by Intevation GmbH + * + * This file is Free Software under the GNU GPL (v>=3) + * and comes with ABSOLUTELY NO WARRANTY! Check out + * the documentation coming with IMIS-Labordaten-Application for details. + */ +package de.intevation.lada.util.data; + +import javax.ejb.EJBTransactionRolledbackException; +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@intevation.de">Raimund Renkert</a> + */ +@Stateless +public class DataTransaction +{ + @Inject + protected EntityManagerProducer emp; + + protected String jndiPath; + + /** + * Create object in the database. + * This operation can not be undone. + * + * @param object The object to create + * + * @throws EntityExistsException + * @throws IllegalArgumentException + * @throws EJBTransactionRolledbackException + * @throws TransactionRequiredException + */ + @TransactionAttribute(TransactionAttributeType.REQUIRED) + public void persistInDatabase(Object object, String dataSource) + throws EntityExistsException, + IllegalArgumentException, + EJBTransactionRolledbackException, + TransactionRequiredException + { + emp.entityManager(dataSource).persist(object); + } + + /** + * Create object in the database. + * This operation can not be undone. + * + * @param object The object to create + * + * @throws EntityExistsException + * @throws IllegalArgumentException + * @throws EJBTransactionRolledbackException + * @throws TransactionRequiredException + */ + @TransactionAttribute(TransactionAttributeType.REQUIRED) + public void updateInDatabase(Object object, String dataSource) + throws EntityExistsException, + IllegalArgumentException, + EJBTransactionRolledbackException, + TransactionRequiredException + { + emp.entityManager(dataSource).merge(object); + } + + /** + * Remove an object from the datebase. + * This operation can not be undone. + * + * @param object The object to remove + * + * @throws IllegalArgumentException + * @throws TransactionRequiredException + */ + @TransactionAttribute(TransactionAttributeType.REQUIRED) + public void removeFromDatabase(Object object, String dataSource) + throws IllegalArgumentException, + TransactionRequiredException + { + EntityManager em = emp.entityManager(dataSource); + em.remove( + em.contains(object) ? + object : em.merge(object)); + } + + public Query queryFromString(String sql, String dataSource) { + EntityManager em = emp.entityManager(dataSource); + return em.createNativeQuery(sql); + } + + public EntityManager entityManager(String dataSource) { + return emp.entityManager(dataSource); + } +}
--- a/src/main/java/de/intevation/lada/util/data/DefaultRepository.java Mon Feb 16 15:13:08 2015 +0100 +++ b/src/main/java/de/intevation/lada/util/data/DefaultRepository.java Mon Feb 16 15:14:48 2015 +0100 @@ -7,14 +7,19 @@ */ package de.intevation.lada.util.data; +import java.util.List; + import javax.ejb.EJBTransactionRolledbackException; -import javax.ejb.Stateless; +import javax.enterprise.context.ApplicationScoped; import javax.inject.Inject; import javax.persistence.EntityExistsException; +import javax.persistence.EntityManager; import javax.persistence.TransactionRequiredException; +import javax.persistence.criteria.CriteriaQuery; import org.apache.log4j.Logger; +import de.intevation.lada.util.annotation.RepositoryConfig; import de.intevation.lada.util.rest.Response; @@ -23,12 +28,16 @@ * * @author <a href="mailto:rrenkert@intevation.de">Raimund Renkert</a> */ -@Stateless +@RepositoryConfig(type=RepositoryType.RW) +@ApplicationScoped public class DefaultRepository extends ReadOnlyRepository { @Inject private Logger logger; + @Inject + private DataTransaction transaction; + /** * Create and persist a new object in the database. * @@ -40,7 +49,7 @@ @Override public Response create(Object object, String dataSource) { try { - this.persistInDatabase(object, dataSource); + transaction.persistInDatabase(object, dataSource); } catch (EntityExistsException eee) { logger.error("Could not persist " + object.getClass().getName() + @@ -82,7 +91,7 @@ public Response update(Object object, String dataSource) { Response response = new Response(true, 200, object); try { - this.updateInDatabase(object, dataSource); + transaction.updateInDatabase(object, dataSource); } catch (EntityExistsException eee) { return new Response(false, 601, object); @@ -111,7 +120,7 @@ public Response delete(Object object, String dataSource) { Response response = new Response(true, 200, null); try { - this.removeFromDatabase(object, dataSource); + transaction.removeFromDatabase(object, dataSource); } catch (IllegalArgumentException iae) { return new Response(false, 602, object); @@ -121,4 +130,80 @@ } return response; } + /** + * Get objects from database using the given filter. + * + * @param filter Filter used to request objects. + * @param datasource The datasource. + * + * @return Response object containing the filtered list of objects. + */ + @Override + public <T> Response filter(CriteriaQuery<T> filter, String dataSource) { + List<T> result = + transaction.entityManager(dataSource).createQuery(filter).getResultList(); + return new Response(true, 200, result); + } + + + /** + * Get objects from database using the given filter. + * + * @param filter Filter used to request objects. + * @param size The maximum amount of objects. + * @param start The start index. + * @param datasource The datasource. + * + * @return Response object containing the filtered list of objects. + */ + @Override + public <T> Response filter( + CriteriaQuery<T> filter, + int size, + int start, + String dataSource + ) { + List<T> result = + transaction.entityManager(dataSource).createQuery(filter).getResultList(); + if (size > 0 && start > -1) { + List<T> newList = result.subList(start, size + start); + return new Response(true, 200, newList, result.size()); + } + return new Response(true, 200, result); + } + + /** + * Get all objects. + * + * @param clazz The type of the objects. + * @param dataSource The datasource. + * + * @return Response object containg all requested objects. + */ + public <T> Response getAll(Class<T> clazz, String dataSource) { + EntityManager manager = transaction.entityManager(dataSource); + QueryBuilder<T> builder = + new QueryBuilder<T>(manager, clazz); + List<T> result = + manager.createQuery(builder.getQuery()).getResultList(); + return new Response(true, 200, result); + } + + /** + * Get an object by its id. + * + * @param clazz The type of the object. + * @param id The id of the object. + * @param dataSource The datasource. + * + * @return Response object containg the requested object. + */ + @Override + public <T> Response getById(Class<T> clazz, Object id, String dataSource) { + T item = transaction.entityManager(dataSource).find(clazz, id); + if (item == null) { + return new Response(false, 600, null); + } + return new Response(true, 200, item); + } }
--- a/src/main/java/de/intevation/lada/util/data/ReadOnlyRepository.java Mon Feb 16 15:13:08 2015 +0100 +++ b/src/main/java/de/intevation/lada/util/data/ReadOnlyRepository.java Mon Feb 16 15:14:48 2015 +0100 @@ -9,13 +9,13 @@ import java.util.List; -import javax.ejb.Stateless; +import javax.enterprise.context.ApplicationScoped; import javax.inject.Inject; import javax.persistence.EntityManager; +import javax.persistence.Query; import javax.persistence.criteria.CriteriaQuery; -import org.apache.log4j.Logger; - +import de.intevation.lada.util.annotation.RepositoryConfig; import de.intevation.lada.util.rest.Response; @@ -24,11 +24,12 @@ * * @author <a href="mailto:rrenkert@intevation.de">Raimund Renkert</a> */ -@Stateless -public class ReadOnlyRepository extends AbstractRepository { +@RepositoryConfig(type=RepositoryType.RO) +@ApplicationScoped +public class ReadOnlyRepository implements Repository { @Inject - private Logger logger; + private DataTransaction transaction; public ReadOnlyRepository() { } @@ -74,7 +75,7 @@ @Override public <T> Response filter(CriteriaQuery<T> filter, String dataSource) { List<T> result = - emp.entityManager(dataSource).createQuery(filter).getResultList(); + transaction.entityManager(dataSource).createQuery(filter).getResultList(); return new Response(true, 200, result); } @@ -97,7 +98,7 @@ String dataSource ) { List<T> result = - emp.entityManager(dataSource).createQuery(filter).getResultList(); + transaction.entityManager(dataSource).createQuery(filter).getResultList(); if (size > 0 && start > -1) { List<T> newList = result.subList(start, size + start); return new Response(true, 200, newList, result.size()); @@ -114,8 +115,7 @@ * @return Response object containg all requested objects. */ public <T> Response getAll(Class<T> clazz, String dataSource) { - logger.warn("ich bin ein logger"); - EntityManager manager = emp.entityManager(dataSource); + EntityManager manager = transaction.entityManager(dataSource); QueryBuilder<T> builder = new QueryBuilder<T>(manager, clazz); List<T> result = @@ -134,10 +134,21 @@ */ @Override public <T> Response getById(Class<T> clazz, Object id, String dataSource) { - T item = emp.entityManager(dataSource).find(clazz, id); + T item = transaction.entityManager(dataSource).find(clazz, id); if (item == null) { return new Response(false, 600, null); } return new Response(true, 200, item); } + + @Override + public Query queryFromString(String sql, String dataSource) { + EntityManager em = transaction.entityManager(dataSource); + return em.createNativeQuery(sql); + } + + @Override + public EntityManager entityManager(String dataSource) { + return transaction.entityManager(dataSource); + } }
--- a/src/main/java/de/intevation/lada/util/factory/RepositoryFactory.java Mon Feb 16 15:13:08 2015 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,45 +0,0 @@ -/* Copyright (C) 2013 by Bundesamt fuer Strahlenschutz - * Software engineering by Intevation GmbH - * - * This file is Free Software under the GNU GPL (v>=3) - * and comes with ABSOLUTELY NO WARRANTY! Check out - * the documentation coming with IMIS-Labordaten-Application for details. - */ -package de.intevation.lada.util.factory; - -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 de.intevation.lada.util.annotation.RepositoryConfig; -import de.intevation.lada.util.data.DefaultRepository; -import de.intevation.lada.util.data.ReadOnlyRepository; -import de.intevation.lada.util.data.Repository; -import de.intevation.lada.util.data.RepositoryType; - - -@RequestScoped -public class RepositoryFactory { - - @Produces - Repository createRepository(InjectionPoint injectionPoint, - @New ReadOnlyRepository readOnlyRepo, - @New DefaultRepository defaultRepo) { - Annotated annotated = injectionPoint.getAnnotated(); - RepositoryConfig config = - annotated.getAnnotation(RepositoryConfig.class); - if (config == null) { - return readOnlyRepo; - } - Repository repository; - if (config.type() == RepositoryType.RW) { - repository = defaultRepo; - } - else { - repository = readOnlyRepo; - } - return repository; - } -}