changeset 1035:4d95cc7f0a43 schema-update

Updated status workflow. * Using status_kombi instead of status_wert and status_stufe * new service for status_kombi * updated validators
author Raimund Renkert <raimund.renkert@intevation.de>
date Wed, 31 Aug 2016 15:52:15 +0200
parents 61354a9fa58d
children 4e0a9c7a8f23
files src/main/java/de/intevation/lada/rest/StatusService.java src/main/java/de/intevation/lada/rest/stamm/StatusKombiService.java src/main/java/de/intevation/lada/validation/rules/status/StatusFolge.java src/main/java/de/intevation/lada/validation/rules/status/StatusKombination.java
diffstat 4 files changed, 233 insertions(+), 156 deletions(-) [+]
line wrap: on
line diff
--- a/src/main/java/de/intevation/lada/rest/StatusService.java	Wed Aug 31 15:50:54 2016 +0200
+++ b/src/main/java/de/intevation/lada/rest/StatusService.java	Wed Aug 31 15:52:15 2016 +0200
@@ -36,6 +36,7 @@
 import de.intevation.lada.model.land.Probe;
 import de.intevation.lada.model.land.StatusProtokoll;
 import de.intevation.lada.model.stammdaten.MessStelle;
+import de.intevation.lada.model.stammdaten.StatusErreichbar;
 import de.intevation.lada.model.stammdaten.StatusKombi;
 import de.intevation.lada.model.stammdaten.StatusReihenfolge;
 import de.intevation.lada.util.annotation.AuthorizationConfig;
