# HG changeset patch # User Raimund Renkert # Date 1371213787 -7200 # Node ID 32f5c27883beec32b5c836d92d6483d382a66b48 # Parent 490a4766290154ea5e4b50216c0fcb15e20fc342 New repository for LStatus objects. diff -r 490a47662901 -r 32f5c27883be src/main/java/de/intevation/lada/data/LStatusRepository.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/de/intevation/lada/data/LStatusRepository.java Fri Jun 14 14:43:07 2013 +0200 @@ -0,0 +1,125 @@ +package de.intevation.lada.data; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +import javax.ejb.EJBTransactionRolledbackException; +import javax.inject.Inject; +import javax.inject.Named; +import javax.persistence.EntityExistsException; +import javax.persistence.EntityManager; +import javax.persistence.TransactionRequiredException; +import javax.persistence.criteria.CriteriaBuilder; +import javax.persistence.criteria.CriteriaQuery; +import javax.persistence.criteria.Predicate; +import javax.persistence.criteria.Root; + +import de.intevation.lada.manage.LStatusManager; +import de.intevation.lada.manage.LZusatzwertManager; +import de.intevation.lada.model.LStatus; +import de.intevation.lada.rest.Response; + +/** + * This Container is an interface to request, filter and select LMesswert + * obejcts from the connected database. + * + * @author Raimund Renkert + */ +@Named("lstatusrepository") +public class LStatusRepository +extends Repository +{ + /** + * The entitymanager managing the data. + */ + @Inject + private EntityManager em; + + /** + * Manager class for LPRobe. Used to manipulate data objects. + */ + @Inject + private LStatusManager manager; + + @Override + public Response create(Object object) { + if (!(object instanceof LStatus)) { + return new Response(false, 602, object); + } + LStatus status = (LStatus)object; + Response response = new Response(true, 200, status); + // Try to save the new LProbe. + try { + manager.create(status); + return response; + } + catch (EntityExistsException eee) { + response.setSuccess(false); + response.setMessage(601); + } + catch (IllegalArgumentException iae) { + response.setSuccess(false); + response.setMessage(602); + } + catch (TransactionRequiredException tre) { + response.setSuccess(false); + response.setMessage(603); + } + catch (EJBTransactionRolledbackException te) { + response.setSuccess(false); + response.setMessage(604); + } + return response; + } + + @Override + public Response update(Object object) { + if (!(object instanceof LStatus)) { + return new Response(false, 602, object); + } + LStatus status = (LStatus)object; + Response response = new Response(true, 200, status); + try { + manager.update(status); + return response; + } + catch (EntityExistsException eee) { + response.setSuccess(false); + response.setMessage(601); + } + catch (IllegalArgumentException iae) { + response.setSuccess(false); + response.setMessage(602); + } + catch (TransactionRequiredException tre) { + response.setSuccess(false); + response.setMessage(603); + } + catch (EJBTransactionRolledbackException te) { + response.setSuccess(false); + response.setMessage(604); + } + return response; + } + + @Override + public Response filter(Map filter) { + CriteriaBuilder cb = em.getCriteriaBuilder(); + CriteriaQuery criteria = cb.createQuery(LStatus.class); + Root member = criteria.from(LStatus.class); + List andFilter = new ArrayList(); + if (filter.containsKey("probe")) { + andFilter.add(cb.equal(member.get("probeId"), filter.get("probe"))); + } + if (filter.containsKey("messung")) { + andFilter.add(cb.equal(member.get("messungsId"), filter.get("messung"))); + } + criteria.distinct(true); + Predicate ap = cb.and(andFilter.toArray(new Predicate[andFilter.size()])); + criteria.where(ap); + List result = filter(criteria); + return new Response(true, 200, result); + } + +}