# HG changeset patch # User Raimund Renkert # Date 1381323448 -7200 # Node ID 701b52518dcd85a0d1b6267272deab4c75f85a8d # Parent 816291da1ddeb06c878adc5c9b4958139d299c7e# Parent a72df178f1b9ca41b7f57e706ff8af3c3618a4d1 merged. diff -r a72df178f1b9 -r 701b52518dcd src/main/java/de/intevation/lada/data/LKommentarMRepository.java --- a/src/main/java/de/intevation/lada/data/LKommentarMRepository.java Fri Sep 27 14:54:02 2013 +0200 +++ b/src/main/java/de/intevation/lada/data/LKommentarMRepository.java Wed Oct 09 14:57:28 2013 +0200 @@ -179,4 +179,8 @@ } return response; } + @Override + public Response filter(CriteriaQuery filter, int size, int start) { + return null; + } } diff -r a72df178f1b9 -r 701b52518dcd src/main/java/de/intevation/lada/data/LKommentarPRepository.java --- a/src/main/java/de/intevation/lada/data/LKommentarPRepository.java Fri Sep 27 14:54:02 2013 +0200 +++ b/src/main/java/de/intevation/lada/data/LKommentarPRepository.java Wed Oct 09 14:57:28 2013 +0200 @@ -187,4 +187,9 @@ } return response; } + + @Override + public Response filter(CriteriaQuery filter, int size, int start) { + return null; + } } diff -r a72df178f1b9 -r 701b52518dcd src/main/java/de/intevation/lada/data/LMessungRepository.java --- a/src/main/java/de/intevation/lada/data/LMessungRepository.java Fri Sep 27 14:54:02 2013 +0200 +++ b/src/main/java/de/intevation/lada/data/LMessungRepository.java Wed Oct 09 14:57:28 2013 +0200 @@ -207,4 +207,8 @@ } return response; } + @Override + public Response filter(CriteriaQuery filter, int size, int start) { + return null; + } } diff -r a72df178f1b9 -r 701b52518dcd src/main/java/de/intevation/lada/data/LMesswertRepository.java --- a/src/main/java/de/intevation/lada/data/LMesswertRepository.java Fri Sep 27 14:54:02 2013 +0200 +++ b/src/main/java/de/intevation/lada/data/LMesswertRepository.java Wed Oct 09 14:57:28 2013 +0200 @@ -195,4 +195,8 @@ } return response; } + @Override + public Response filter(CriteriaQuery filter, int size, int start) { + return null; + } } \ No newline at end of file diff -r a72df178f1b9 -r 701b52518dcd src/main/java/de/intevation/lada/data/LOrtRepository.java --- a/src/main/java/de/intevation/lada/data/LOrtRepository.java Fri Sep 27 14:54:02 2013 +0200 +++ b/src/main/java/de/intevation/lada/data/LOrtRepository.java Wed Oct 09 14:57:28 2013 +0200 @@ -208,4 +208,9 @@ } return response; } + + @Override + public Response filter(CriteriaQuery filter, int size, int start) { + return null; + } } diff -r a72df178f1b9 -r 701b52518dcd src/main/java/de/intevation/lada/data/LProbeRepository.java --- a/src/main/java/de/intevation/lada/data/LProbeRepository.java Fri Sep 27 14:54:02 2013 +0200 +++ b/src/main/java/de/intevation/lada/data/LProbeRepository.java Wed Oct 09 14:57:28 2013 +0200 @@ -223,4 +223,9 @@ public Response delete(Object object) { return null; } + + @Override + public Response filter(CriteriaQuery filter, int size, int start) { + return null; + } } diff -r a72df178f1b9 -r 701b52518dcd src/main/java/de/intevation/lada/data/LStatusRepository.java --- a/src/main/java/de/intevation/lada/data/LStatusRepository.java Fri Sep 27 14:54:02 2013 +0200 +++ b/src/main/java/de/intevation/lada/data/LStatusRepository.java Wed Oct 09 14:57:28 2013 +0200 @@ -181,4 +181,9 @@ } return response; } + + @Override + public Response filter(CriteriaQuery filter, int size, int start) { + return null; + } } diff -r a72df178f1b9 -r 701b52518dcd src/main/java/de/intevation/lada/data/LZusatzwertRepository.java --- a/src/main/java/de/intevation/lada/data/LZusatzwertRepository.java Fri Sep 27 14:54:02 2013 +0200 +++ b/src/main/java/de/intevation/lada/data/LZusatzwertRepository.java Wed Oct 09 14:57:28 2013 +0200 @@ -192,4 +192,9 @@ } return response; } + + @Override + public Response filter(CriteriaQuery filter, int size, int start) { + return null; + } } diff -r a72df178f1b9 -r 701b52518dcd src/main/java/de/intevation/lada/data/OrtRepository.java --- a/src/main/java/de/intevation/lada/data/OrtRepository.java Fri Sep 27 14:54:02 2013 +0200 +++ b/src/main/java/de/intevation/lada/data/OrtRepository.java Wed Oct 09 14:57:28 2013 +0200 @@ -171,4 +171,9 @@ } return response; } + + @Override + public Response filter(CriteriaQuery filter, int size, int start) { + return null; + } } \ No newline at end of file diff -r a72df178f1b9 -r 701b52518dcd src/main/java/de/intevation/lada/data/QueryBuilder.java --- a/src/main/java/de/intevation/lada/data/QueryBuilder.java Fri Sep 27 14:54:02 2013 +0200 +++ b/src/main/java/de/intevation/lada/data/QueryBuilder.java Wed Oct 09 14:57:28 2013 +0200 @@ -5,6 +5,7 @@ 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; @@ -63,6 +64,25 @@ } /** + * Logical AND with like operation. + * + * @param id The database column name. + * @param value The filter value + * @return The builder itself. + */ + public QueryBuilder andLike(String id, String value) { + Path 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 @@ -81,6 +101,25 @@ } /** + * Logical OR with like operation. + * + * @param column The database column name. + * @param value The filter value + * @return The builder itself. + */ + public QueryBuilder orLike(String id, String value) { + Path 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 values will be concatenated with AND operator. * diff -r a72df178f1b9 -r 701b52518dcd src/main/java/de/intevation/lada/data/ReadOnlyRepository.java --- a/src/main/java/de/intevation/lada/data/ReadOnlyRepository.java Fri Sep 27 14:54:02 2013 +0200 +++ b/src/main/java/de/intevation/lada/data/ReadOnlyRepository.java Wed Oct 09 14:57:28 2013 +0200 @@ -43,6 +43,21 @@ } /** + * Filter object list by the given criteria. + * + * @param filter The filter query. + * @return Response object. + */ + public Response filter(CriteriaQuery filter, int size, int start) { + List result = em.createQuery(filter).getResultList(); + if (size > 0 && start > -1) { + List newList = result.subList(start, size + start); + return new Response(true, 200, newList, result.size()); + } + return new Response(true, 200, result); + } + + /** * Get all objects of type clazzfrom database. * * @param clazz The object type. diff -r a72df178f1b9 -r 701b52518dcd src/main/java/de/intevation/lada/data/Repository.java --- a/src/main/java/de/intevation/lada/data/Repository.java Fri Sep 27 14:54:02 2013 +0200 +++ b/src/main/java/de/intevation/lada/data/Repository.java Wed Oct 09 14:57:28 2013 +0200 @@ -21,6 +21,8 @@ public Response filter(CriteriaQuery filter); + public Response filter(CriteriaQuery filter, int size, int start); + public Response findAll(Class clazz); public Response findById(Class clazz, String id); diff -r a72df178f1b9 -r 701b52518dcd src/main/java/de/intevation/lada/rest/Response.java --- a/src/main/java/de/intevation/lada/rest/Response.java Fri Sep 27 14:54:02 2013 +0200 +++ b/src/main/java/de/intevation/lada/rest/Response.java Wed Oct 09 14:57:28 2013 +0200 @@ -28,6 +28,7 @@ private Map errors; private Map warnings; private Boolean readonly; + private int totalCount; /** * Constructor to create a basic Response object. @@ -43,6 +44,24 @@ this.errors = new HashMap(); this.warnings = new HashMap(); this.readonly = Boolean.FALSE; + this.totalCount = 0; + } + + /** + * Constructor to create a basic Response object. + * + * @param success Information if the operation was successful. + * @param code The return code. + * @param data The data object wrapped by the response. + */ + public Response(boolean success, int code, Object data, int totalCount) { + this.success = success; + this.message = Integer.toString(code); + this.data = data; + this.errors = new HashMap(); + this.warnings = new HashMap(); + this.readonly = Boolean.FALSE; + this.totalCount = totalCount; } public Boolean getSuccess() { @@ -93,6 +112,20 @@ this.readonly = readonly; } + /** + * @return the totalCount + */ + public int getTotalCount() { + return totalCount; + } + + /** + * @param totalCount the totalCount to set + */ + public void setTotalCount(int totalCount) { + this.totalCount = totalCount; + } + private HashMap convertCodes(Map codes) { HashMap converted = new HashMap(); if (codes == null || codes.isEmpty()) { diff -r a72df178f1b9 -r 701b52518dcd src/main/java/de/intevation/lada/rest/SVerwaltungseinheitService.java --- a/src/main/java/de/intevation/lada/rest/SVerwaltungseinheitService.java Fri Sep 27 14:54:02 2013 +0200 +++ b/src/main/java/de/intevation/lada/rest/SVerwaltungseinheitService.java Wed Oct 09 14:57:28 2013 +0200 @@ -11,9 +11,12 @@ import javax.ws.rs.Produces; import javax.ws.rs.core.Context; import javax.ws.rs.core.HttpHeaders; +import javax.ws.rs.core.MultivaluedMap; +import javax.ws.rs.core.UriInfo; import de.intevation.lada.auth.Authentication; import de.intevation.lada.auth.AuthenticationException; +import de.intevation.lada.data.QueryBuilder; import de.intevation.lada.data.Repository; import de.intevation.lada.model.SVerwaltungseinheit; @@ -46,7 +49,7 @@ * @param headers The HTTP header containing authorization information. * @return Response object. */ - @GET +/* @GET @Produces("text/json") public Response findAll(@Context HttpHeaders headers) { try { @@ -59,7 +62,7 @@ return new Response(false, 699, new ArrayList()); } } - +*/ /** * Request a SVerwaltungseinheit object via its id. * @@ -84,4 +87,37 @@ return new Response(false, 699, new ArrayList()); } } + + /** + * Request SVerwaltungseinheit objects filtered by the given criteria. + * + * @param filter The filter string. + * @param headers The HTTP header containing authorization information. + * @return Response object. + */ + @GET + @Produces("text/json") + public Response filter( + @Context UriInfo info, + @Context HttpHeaders headers + ){ + try { + if (!authentication.isAuthorizedUser(headers)) { + return new Response(false, 699, null); + } + MultivaluedMap params = info.getQueryParameters(); + if (params.isEmpty() || !params.containsKey("query")) { + return repository.findAll(SVerwaltungseinheit.class); + } + String filter = params.getFirst("query"); + QueryBuilder builder = + new QueryBuilder( + repository.getEntityManager(), SVerwaltungseinheit.class); + builder.andLike("bezeichnung", filter + "%"); + return repository.filter(builder.getQuery()); + } + catch(AuthenticationException ae) { + return new Response(false, 699, null); + } + } }