@@ -254,7 +255,7 @@
             status.setStatusKombi(1);
         }
         else {
-            StatusProtokoll currentStatus = defaultRepo.getByIdPlain(
+            StatusProtokoll oldStatus = defaultRepo.getByIdPlain(
                 StatusProtokoll.class, messung.getStatus(), "land");
 
             String probeMstId = defaultRepo.getByIdPlain(
@@ -262,118 +263,89 @@
                 messung.getProbeId(),
                 "land").getMstId();
 
-            StatusKombi kombiCurrent = defaultRepo.getByIdPlain(StatusKombi.class, currentStatus.getStatusKombi(), "stamm");
-            StatusKombi kombiStatus = defaultRepo.getByIdPlain(StatusKombi.class, status.getStatusKombi(), "stamm");
-            if (kombiCurrent.getStatusWert().getId() == 4) {
-                if (userInfo.getMessstellen().contains(
-                        currentStatus.getMstId())
-                    && status.getMstId().equals(
-                        currentStatus.getMstId())
-                ) {
-                    // 'edit' currentStatus
-                    status.setStatusKombi(kombiCurrent.getId());
+            StatusKombi oldKombi = defaultRepo.getByIdPlain(StatusKombi.class, oldStatus.getStatusKombi(), "stamm");
+            StatusKombi newKombi = defaultRepo.getByIdPlain(StatusKombi.class, status.getStatusKombi(), "stamm");
+
+            // Check if changing to the requested status_kombi is allowed.
+            QueryBuilder<StatusReihenfolge> builder = new QueryBuilder<StatusReihenfolge>(defaultRepo.entityManager("stamm"), StatusReihenfolge.class);
+            builder.and("vonId", oldStatus.getStatusKombi());
+            List<StatusReihenfolge> reachable = defaultRepo.filterPlain(builder.getQuery(), "stamm");
+            boolean allowed = false;
+            for (int i = 0; i < reachable.size(); i++) {
+                if (reachable.get(i).getZuId() == status.getStatusKombi()) {
+                    allowed = true;
                 }
-                else if (
-                    userInfo.getFunktionenForMst(probeMstId)
-                        .contains(1)
-                    && probeMstId.equals(status.getMstId())
-                ) {
-                    status.setStatusKombi(1);
+            }
+            if (!allowed) {
+                return new Response(false, 604, null);
+            }
+
+            // Check if the user is allowed to change to the requested
+            // status_kombi
+            // 1. The old 'status_wert' is 'rückfrage'
+            //    User has 'funktion' 1 for the given mstId
+            logger.debug("old: " + oldKombi.getStatusStufe().getStufe());
+            logger.debug("new: " + newKombi.getStatusStufe().getStufe());
+            logger.debug("mstold: " + oldStatus.getMstId());
+            logger.debug("mstnew: " + status.getMstId());
+            logger.debug("user: ");
+            for (int i = 0; i < userInfo.getMessstellen().size(); i++) {
+                logger.debug(userInfo.getMessstellen().get(i));
+            }
+            if (oldKombi.getStatusWert().getId() == 4) {
+                logger.debug("old status is rueckfrage");
+                if (userInfo.getFunktionenForMst(status.getMstId()).contains(1)) {
+                    // Set the new status.
+                    logger.debug("user is allowed to set the status");
+                    return setNewStatus(status, newKombi, messung, request);
                 }
                 else {
+                    logger.debug("not allowed");
+                    // Not allowed.
                     return new Response(false, 699, null);
                 }
             }
-            else {
-                boolean next = false; // Do we advance to next 'stufe'?
-                boolean change = false; // Do we change status on same 'stufe'?
-
-                // XXX: It's assumed here, that MessStelle:function is a
-                // 1:1-relationship, which is not enforced by the model
-                // (there is no such constraint in stammdaten.auth).
-                // Thus, next and change will be set based
-                // on whichever function is the first match, which is
-                // not necessary the users intention, if he has more than
-                // one function for the matching Messstelle.
-
-                // XXX: It's assumed here, that an 'Erzeuger' is an instance
-                // of 'Messstelle', but the model does not enforce it!
-                for (Integer function :
-                         userInfo.getFunktionenForMst(status.getMstId())
-                ) {
-                    if (function.equals(kombiCurrent.getStatusStufe().getId() + 1)
-                        && kombiCurrent.getStatusWert().getId() != 0) {
-                        next = true;
-                    }
-                    else if (function == kombiCurrent.getStatusStufe().getId()) {
-                        if (kombiCurrent.getStatusStufe().getId() == 1
-                            && !status.getMstId().equals(probeMstId)) {
-                            logger.debug(
-                                "Messstelle does not match for change");
-                            return new Response(false, 699, null);
-                        }
-
-                        String pNetzbetreiber = defaultRepo.getByIdPlain(
-                            MessStelle.class,
-                            probeMstId,
-                            "land").getNetzbetreiberId();
-                        String sNetzbetreiber = defaultRepo.getByIdPlain(
-                            MessStelle.class,
-                            status.getMstId(),
-                            "stamm").getNetzbetreiberId();
-                        if (kombiCurrent.getStatusStufe().getId() == 2
-                            && !pNetzbetreiber.equals(sNetzbetreiber)){
-                            logger.debug(
-                                "Netzbetreiber does not match for change");
-                            return new Response(false, 699, null);
-                        }
-                        change = true;
-                    }
-                }
-
-                QueryBuilder<StatusKombi> builder =
-                    new QueryBuilder<StatusKombi>(defaultRepo.entityManager("stamm"), StatusKombi.class);
-                if (change &&
-                    kombiStatus.getStatusWert().getId() == 4 &&
-                    kombiStatus.getStatusStufe().getId() > 1
-                ) {
-                    builder.and("StatusStufe", kombiCurrent.getStatusStufe())
-                        .and("statusWert", 4);
-                    status.setStatusKombi(defaultRepo.filterPlain(builder.getQuery(),"stamm").get(0).getId());
-                //    status.setStatusStufe(currentStatus.getStatusStufe());
-                }
-                else if (change && kombiStatus.getStatusWert().getId() == 8) {
+            // 2. user wants to edit the status (stufe stays the same.)
+            //    Users mstId equals the mstId of the old status.
+            else if (oldKombi.getStatusStufe().getStufe().equals(
+                        newKombi.getStatusStufe().getStufe()) &&
+                userInfo.getMessstellen().contains(oldStatus.getMstId()) &&
+                status.getMstId().equals(oldStatus.getMstId())
+            ) {
+                // a) user wants to reset the current status
+                //    'status wert' == 8
+                if (newKombi.getStatusWert().getId() == 8) {
+                    logger.debug("reset the status");
                     return authorization.filter(
                         request,
-                        resetStatus(status, currentStatus, messung),
+                        resetStatus(status, oldStatus, messung),
                         StatusProtokoll.class);
                 }
-                else if (change && kombiStatus.getStatusWert().getId() != 0) {
-                    builder.and("StatusStufe", kombiCurrent.getStatusStufe())
-                        .and("statusWert", kombiStatus.getStatusStufe());
-                    status.setStatusKombi(defaultRepo.filterPlain(builder.getQuery(),"stamm").get(0).getId());
-                }
-                else if (next &&
-                    (kombiStatus.getStatusWert().getId() > 0 &&
-                     kombiStatus.getStatusWert().getId() <= 4 ||
-                     kombiStatus.getStatusWert().getId() == 7)) {
-                    builder.and("StatusStufe", kombiCurrent.getStatusStufe().getId() + 1)
-                        .and("statusWert", kombiStatus.getStatusStufe());
-                    status.setStatusKombi(defaultRepo.filterPlain(builder.getQuery(),"stamm").get(0).getId());
-                }
-                else {
-                    return new Response(false, 699, null);
-                }
+                // b) update the status by the setting the new one.
+                logger.debug("set edited status");
+                return setNewStatus(status, newKombi, messung, request);
             }
-
-            // auto-set 'fertig'-flag
-            if (kombiStatus.getStatusStufe().getId() == 1) {
-                messung.setFertig(true);
-            }
-            else if (kombiStatus.getStatusWert().getId() == 4) {
-                messung.setFertig(false);
+            // 3. user wants to advance to the next 'status_stufe'
+            //    Users 'funktion' equals old 'stufe' + 1
+            else if (userInfo.getFunktionenForMst(status.getMstId()).contains(
+                oldKombi.getStatusStufe().getId() + 1) &&
+                newKombi.getStatusStufe().getId() ==
+                    oldKombi.getStatusStufe().getId() + 1) {
+                // Set the next status
+                logger.debug("set next status");
+                return setNewStatus(status, newKombi, messung, request);
             }
         }
+        logger.debug("something other....");
+        return new Response(false, 699, null);
+    }
+
+    private Response setNewStatus(
+        StatusProtokoll status,
+        StatusKombi newKombi,
+        Messung messung,
+        HttpServletRequest request
+    ) {
         Violation violation = validator.validate(status);
         if (violation.hasErrors()) {
             Response response = new Response(false, 604, status);
@@ -381,11 +353,16 @@
             response.setWarnings(violation.getWarnings());
             return response;
         }
+        if (newKombi.getStatusStufe().getId() == 1) {
+            messung.setFertig(true);
+        }
+        else if (newKombi.getStatusWert().getId() == 4) {
+            messung.setFertig(false);
+        }
         Response response = defaultRepo.create(status, "land");
         StatusProtokoll created = (StatusProtokoll)response.getData();
         messung.setStatus(created.getId());
         defaultRepo.update(messung, "land");
-        /* Persist the new object*/
         return authorization.filter(
             request,
             response,
@@ -462,48 +439,71 @@
     }
 
     private Response resetStatus(
-        StatusProtokoll status,
-        StatusProtokoll currentStatus,
+        StatusProtokoll newStatus,
+        StatusProtokoll oldStatus,
         Messung messung
     ) {
         // Create a new Status with value = 8.
+        QueryBuilder<StatusKombi> kombiFilter =
+            new QueryBuilder<StatusKombi>(
+                    defaultRepo.entityManager("stamm"),
+                    StatusKombi.class);
+        StatusKombi oldKombi = defaultRepo.getByIdPlain(StatusKombi.class, oldStatus.getStatusKombi(), "stamm");
+
+        kombiFilter.and("statusStufe", oldKombi.getStatusStufe().getId());
+        kombiFilter.and("statusWert", 8);
+        List<StatusKombi> newKombi = defaultRepo.filterPlain(kombiFilter.getQuery(), "stamm");
         StatusProtokoll statusNew = new StatusProtokoll();
         statusNew.setDatum(new Timestamp(new Date().getTime()));
-        statusNew.setMstId(status.getMstId());
-        statusNew.setMessungsId(status.getMessungsId());
-//        statusNew.setStatusKombi(currentStatus.getStatusStufe());
-//        statusNew.setStatusWert(8);
-//        statusNew.setText("Reset");
+        statusNew.setMstId(newStatus.getMstId());
+        statusNew.setMessungsId(newStatus.getMessungsId());
+        statusNew.setStatusKombi(newKombi.get(0).getId());
+        statusNew.setText(newStatus.getText());
 
         defaultRepo.create(statusNew, "land");
 
         Response retValue;
         StatusKombi kombi = defaultRepo.getByIdPlain(
             StatusKombi.class,
-            currentStatus.getStatusKombi(),
+            oldStatus.getStatusKombi(),
             "stamm");
         if (kombi.getStatusStufe().getId() == 1) {
             StatusProtokoll nV = new StatusProtokoll();
             nV.setDatum(new Timestamp(new Date().getTime()));
-            nV.setMstId(status.getMstId());
-            nV.setMessungsId(status.getMessungsId());
+            nV.setMstId(newStatus.getMstId());
+            nV.setMessungsId(newStatus.getMessungsId());
             nV.setStatusKombi(1);
             nV.setText("");
             retValue = defaultRepo.create(nV, "land");
             messung.setStatus(((StatusProtokoll)retValue.getData()).getId());
+            messung.setFertig(false);
         }
         else {
             QueryBuilder<StatusProtokoll> lastFilter =
                 new QueryBuilder<StatusProtokoll>(
                         defaultRepo.entityManager("land"),
                         StatusProtokoll.class);
-            lastFilter.and("messungsId", status.getMessungsId());
-// CHECK THIS!            lastFilter.and("statusKombi", currentStatus.getStatusKombi() - 1);
+            lastFilter.and("messungsId", newStatus.getMessungsId());
             lastFilter.orderBy("datum", true);
             List<StatusProtokoll> proto =
                 defaultRepo.filterPlain(lastFilter.getQuery(), "land");
+            // Find a status that has "status_stufe" = "old status_stufe - 1"
+            int ndx = -1;
+            for (int i = proto.size() - 1; i >= 0; i--) {
+                int curKom = proto.get(i).getStatusKombi();
+                StatusKombi sk =
+                    defaultRepo.getByIdPlain(StatusKombi.class, curKom, "stamm");
+                if (sk.getStatusStufe().getId() ==
+                        kombi.getStatusStufe().getId() -1
+                ) {
+                    ndx = i;
+                    break;
+                }
+            }
             StatusProtokoll copy = new StatusProtokoll();
-            StatusProtokoll orig = proto.get(proto.size() - 1);
+            StatusProtokoll orig = proto.get(ndx);
+            logger.debug(orig.getStatusKombi());
+            logger.debug(orig.getMstId());
             copy.setDatum(new Timestamp(new Date().getTime()));
             copy.setMstId(orig.getMstId());
             copy.setMessungsId(orig.getMessungsId());
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/de/intevation/lada/rest/stamm/StatusKombiService.java	Wed Aug 31 15:52:15 2016 +0200
@@ -0,0 +1,102 @@
+/* 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.rest.stamm;
+
+import javax.enterprise.context.RequestScoped;
+import javax.inject.Inject;
+import javax.ws.rs.GET;
+import javax.ws.rs.Path;
+import javax.ws.rs.PathParam;
+import javax.ws.rs.Produces;
+import javax.ws.rs.core.Context;
+import javax.ws.rs.core.HttpHeaders;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.UriInfo;
+
+import de.intevation.lada.model.stammdaten.StatusKombi;
+import de.intevation.lada.model.stammdaten.StatusStufe;
+import de.intevation.lada.util.annotation.RepositoryConfig;
+import de.intevation.lada.util.data.Repository;
+import de.intevation.lada.util.data.RepositoryType;
+import de.intevation.lada.util.rest.Response;
+
+/**
+ * REST service for StatusKombi objects.
+ * <p>
+ * The services produce data in the application/json media type.
+ * A typical response holds information about the action performed and the data.
+ * <pre>
+ * <code>
+ * {
+ *  "success": [boolean];
+ *  "message": [string],
+ *  "data":[{
+ *      "id": [number],
+ *      "stufeId": [number],
+ *      "wertId": [number]
+ *  }],
+ *  "errors": [object],
+ *  "warnings": [object],
+ *  "readonly": [boolean],
+ *  "totalCount": [number]
+ * }
+ * </code>
+ * </pre>
+ *
+ * @author <a href="mailto:rrenkert@intevation.de">Raimund Renkert</a>
+ */
+@Path("rest/statuskombi")
+@RequestScoped
+public class StatusKombiService {
+
+    /**
+     * The data repository granting read access.
+     */
+    @Inject
+    @RepositoryConfig(type=RepositoryType.RO)
+    private Repository repository;
+
+    /**
+     * Get all StatusKombi objects.
+     * <p>
+     * Example: http://example.com/statuskombi
+     *
+     * @return Response object containing all StatusStufe objects.
+     */
+    @GET
+    @Path("/")
+    @Produces(MediaType.APPLICATION_JSON)
+    public Response get(
+        @Context HttpHeaders headers,
+        @Context UriInfo info
+    ) {
+        return repository.getAll(StatusKombi.class, "stamm");
+    }
+
+    /**
+     * Get a single StatusStufe object by id.
+     * <p>
+     * The id is appended to the URL as a path parameter.
+     * <p>
+     * Example: http://example.com/statusstufe/{id}
+     *
+     * @return Response object containing a single StatusStufe.
+     */
+    @GET
+    @Path("/{id}")
+    @Produces(MediaType.APPLICATION_JSON)
+    public Response getById(
+        @Context HttpHeaders headers,
+        @PathParam("id") String id
+    ) {
+        return repository.getById(
+            StatusKombi.class,
+            Integer.valueOf(id),
+            "stamm");
+    }
+}
--- a/src/main/java/de/intevation/lada/validation/rules/status/StatusFolge.java	Wed Aug 31 15:50:54 2016 +0200
+++ b/src/main/java/de/intevation/lada/validation/rules/status/StatusFolge.java	Wed Aug 31 15:52:15 2016 +0200
@@ -11,6 +11,8 @@
 
 import javax.inject.Inject;
 
+import org.apache.log4j.Logger;
+
 import de.intevation.lada.model.land.StatusProtokoll;
 import de.intevation.lada.model.stammdaten.StatusKombi;
 import de.intevation.lada.model.stammdaten.StatusReihenfolge;
@@ -30,57 +32,36 @@
 @ValidationRule("Status")
 public class StatusFolge implements Rule {
 
+    @Inject Logger logger;
+
     @Inject
     @RepositoryConfig(type=RepositoryType.RO)
     private Repository repository;
 
     @Override
     public Violation execute(Object object) {
-        /*
         StatusProtokoll status = (StatusProtokoll)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>(
+        QueryBuilder<StatusProtokoll> lastFilter =
+            new QueryBuilder<StatusProtokoll>(
                     repository.entityManager("land"),
-                    LStatusProtokoll.class);
+                    StatusProtokoll.class);
 
         lastFilter.and("messungsId", status.getMessungsId());
         lastFilter.orderBy("datum", true);
-        List<LStatusProtokoll> protos =
+        List<StatusProtokoll> protos =
             repository.filterPlain(lastFilter.getQuery(), "land");
         if (protos.isEmpty()) {
             return null;
         }
-        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(kombi2.getQuery(), "stamm");
-        if (result2.isEmpty()) {
-            Violation violation = new Violation();
-            violation.addError("status", 632);
-            return violation;
-        }
+        StatusProtokoll last = protos.get(protos.size() - 1);
         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());
+        folgeFilter.and("vonId", last.getStatusKombi());
+        folgeFilter.and("zuId", status.getStatusKombi());
         List<StatusReihenfolge> reihenfolge =
             repository.filterPlain(folgeFilter.getQuery(), "stamm");
         if (reihenfolge.isEmpty()) {
@@ -89,10 +70,6 @@
             return violation;
         }
 
-        return null;*/
-
-        Violation violation = new Violation();
-        violation.addError("status", 000);
-        return violation;
+        return null;
     }
 }
--- a/src/main/java/de/intevation/lada/validation/rules/status/StatusKombination.java	Wed Aug 31 15:50:54 2016 +0200
+++ b/src/main/java/de/intevation/lada/validation/rules/status/StatusKombination.java	Wed Aug 31 15:52:15 2016 +0200
@@ -11,6 +11,8 @@
 
 import javax.inject.Inject;
 
+import org.apache.log4j.Logger;
+
 import de.intevation.lada.model.land.StatusProtokoll;
 import de.intevation.lada.model.stammdaten.StatusKombi;
 import de.intevation.lada.util.annotation.RepositoryConfig;
@@ -29,19 +31,19 @@
 @ValidationRule("Status")
 public class StatusKombination implements Rule {
 
+    @Inject Logger logger;
+
     @Inject
     @RepositoryConfig(type=RepositoryType.RO)
     private Repository repository;
 
     @Override
     public Violation execute(Object object) {
-        /*
         StatusProtokoll status = (StatusProtokoll)object;
         QueryBuilder<StatusKombi> kombi = new QueryBuilder<StatusKombi>(
             repository.entityManager("stamm"),
             StatusKombi.class);
-        kombi.and("stufeId", status.getStatusStufe());
-        kombi.and("wertId", status.getStatusWert());
+        kombi.and("id", status.getStatusKombi());
         List<StatusKombi> result =
             repository.filterPlain(kombi.getQuery(), "stamm");
         if (result.isEmpty()) {
@@ -50,9 +52,5 @@
             return violation;
         }
         return null;
-        */
-        Violation violation = new Violation();
-        violation.addError("status", 000);
-        return violation;
     }
 }
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)