Mercurial > lada > lada-server
comparison src/main/java/de/intevation/lada/data/LProbeRepository.java @ 303:c96fcc4dd5fd
New filter method with free parameters and resultset.
author | Raimund Renkert <rrenkert@intevation.de> |
---|---|
date | Fri, 16 Aug 2013 14:39:27 +0200 |
parents | 0de24f5e7c01 |
children | 567ce7697fc7 |
comparison
equal
deleted
inserted
replaced
299:a529909cffeb | 303:c96fcc4dd5fd |
---|---|
1 package de.intevation.lada.data; | 1 package de.intevation.lada.data; |
2 | 2 |
3 import java.util.ArrayList; | |
4 import java.util.HashMap; | |
3 import java.util.List; | 5 import java.util.List; |
4 import java.util.Map; | 6 import java.util.Map; |
5 | 7 |
6 import javax.ejb.EJBTransactionRolledbackException; | 8 import javax.ejb.EJBTransactionRolledbackException; |
7 import javax.enterprise.context.ApplicationScoped; | 9 import javax.enterprise.context.ApplicationScoped; |
8 import javax.inject.Inject; | 10 import javax.inject.Inject; |
9 import javax.inject.Named; | 11 import javax.inject.Named; |
10 import javax.persistence.EntityExistsException; | 12 import javax.persistence.EntityExistsException; |
11 import javax.persistence.EntityManager; | 13 import javax.persistence.EntityManager; |
14 import javax.persistence.Query; | |
12 import javax.persistence.TransactionRequiredException; | 15 import javax.persistence.TransactionRequiredException; |
13 import javax.persistence.criteria.CriteriaQuery; | 16 import javax.persistence.criteria.CriteriaQuery; |
14 | 17 import javax.ws.rs.core.MultivaluedMap; |
18 | |
19 import de.intevation.lada.auth.Authorization; | |
15 import de.intevation.lada.manage.Manager; | 20 import de.intevation.lada.manage.Manager; |
16 import de.intevation.lada.model.LProbe; | 21 import de.intevation.lada.model.LProbe; |
17 import de.intevation.lada.model.LProbeInfo; | 22 import de.intevation.lada.model.LProbeInfo; |
18 import de.intevation.lada.rest.Response; | 23 import de.intevation.lada.rest.Response; |
19 import de.intevation.lada.validation.ValidationException; | 24 import de.intevation.lada.validation.ValidationException; |
45 */ | 50 */ |
46 @Inject | 51 @Inject |
47 @Named("lprobevalidator") | 52 @Named("lprobevalidator") |
48 private Validator validator; | 53 private Validator validator; |
49 | 54 |
55 @Inject | |
56 @Named("dataauthorization") | |
57 private Authorization authorization; | |
58 | |
50 public EntityManager getEntityManager() { | 59 public EntityManager getEntityManager() { |
51 return this.em; | 60 return this.em; |
52 } | 61 } |
53 | 62 |
54 /** | 63 /** |
60 public <T> Response filter(CriteriaQuery<T> filter) { | 69 public <T> Response filter(CriteriaQuery<T> filter) { |
61 List<T> result = em.createQuery(filter).getResultList(); | 70 List<T> result = em.createQuery(filter).getResultList(); |
62 return new Response(true, 200, result); | 71 return new Response(true, 200, result); |
63 } | 72 } |
64 | 73 |
74 public Response filterFree( | |
75 String sql, | |
76 List<String> filters, | |
77 List<String> results, | |
78 MultivaluedMap<String, String> params) { | |
79 Query query = em.createNativeQuery(sql); | |
80 for (String filter: filters) { | |
81 query.setParameter(filter, params.get(filter)); | |
82 } | |
83 List<Object[]> result = query.getResultList(); | |
84 List<Map<String, Object>> res = new ArrayList<Map<String, Object>>(); | |
85 for (Object[] row: result) { | |
86 Map<String, Object> set = new HashMap<String, Object>(); | |
87 for (int i = 0; i < row.length; i++) { | |
88 set.put(results.get(i), row[i]); | |
89 if (results.get(i).equals("probeId")) { | |
90 if (authorization.isReadOnly((String)row[i])) { | |
91 set.put("readonly", Boolean.TRUE); | |
92 } | |
93 else { | |
94 set.put("readonly", Boolean.FALSE); | |
95 } | |
96 } | |
97 } | |
98 res.add(set); | |
99 } | |
100 return new Response(true, 200, res); | |
101 } | |
65 /** | 102 /** |
66 * Get all objects. | 103 * Get all objects. |
67 * | 104 * |
68 * @param clazz The object type. (unused) | 105 * @param clazz The object type. (unused) |
69 * @return Response object. | 106 * @return Response object. |