Mercurial > lada > lada-server
changeset 848:c2725534f08b
Services no use the database based query filter.
author | Raimund Renkert <raimund.renkert@intevation.de> |
---|---|
date | Wed, 27 Jan 2016 14:54:28 +0100 |
parents | 35bbaf423128 |
children | d0a591b3eade |
files | src/main/java/de/intevation/lada/query/QueryTools.java src/main/java/de/intevation/lada/rest/MessprogrammService.java src/main/java/de/intevation/lada/rest/ProbeService.java src/main/java/de/intevation/lada/rest/stamm/DatensatzErzeugerService.java src/main/java/de/intevation/lada/rest/stamm/MessprogrammKategorieService.java src/main/java/de/intevation/lada/rest/stamm/ProbenehmerService.java |
diffstat | 6 files changed, 239 insertions(+), 126 deletions(-) [+] |
line wrap: on
line diff
--- a/src/main/java/de/intevation/lada/query/QueryTools.java Wed Jan 27 14:52:44 2016 +0100 +++ b/src/main/java/de/intevation/lada/query/QueryTools.java Wed Jan 27 14:54:28 2016 +0100 @@ -7,7 +7,6 @@ */ package de.intevation.lada.query; -import java.io.IOException; import java.io.InputStream; import java.io.StringReader; import java.util.ArrayList; @@ -16,15 +15,26 @@ import java.util.Map; import java.util.Scanner; +import javax.inject.Inject; import javax.json.Json; import javax.json.JsonArray; import javax.json.JsonException; import javax.json.JsonObject; import javax.json.JsonReader; import javax.persistence.EntityManager; -import javax.persistence.Query; import javax.ws.rs.core.MultivaluedMap; +import org.apache.log4j.Logger; + +import de.intevation.lada.model.stamm.Filter; +import de.intevation.lada.model.stamm.Query; +import de.intevation.lada.model.stamm.Result; +import de.intevation.lada.util.annotation.RepositoryConfig; +import de.intevation.lada.util.data.QueryBuilder; +import de.intevation.lada.util.data.Repository; +import de.intevation.lada.util.data.RepositoryType; +import de.intevation.lada.util.rest.Response; + /** * Utility class to handle the SQL query configuration. @@ -33,9 +43,18 @@ */ public class QueryTools { + + @Inject + @RepositoryConfig(type=RepositoryType.RO) + private Repository repository; + private static String PROBE_CONFIG = "/probequery.json"; private static String MESSPROGRAMM_CONFIG = "/messprogrammquery.json"; private static String STAMMDATEN_CONFIG = "/stammdatenquery.json"; + + @Inject + private Logger logger; + /** * Read the config file using the system property * "de.intevation.lada.sqlconfig". @@ -154,33 +173,74 @@ } } - public static Query prepareQuery( + public List<Map<String, Object>> getResultForQuery(MultivaluedMap<String, String> params, Integer qId, String type) { + QueryBuilder<Query> builder = new QueryBuilder<Query>( + repository.entityManager("stamm"), + Query.class + ); + builder.and("id", qId); + Query query = repository.filterPlain(builder.getQuery(), "stamm").get(0); + if (!query.getType().equals(type)) { + return null; + } + + String sql = query.getSql(); + + List<Filter> filters = query.getFilters(); + QueryBuilder<Result> rBuilder = new QueryBuilder<Result>( + repository.entityManager("stamm"), + Result.class + ); + rBuilder.and("query", qId); + rBuilder.orderBy("index", true); + List<Result> results = repository.filterPlain(rBuilder.getQuery(), "stamm"); + Result idResult = new Result(); + idResult.setDataIndex("id"); + results.add(0, idResult); + if (params.containsKey("sort")) { + String sort = params.getFirst("sort"); + logger.debug("Sort parameter: " + sort); + JsonReader reader = Json.createReader(new StringReader(sort)); + JsonObject sortProperties = reader.readArray().getJsonObject(0); + sql += " ORDER BY "; + sql += sortProperties.getJsonString("property").getString() + " "; + sql += sortProperties.getJsonString("direction").getString(); + } + javax.persistence.Query q = prepareQuery( + sql, + filters, + params, + repository.entityManager("land")); + return prepareResult(q.getResultList(), results); + } + + public javax.persistence.Query prepareQuery( String sql, - List<String> filters, + List<Filter> filters, MultivaluedMap<String, String> params, EntityManager manager ) { - Query query = manager.createNativeQuery(sql); - for (String filter: filters) { - List<String> param = params.get(filter); + javax.persistence.Query query = manager.createNativeQuery(sql); + for (Filter filter: filters) { + List<String> param = params.get(filter.getDataIndex()); List<String> clean = new ArrayList<String>(); for(String p : param) { clean.add(p.replace(",", "|")); } - query.setParameter(filter, clean); + query.setParameter(filter.getDataIndex(), clean); } return query; } - public static List<Map<String, Object>> prepareResult( + public List<Map<String, Object>> prepareResult( List<Object[]> result, - List<String> names + List<Result> names ) { List<Map<String, Object>> ret = new ArrayList<Map<String, Object>>(); for (Object[] row: result) { Map<String, Object> set = new HashMap<String, Object>(); for (int i = 0; i < row.length; i++) { - set.put(names.get(i), row[i]); + set.put(names.get(i).getDataIndex(), row[i]); } ret.add(set); }
--- a/src/main/java/de/intevation/lada/rest/MessprogrammService.java Wed Jan 27 14:52:44 2016 +0100 +++ b/src/main/java/de/intevation/lada/rest/MessprogrammService.java Wed Jan 27 14:54:28 2016 +0100 @@ -7,21 +7,13 @@ */ package de.intevation.lada.rest; -import java.io.StringReader; import java.sql.Timestamp; -import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Map; import javax.enterprise.context.RequestScoped; import javax.inject.Inject; -import javax.json.Json; -import javax.json.JsonArray; -import javax.json.JsonException; -import javax.json.JsonObject; -import javax.json.JsonReader; -import javax.persistence.Query; import javax.servlet.http.HttpServletRequest; import javax.ws.rs.DELETE; import javax.ws.rs.GET; @@ -113,6 +105,9 @@ @Inject private ProbeFactory factory; + @Inject + private QueryTools queryTools; + /** * Get all Messprogramm objects. * <p> @@ -143,44 +138,18 @@ if (params.isEmpty() || !params.containsKey("qid")) { return defaultRepo.getAll(Messprogramm.class, "land"); } - String qid = params.getFirst("qid"); - JsonObject jsonQuery = QueryTools.getMpQueryById(qid); - String sql = ""; - List<String> filters = new ArrayList<String>(); - List<String> results = new ArrayList<String>(); + Integer id = null; try { - sql = jsonQuery.getString("sql"); - if (params.containsKey("sort")) { - String sort = params.getFirst("sort"); - JsonReader reader = Json.createReader(new StringReader(sort)); - JsonObject sortProperties = reader.readArray().getJsonObject(0); - sql += " ORDER BY "; - sql += sortProperties.getJsonString("property").getString() + " "; - sql += sortProperties.getJsonString("direction").getString(); - } - JsonArray jsonFilters = jsonQuery.getJsonArray("filters"); - JsonArray jsonResults = jsonQuery.getJsonArray("result"); - for (int i = 0; i < jsonFilters.size(); i++) { - filters.add( - jsonFilters.getJsonObject(i).getString("dataIndex")); - } - results.add("id"); - for (int i = 0; i < jsonResults.size(); i++) { - results.add( - jsonResults.getJsonObject(i).getString("dataIndex")); - } + id = Integer.valueOf(params.getFirst("qid")); } - catch (JsonException je) { - return new Response(false, 603, new ArrayList<Object>()); + catch (NumberFormatException e) { + return new Response(false, 603, "Not a valid filter id"); } - Query query = QueryTools.prepareQuery( - sql, - filters, - params, - defaultRepo.entityManager("land")); - @SuppressWarnings("unchecked") + List<Map<String, Object>> result = - QueryTools.prepareResult(query.getResultList(), results); + queryTools.getResultForQuery(params, id, "messprogramm"); + + int size = result.size(); if (params.containsKey("start") && params.containsKey("limit")) { int start = Integer.valueOf(params.getFirst("start")); int limit = Integer.valueOf(params.getFirst("limit")); @@ -188,10 +157,9 @@ if (start + limit > result.size()) { end = result.size(); } - List<Map<String, Object>> subList = result.subList(start, end); - return new Response(true, 200, subList, result.size()); + result = result.subList(start, end); } - return new Response(true, 200, result, result.size()); + return new Response(true, 200, result, size); } /**
--- a/src/main/java/de/intevation/lada/rest/ProbeService.java Wed Jan 27 14:52:44 2016 +0100 +++ b/src/main/java/de/intevation/lada/rest/ProbeService.java Wed Jan 27 14:54:28 2016 +0100 @@ -7,9 +7,7 @@ */ package de.intevation.lada.rest; -import java.io.StringReader; import java.sql.Timestamp; -import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Map; @@ -17,12 +15,7 @@ import javax.ejb.EJBTransactionRolledbackException; import javax.enterprise.context.RequestScoped; import javax.inject.Inject; -import javax.json.Json; -import javax.json.JsonArray; -import javax.json.JsonException; import javax.json.JsonObject; -import javax.json.JsonReader; -import javax.persistence.Query; import javax.persistence.TransactionRequiredException; import javax.servlet.http.HttpServletRequest; import javax.ws.rs.DELETE; @@ -51,7 +44,6 @@ import de.intevation.lada.util.annotation.RepositoryConfig; import de.intevation.lada.util.auth.Authorization; import de.intevation.lada.util.auth.AuthorizationType; -import de.intevation.lada.util.auth.UserInfo; import de.intevation.lada.util.data.QueryBuilder; import de.intevation.lada.util.data.Repository; import de.intevation.lada.util.data.RepositoryType; @@ -126,7 +118,7 @@ */ @Inject @RepositoryConfig(type=RepositoryType.RW) - private Repository defaultRepo; + private Repository repository; /** * The authorization module. @@ -156,6 +148,9 @@ @Inject private ProbeFactory factory; + @Inject + private QueryTools queryTools; + /** * Get all Probe objects. * <p> @@ -186,46 +181,19 @@ ) { MultivaluedMap<String, String> params = info.getQueryParameters(); if (params.isEmpty() || !params.containsKey("qid")) { - return defaultRepo.getAll(LProbe.class, "land"); + return repository.getAll(LProbe.class, "land"); } - String qid = params.getFirst("qid"); - JsonObject jsonQuery = QueryTools.getQueryById(qid); - String sql = ""; - List<String> filters = new ArrayList<String>(); - List<String> results = new ArrayList<String>(); + Integer id = null; try { - sql = jsonQuery.getString("sql"); - if (params.containsKey("sort")) { - String sort = params.getFirst("sort"); - logger.debug("Sort parameter: " + sort); - JsonReader reader = Json.createReader(new StringReader(sort)); - JsonObject sortProperties = reader.readArray().getJsonObject(0); - sql += " ORDER BY "; - sql += sortProperties.getJsonString("property").getString() + " "; - sql += sortProperties.getJsonString("direction").getString(); - } - JsonArray jsonFilters = jsonQuery.getJsonArray("filters"); - JsonArray jsonResults = jsonQuery.getJsonArray("result"); - for (int i = 0; i < jsonFilters.size(); i++) { - filters.add( - jsonFilters.getJsonObject(i).getString("dataIndex")); - } - results.add("id"); - for (int i = 0; i < jsonResults.size(); i++) { - results.add( - jsonResults.getJsonObject(i).getString("dataIndex")); - } + id = Integer.valueOf(params.getFirst("qid")); } - catch (JsonException je) { - return new Response(false, 603, new ArrayList<Object>()); + catch (NumberFormatException e) { + return new Response(false, 603, "Not a valid filter id"); } - Query query = QueryTools.prepareQuery( - sql, - filters, - params, - defaultRepo.entityManager("land")); List<Map<String, Object>> result = - QueryTools.prepareResult(query.getResultList(), results); + queryTools.getResultForQuery(params, id, "probe"); + + int size = result.size(); if (params.containsKey("start") && params.containsKey("limit")) { int start = Integer.valueOf(params.getFirst("start")); int limit = Integer.valueOf(params.getFirst("limit")); @@ -233,22 +201,36 @@ if (start + limit > result.size()) { end = result.size(); } - List<Map<String, Object>> subList = result.subList(start, end); - for (Map<String, Object> entry: subList) { - boolean readOnly = - authorization.isReadOnly((Integer)entry.get("id")); - entry.put("readonly", readOnly); - QueryBuilder<LProbe> builder = new QueryBuilder<LProbe>( - defaultRepo.entityManager("land"), LProbe.class); - builder.and("id", (Integer)entry.get("id")); - Response r = defaultRepo.filter(builder.getQuery(), "land"); - List<LProbe> probe = (List<LProbe>)r.getData(); - entry.put("owner", authorization.isAuthorized( - request, probe.get(0), RequestMethod.GET, LProbe.class)); + result = result.subList(start, end); + } + + QueryBuilder<LProbe> pBuilder = new QueryBuilder<LProbe>( + repository.entityManager("land"), LProbe.class); + for (Map<String, Object> entry: result) { + pBuilder.or("id", (Integer)entry.get("id")); + } + Response r = repository.filter(pBuilder.getQuery(), "land"); + r = authorization.filter(request, r, LProbe.class); + List<LProbe> proben = (List<LProbe>)r.getData(); + for (Map<String, Object> entry: result) { + Integer pId = Integer.valueOf(entry.get("id").toString()); + setAuthData(proben, entry, pId); + } + return new Response(true, 200, result, size); + } + + private void setAuthData( + List<LProbe> proben, + Map<String, Object> entry, + Integer id + ) { + for (int i = 0; i < proben.size(); i++) { + if (id.equals(proben.get(i).getId())) { + entry.put("readonly", proben.get(i).getReadonly()); + entry.put("owner", proben.get(i).getOwner()); + return; } - return new Response(true, 200, subList, result.size()); } - return new Response(true, 200, result, result.size()); } /** @@ -269,7 +251,7 @@ @Context HttpServletRequest request ) { Response response = - defaultRepo.getById(LProbe.class, Integer.valueOf(id), "land"); + repository.getById(LProbe.class, Integer.valueOf(id), "land"); Violation violation = validator.validate(response.getData()); if (violation.hasWarnings()) { response.setWarnings(violation.getWarnings()); @@ -342,15 +324,15 @@ } probe = factory.findMediaDesk(probe); /* Persist the new probe object*/ - Response newProbe = defaultRepo.create(probe, "land"); + Response newProbe = repository.create(probe, "land"); LProbe ret = (LProbe)newProbe.getData(); /* Create and persist a new probe translation object*/ ProbeTranslation trans = new ProbeTranslation(); trans.setProbeId(ret); - defaultRepo.create(trans, "land"); + repository.create(trans, "land"); /* Get and return the new probe object*/ Response response = - defaultRepo.getById(LProbe.class, ret.getId(), "land"); + repository.getById(LProbe.class, ret.getId(), "land"); if(violation.hasWarnings()) { response.setWarnings(violation.getWarnings()); } @@ -464,8 +446,8 @@ factory.findUmweltId(probe); } probe.setLetzteAenderung(new Timestamp(new Date().getTime())); - Response response = defaultRepo.update(probe, "land"); - Response updated = defaultRepo.getById( + Response response = repository.update(probe, "land"); + Response updated = repository.getById( LProbe.class, ((LProbe)response.getData()).getId(), "land"); if (violation.hasWarnings()) { @@ -496,7 +478,7 @@ ) { /* Get the probe object by id*/ Response probe = - defaultRepo.getById(LProbe.class, Integer.valueOf(id), "land"); + repository.getById(LProbe.class, Integer.valueOf(id), "land"); LProbe probeObj = (LProbe)probe.getData(); if (!authorization.isAuthorized( request, @@ -508,7 +490,7 @@ } /* Delete the probe object*/ try { - Response response = defaultRepo.delete(probeObj, "land"); + Response response = repository.delete(probeObj, "land"); return response; } catch(IllegalArgumentException | EJBTransactionRolledbackException |
--- a/src/main/java/de/intevation/lada/rest/stamm/DatensatzErzeugerService.java Wed Jan 27 14:52:44 2016 +0100 +++ b/src/main/java/de/intevation/lada/rest/stamm/DatensatzErzeugerService.java Wed Jan 27 14:54:28 2016 +0100 @@ -26,10 +26,13 @@ import javax.ws.rs.core.UriInfo; import de.intevation.lada.model.stamm.DatensatzErzeuger; +import de.intevation.lada.model.stamm.Filter; +import de.intevation.lada.model.stamm.MessprogrammKategorie; import de.intevation.lada.util.annotation.AuthorizationConfig; import de.intevation.lada.util.annotation.RepositoryConfig; import de.intevation.lada.util.auth.Authorization; import de.intevation.lada.util.auth.AuthorizationType; +import de.intevation.lada.util.data.QueryBuilder; import de.intevation.lada.util.data.Repository; import de.intevation.lada.util.data.RepositoryType; import de.intevation.lada.util.rest.RequestMethod; @@ -93,8 +96,40 @@ @Context UriInfo info ) { MultivaluedMap<String, String> params = info.getQueryParameters(); - List<DatensatzErzeuger> erzeuger = - repository.getAllPlain(DatensatzErzeuger.class, "stamm"); + List<DatensatzErzeuger> erzeuger; + if (params.containsKey("qid")) { + Integer id = null; + try { + id = Integer.valueOf(params.getFirst("qid")); + } + catch (NumberFormatException e) { + return new Response(false, 603, "Not a valid filter id"); + } + QueryBuilder<Filter> fBuilder = new QueryBuilder<Filter>( + repository.entityManager("stamm"), + Filter.class + ); + fBuilder.and("query", id); + List<Filter> filters = repository.filterPlain(fBuilder.getQuery(), "stamm"); + QueryBuilder<DatensatzErzeuger> builder = + new QueryBuilder<DatensatzErzeuger>( + repository.entityManager("stamm"), + DatensatzErzeuger.class + ); + for (Filter filter: filters) { + String param = params.get(filter.getDataIndex()).get(0); + if (param == null || param.isEmpty()) { + continue; + } + builder.or(filter.getDataIndex(), param); + } + + erzeuger = repository.filterPlain(builder.getQuery(), "stamm"); + } + else { + erzeuger = repository.getAllPlain(DatensatzErzeuger.class, "stamm"); + } + int size = erzeuger.size(); if (params.containsKey("start") && params.containsKey("limit")) { int start = Integer.valueOf(params.getFirst("start"));
--- a/src/main/java/de/intevation/lada/rest/stamm/MessprogrammKategorieService.java Wed Jan 27 14:52:44 2016 +0100 +++ b/src/main/java/de/intevation/lada/rest/stamm/MessprogrammKategorieService.java Wed Jan 27 14:54:28 2016 +0100 @@ -25,11 +25,13 @@ import javax.ws.rs.core.MultivaluedMap; import javax.ws.rs.core.UriInfo; +import de.intevation.lada.model.stamm.Filter; import de.intevation.lada.model.stamm.MessprogrammKategorie; import de.intevation.lada.util.annotation.AuthorizationConfig; import de.intevation.lada.util.annotation.RepositoryConfig; import de.intevation.lada.util.auth.Authorization; import de.intevation.lada.util.auth.AuthorizationType; +import de.intevation.lada.util.data.QueryBuilder; import de.intevation.lada.util.data.Repository; import de.intevation.lada.util.data.RepositoryType; import de.intevation.lada.util.rest.RequestMethod; @@ -93,8 +95,39 @@ @Context UriInfo info ) { MultivaluedMap<String, String> params = info.getQueryParameters(); - List<MessprogrammKategorie> kategorie = - repository.getAllPlain(MessprogrammKategorie.class, "stamm"); + List<MessprogrammKategorie> kategorie; + if (params.containsKey("qid")) { + Integer id = null; + try { + id = Integer.valueOf(params.getFirst("qid")); + } + catch (NumberFormatException e) { + return new Response(false, 603, "Not a valid filter id"); + } + QueryBuilder<Filter> fBuilder = new QueryBuilder<Filter>( + repository.entityManager("stamm"), + Filter.class + ); + fBuilder.and("query", id); + List<Filter> filters = repository.filterPlain(fBuilder.getQuery(), "stamm"); + QueryBuilder<MessprogrammKategorie> mBuilder = + new QueryBuilder<MessprogrammKategorie>( + repository.entityManager("stamm"), + MessprogrammKategorie.class + ); + for (Filter filter: filters) { + String param = params.get(filter.getDataIndex()).get(0); + if (param == null || param.isEmpty()) { + continue; + } + mBuilder.or(filter.getDataIndex(), param); + } + + kategorie = repository.filterPlain(mBuilder.getQuery(), "stamm"); + } + else { + kategorie = repository.getAllPlain(MessprogrammKategorie.class, "stamm"); + } int size = kategorie.size(); if (params.containsKey("start") && params.containsKey("limit")) { int start = Integer.valueOf(params.getFirst("start"));
--- a/src/main/java/de/intevation/lada/rest/stamm/ProbenehmerService.java Wed Jan 27 14:52:44 2016 +0100 +++ b/src/main/java/de/intevation/lada/rest/stamm/ProbenehmerService.java Wed Jan 27 14:54:28 2016 +0100 @@ -25,11 +25,13 @@ import javax.ws.rs.core.MultivaluedMap; import javax.ws.rs.core.UriInfo; +import de.intevation.lada.model.stamm.Filter; import de.intevation.lada.model.stamm.Probenehmer; import de.intevation.lada.util.annotation.AuthorizationConfig; import de.intevation.lada.util.annotation.RepositoryConfig; import de.intevation.lada.util.auth.Authorization; import de.intevation.lada.util.auth.AuthorizationType; +import de.intevation.lada.util.data.QueryBuilder; import de.intevation.lada.util.data.Repository; import de.intevation.lada.util.data.RepositoryType; import de.intevation.lada.util.rest.RequestMethod; @@ -92,6 +94,39 @@ MultivaluedMap<String, String> params = info.getQueryParameters(); List<Probenehmer> nehmer = repository.getAllPlain(Probenehmer.class, "stamm"); + if (params.containsKey("qid")) { + Integer id = null; + try { + id = Integer.valueOf(params.getFirst("qid")); + } + catch (NumberFormatException e) { + return new Response(false, 603, "Not a valid filter id"); + } + QueryBuilder<Filter> fBuilder = new QueryBuilder<Filter>( + repository.entityManager("stamm"), + Filter.class + ); + fBuilder.and("query", id); + List<Filter> filters = repository.filterPlain(fBuilder.getQuery(), "stamm"); + QueryBuilder<Probenehmer> builder = + new QueryBuilder<Probenehmer>( + repository.entityManager("stamm"), + Probenehmer.class + ); + for (Filter filter: filters) { + String param = params.get(filter.getDataIndex()).get(0); + if (param == null || param.isEmpty()) { + continue; + } + builder.or(filter.getDataIndex(), param); + } + + nehmer = repository.filterPlain(builder.getQuery(), "stamm"); + } + else { + nehmer = repository.getAllPlain(Probenehmer.class, "stamm"); + } + int size = nehmer.size(); if (params.containsKey("start") && params.containsKey("limit")) { int start = Integer.valueOf(params.getFirst("start"));