Mercurial > lada > lada-server
changeset 791:48fab56ae827
mergedmerged..
author | Raimund Renkert <raimund.renkert@intevation.de> |
---|---|
date | Wed, 18 Nov 2015 15:40:00 +0100 |
parents | 4262acdd466d (diff) 353269b89c4c (current diff) |
children | 1cf168a038c9 |
files | |
diffstat | 11 files changed, 236 insertions(+), 44 deletions(-) [+] |
line wrap: on
line diff
--- a/src/main/java/de/intevation/lada/rest/StatusService.java Mon Nov 16 16:34:50 2015 +0100 +++ b/src/main/java/de/intevation/lada/rest/StatusService.java Wed Nov 18 15:40:00 2015 +0100 @@ -7,6 +7,8 @@ */ package de.intevation.lada.rest; +import java.util.List; + import javax.enterprise.context.RequestScoped; import javax.inject.Inject; import javax.servlet.http.HttpServletRequest; @@ -23,6 +25,8 @@ import javax.ws.rs.core.MultivaluedMap; import javax.ws.rs.core.UriInfo; +import org.apache.log4j.Logger; + import de.intevation.lada.lock.LockConfig; import de.intevation.lada.lock.LockType; import de.intevation.lada.lock.ObjectLocker; @@ -38,6 +42,9 @@ import de.intevation.lada.util.data.RepositoryType; import de.intevation.lada.util.rest.RequestMethod; import de.intevation.lada.util.rest.Response; +import de.intevation.lada.validation.Validator; +import de.intevation.lada.validation.Violation; +import de.intevation.lada.validation.annotation.ValidationConfig; /** * REST service for Status objects. @@ -77,6 +84,9 @@ @RequestScoped public class StatusService { + @Inject + private Logger logger = Logger.getLogger(StatusService.class); + /** * The data repository granting read/write access. */ @@ -98,6 +108,10 @@ @AuthorizationConfig(type=AuthorizationType.HEADER) private Authorization authorization; + @Inject + @ValidationConfig(type="Status") + private Validator validator; + /** * Get all Status objects. * <p> @@ -149,9 +163,20 @@ @Context HttpServletRequest request, @PathParam("id") String id ) { + Response response = defaultRepo.getById( + LStatusProtokoll.class, + Integer.valueOf(id), + "land"); + LStatusProtokoll status = (LStatusProtokoll)response.getData(); + Violation violation = validator.validate(status); + if (violation.hasErrors() || violation.hasWarnings()) { + response.setErrors(violation.getErrors()); + response.setWarnings(violation.getWarnings()); + } + return authorization.filter( request, - defaultRepo.getById(LStatusProtokoll.class, Integer.valueOf(id), "land"), + response, LStatusProtokoll.class); } @@ -227,6 +252,13 @@ return new Response(false, 699, null); } } + Violation violation = validator.validate(status); + if (violation.hasErrors()) { + Response response = new Response(false, 604, status); + response.setErrors(violation.getErrors()); + response.setWarnings(violation.getWarnings()); + return response; + } Response response = defaultRepo.create(status, "land"); LStatusProtokoll created = (LStatusProtokoll)response.getData(); messung.setStatus(created.getId()); @@ -285,9 +317,32 @@ statusNew.setStatusStufe(status.getStatusStufe()); statusNew.setStatusWert(status.getStatusWert()); statusNew.setText(status.getText()); + Violation violation = validator.validate(statusNew); + if (violation.hasErrors()) { + Response response = new Response(false, 604, statusNew); + response.setErrors(violation.getErrors()); + response.setWarnings(violation.getWarnings()); + return response; + } + Response response = defaultRepo.create(statusNew, "land"); LStatusProtokoll created = (LStatusProtokoll)response.getData(); - messung.setStatus(created.getId()); + if (status.getStatusWert() == 0) { + QueryBuilder<LStatusProtokoll> lastFilter = + new QueryBuilder<LStatusProtokoll>( + defaultRepo.entityManager("land"), + LStatusProtokoll.class); + + lastFilter.and("messungsId", status.getMessungsId()); + lastFilter.orderBy("datum", false); + List<LStatusProtokoll> protos = + defaultRepo.filterPlain(lastFilter.getQuery(), "land"); + messung.setStatus(protos.get(protos.size() - 3).getId()); + } + else { + messung.setStatus(created.getId()); + } + defaultRepo.update(messung, "land"); return authorization.filter(
--- a/src/main/java/de/intevation/lada/util/data/QueryBuilder.java Mon Nov 16 16:34:50 2015 +0100 +++ b/src/main/java/de/intevation/lada/util/data/QueryBuilder.java Wed Nov 18 15:40:00 2015 +0100 @@ -7,12 +7,15 @@ */ package de.intevation.lada.util.data; +import java.util.ArrayList; import java.util.List; +import java.util.Map; import javax.persistence.EntityManager; import javax.persistence.criteria.CriteriaBuilder; import javax.persistence.criteria.CriteriaQuery; import javax.persistence.criteria.Expression; +import javax.persistence.criteria.Order; import javax.persistence.criteria.Path; import javax.persistence.criteria.Predicate; import javax.persistence.criteria.Root; @@ -301,6 +304,24 @@ } /** + * Order result by the specified column name + * + * @param ids Map of column names and boolean for asc/desc. + */ + public void orderBy(Map<String, Boolean> ids) { + List<Order> order = new ArrayList<Order>(); + for (String id : ids.keySet()) { + if (ids.get(id)) { + order.add(this.builder.asc(this.root.get(id))); + } + else { + order.add(this.builder.desc(this.root.get(id))); + } + } + this.query.orderBy(order); + } + + /** * Get an empty instance of this builder to create subfilters. * * @return An empty instance of this builder.
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/de/intevation/lada/validation/rules/status/StatusFolge.java Wed Nov 18 15:40:00 2015 +0100 @@ -0,0 +1,90 @@ +/* Copyright (C) 2013 by Bundesamt fuer Strahlenschutz + * Software engineering by Intevation GmbH + * + * This file is Free Software under the GNU GPL (v>=3) + * and comes with ABSOLUTELY NO WARRANTY! Check out + * the documentation coming with IMIS-Labordaten-Application for details. + */ +package de.intevation.lada.validation.rules.status; + +import java.util.List; + +import javax.inject.Inject; + +import de.intevation.lada.model.land.LStatusProtokoll; +import de.intevation.lada.model.stamm.StatusKombi; +import de.intevation.lada.model.stamm.StatusReihenfolge; +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.validation.Violation; +import de.intevation.lada.validation.annotation.ValidationRule; +import de.intevation.lada.validation.rules.Rule; + +/** + * Validation rule for status. + * + * @author <a href="mailto:rrenkert@intevation.de">Raimund Renkert</a> + */ +@ValidationRule("Status") +public class StatusFolge implements Rule { + + @Inject + @RepositoryConfig(type=RepositoryType.RO) + private Repository repository; + + @Override + public Violation execute(Object object) { + LStatusProtokoll status = (LStatusProtokoll)object; + QueryBuilder<StatusKombi> kombi = new QueryBuilder<StatusKombi>( + repository.entityManager("stamm"), + StatusKombi.class); + kombi.and("stufeId", status.getStatusStufe()); + kombi.and("wertId", status.getStatusWert()); + List<StatusKombi> result = + repository.filterPlain(kombi.getQuery(), "stamm"); + if (result.isEmpty()) { + Violation violation = new Violation(); + violation.addError("status", 632); + return violation; + } + + // Get the previous status + QueryBuilder<LStatusProtokoll> lastFilter = + new QueryBuilder<LStatusProtokoll>( + repository.entityManager("land"), + LStatusProtokoll.class); + + lastFilter.and("messungsId", status.getMessungsId()); + lastFilter.orderBy("datum", true); + List<LStatusProtokoll> protos = + repository.filterPlain(lastFilter.getQuery(), "land"); + LStatusProtokoll last = protos.get(protos.size() - 1); + QueryBuilder<StatusKombi> kombi2 = kombi.getEmptyBuilder(); + kombi2.and("stufeId", last.getStatusStufe()); + kombi2.and("wertId", last.getStatusWert()); + List<StatusKombi> result2 = + repository.filterPlain(kombi.getQuery(), "stamm"); + if (result2.isEmpty()) { + Violation violation = new Violation(); + violation.addError("status", 632); + return violation; + } + QueryBuilder<StatusReihenfolge> folgeFilter = + new QueryBuilder<StatusReihenfolge>( + repository.entityManager("stamm"), + StatusReihenfolge.class); + folgeFilter.and("von", result2.get(0).getId()); + folgeFilter.and("zu", result.get(0).getId()); + List<StatusReihenfolge> reihenfolge = + repository.filterPlain(folgeFilter.getQuery(), "stamm"); + if (reihenfolge.isEmpty()) { + Violation violation = new Violation(); + violation.addError("status", 632); + return violation; + } + + return null; + } +}
--- a/src/main/java/de/intevation/lada/validation/rules/status/StatusKombination.java Mon Nov 16 16:34:50 2015 +0100 +++ b/src/main/java/de/intevation/lada/validation/rules/status/StatusKombination.java Wed Nov 18 15:40:00 2015 +0100 @@ -46,6 +46,7 @@ if (result.isEmpty()) { Violation violation = new Violation(); violation.addError("status", 632); + return violation; } return null; }
--- a/src/test/java/de/intevation/lada/BaseTest.java Mon Nov 16 16:34:50 2015 +0100 +++ b/src/test/java/de/intevation/lada/BaseTest.java Wed Nov 18 15:40:00 2015 +0100 @@ -28,7 +28,7 @@ public static String TEST_USER = "testeins"; - public static String TEST_ROLES = "cn=Imis-World, cn=mst_06010, cn=mst_11010"; + public static String TEST_ROLES = "cn=Imis-World, cn=mst_06010, cn=mst_11010, cn=mst_11010_status"; private static Logger logger = Logger.getLogger(BaseTest.class);
--- a/src/test/java/de/intevation/lada/LadaLandTest.java Mon Nov 16 16:34:50 2015 +0100 +++ b/src/test/java/de/intevation/lada/LadaLandTest.java Wed Nov 18 15:40:00 2015 +0100 @@ -17,7 +17,6 @@ import org.junit.Assert; import org.junit.BeforeClass; import org.junit.FixMethodOrder; -import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.MethodSorters; @@ -270,7 +269,6 @@ * Testing GET Services. */ @Test - @Ignore @RunAsClient public final void testA_StatusGetAllServices(@ArquillianResource URL baseUrl) throws Exception { @@ -281,7 +279,6 @@ * Testing GET Services. */ @Test - @Ignore @RunAsClient public final void testA_StatusGetByIdServices(@ArquillianResource URL baseUrl) throws Exception { @@ -292,7 +289,6 @@ * Testing GET Services. */ @Test - @Ignore @RunAsClient public final void testA_StatusGetFilterServices(@ArquillianResource URL baseUrl) throws Exception { @@ -393,10 +389,6 @@ baseUrl, testProtocol, this.messungTest.getCreatedMessungId()); - //this.statusTest.createService( - // baseUrl, - // testProtocol, - // this.messungTest.getCreatedMessungId()); this.messprogrammTest.createService( baseUrl, testProtocol); @@ -472,12 +464,21 @@ * Testing UPDATE services. */ @Test - @Ignore @RunAsClient - public final void testC_statusUpdateService(@ArquillianResource URL baseUrl) + public final void testC_messprogrammUpdateService(@ArquillianResource URL baseUrl) throws Exception { Assert.assertNotNull(this.ortTest.getCreatedId()); - this.statusTest.updateService(baseUrl, testProtocol); + this.messprogrammTest.updateService(baseUrl, testProtocol); + } + + @Test + @RunAsClient + public final void testC_zstatusCreateService(@ArquillianResource URL baseUrl) + throws Exception { + this.statusTest.createService( + baseUrl, + testProtocol, + this.messungTest.getCreatedMessungId()); } /** @@ -485,10 +486,10 @@ */ @Test @RunAsClient - public final void testC_messprogrammUpdateService(@ArquillianResource URL baseUrl) + public final void testC_zstatusUpdateService(@ArquillianResource URL baseUrl) throws Exception { Assert.assertNotNull(this.ortTest.getCreatedId()); - this.messprogrammTest.updateService(baseUrl, testProtocol); + this.statusTest.updateService(baseUrl, testProtocol); } /** @@ -498,10 +499,10 @@ @RunAsClient public final void testD_DeleteServices(@ArquillianResource URL baseUrl) throws Exception { + //Assert.assertNotNull(this.statusTest.getCreatedId()); + //this.statusTest.deleteService(baseUrl, testProtocol); Assert.assertNotNull(this.zusatzwertTest.getCreatedId()); this.zusatzwertTest.deleteService(baseUrl, testProtocol); - // Assert.assertNotNull(this.statusTest.getCreatedId()); - // this.statusTest.deleteService(baseUrl, testProtocol); Assert.assertNotNull(this.ortTest.getCreatedId()); this.ortTest.deleteService(baseUrl, testProtocol); Assert.assertNotNull(this.messwertTest.getCreatedMesswertId());
--- a/src/test/java/de/intevation/lada/test/land/KommentarM.java Mon Nov 16 16:34:50 2015 +0100 +++ b/src/test/java/de/intevation/lada/test/land/KommentarM.java Wed Nov 18 15:40:00 2015 +0100 @@ -9,7 +9,9 @@ import java.io.StringReader; import java.net.URL; +import java.sql.Date; import java.util.List; +import java.util.TimeZone; import javax.json.Json; import javax.json.JsonException; @@ -36,7 +38,7 @@ public class KommentarM { private static final String COMPARE_KOMMENTARM = - "{\"messungsId\":5,\"datum\":1336627500000,\"erzeuger\":\"12010\"," + + "{\"messungsId\":5,\"datum\":1336634700000,\"erzeuger\":\"12010\"," + "\"id\":478,\"text\":\"Hofladen Lenzen geschlossen\",\"owner\":false," + "\"readonly\":true}";
--- a/src/test/java/de/intevation/lada/test/land/KommentarP.java Mon Nov 16 16:34:50 2015 +0100 +++ b/src/test/java/de/intevation/lada/test/land/KommentarP.java Wed Nov 18 15:40:00 2015 +0100 @@ -36,10 +36,10 @@ public class KommentarP { private static final String COMPARE_KOMMENTARP = - "{\"datum\":1321002077000,\"erzeuger\":\"06010\",\"id\":1,\"text\":" + + "{\"datum\":1321005677000,\"erzeuger\":\"06010\",\"id\":1,\"text\":" + "\"Die Probe wurde in Darmstadt gammaspektrometrisch gemessen und " + "für die Sr-Bestimmung verascht. \",\"probeId\":361,\"owner\":true," + - "\"readonly\":true}"; + "\"readonly\":false}"; private static final String CREATE_KOMMENTARP = "{\"probeId\":\"PID\",\"erzeuger\":\"11010\",\"text\":" +
--- a/src/test/java/de/intevation/lada/test/land/Messprogramm.java Mon Nov 16 16:34:50 2015 +0100 +++ b/src/test/java/de/intevation/lada/test/land/Messprogramm.java Wed Nov 18 15:40:00 2015 +0100 @@ -37,7 +37,7 @@ private static final String COMPARE = "{\"id\":1,\"baId\":null," + "\"datenbasisId\":2,\"gemId\":\"11000000\",\"gueltigBis\":null," + "\"gueltigVon\":null,\"intervallOffset\":null,\"letzteAenderung\":" + - "1381413048800,\"mediaDesk\":\"D: 50 90 01 06 02 05 00 00 00 00 00 00\"," + + "1381420248800,\"mediaDesk\":\"D: 50 90 01 06 02 05 00 00 00 00 00 00\"," + "\"mstId\":\"11010\",\"name\":null,\"netzbetreiberId\":\"11\"," + "\"ortId\":null,\"probeKommentar\":null,\"probeNehmerId\":null," + "\"probenartId\":3,\"probenintervall\":\"M\",\"teilintervallBis\":30," +
--- a/src/test/java/de/intevation/lada/test/land/Probe.java Mon Nov 16 16:34:50 2015 +0100 +++ b/src/test/java/de/intevation/lada/test/land/Probe.java Wed Nov 18 15:40:00 2015 +0100 @@ -275,11 +275,11 @@ JsonObject oldProbe = reader.readObject().getJsonObject("data"); /* Change the hauptprobenNr*/ String updatedEntity = - oldProbe.toString().replace("4554567890", "2345678901"); + oldProbe.toString().replace("4554567890", "2345678911"); prot.addInfo("updated datafield", "hauptprobenNr"); prot.addInfo("updated value", "1234567890"); - prot.addInfo("updated to", "2345678901"); - /* Send the updated probe via put reauest*/ + prot.addInfo("updated to", "2345678911"); + /* Send the updated probe via put request*/ WebTarget putTarget = client.target(baseUrl + "probe/" + createdProbeId); Response updated = putTarget.request() .header("X-SHIB-user", BaseTest.TEST_USER) @@ -294,7 +294,7 @@ prot.addInfo("success", updatedProbe.getBoolean("success")); Assert.assertEquals("200", updatedProbe.getString("message")); prot.addInfo("message", updatedProbe.getString("message")); - Assert.assertEquals("2345678901", + Assert.assertEquals("2345678911", updatedProbe.getJsonObject("data").getString("hauptprobenNr")); } catch(JsonException je) {
--- a/src/test/java/de/intevation/lada/test/land/Status.java Mon Nov 16 16:34:50 2015 +0100 +++ b/src/test/java/de/intevation/lada/test/land/Status.java Wed Nov 18 15:40:00 2015 +0100 @@ -24,6 +24,7 @@ import org.junit.Assert; +import de.intevation.lada.BaseTest; import de.intevation.lada.Protocol; /** @@ -40,8 +41,9 @@ "\"sdatum\":1373846400000,\"skommentar\":\"test\"}"; private static final String CREATE = - "{\"erzeuger\":\"06010\",\"messungsId\":MID,\"status\":3," + - "\"sdatum\":1373846400000,\"skommentar\":\"status3\"}"; + "{\"erzeuger\":\"06010\",\"messungsId\":MID,\"statusStufe\":1," + + "\"statusWert\":1," + + "\"datum\":1373846400000,\"text\":\"status test\"}"; private List<Protocol> protocol; @@ -75,7 +77,10 @@ Client client = ClientBuilder.newClient(); WebTarget target = client.target(baseUrl + "status"); /* Request all objects*/ - Response response = target.request().get(); + Response response = target.request() + .header("X-SHIB-user", BaseTest.TEST_USER) + .header("X-SHIB-roles", BaseTest.TEST_ROLES) + .get(); String entity = response.readEntity(String.class); try{ /* Try to parse the response*/ @@ -112,10 +117,13 @@ try { /* Create a client*/ Client client = ClientBuilder.newClient(); - WebTarget target = client.target(baseUrl + "status/1"); + WebTarget target = client.target(baseUrl + "status/511"); prot.addInfo("statusId", 1); /* Request a object by id*/ - Response response = target.request().get(); + Response response = target.request() + .header("X-SHIB-user", BaseTest.TEST_USER) + .header("X-SHIB-roles", BaseTest.TEST_ROLES) + .get(); String entity = response.readEntity(String.class); /* Try to parse the response*/ JsonReader fromServiceReader = @@ -155,7 +163,10 @@ client.target(baseUrl + "status?messungId=1"); prot.addInfo("filter", "messungId=1"); /* Request the objects using the filter*/ - Response response = target.request().get(); + Response response = target.request() + .header("X-SHIB-user", BaseTest.TEST_USER) + .header("X-SHIB-roles", BaseTest.TEST_ROLES) + .get(); String entity = response.readEntity(String.class); /* Try to parse the response*/ JsonReader reader = Json.createReader(new StringReader(entity)); @@ -197,8 +208,10 @@ WebTarget target = client.target(baseUrl + "status"); /* Send a post request containing a new object*/ String stat = CREATE.replace("MID", messungId.toString()); - Response response = target.request().post( - Entity.entity(stat, MediaType.APPLICATION_JSON)); + Response response = target.request() + .header("X-SHIB-user", BaseTest.TEST_USER) + .header("X-SHIB-roles", BaseTest.TEST_ROLES) + .post(Entity.entity(stat, MediaType.APPLICATION_JSON)); String entity = response.readEntity(String.class); /* Try to parse the response*/ JsonReader fromServiceReader = @@ -241,21 +254,27 @@ client.target(baseUrl + "status/" + createdId); prot.addInfo("statusId", createdId); /* Request an object with the saved id*/ - Response response = target.request().get(); + Response response = target.request() + .header("X-SHIB-user", BaseTest.TEST_USER) + .header("X-SHIB-roles", BaseTest.TEST_ROLES) + .get(); String entity = response.readEntity(String.class); /* Try to parse the response*/ JsonReader reader = Json.createReader(new StringReader(entity)); JsonObject old = reader.readObject().getJsonObject("data"); /* Change the mmtId*/ String updatedEntity = - old.toString().replace("status3", "status3updated"); - prot.addInfo("updated field", "skommentar"); - prot.addInfo("updated value", "status3"); - prot.addInfo("updated to", "status3updated"); + old.toString().replace("statusWert\":1", "statusWert\":0"); + System.out.println(updatedEntity); + prot.addInfo("updated field", "statusWert"); + prot.addInfo("updated value", "1"); + prot.addInfo("updated to", "0"); /* Send the updated messwert via put request*/ WebTarget putTarget = client.target(baseUrl + "status/" + createdId); - Response updated = putTarget.request().put( - Entity.entity(updatedEntity, MediaType.APPLICATION_JSON)); + Response updated = putTarget.request() + .header("X-SHIB-user", BaseTest.TEST_USER) + .header("X-SHIB-roles", BaseTest.TEST_ROLES) + .put(Entity.entity(updatedEntity, MediaType.APPLICATION_JSON)); /* Try to parse the response*/ JsonReader updatedReader = Json.createReader( new StringReader(updated.readEntity(String.class))); @@ -265,8 +284,8 @@ prot.addInfo("success", updatedObj.getBoolean("success")); Assert.assertEquals("200", updatedObj.getString("message")); prot.addInfo("message", updatedObj.getString("message")); - Assert.assertEquals("status3updated", - updatedObj.getJsonObject("data").getString("skommentar")); + Assert.assertEquals(0, + updatedObj.getJsonObject("data").getJsonNumber("statusWert").intValue()); } catch(JsonException je) { prot.addInfo("exception", je.getMessage()); @@ -294,7 +313,10 @@ client.target(baseUrl + "status/" + createdId); prot.addInfo("statusId", createdId); /* Delete the object with the saved id*/ - Response response = target.request().delete(); + Response response = target.request() + .header("X-SHIB-user", BaseTest.TEST_USER) + .header("X-SHIB-roles", BaseTest.TEST_ROLES) + .delete(); String entity = response.readEntity(String.class); /* Try to parse the response*/ JsonReader reader = Json.createReader(new StringReader(entity));