changeset 615:3f57484e06f1

Updated importer: Do validation and object creation in the correct order.
author Raimund Renkert <raimund.renkert@intevation.de>
date Fri, 17 Apr 2015 13:21:32 +0200
parents d51d0418d027
children e62d1703f099
files src/main/java/de/intevation/lada/importer/laf/AttributeMapper.java src/main/java/de/intevation/lada/importer/laf/LafParser.java src/main/java/de/intevation/lada/importer/laf/LafProducer.java src/main/java/de/intevation/lada/importer/laf/LafWriter.java src/main/java/de/intevation/lada/importer/laf/OrtCreator.java
diffstat 5 files changed, 113 insertions(+), 77 deletions(-) [+]
line wrap: on
line diff
--- a/src/main/java/de/intevation/lada/importer/laf/AttributeMapper.java	Fri Apr 17 13:17:45 2015 +0200
+++ b/src/main/java/de/intevation/lada/importer/laf/AttributeMapper.java	Fri Apr 17 13:21:32 2015 +0200
@@ -46,7 +46,7 @@
     private Logger logger;
 
     @Inject
-    @RepositoryConfig(type=RepositoryType.RW)
+    @RepositoryConfig(type=RepositoryType.RO)
     private Repository repository;
 
     private List<ReportItem> warnings;
@@ -91,35 +91,6 @@
             this.warnings.add(new ReportItem(key, value.toString(), 672));
         }
 
-        if ("probe_id".equals(key)) {
-            QueryBuilder<ProbeTranslation> builder =
-                new QueryBuilder<ProbeTranslation>(
-                    repository.entityManager("land"), ProbeTranslation.class);
-            builder.and("probeIdAlt", value);
-            Response response =
-                repository.filter(builder.getQuery(), "land");
-            List<ProbeTranslation> info = (List<ProbeTranslation>)response.getData();
-            logger.debug("found " + info.size() + " items");
-            if (info != null && info.size() > 0) {
-                logger.debug("found probe with old id: " + value);
-                errors.add(new ReportItem("probe_id", value.toString(), 671));
-                return null;
-            }
-            probe.setTest(true);
-            probe.setProbenartId(1);
-            repository.create(probe, "land");
-            ProbeTranslation trans = new ProbeTranslation();
-
-            logger.debug("###### probeidalt: " + value);
-            trans.setProbeIdAlt(value.toString());
-            //Query q = repository.queryFromString("select nextval('bund.probe_id_seq')");
-            //BigInteger id = (BigInteger)q.getSingleResult();
-            //probe.setId(id.intValue());
-            //logger.debug("id of the new probe: " + id);
-            trans.setProbeId(probe);
-            repository.create(trans, "land");
-        }
-
         if ("hauptprobennummer".equals(key)) {
             probe.setHauptprobenNr(value.toString());
         }
@@ -226,6 +197,28 @@
         return probe;
     }
 
