Mercurial > lada > lada-server
changeset 438:f0ad10e0e1b1
Moved Repositories, etc. into data package.
line wrap: on
line diff
--- a/src/main/java/de/intevation/lada/util/AbstractRepository.java Fri Feb 06 15:58:28 2015 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,89 +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; - -import javax.ejb.EJBTransactionRolledbackException; -import javax.ejb.Stateless; -import javax.ejb.TransactionAttribute; -import javax.ejb.TransactionAttributeType; -import javax.persistence.EntityExistsException; -import javax.persistence.EntityManager; -import javax.persistence.Query; -import javax.persistence.TransactionRequiredException; - - -@Stateless -public abstract class AbstractRepository -implements Repository -{ - protected EntityManagerProducer emp; - - protected String dataSource; - - protected String jndiPath; - - /** - * Create or update an object in the database. - * This operation can not be undone. - * - * @param object The object to create or update - * - * @throws EntityExistsException - * @throws IllegalArgumentException - * @throws EJBTransactionRolledbackException - * @throws TransactionRequiredException - */ - @TransactionAttribute(TransactionAttributeType.REQUIRED) - protected void persistInDatabase(Object object) - 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) - throws IllegalArgumentException, - TransactionRequiredException - { - EntityManager em = emp.entityManager(dataSource); - em.remove( - em.contains(object) ? - object : em.merge(object)); - } - - public Query queryFromString(String sql) { - 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; - } -}
--- a/src/main/java/de/intevation/lada/util/DefaultRepository.java Fri Feb 06 15:58:28 2015 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,81 +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; - -import javax.ejb.EJBTransactionRolledbackException; -import javax.persistence.EntityExistsException; -import javax.persistence.TransactionRequiredException; - -import de.intevation.lada.util.rest.Response; - - -/** - * @author rrenkert - */ -public class DefaultRepository extends ReadOnlyRepository { - - public DefaultRepository() { - } - - @Override - public Response create(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 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) { - Response response = new Response(true, 200, null); - try { - this.removeFromDatabase(object); - } - catch (IllegalArgumentException iae) { - return new Response(false, 602, object); - } - catch (TransactionRequiredException tre) { - return new Response(false, 603, object); - } - return response; - } -}
--- a/src/main/java/de/intevation/lada/util/EntityManagerProducer.java Fri Feb 06 15:58:28 2015 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,64 +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; - -import javax.annotation.Resource; -import javax.ejb.LocalBean; -import javax.ejb.SessionContext; -import javax.ejb.Stateless; -import javax.persistence.EntityManager; -import javax.validation.UnexpectedTypeException; - - -@Stateless -@LocalBean -public class EntityManagerProducer { - - @Resource - private SessionContext ctx; - - private String jndiPath = "java:app/entitymanager/"; - - /** - * Constructor for multi-tenancy entity manager delegate. - * Default jndi search path is 'java:app/entitymanager'. - */ - public EntityManagerProducer() { - } - - /** - * Constructor for multi-tenancy entity manager delegate. - * - * @param jndiEnv The jndi path to search for datasources. - * Defaults to 'java:app/entitymanager'. - */ - public EntityManagerProducer(String jndiPath) { - this.jndiPath = jndiPath; - } - - /** - * Create an entity manager for a datasource. - * - * @throws UnexpectedTypeException - * @param dataSourceName The jndi name of the datasource. - * @return The entity manager for the datasource. - */ - public EntityManager entityManager(String dataSourceName) { - - EntityManager entityManager = - (EntityManager) this.ctx.lookup(this.jndiPath + dataSourceName); - - if (entityManager == null) { - throw new UnexpectedTypeException("Unknown data source name '" + - dataSourceName + "'."); - } - - return entityManager; - - } -}
--- a/src/main/java/de/intevation/lada/util/QueryBuilder.java Fri Feb 06 15:58:28 2015 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,238 +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; - -import java.util.List; - -import javax.persistence.EntityManager; -import javax.persistence.criteria.CriteriaBuilder; -import javax.persistence.criteria.CriteriaQuery; -import javax.persistence.criteria.Path; -import javax.persistence.criteria.Predicate; -import javax.persistence.criteria.Root; - -/** - * @author <a href="mailto:rrenkert@intevation.de">Raimund Renkert</a> - */ -public class QueryBuilder<T> { - - private EntityManager manager; - private CriteriaBuilder builder; - private CriteriaQuery<T> query; - private Root<T> root; - private Class<T> clazz; - private Predicate filter; - - /** - * Create a new QueryBuilder for the specified class. - * - * @param manager - * @param clazz - */ - public QueryBuilder(EntityManager manager, Class<T> clazz) { - this.manager = manager; - this.clazz = clazz; - this.builder = this.manager.getCriteriaBuilder(); - this.query = this.builder.createQuery(this.clazz); - this.root = this.query.from(this.clazz); - } - - /** - * Get the criteria query build with this class. - * - * @return The query. - */ - public CriteriaQuery<T> getQuery() { - if (this.filter == null) { - this.query.where(); - return this.query; - } - this.query.where(this.filter); - return this.query; - } - - /** - * Logical AND operation. - * - * @param id The database column name. - * @param value The filter value - * @return The builder itself. - */ - public QueryBuilder<T> and(String id, Object value) { - Predicate p = this.builder.equal(this.root.get(id), value); - if (this.filter != null) { - this.filter = this.builder.and(this.filter, p); - } - else { - this.filter = this.builder.and(p); - } - return this; - } - - /** - * Logical AND with like operation. - * - * @param id The database column name. - * @param value The filter value - * @return The builder itself. - */ - public QueryBuilder<T> andLike(String id, String value) { - Path<String> path = this.root.get(id); - Predicate p = this.builder.like(path, value); - if (this.filter != null) { - this.filter = this.builder.and(this.filter, p); - } - else { - this.filter = this.builder.and(p); - } - return this; - } - - /** - * Logical OR operation. - * - * @param id The database column name - * @param value The filter value. - * @return The builder itself. - */ - public QueryBuilder<T> or(String id, Object value) { - Predicate p = this.builder.equal(this.root.get(id), value); - if (this.filter != null) { - this.filter = this.builder.or(this.filter, p); - } - else { - this.filter = this.builder.or(p); - } - return this; - } - - /** - * Logical OR with like operation. - * - * @param column The database column name. - * @param value The filter value - * @return The builder itself. - */ - public QueryBuilder<T> orLike(String id, String value) { - Path<String> path = this.root.get(id); - Predicate p = this.builder.like(path, value); - if (this.filter != null) { - this.filter = this.builder.or(this.filter, p); - } - else { - this.filter = this.builder.or(p); - } - return this; - } - - /** - * Logical AND operation. - * All elements in <i>values</i> will be concatenated with AND operator. - * - * @param id The database column name. - * @param values List of values. - * @return The builder itself. - */ - public QueryBuilder<T> and(String id, List<String> values) { - for(String v: values) { - this.and(id, v); - } - return this; - } - - /** - * Logical OR operation. - * All elements in <i>values</i> will be concatenated with OR operator. - * - * @param id The database column name. - * @param values List of values. - * @return The builder itself. - */ - public QueryBuilder<T> or(String id, List<String> values) { - for (String v: values) { - this.or(id, v); - } - return this; - } - - /** - * Logical AND operation. - * The actually defined query will be concatenated with the query defined - * in the builder <i>b</i>. - * - * @param b A builder. - * @return The builder itself. - */ - public QueryBuilder<T> and(QueryBuilder<T> b) { - if (b == null || b.filter == null) { - return this; - } - if (this.filter != null) { - this.filter = this.builder.and(this.filter, b.filter); - } - else { - this.filter = this.builder.and(b.filter); - } - return this; - } - - /** - * Logical OR operation. - * The actually defined query will be concatenated with the query defined - * in the builder <i>b</i>. - * - * @param b A builder. - * @return The builder itself. - */ - public QueryBuilder<T> or(QueryBuilder<T> b) { - if (b == null || b.filter == null) { - return this; - } - if (this.filter != null) { - this.filter = this.builder.or(this.filter, b.filter); - } - else { - this.filter = this.builder.or(b.filter); - } - return this; - } - - /** - * Use 'distinct' in the query. - */ - public void distinct() { - this.query.distinct(true); - } - - /** - * Order result by the specified column name - * - * @param id The column name. - * @param asc Ascending(true), Descending(false). - */ - public void orderBy(String id, boolean asc) { - if (asc) { - this.query.orderBy(this.builder.asc(this.root.get(id))); - } - else { - this.query.orderBy(this.builder.desc(this.root.get(id))); - } - } - - /** - * Get an empty instance of this builder to create subfilters. - * - * @return An empty instance of this builder. - */ - public QueryBuilder<T> getEmptyBuilder(){ - QueryBuilder<T> copy = new QueryBuilder<T>(manager, clazz); - copy.builder = this.builder; - copy.root = this.root; - return copy; - } -}
--- a/src/main/java/de/intevation/lada/util/ReadOnlyRepository.java Fri Feb 06 15:58:28 2015 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,75 +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; - -import java.util.List; - -import javax.persistence.EntityManager; -import javax.persistence.criteria.CriteriaQuery; - -import de.intevation.lada.util.rest.Response; - -/** - * @author rrenkert - */ -public class ReadOnlyRepository extends AbstractRepository { - - public ReadOnlyRepository() { - } - - @Override - public Response create(Object object) { - return null; - } - - @Override - public Response update(Object object) { - return null; - } - - @Override - public Response delete(Object object) { - return null; - } - - @Override - public <T> Response filter(CriteriaQuery<T> filter) { - 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) { - List<T> result = - emp.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); - } - - public <T> Response getAll(Class<T> clazz) { - EntityManager manager = emp.entityManager(dataSource); - QueryBuilder<T> builder = - new QueryBuilder<T>(manager, clazz); - List<T> result = - manager.createQuery(builder.getQuery()).getResultList(); - return new Response(true, 200, result); - } - - @Override - public <T> Response getById(Class<T> clazz, String id) { - T item = emp.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/Repository.java Fri Feb 06 15:58:28 2015 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,44 +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; - -import javax.persistence.Query; -import javax.persistence.criteria.CriteriaQuery; - -import de.intevation.lada.util.rest.Response; - -/** - * This generic Container is an interface to request and select Data - * obejcts from the connected database. - * - * @author <a href="mailto:rrenkert@intevation.de">Raimund Renkert</a> - */ -public interface Repository { - - public Response create(Object object); - - public Response update(Object object); - - public Response delete(Object object); - - public <T> Response filter(CriteriaQuery<T> filter); - - public <T> Response filter(CriteriaQuery<T> filter, int size, int start); - - public <T> Response getAll(Class<T> clazz); - - public <T> Response getById(Class<T> clazz, String id); - - public Query queryFromString(String sql); - - public void setDataSource(String dataSource); - - public String getDataSource(); - - public void setEntityManagerProducer(EntityManagerProducer emp); -}
--- a/src/main/java/de/intevation/lada/util/RepositoryType.java Fri Feb 06 15:58:28 2015 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,13 +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; - - -public enum RepositoryType { - RO, RW -}
--- a/src/main/java/de/intevation/lada/util/annotation/RepositoryConfig.java Fri Feb 06 15:58:28 2015 +0100 +++ b/src/main/java/de/intevation/lada/util/annotation/RepositoryConfig.java Fri Feb 06 17:55:54 2015 +0100 @@ -10,7 +10,7 @@ import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; -import de.intevation.lada.util.RepositoryType; +import de.intevation.lada.util.data.RepositoryType; @Retention(RetentionPolicy.RUNTIME)
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/de/intevation/lada/util/data/AbstractRepository.java Fri Feb 06 17:55:54 2015 +0100 @@ -0,0 +1,89 @@ +/* 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.persistence.EntityExistsException; +import javax.persistence.EntityManager; +import javax.persistence.Query; +import javax.persistence.TransactionRequiredException; + + +@Stateless +public abstract class AbstractRepository +implements Repository +{ + protected EntityManagerProducer emp; + + protected String dataSource; + + protected String jndiPath; + + /** + * Create or update an object in the database. + * This operation can not be undone. + * + * @param object The object to create or update + * + * @throws EntityExistsException + * @throws IllegalArgumentException + * @throws EJBTransactionRolledbackException + * @throws TransactionRequiredException + */ + @TransactionAttribute(TransactionAttributeType.REQUIRED) + protected void persistInDatabase(Object object) + 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) + throws IllegalArgumentException, + TransactionRequiredException + { + EntityManager em = emp.entityManager(dataSource); + em.remove( + em.contains(object) ? + object : em.merge(object)); + } + + public Query queryFromString(String sql) { + 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; + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/de/intevation/lada/util/data/DefaultRepository.java Fri Feb 06 17:55:54 2015 +0100 @@ -0,0 +1,81 @@ +/* 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.persistence.EntityExistsException; +import javax.persistence.TransactionRequiredException; + +import de.intevation.lada.util.rest.Response; + + +/** + * @author rrenkert + */ +public class DefaultRepository extends ReadOnlyRepository { + + public DefaultRepository() { + } + + @Override + public Response create(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 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) { + Response response = new Response(true, 200, null); + try { + this.removeFromDatabase(object); + } + catch (IllegalArgumentException iae) { + return new Response(false, 602, object); + } + catch (TransactionRequiredException tre) { + return new Response(false, 603, object); + } + return response; + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/de/intevation/lada/util/data/EntityManagerProducer.java Fri Feb 06 17:55:54 2015 +0100 @@ -0,0 +1,64 @@ +/* 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.annotation.Resource; +import javax.ejb.LocalBean; +import javax.ejb.SessionContext; +import javax.ejb.Stateless; +import javax.persistence.EntityManager; +import javax.validation.UnexpectedTypeException; + + +@Stateless +@LocalBean +public class EntityManagerProducer { + + @Resource + private SessionContext ctx; + + private String jndiPath = "java:app/entitymanager/"; + + /** + * Constructor for multi-tenancy entity manager delegate. + * Default jndi search path is 'java:app/entitymanager'. + */ + public EntityManagerProducer() { + } + + /** + * Constructor for multi-tenancy entity manager delegate. + * + * @param jndiEnv The jndi path to search for datasources. + * Defaults to 'java:app/entitymanager'. + */ + public EntityManagerProducer(String jndiPath) { + this.jndiPath = jndiPath; + } + + /** + * Create an entity manager for a datasource. + * + * @throws UnexpectedTypeException + * @param dataSourceName The jndi name of the datasource. + * @return The entity manager for the datasource. + */ + public EntityManager entityManager(String dataSourceName) { + + EntityManager entityManager = + (EntityManager) this.ctx.lookup(this.jndiPath + dataSourceName); + + if (entityManager == null) { + throw new UnexpectedTypeException("Unknown data source name '" + + dataSourceName + "'."); + } + + return entityManager; + + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/de/intevation/lada/util/data/QueryBuilder.java Fri Feb 06 17:55:54 2015 +0100 @@ -0,0 +1,238 @@ +/* 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 java.util.List; + +import javax.persistence.EntityManager; +import javax.persistence.criteria.CriteriaBuilder; +import javax.persistence.criteria.CriteriaQuery; +import javax.persistence.criteria.Path; +import javax.persistence.criteria.Predicate; +import javax.persistence.criteria.Root; + +/** + * @author <a href="mailto:rrenkert@intevation.de">Raimund Renkert</a> + */ +public class QueryBuilder<T> { + + private EntityManager manager; + private CriteriaBuilder builder; + private CriteriaQuery<T> query; + private Root<T> root; + private Class<T> clazz; + private Predicate filter; + + /** + * Create a new QueryBuilder for the specified class. + * + * @param manager + * @param clazz + */ + public QueryBuilder(EntityManager manager, Class<T> clazz) { + this.manager = manager; + this.clazz = clazz; + this.builder = this.manager.getCriteriaBuilder(); + this.query = this.builder.createQuery(this.clazz); + this.root = this.query.from(this.clazz); + } + + /** + * Get the criteria query build with this class. + * + * @return The query. + */ + public CriteriaQuery<T> getQuery() { + if (this.filter == null) { + this.query.where(); + return this.query; + } + this.query.where(this.filter); + return this.query; + } + + /** + * Logical AND operation. + * + * @param id The database column name. + * @param value The filter value + * @return The builder itself. + */ + public QueryBuilder<T> and(String id, Object value) { + Predicate p = this.builder.equal(this.root.get(id), value); + if (this.filter != null) { + this.filter = this.builder.and(this.filter, p); + } + else { + this.filter = this.builder.and(p); + } + return this; + } + + /** + * Logical AND with like operation. + * + * @param id The database column name. + * @param value The filter value + * @return The builder itself. + */ + public QueryBuilder<T> andLike(String id, String value) { + Path<String> path = this.root.get(id); + Predicate p = this.builder.like(path, value); + if (this.filter != null) { + this.filter = this.builder.and(this.filter, p); + } + else { + this.filter = this.builder.and(p); + } + return this; + } + + /** + * Logical OR operation. + * + * @param id The database column name + * @param value The filter value. + * @return The builder itself. + */ + public QueryBuilder<T> or(String id, Object value) { + Predicate p = this.builder.equal(this.root.get(id), value); + if (this.filter != null) { + this.filter = this.builder.or(this.filter, p); + } + else { + this.filter = this.builder.or(p); + } + return this; + } + + /** + * Logical OR with like operation. + * + * @param column The database column name. + * @param value The filter value + * @return The builder itself. + */ + public QueryBuilder<T> orLike(String id, String value) { + Path<String> path = this.root.get(id); + Predicate p = this.builder.like(path, value); + if (this.filter != null) { + this.filter = this.builder.or(this.filter, p); + } + else { + this.filter = this.builder.or(p); + } + return this; + } + + /** + * Logical AND operation. + * All elements in <i>values</i> will be concatenated with AND operator. + * + * @param id The database column name. + * @param values List of values. + * @return The builder itself. + */ + public QueryBuilder<T> and(String id, List<String> values) { + for(String v: values) { + this.and(id, v); + } + return this; + } + + /** + * Logical OR operation. + * All elements in <i>values</i> will be concatenated with OR operator. + * + * @param id The database column name. + * @param values List of values. + * @return The builder itself. + */ + public QueryBuilder<T> or(String id, List<String> values) { + for (String v: values) { + this.or(id, v); + } + return this; + } + + /** + * Logical AND operation. + * The actually defined query will be concatenated with the query defined + * in the builder <i>b</i>. + * + * @param b A builder. + * @return The builder itself. + */ + public QueryBuilder<T> and(QueryBuilder<T> b) { + if (b == null || b.filter == null) { + return this; + } + if (this.filter != null) { + this.filter = this.builder.and(this.filter, b.filter); + } + else { + this.filter = this.builder.and(b.filter); + } + return this; + } + + /** + * Logical OR operation. + * The actually defined query will be concatenated with the query defined + * in the builder <i>b</i>. + * + * @param b A builder. + * @return The builder itself. + */ + public QueryBuilder<T> or(QueryBuilder<T> b) { + if (b == null || b.filter == null) { + return this; + } + if (this.filter != null) { + this.filter = this.builder.or(this.filter, b.filter); + } + else { + this.filter = this.builder.or(b.filter); + } + return this; + } + + /** + * Use 'distinct' in the query. + */ + public void distinct() { + this.query.distinct(true); + } + + /** + * Order result by the specified column name + * + * @param id The column name. + * @param asc Ascending(true), Descending(false). + */ + public void orderBy(String id, boolean asc) { + if (asc) { + this.query.orderBy(this.builder.asc(this.root.get(id))); + } + else { + this.query.orderBy(this.builder.desc(this.root.get(id))); + } + } + + /** + * Get an empty instance of this builder to create subfilters. + * + * @return An empty instance of this builder. + */ + public QueryBuilder<T> getEmptyBuilder(){ + QueryBuilder<T> copy = new QueryBuilder<T>(manager, clazz); + copy.builder = this.builder; + copy.root = this.root; + return copy; + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/de/intevation/lada/util/data/ReadOnlyRepository.java Fri Feb 06 17:55:54 2015 +0100 @@ -0,0 +1,75 @@ +/* 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 java.util.List; + +import javax.persistence.EntityManager; +import javax.persistence.criteria.CriteriaQuery; + +import de.intevation.lada.util.rest.Response; + +/** + * @author rrenkert + */ +public class ReadOnlyRepository extends AbstractRepository { + + public ReadOnlyRepository() { + } + + @Override + public Response create(Object object) { + return null; + } + + @Override + public Response update(Object object) { + return null; + } + + @Override + public Response delete(Object object) { + return null; + } + + @Override + public <T> Response filter(CriteriaQuery<T> filter) { + 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) { + List<T> result = + emp.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); + } + + public <T> Response getAll(Class<T> clazz) { + EntityManager manager = emp.entityManager(dataSource); + QueryBuilder<T> builder = + new QueryBuilder<T>(manager, clazz); + List<T> result = + manager.createQuery(builder.getQuery()).getResultList(); + return new Response(true, 200, result); + } + + @Override + public <T> Response getById(Class<T> clazz, Object id) { + T item = emp.entityManager(dataSource).find(clazz, id); + if (item == null) { + return new Response(false, 600, null); + } + return new Response(true, 200, item); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/de/intevation/lada/util/data/Repository.java Fri Feb 06 17:55:54 2015 +0100 @@ -0,0 +1,44 @@ +/* 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.persistence.Query; +import javax.persistence.criteria.CriteriaQuery; + +import de.intevation.lada.util.rest.Response; + +/** + * This generic Container is an interface to request and select Data + * obejcts from the connected database. + * + * @author <a href="mailto:rrenkert@intevation.de">Raimund Renkert</a> + */ +public interface Repository { + + public Response create(Object object); + + public Response update(Object object); + + public Response delete(Object object); + + public <T> Response filter(CriteriaQuery<T> filter); + + public <T> Response filter(CriteriaQuery<T> filter, int size, int start); + + public <T> Response getAll(Class<T> clazz); + + public <T> Response getById(Class<T> clazz, Object id); + + public Query queryFromString(String sql); + + public void setDataSource(String dataSource); + + public String getDataSource(); + + public void setEntityManagerProducer(EntityManagerProducer emp); +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/de/intevation/lada/util/data/RepositoryType.java Fri Feb 06 17:55:54 2015 +0100 @@ -0,0 +1,13 @@ +/* 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; + + +public enum RepositoryType { + RO, RW +}
--- a/src/main/java/de/intevation/lada/util/factory/RepositoryFactory.java Fri Feb 06 15:58:28 2015 +0100 +++ b/src/main/java/de/intevation/lada/util/factory/RepositoryFactory.java Fri Feb 06 17:55:54 2015 +0100 @@ -13,12 +13,12 @@ import javax.enterprise.inject.spi.InjectionPoint; import javax.inject.Inject; -import de.intevation.lada.util.DefaultRepository; -import de.intevation.lada.util.EntityManagerProducer; -import de.intevation.lada.util.ReadOnlyRepository; -import de.intevation.lada.util.Repository; -import de.intevation.lada.util.RepositoryType; 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; @ApplicationScoped