[Lada-commits] [PATCH 1 of 2] Validate objects after creation in importer

Wald Commits scm-commit at wald.intevation.org
Mon Jan 13 15:40:57 CET 2014


# HG changeset patch
# User Raimund Renkert <rrenkert at intevation.de>
# Date 1389624360 -3600
# Node ID c63281230bc372f8e3858aa2450ec774113a6976
# Parent  0bc65c128530cf25f0a751ee416f1b8ff6ab6d8f
Validate objects after creation in importer.

diff -r 0bc65c128530 -r c63281230bc3 src/main/java/de/intevation/lada/data/importer/LAFParser.java
--- a/src/main/java/de/intevation/lada/data/importer/LAFParser.java	Wed Jan 08 15:08:29 2014 +0100
+++ b/src/main/java/de/intevation/lada/data/importer/LAFParser.java	Mon Jan 13 15:46:00 2014 +0100
@@ -9,6 +9,12 @@
 import javax.inject.Named;
 
 import de.intevation.lada.auth.AuthenticationResponse;
+import de.intevation.lada.model.LMessung;
+import de.intevation.lada.model.LMesswert;
+import de.intevation.lada.model.LOrt;
+import de.intevation.lada.model.LProbe;
+import de.intevation.lada.validation.ValidationException;
+import de.intevation.lada.validation.Validator;
 
 /**
  * This parser is used to read data in LAF based key-value pair structure.
@@ -29,6 +35,19 @@
     @Named("lafwriter")
     private Writer writer;
 
+    @Inject
+    @Named("lprobevalidator")
+    private Validator probeValidator;
+    @Inject
+    @Named("lmessungvalidator")
+    private Validator messungValidator;
+    @Inject
+    @Named("lmesswertvalidator")
+    private Validator messwertValidator;
+    @Inject
+    @Named("lortvalidator")
+    private Validator ortValidator;
+
     private Map<String, List<ReportData>> warnings;
     private Map<String, List<ReportData>> errors;
 
@@ -128,7 +147,6 @@
         boolean p = writer.writeProbe(auth, producer.getProbe());
         if (!p) {
             this.errors.put(probeId, writer.getErrors());
-            this.warnings.put(probeId, writer.getWarnings());
             return;
         }
         writer.writeProbenKommentare(auth, producer.getProbenKommentare());
@@ -140,20 +158,156 @@
         writer.writeLOrte(auth, producer.getLOrte());
         writer.writeMessungKommentare(auth, producer.getMessungsKommentare());
         writer.writeMesswerte(auth, producer.getMesswerte());
-        List<ReportData> err = this.errors.get(probeId);
-        if (err == null) {
-            this.errors.put(probeId, writer.getErrors());
+        this.validateProbe(producer.getProbe());
+        this.validateMessungen(producer.getMessungen());
+        this.validateMesswerte(producer.getMesswerte());
+        this.validateLOrte(producer.getLOrte());
+    }
+
+    private boolean validateProbe(LProbe probe) {
+    	try {
+            Map<String, Integer> warn =
+                probeValidator.validate(probe, false);
+            if (warn != null) {
+            	List<ReportData> warns = new ArrayList<ReportData>();
+                for (String key: warn.keySet()) {
+                	warns.add(new ReportData(key, "validation", warn.get(key)));
+                }
+                this.appendWarnings(probe.getProbeId(), warns);
+            }
         }
-        else {
-            err.addAll(writer.getErrors());
+        catch (ValidationException e) {
+            Map<String, Integer> err = e.getErrors();
+          	List<ReportData> errs = new ArrayList<ReportData>();
+            for(String key: err.keySet()) {
+                errs.add(new ReportData(key, "validation", err.get(key)));
+            }
+            this.appendErrors(probe.getProbeId(), errs);
+            Map<String, Integer> warn = e.getWarnings();
+            if (warn != null) {
+            	List<ReportData> warns = new ArrayList<ReportData>();
+                for (String key: warn.keySet()) {
+                    warns.add(new ReportData(key, "validation", warn.get(key)));
+                }
+                this.appendWarnings(probe.getProbeId(), warns);
+            }
+            return false;
         }
-        List<ReportData> warn = this.warnings.get(probeId);
-        if (warn == null) {
-            this.warnings.put(probeId, writer.getWarnings());
+    	return true;
+    }
+
+	private boolean validateMessungen(List<LMessung> messungen) {
+        for(LMessung messung: messungen) {
+            try {
+                Map<String, Integer> warn =
+                    messungValidator.validate(messung, false);
+                if (warn != null) {
+                	List<ReportData> warns = new ArrayList<ReportData>();
+                    for (String key : warn.keySet()) {
+                        warns.add(
+                            new ReportData(key, "validation", warn.get(key)));
+                    }
+                    this.appendWarnings(messung.getProbeId(), warns);
+                }
+            }
+            catch (ValidationException e) {
+                Map<String, Integer> err = e.getErrors();
+                List<ReportData> errs = new ArrayList<ReportData>();
+                for(String key: err.keySet()) {
+                    errs.add(
+                        new ReportData(key, "validation", err.get(key)));
+                }
+                this.appendErrors(messung.getProbeId(), errs);
+                Map<String, Integer> warn = e.getWarnings();
+                if (warn != null) {
+                	List<ReportData> warns = new ArrayList<ReportData>();
+                    for (String key: warn.keySet()) {
+                        warns.add(
+                            new ReportData(key, "validation", warn.get(key)));
+                    }
+                    this.appendWarnings(messung.getProbeId(), warns);
+                }
+                return false;
+            }
         }
-        else {
-            warn.addAll(writer.getWarnings());
+
+        return true;
+    	
+    }
+    
+    private boolean validateMesswerte(List<LMesswert> werte) {
+        for(LMesswert messwert: werte) {
+            try {
+                Map<String, Integer> warn =
+                    messwertValidator.validate(messwert, false);
+                if (warn != null) {
+                	List<ReportData> warns = new ArrayList<ReportData>();
+                    for (String key : warn.keySet()) {
+                        warns.add(
+                            new ReportData(key, "validation", warn.get(key)));
+                    }
+                    this.appendWarnings(messwert.getProbeId(), warns);
+                }
+            }
+            catch (ValidationException e) {
+                Map<String, Integer> err = e.getErrors();
+                List<ReportData> errs = new ArrayList<ReportData>();
+                for(String key: err.keySet()) {
+                    errs.add(
+                        new ReportData(key, "validation", err.get(key)));
+                }
+                this.appendErrors(messwert.getProbeId(), errs);
+                Map<String, Integer> warn = e.getWarnings();
+                if (warn != null) {
+                	List<ReportData> warns = new ArrayList<ReportData>();
+                    for (String key: warn.keySet()) {
+                        warns.add(
+                            new ReportData(key, "validation", warn.get(key)));
+                    }
+                    this.appendWarnings(messwert.getProbeId(), warns);
+                }
+                return false;
+            }
         }
+        return true;
+    } 
+    
+    private boolean validateLOrte(List<LOrt> orte) {
+        for(LOrt ort: orte) {
+            try {
+                Map<String, Integer> warn =
+                    ortValidator.validate(ort, false);
+                if (warn != null) {
+                	List<ReportData> warns = new ArrayList<ReportData>();
+                    for (String key : warn.keySet()) {
+                        warns.add(
+                            new ReportData(key, "validation", warn.get(key)));
+                    }
+                    this.appendWarnings(ort.getProbeId(), warns);
+                }
+            }
+            catch (ValidationException e) {
+                Map<String, Integer> err = e.getErrors();
+                List<ReportData> errs = new ArrayList<ReportData>();
+                for(String key: err.keySet()) {
+                    errs.add(
+                        new ReportData(key, "validation", err.get(key)));
+                }
+                this.appendErrors(ort.getProbeId(), errs);
+                Map<String, Integer> warn = e.getWarnings();
+                if (warn != null) {
+                	List<ReportData> warns = new ArrayList<ReportData>();
+                    for (String key: warn.keySet()) {
+                        warns.add(
+                            new ReportData(key, "validation", warn.get(key)));
+                    }
+                    this.appendWarnings(ort.getProbeId(), warns);
+                }
+                return false;
+            }
+        }
+        return true;
+    	
     }
 
     /**
@@ -306,4 +460,27 @@
         this.errors = new HashMap<String, List<ReportData>>();
         this.warnings = new HashMap<String, List<ReportData>>();
     }
+
+    private void appendErrors(String probeId, List<ReportData> errs) {
+		List<ReportData> err = this.errors.get(probeId);
+		if (err == null) {
+			this.errors.put(probeId, errs);
+		}
+		else {
+			err.addAll(errs);
+			this.errors.put(probeId, err);
+		}
+	}
+
+	private void appendWarnings(String probeId, List<ReportData> warns) {
+    	List<ReportData> warn = this.warnings.get(probeId);
+    	if (warn == null) {
+    		this.warnings.put(probeId, warns);
+    	}
+    	else {
+    		warn.addAll(warns);
+    		this.warnings.put(probeId, warn);
+    	}
+	}
+
 }
diff -r 0bc65c128530 -r c63281230bc3 src/main/java/de/intevation/lada/data/importer/LAFWriter.java
--- a/src/main/java/de/intevation/lada/data/importer/LAFWriter.java	Wed Jan 08 15:08:29 2014 +0100
+++ b/src/main/java/de/intevation/lada/data/importer/LAFWriter.java	Mon Jan 13 15:46:00 2014 +0100
@@ -41,19 +41,6 @@
     private EntityManager em;
 
     @Inject
-    @Named("lprobevalidator")
-    private Validator probeValidator;
-    @Inject
-    @Named("lmessungvalidator")
-    private Validator messungValidator;
-    @Inject
-    @Named("lortvalidator")
-    private Validator ortValidator;
-    @Inject
-    @Named("lmesswertvalidator")
-    private Validator messwertValidator;
-
-    @Inject
     @Named("lproberepository")
     private Repository probeRepository;
     @Inject
@@ -100,32 +87,7 @@
             errors.add(new ReportData("auth", "not authorized", 699));
             return false;
         }
-        try {
-            Map<String, Integer> warn =
-                probeValidator.validate(probe, false);
-            if (warn != null) {
-                for (String key: warn.keySet()) {
-                    warnings.add(new ReportData(key, "validation", warn.get(key)));
-                }
-            }
-        }
-        catch (ValidationException e) {
-            Map<String, Integer> err = e.getErrors();
-            for(String key: err.keySet()) {
-                errors.add(new ReportData(key, "validation", err.get(key)));
-            }
-            Map<String, Integer> warn = e.getWarnings();
-            if (warn != null) {
-                for (String key: warn.keySet()) {
-                    warnings.add(new ReportData(key, "validation", warn.get(key)));
-                }
-            }
-            if (probe.getProbeId() == null) {
-                errors.add(new ReportData("probeId", "missing", 673));
-            }
-            return false;
-        }
-        if (probe.getProbeId() == null) {
+         if (probe.getProbeId() == null) {
             errors.add(new ReportData("probeId", "missing", 673));
             return false;
         }
@@ -153,34 +115,8 @@
         List<LMessung> messungen
     ) {
         for(LMessung messung: messungen) {
-            try {
-                Map<String, Integer> warn =
-                    messungValidator.validate(messung, false);
-                messungRepository.create(messung);
-                if (warn != null) {
-                    for (String key : warn.keySet()) {
-                        warnings.add(
-                            new ReportData(key, "validation", warn.get(key)));
-                    }
-                }
-            }
-            catch (ValidationException e) {
-                Map<String, Integer> err = e.getErrors();
-                for(String key: err.keySet()) {
-                    errors.add(
-                        new ReportData(key, "validation", err.get(key)));
-                }
-                Map<String, Integer> warn = e.getWarnings();
-                if (warn != null) {
-                    for (String key: warn.keySet()) {
-                        warnings.add(
-                            new ReportData(key, "validation", warn.get(key)));
-                    }
-                }
-                return false;
-            }
+            messungRepository.create(messung);
         }
-
         return true;
     }
 
@@ -209,32 +145,7 @@
     @Override
     public boolean writeLOrte(AuthenticationResponse auth, List<LOrt> orte) {
         for(LOrt ort: orte) {
-            try {
-                Map<String, Integer> warn =
-                    ortValidator.validate(ort, false);
-                lortRepository.create(ort);
-                if (warn != null) {
-                    for (String key : warn.keySet()) {
-                        warnings.add(
-                            new ReportData(key, "validation", warn.get(key)));
-                    }
-                }
-            }
-            catch (ValidationException e) {
-                Map<String, Integer> err = e.getErrors();
-                for(String key: err.keySet()) {
-                    errors.add(
-                        new ReportData(key, "validation", err.get(key)));
-                }
-                Map<String, Integer> warn = e.getWarnings();
-                if (warn != null) {
-                    for (String key: warn.keySet()) {
-                        warnings.add(
-                            new ReportData(key, "validation", warn.get(key)));
-                    }
-                }
-                return false;
-            }
+            lortRepository.create(ort);
         }
         return true;
     }
@@ -293,34 +204,9 @@
         List<LMesswert> werte
     ) {
         for(LMesswert messwert: werte) {
-            try {
-                Map<String, Integer> warn =
-                    messwertValidator.validate(messwert, false);
-                messwertRepository.create(messwert);
-                if (warn != null) {
-                    for (String key : warn.keySet()) {
-                        warnings.add(
-                            new ReportData(key, "validation", warn.get(key)));
-                    }
-                }
-            }
-            catch (ValidationException e) {
-                Map<String, Integer> err = e.getErrors();
-                for(String key: err.keySet()) {
-                    errors.add(
-                        new ReportData(key, "validation", err.get(key)));
-                }
-                Map<String, Integer> warn = e.getWarnings();
-                if (warn != null) {
-                    for (String key: warn.keySet()) {
-                        warnings.add(
-                            new ReportData(key, "validation", warn.get(key)));
-                    }
-                }
-                return false;
-            }
+            messwertRepository.create(messwert);
         }
-        return false;
+        return true;
     }
 
     /**


More information about the Lada-commits mailing list