+    public ProbeTranslation addAttribute(
+        String key,
+        Object value,
+        ProbeTranslation probeTranslation
+    ) {
+        if ("probe_id".equals(key)) {
+            QueryBuilder<ProbeTranslation> builder =
+                new QueryBuilder<ProbeTranslation>(
+                    repository.entityManager("land"), ProbeTranslation.class);
+            builder.and("probeIdAlt", value);
+            Response response =
+                repository.filter(builder.getQuery(), "land");
+            List<ProbeTranslation> info = (List<ProbeTranslation>)response.getData();
+            if (info != null && info.size() > 0) {
+                errors.add(new ReportItem("probe_id", value.toString(), 671));
+                return null;
+            }
+            probeTranslation.setProbeIdAlt(value.toString());
+        }
+        return probeTranslation;
+    }
+
     /**
      * Add an attribute to the given LKommentarP object.
      *
@@ -242,7 +235,6 @@
         DateFormat format = new SimpleDateFormat("yyyyMMdd HHmm");
         String v = value.toString();
         String erzeuger = v.substring(1, 6);
-        logger.debug("erzeuger is " + erzeuger);
         String date = v.substring(8, 21);
         Date d;
         try {
@@ -341,9 +333,7 @@
         Object value,
         MessungTranslation mt
     ) {
-        logger.debug("###### set messungsidalt");
         if ("messungs_id".equals(key)) {
-            logger.debug("###### set messungsid alt: " + value);
             mt.setMessungsIdAlt(Integer.valueOf(value.toString()));
         }
         return mt;
--- a/src/main/java/de/intevation/lada/importer/laf/LafParser.java	Fri Apr 17 13:17:45 2015 +0200
+++ b/src/main/java/de/intevation/lada/importer/laf/LafParser.java	Fri Apr 17 13:21:32 2015 +0200
@@ -17,6 +17,7 @@
 import de.intevation.lada.model.land.MessungTranslation;
 import de.intevation.lada.util.auth.UserInfo;
 import de.intevation.lada.validation.Validator;
+import de.intevation.lada.validation.Violation;
 import de.intevation.lada.validation.annotation.ValidationConfig;
 
 /**
@@ -153,43 +154,61 @@
      * @param auth  The authentication information.
      */
     private void writeAll(UserInfo userInfo) {
-        String probeId = producer.getProbe().getId() == null ?
-            "probeId" : producer.getProbe().getId().toString();
-        boolean p = writer.writeProbe(userInfo, producer.getProbe());
-        logger.debug("write probe: " + p);
+        String probeId = producer.getProbeTranslation().getProbeIdAlt() == null ?
+            "probeId" : producer.getProbeTranslation().getProbeIdAlt().toString();
+        Violation violation = validateProbe(producer.getProbe());
+        if (violation.hasErrors()) {
+            ReportItem err = new ReportItem("validation", violation.getErrors(), null);
+            List<ReportItem> errs= new ArrayList<ReportItem>();
+            errs.add(err);
+            this.appendErrors(probeId, errs);
+            return;
+        }
+        boolean p = writer.writeProbe(userInfo, producer.getProbe(), producer.getProbeTranslation());
         if (!p) {
             this.errors.put(probeId, writer.getErrors());
             return;
         }
         writer.writeProbenKommentare(userInfo, producer.getProbenKommentare());
-        boolean m = writer.writeMessungen(userInfo, producer.getMessungen());
-        if (!m) {
-            return;
-        }
-        for (LMessung tm : producer.getMessungen().keySet()) {
-            logger.debug("messungsid: " + tm.getId());
+        for (LMessung messung: producer.getMessungen().keySet()) {
+            Violation mViolation = messungValidator.validate(messung);
+            if (mViolation.hasErrors()) {
+                ReportItem mErr = new ReportItem("validation", mViolation.getErrors(), null);
+                List<ReportItem> mErrs = new ArrayList<ReportItem>();
+                mErrs.add(mErr);
+                this.appendErrors(probeId, mErrs);
+                continue;
+            }
+            boolean m = writer.writeMessungen(userInfo, messung, producer.getMessungen().get(messung));
+            if (!m) {
+                return;
+            }
         }
         writer.writeOrte(userInfo, producer.getOrte());
         logger.debug("### i have " + producer.getLOrte().size() + " orte");
         writer.writeLOrte(userInfo, producer.getLOrte());
         writer.writeMessungKommentare(userInfo, producer.getMessungsKommentare());
         writer.writeMesswerte(userInfo, producer.getMesswerte());
-        this.validateProbe(producer.getProbe());
-        this.validateMessungen(producer.getMessungen());
-        this.validateMesswerte(producer.getMesswerte());
-        this.validateLOrte(producer.getLOrte());
+        Violation postViolation = validateProbe(producer.getProbe());
+        if (postViolation.hasWarnings()) {
+            ReportItem warn = new ReportItem("validation", postViolation.getWarnings(), null);
+            List<ReportItem> warns = new ArrayList<ReportItem>();
+            warns.add(warn);
+            this.appendWarnings(probeId, warns);
+        }
+        for (LMessung messung: producer.getMessungen().keySet()) {
+            Violation mViolation = messungValidator.validate(messung);
+            if (mViolation.hasWarnings()) {
+                ReportItem mWarn = new ReportItem("validation", mViolation.getWarnings(), null);
+                List<ReportItem> mWarns = new ArrayList<ReportItem>();
+                mWarns.add(mWarn);
+                this.appendWarnings(probeId, mWarns);
+            }
+        }
     }
 
-    private void validateProbe(LProbe probe) {
-    }
-
-    private void validateMessungen(Map<LMessung, MessungTranslation> messungen) {
-    }
-
-    private void validateMesswerte(Map<LMessung, List<LMesswert>> werte) {
-    }
-
-    private void validateLOrte(List<LOrt> orte) {
+    private Violation validateProbe(LProbe probe) {
+        return probeValidator.validate(probe);
     }
 
     /**
@@ -285,7 +304,10 @@
                 continue;
             }
             if ((current == '\n' || current == '\r') && (key || white)) {
-                //TODO error!!!
+                ReportItem item = new ReportItem("parser", "general error", 603);
+                List<ReportItem> items = new ArrayList<ReportItem>();
+                items.add(item);
+                this.appendErrors("parser", items);
                 return;
             }
 
--- a/src/main/java/de/intevation/lada/importer/laf/LafProducer.java	Fri Apr 17 13:17:45 2015 +0200
+++ b/src/main/java/de/intevation/lada/importer/laf/LafProducer.java	Fri Apr 17 13:21:32 2015 +0200
@@ -19,6 +19,7 @@
 import de.intevation.lada.model.land.LProbe;
 import de.intevation.lada.model.land.LZusatzWert;
 import de.intevation.lada.model.land.MessungTranslation;
+import de.intevation.lada.model.land.ProbeTranslation;
 import de.intevation.lada.model.stamm.SOrt;
 
 /**
@@ -33,6 +34,7 @@
     private Logger logger;
 
     private LProbe probe;
+    private ProbeTranslation probeTranslation;
     private LMessung messung;
     private MessungTranslation messungTranslation;
 
@@ -65,6 +67,7 @@
         this.warnings = new HashMap<String, List<ReportItem>>();
         this.errors = new HashMap<String, List<ReportItem>>();
         this.probe = new LProbe();
+        this.probeTranslation = new ProbeTranslation();
         this.pKommentare = new ArrayList<LKommentarP>();
         this.mKommentare = new HashMap<LMessung, List<LKommentarM>>();
         this.messungen = new HashMap<LMessung, MessungTranslation>();
@@ -175,7 +178,8 @@
         }
         else if (isValidProbe(lKey, values.toString())) {
             this.probe = mapper.addAttribute(lKey, values, this.probe);
-            if (this.probe == null) {
+            this.probeTranslation = mapper.addAttribute(lKey, values, this.probeTranslation);
+            if (this.probe == null || this.probeTranslation == null) {
                 this.errors.put(values.toString(), mapper.getErrors());
                 throw new LafParserException(values.toString() + " exists");
             }
@@ -249,6 +253,10 @@
         return this.probe;
     }
 
+    public ProbeTranslation getProbeTranslation() {
+        return this.probeTranslation;
+    }
+
     /**
      * @return List of {@link LMessung} entities.
      */
@@ -331,7 +339,6 @@
         }
         this.messung = new LMessung();
         this.messungTranslation = new MessungTranslation();
-        this.messung.setProbeId(this.probe.getId());
     }
 
     /**
@@ -367,6 +374,9 @@
         if (this.probe == null) {
             return this.warnings;
         }
+        if (mapper.getWarnings() == null || mapper.getWarnings().size() == 0) {
+            return this.warnings;
+        }
         String key = probe.getId() == null ? "probeId" : probe.getId().toString();
         List<ReportItem> warn = this.warnings.get(key);
         if (warn == null) {
@@ -382,11 +392,14 @@
      * @return the errors
      */
     public Map<String, List<ReportItem>> getErrors() {
-        if (this.probe == null) {
+        if (this.probe == null || this.probeTranslation == null) {
             return this.errors;
         }
-        String key = probe.getId() == null ?
-            "probeId" : probe.getId().toString();
+        if (mapper.getErrors() == null || mapper.getErrors().size() == 0) {
+            return this.errors;
+        }
+        String key = probeTranslation.getProbeIdAlt() == null ?
+            "probeId" : probeTranslation.getProbeIdAlt();
         List<ReportItem> err = this.errors.get(key);
         if (err == null) {
             this.errors.put(key, mapper.getErrors());
--- a/src/main/java/de/intevation/lada/importer/laf/LafWriter.java	Fri Apr 17 13:17:45 2015 +0200
+++ b/src/main/java/de/intevation/lada/importer/laf/LafWriter.java	Fri Apr 17 13:21:32 2015 +0200
@@ -4,10 +4,11 @@
 import java.util.List;
 import java.util.Map;
 
-import javax.ejb.Stateless;
 import javax.inject.Inject;
 import javax.persistence.PersistenceException;
 
+import org.apache.log4j.Logger;
+
 import de.intevation.lada.importer.ReportItem;
 import de.intevation.lada.model.land.LKommentarM;
 import de.intevation.lada.model.land.LKommentarP;
@@ -16,6 +17,7 @@
 import de.intevation.lada.model.land.LOrt;
 import de.intevation.lada.model.land.LProbe;
 import de.intevation.lada.model.land.MessungTranslation;
+import de.intevation.lada.model.land.ProbeTranslation;
 import de.intevation.lada.model.stamm.SOrt;
 import de.intevation.lada.util.annotation.AuthorizationConfig;
 import de.intevation.lada.util.annotation.RepositoryConfig;
@@ -34,6 +36,9 @@
 public class LafWriter {
 
     @Inject
+    private Logger logger;
+
+    @Inject
     @RepositoryConfig(type=RepositoryType.RW)
     private Repository repository;
 
@@ -44,6 +49,8 @@
     private List<ReportItem> errors;
     private List<ReportItem> warnings;
 
+    private Integer currentProbeId;
+
     /**
      * Default constructor.
      */
@@ -60,17 +67,20 @@
      * @param probe     The new {@link LProbe} object.
      * @return success
      */
-    public boolean writeProbe(UserInfo userInfo, LProbe probe) {
+    public boolean writeProbe(UserInfo userInfo, LProbe probe, ProbeTranslation probeTranslation) {
         if (!authorization.isAuthorized(userInfo, probe)) {
             errors.add(new ReportItem("auth", "not authorized", 699));
             return false;
         }
-        if (probe.getId() == null) {
+        if (probeTranslation.getProbeIdAlt() == null) {
             errors.add(new ReportItem("probeId", "missing", 673));
             return false;
         }
         try {
-            repository.update(probe, "land");
+            repository.create(probe, "land");
+            probeTranslation.setProbeId(probe);
+            this.currentProbeId = probe.getId();
+            repository.create(probeTranslation, "land");
         }
         catch (PersistenceException e) {
             errors.add(new ReportItem("probe", "writing", 670));
@@ -89,14 +99,13 @@
      */
     public boolean writeMessungen(
         UserInfo userInfo,
-        Map<LMessung, MessungTranslation> messungen
+        LMessung messung,
+        MessungTranslation messungTranslation
     ) {
-        for(LMessung messung: messungen.keySet()) {
-            repository.create(messung, "land");
-            MessungTranslation mt = messungen.get(messung);
-            mt.setMessungsId(messung);
-            repository.create(mt, "land");
-        }
+        messung.setProbeId(this.currentProbeId);
+        repository.create(messung, "land");
+        messungTranslation.setMessungsId(messung);
+        repository.create(messungTranslation, "land");
         return true;
     }
 
@@ -124,6 +133,7 @@
      */
     public boolean writeLOrte(UserInfo userInfo, List<LOrt> orte) {
         for(LOrt ort: orte) {
+            ort.setProbeId(this.currentProbeId);
             repository.create(ort, "land");
         }
         return true;
@@ -141,6 +151,7 @@
         List<LKommentarP> kommentare
     ) {
         for(LKommentarP kommentar: kommentare) {
+            kommentar.setProbeId(this.currentProbeId);
             repository.create(kommentar, "land");
         }
         return true;
@@ -206,5 +217,6 @@
     public void reset() {
         this.warnings = new ArrayList<ReportItem>();
         this.errors = new ArrayList<ReportItem>();
+        this.currentProbeId = null;
     }
 }
--- a/src/main/java/de/intevation/lada/importer/laf/OrtCreator.java	Fri Apr 17 13:17:45 2015 +0200
+++ b/src/main/java/de/intevation/lada/importer/laf/OrtCreator.java	Fri Apr 17 13:21:32 2015 +0200
@@ -668,9 +668,8 @@
     public LOrt toLOrt() {
         logger.debug("#### getting lort");
         if (this.ortId == null &&
-            (this.ortCode == null || this.ortCode.length() == 0) ||
-            this.probeId == null) {
-            logger.debug("return null: " + this.probeId + "; " + this.ortCode);
+            (this.ortCode == null || this.ortCode.length() == 0)
+        ) {
             return null;
         }
         if(this.ortCode != null && this.ortCode.length() > 0) {
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)