Mercurial > lada > lada-server
view src/main/java/de/intevation/lada/data/LProbeRepository.java @ 109:a2427e766398
Extracted filter method to have a generic criteria filter in the repository.
author | Raimund Renkert <rrenkert@intevation.de> |
---|---|
date | Wed, 12 Jun 2013 13:15:19 +0200 |
parents | 5abec5413d65 |
children | 39a501f3ace2 |
line wrap: on
line source
package de.intevation.lada.data; import java.util.Date; import java.util.List; import javax.ejb.EJBTransactionRolledbackException; import javax.enterprise.context.ApplicationScoped; 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.LProbeManager; import de.intevation.lada.model.LProbe; import de.intevation.lada.rest.Response; import de.intevation.lada.validation.ValidationException; import de.intevation.lada.validation.Validator; /** * This Container is an interface to request, filter and select LProbe * obejcts from the connected database. * * @author <a href="mailto:rrenkert@intevation.de">Raimund Renkert</a> */ @ApplicationScoped public class LProbeRepository extends Repository{ @Inject @Named("lprobevalidator") private Validator validator; /** * The entitymanager managing the data. */ @Inject private EntityManager em; /** * Manager class for LPRobe. Used to manipulate data objects. */ @Inject private LProbeManager manager; /** * Filter for LProbe objects used for calls from a service. * * @param mstId mst_id * @param uwbId umw_id * @param begin probeentnahmebegin * @return */ 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); Predicate mst = cb.equal(member.get("mstId"), mstId); Predicate uwb = cb.equal(member.get("umwId"), uwbId); if (!mstId.isEmpty() && !uwbId.isEmpty() && begin != null) { Predicate beg = cb.equal(member.get("probeentnahmeBeginn"), new Date(begin)); criteria.where(cb.and(mst, uwb, beg)); } else if (!mstId.isEmpty() && !uwbId.isEmpty() && begin == null) { criteria.where(cb.and(mst, uwb)); } else if (!mstId.isEmpty() && uwbId.isEmpty() && begin != null) { Predicate beg = cb.equal(member.get("probeentnahmeBeginn"), new Date(begin)); criteria.where(cb.and(mst, beg)); } else if (mstId.isEmpty() && !uwbId.isEmpty() && begin != null) { Predicate beg = cb.equal(member.get("probeentnahmeBeginn"), new Date(begin)); criteria.where(cb.and(uwb, beg)); } else if (!mstId.isEmpty() && uwbId.isEmpty() && begin == null) { criteria.where(mst); } else if (mstId.isEmpty() && !uwbId.isEmpty() && begin == null) { criteria.where(uwb); } else if (mstId.isEmpty() && uwbId.isEmpty() && begin != null) { Predicate beg = cb.equal(member.get("probeentnahmeBeginn"), new Date(begin)); criteria.where(beg); } List<LProbe> result = filter(criteria); return new Response(true, 200, result); } /** * Filter LProbe object list by the given criteria. * * @param criteria * @return List of LProbe objects. */ public List<LProbe> filter(CriteriaQuery<LProbe> criteria) { return em.createQuery(criteria).getResultList(); } /** * Validate and persist a new LProbe object. * * @param probe The new LProbe object * @return Response. */ public Response create(LProbe probe) { Response response = new Response(true, 200, probe); // Try to save the new LProbe. try { validator.validate(probe); manager.create(probe); response.setWarnings(validator.getWarnings()); 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 (ValidationException ve) { response.setSuccess(false); response.setMessage(604); response.setErrors(ve.getErrors()); response.setWarnings(validator.getWarnings()); } catch (EJBTransactionRolledbackException te) { response.setSuccess(false); response.setMessage(604); } return response; } public Response update(LProbe probe) { Response response = new Response(true, 200, probe); // Try to save the new LProbe. try { validator.validate(probe); manager.update(probe); response.setWarnings(validator.getWarnings()); 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 (ValidationException ve) { response.setSuccess(false); response.setMessage(604); response.setErrors(ve.getErrors()); response.setWarnings(validator.getWarnings()); } catch (EJBTransactionRolledbackException te) { response.setSuccess(false); response.setMessage(604); } return response; } }