# HG changeset patch # User Raimund Renkert # Date 1376656767 -7200 # Node ID c96fcc4dd5fdf8ee4e9df0096a20cd88de56910a # Parent a529909cffeb1d377050cff935e01096cc54ea40 New filter method with free parameters and resultset. diff -r a529909cffeb -r c96fcc4dd5fd src/main/java/de/intevation/lada/data/LProbeRepository.java --- a/src/main/java/de/intevation/lada/data/LProbeRepository.java Thu Aug 15 15:20:41 2013 +0200 +++ b/src/main/java/de/intevation/lada/data/LProbeRepository.java Fri Aug 16 14:39:27 2013 +0200 @@ -1,5 +1,7 @@ package de.intevation.lada.data; +import java.util.ArrayList; +import java.util.HashMap; import java.util.List; import java.util.Map; @@ -9,9 +11,12 @@ import javax.inject.Named; import javax.persistence.EntityExistsException; import javax.persistence.EntityManager; +import javax.persistence.Query; import javax.persistence.TransactionRequiredException; import javax.persistence.criteria.CriteriaQuery; +import javax.ws.rs.core.MultivaluedMap; +import de.intevation.lada.auth.Authorization; import de.intevation.lada.manage.Manager; import de.intevation.lada.model.LProbe; import de.intevation.lada.model.LProbeInfo; @@ -47,6 +52,10 @@ @Named("lprobevalidator") private Validator validator; + @Inject + @Named("dataauthorization") + private Authorization authorization; + public EntityManager getEntityManager() { return this.em; } @@ -62,6 +71,34 @@ return new Response(true, 200, result); } + public Response filterFree( + String sql, + List filters, + List results, + MultivaluedMap params) { + Query query = em.createNativeQuery(sql); + for (String filter: filters) { + query.setParameter(filter, params.get(filter)); + } + List result = query.getResultList(); + List> res = new ArrayList>(); + for (Object[] row: result) { + Map set = new HashMap(); + for (int i = 0; i < row.length; i++) { + set.put(results.get(i), row[i]); + if (results.get(i).equals("probeId")) { + if (authorization.isReadOnly((String)row[i])) { + set.put("readonly", Boolean.TRUE); + } + else { + set.put("readonly", Boolean.FALSE); + } + } + } + res.add(set); + } + return new Response(true, 200, res); + } /** * Get all objects. *