changeset 321:5844d7457dde

Completed importer for LAF format. Ort objects still need some attention.
author Raimund Renkert <rrenkert@intevation.de>
date Fri, 23 Aug 2013 11:35:24 +0200
parents 6621f7345c06
children 81cad9e67a21
files pom.xml src/main/java/de/intevation/lada/data/importer/AttributeMapper.java src/main/java/de/intevation/lada/data/importer/Importer.java src/main/java/de/intevation/lada/data/importer/LAFFormat.java src/main/java/de/intevation/lada/data/importer/LAFImporter.java src/main/java/de/intevation/lada/data/importer/LAFParser.java src/main/java/de/intevation/lada/data/importer/LAFProducer.java src/main/java/de/intevation/lada/rest/LProbeService.java
diffstat 8 files changed, 568 insertions(+), 25 deletions(-) [+]
line wrap: on
line diff
--- a/pom.xml	Fri Aug 23 11:32:21 2013 +0200
+++ b/pom.xml	Fri Aug 23 11:35:24 2013 +0200
@@ -209,8 +209,8 @@
                 <artifactId>maven-compiler-plugin</artifactId>
                 <version>2.3.1</version>
                 <configuration>
-                    <source>1.6</source>
-                    <target>1.6</target>
+                    <source>1.7</source>
+                    <target>1.7</target>
                 </configuration>
             </plugin>
             <plugin>
--- a/src/main/java/de/intevation/lada/data/importer/AttributeMapper.java	Fri Aug 23 11:32:21 2013 +0200
+++ b/src/main/java/de/intevation/lada/data/importer/AttributeMapper.java	Fri Aug 23 11:35:24 2013 +0200
@@ -4,12 +4,16 @@
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
 import java.util.Date;
+import java.util.List;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
+import javax.ejb.Stateless;
 import javax.inject.Inject;
+import javax.inject.Named;
 import javax.persistence.EntityManager;
 
+import de.intevation.lada.data.Repository;
 import de.intevation.lada.model.LKommentarM;
 import de.intevation.lada.model.LKommentarP;
 import de.intevation.lada.model.LMessung;
@@ -17,15 +21,22 @@
 import de.intevation.lada.model.LMesswert;
 import de.intevation.lada.model.LOrt;
 import de.intevation.lada.model.LProbe;
+import de.intevation.lada.model.SMessEinheit;
+import de.intevation.lada.model.SMessgroesse;
+import de.intevation.lada.rest.Response;
 
-
+@Stateless
 public class AttributeMapper
 {
     @Inject
     private EntityManager em;
 
+    @Inject
+    @Named("readonlyrepository")
+    private Repository sRepository;
+
     public LProbe addAttribute(String key, Object value, LProbe probe) {
-        DateFormat format = new SimpleDateFormat("yyyyMMDD hhmm");
+        DateFormat format = new SimpleDateFormat("yyyyMMdd HHmm");
         if ("datenbasis_s".equals(key)) {
             Integer v = Integer.valueOf(value.toString());
             probe.setDatenbasisId(v);
@@ -40,6 +51,9 @@
             Integer v = Integer.valueOf(value.toString());
             probe.setMprId(v);
         }
+        else if ("netzkennung".equals(key)) {
+            probe.setNetzbetreiberId(value.toString());
+        }
         else if ("messprogramm_land".equals(key)) {
             probe.setMplId(value.toString());
         }
@@ -99,6 +113,8 @@
                 probe.setTest(false);
             }
         }
+        
+        probe.setProbenartId(1);
         return probe;
     }
 
@@ -107,7 +123,7 @@
         Object values,
         LKommentarP kommentar
     ) {
-        DateFormat format = new SimpleDateFormat("yyyyMMDD hhmm");
+        DateFormat format = new SimpleDateFormat("yyyyMMdd HHmm");
         String v = values.toString();
         String erzeuger = v.substring(1, 5);
         String date = v.substring(8, 21);
@@ -130,7 +146,7 @@
         Object values,
         LKommentarM kommentar
     ) {
-        DateFormat format = new SimpleDateFormat("yyyyMMDD hhmm");
+        DateFormat format = new SimpleDateFormat("yyyyMMdd HHmm");
         String v = values.toString();
         String erzeuger = v.substring(1, 5);
         String date = v.substring(8, 21);
@@ -153,11 +169,12 @@
         Object values,
         LMessung messung
     ) {
-        DateFormat format = new SimpleDateFormat("yyyyMMDD hhmm");
+        DateFormat format = new SimpleDateFormat("yyyyMMdd HHmm");
         if ("messungs_id".equals(key)) {
             LMessungId id = messung.getId();
             Integer v = Integer.valueOf(values.toString());
             id.setMessungsId(v);
+            messung.setMessungsId(v);
             messung.setId(id);
         }
         else if ("nebenprobennummer".equals(key)) {
@@ -203,9 +220,9 @@
         //TODO Does not perfectly match... Use better matching for floats.
         Matcher m = p.matcher(values.toString());
         if (m.matches()) {
-            String messgroesse = m.group(1);
+            String messgroesse = m.group(1).substring(1, m.group(1).length() - 1);
             String wert = m.group(2);
-            String einheit = m.group(3);
+            String einheit = m.group(3).substring(1, m.group(3).length() - 1);
             if (wert.startsWith(" <")) {
                 wert = wert.substring(2);
                 messwert.setGrenzwertueberschreitung(false);
@@ -216,6 +233,35 @@
             }
             float fWert = Float.valueOf(wert);
             messwert.setMesswert(fWert);
+            Response responseEinheit = sRepository.findAll(SMessEinheit.class);
+            List<SMessEinheit> einheiten = 
+                (List<SMessEinheit>)responseEinheit.getData();
+            boolean foundEinheit = false;
+            for (SMessEinheit e: einheiten) {
+                if(e.getEinheit().equals(einheit)) {
+                    foundEinheit = true;
+                    messwert.setMehId((int) e.getMehId());
+                }
+            }
+            if (!foundEinheit) {
+                return null;
+                //TODO: handle warning!
+            }
+            Response responseGroesse = sRepository.findAll(SMessgroesse.class);
+            List<SMessgroesse> messgroessen = 
+                (List<SMessgroesse>)responseGroesse.getData();
+            boolean foundGroesse = false;
+            for (SMessgroesse g: messgroessen) {
+                if(g.getMessgro0esse().equals(messgroesse)) {
+                    foundGroesse = true;
+                    messwert.setMessgroesseId(g.getMessgroesseId());
+                    messwert.getId().setMessgroesseId(g.getMessgroesseId());
+                }
+            }
+            if (!foundGroesse) {
+                return null;
+                //TODO: handle warning!
+            }
         }
         //TODO: Match the other values.
         return messwert;
--- a/src/main/java/de/intevation/lada/data/importer/Importer.java	Fri Aug 23 11:32:21 2013 +0200
+++ b/src/main/java/de/intevation/lada/data/importer/Importer.java	Fri Aug 23 11:35:24 2013 +0200
@@ -1,10 +1,14 @@
 package de.intevation.lada.data.importer;
 
+import java.util.Map;
+
 import de.intevation.lada.auth.AuthenticationResponse;
 import de.intevation.lada.rest.Response;
 
 
 public interface Importer
 {
-    public Response importData(String content, AuthenticationResponse auth);
+    public boolean importData(String content, AuthenticationResponse auth);
+    public Map<String, Map<String, Integer>> getErrors();
+    public Map<String, Map<String, Integer>> getWarnings();
 }
--- a/src/main/java/de/intevation/lada/data/importer/LAFFormat.java	Fri Aug 23 11:32:21 2013 +0200
+++ b/src/main/java/de/intevation/lada/data/importer/LAFFormat.java	Fri Aug 23 11:35:24 2013 +0200
@@ -9,8 +9,6 @@
 import java.util.List;
 import java.util.regex.Pattern;
 
-import javax.ejb.Singleton;
-
 import org.json.JSONArray;
 import org.json.JSONException;
 import org.json.JSONObject;
--- a/src/main/java/de/intevation/lada/data/importer/LAFImporter.java	Fri Aug 23 11:32:21 2013 +0200
+++ b/src/main/java/de/intevation/lada/data/importer/LAFImporter.java	Fri Aug 23 11:35:24 2013 +0200
@@ -1,16 +1,391 @@
 package de.intevation.lada.data.importer;
 
+import java.math.BigInteger;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import javax.ejb.Stateless;
+import javax.ejb.TransactionAttribute;
+import javax.ejb.TransactionAttributeType;
+import javax.inject.Inject;
 import javax.inject.Named;
+import javax.persistence.EntityManager;
+import javax.persistence.Query;
 
 import de.intevation.lada.auth.AuthenticationResponse;
+import de.intevation.lada.data.Repository;
+import de.intevation.lada.model.LKommentarM;
+import de.intevation.lada.model.LKommentarP;
+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.rest.Response;
+import de.intevation.lada.validation.ValidationException;
+import de.intevation.lada.validation.Validator;
 
 @Named("lafimporter")
+@Stateless
 public class LAFImporter
 implements Importer
 {
+
+    @Inject
+    private EntityManager em;
+
+    @Inject
+    private LAFParser parser;
+
+    @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
+    @Named("lmessungrepository")
+    private Repository messungRepository;
+    @Inject
+    @Named("lortrepository")
+    private Repository ortRepository;
+    @Inject
+    @Named("lkommentarRepository")
+    private Repository pKommentarRepository;
+    @Inject
+    @Named("lkommentarmrepository")
+    private Repository mKommentarRepository;
+    @Inject
+    @Named("lmesswertrepository")
+    private Repository messwertRepository;
+
+    private Map<String, Map<String, Integer>> warnings;
+    private Map<String, Map<String, Integer>> errors;
+
+    public LAFImporter() {
+        warnings = new HashMap<String, Map<String, Integer>>();
+        errors = new HashMap<String, Map<String, Integer>>();
+    }
+
+    /**
+     * @return the warnings
+     */
+    public Map<String, Map<String, Integer>> getWarnings() {
+        return warnings;
+    }
+
+    /**
+     * @return the errors
+     */
+    public Map<String, Map<String, Integer>> getErrors() {
+        return errors;
+    }
+
     @Override
-    public Response importData(String content, AuthenticationResponse auth) {
-        return new Response(true, 200, null);
+    public boolean importData(String content, AuthenticationResponse auth) {
+        try {
+            boolean success = parser.parse(content);
+            if (success) {
+                List<LProbe> proben = parser.getProben();
+                List<LMessung> messungen = parser.getMessungen();
+                List<LOrt> orte = parser.getOrte();
+                List<LKommentarP> pKommentare = parser.getProbeKommentare();
+                List<LKommentarM> mKommentare = parser.getMessungKommentare();
+                List<LMesswert> messwerte = parser.getMesswerte();
+                writeProben(auth, proben);
+                writeMessungen(auth, messungen);
+                //writeOrte(auth, orte);
+                writePKommentare(auth, pKommentare);
+                writeMKommentare(auth, mKommentare);
+                writeMesswerte(auth, messwerte);
+            }
+            else {
+                Map<String, Integer> err = new HashMap<String, Integer>();
+                err.put("no success", 660);
+                errors.put("parser", err);
+                return false;
+            }
+        }
+        catch (LAFParserException e) {
+            Map<String, Integer> err = new HashMap<String, Integer>();
+            err.put(e.getMessage(), 660);
+            errors.put("parser", err);
+            return false;
+        }
+        Map<String, Map<String, Map<String, Integer>>> data =
+            new HashMap<String, Map<String,Map<String, Integer>>>();
+        data.put("warnings", warnings);
+        data.put("errors", errors);
+        return true;
+    }
+
+    private void writeMessungen(
+        AuthenticationResponse auth,
+        List<LMessung> messungen
+    ) {
+        for(LMessung messung: messungen) {
+            try {
+
+                Map<String, Integer> warn =
+                    messungValidator.validate(messung, false);
+                messungRepository.create(messung);
+                if (warn != null) {
+                    warnings.put(
+                        messung.getMessungsId().toString(),
+                        warn);
+                }
+            }
+            catch (ValidationException e) {
+                errors.put(messung.getProbeId(), e.getErrors());
+                warnings.put(
+                    messung.getProbeId(),
+                    e.getWarnings());
+            }
+        }
+    }
+
+    @TransactionAttribute(TransactionAttributeType.REQUIRED)
+    private void writeMesswerte(
+        AuthenticationResponse auth,
+        List<LMesswert> messwerte
+    ) {
+        for(LMesswert messwert: messwerte) {
+            try {
+                Map<String, Integer> warn =
+                    messwertValidator.validate(messwert, false);
+                Response r = messwertRepository.create(messwert);
+                if (warn != null) {
+                    warnings.put(
+                        messwert.getProbeId(),
+                        warn);
+                }
+            }
+            catch (ValidationException e) {
+                errors.put(messwert.getProbeId(), e.getErrors());
+                warnings.put(
+                    messwert.getProbeId(),
+                    e.getWarnings());
+            }
+        }
+    }
+
+    @TransactionAttribute(TransactionAttributeType.REQUIRED)
+    private void writePKommentare(
+        AuthenticationResponse auth,
+        List<LKommentarP> kommentare
+    ) {
+        for(LKommentarP kommentar: kommentare) {
+            try {
+                pKommentarRepository.create(kommentar);
+            }
+            catch(Exception e) {
+                Map<String, Integer> err = new HashMap<String, Integer>();
+                err.put(
+                    kommentar.getProbeId() + " - " +
+                    kommentar.getkId(), 661);
+                errors.put("lkommentarp", err);
+            }
+        }
+    }
+
+    @TransactionAttribute(TransactionAttributeType.REQUIRED)
+    private void writeMKommentare(
+        AuthenticationResponse auth,
+        List<LKommentarM> kommentare
+    ) {
+        for(LKommentarM kommentar: kommentare) {
+            Query q =
+            em.createNativeQuery(
+                "select nextval('kommentar_m_id_seq')");
+            BigInteger seqId = (BigInteger)q.getSingleResult();
+            kommentar.getId().setKId(seqId.intValue());
+            mKommentarRepository.create(kommentar);
+        }
+    }
+
+    @TransactionAttribute(TransactionAttributeType.REQUIRED)
+    private void writeOrte(
+        AuthenticationResponse auth,
+        List<LOrt> orte
+    ) {
+        for(LOrt ort: orte) {
+            try {
+                Map<String, Integer> warn =
+                    ortValidator.validate(ort, false);
+                ortRepository.create(ort);
+                if (warn != null) {
+                    warnings.put(String.valueOf(ort.getOrtId()), warn);
+                }
+            }
+            catch (ValidationException e) {
+                errors.put(String.valueOf(ort.getOrtId()), e.getErrors());
+                warnings.put(
+                    String.valueOf(ort.getOrtId()),
+                    e.getWarnings());
+            }
+        }
+    }
+
+    @TransactionAttribute(TransactionAttributeType.REQUIRED)
+    private void writeProben(AuthenticationResponse auth, List<LProbe> proben) {
+        for (LProbe probe: proben) {
+            if (!authorized(probe, auth)) {
+                Map<String, Integer> err = new HashMap<String, Integer>();
+                err.put("not authorized", 699);
+                errors.put(probe.getProbeId(), err);
+                continue;
+            }
+            try {
+                Map<String, Integer> warn =
+                    probeValidator.validate(probe, false);
+                if (warn != null) {
+                    warnings.put(probe.getProbeId(), warn);
+                }
+            }
+            catch (ValidationException e) {
+                errors.put(probe.getProbeId(), e.getErrors());
+                warnings.put(probe.getProbeId(), e.getWarnings());
+                continue;
+            }
+            persist(probe);
+        }
+    }
+
+    private boolean authorized(LProbe probe, AuthenticationResponse auth) {
+        if (auth.getNetzbetreiber().contains(probe.getNetzbetreiberId()) &&
+            auth.getMst().contains(probe.getMstId())) {
+            return true;
+        }
+        return false;
+    }
+
+    @TransactionAttribute(TransactionAttributeType.REQUIRED)
+    private void persist(LProbe probe) {
+        String queryColumns = "insert into l_probe (probe_id, ba_id, test," +
+            " datenbasis_id, netzbetreiber_id, mst_id, probenart_id, umw_id";
+        String queryParameter = " values (:probe_id, :ba_id, :test," +
+            " :datenbasis_id, :netzbetreiber_id, :mst_id, :probenart_id," +
+            " :umw_id";
+        if (probe.getErzeugerId() != null) {
+            queryColumns += ", erzeuger_id";
+            queryParameter += ", :erzeuger_id";
+        }
+        if (probe.getHauptprobenNr() != null) {
+            queryColumns += ", hauptproben_nr";
+            queryParameter += ", :hauptproben_nr";
+        }
+        if (probe.getLetzteAenderung() != null) {
+            queryColumns += ", letzte_aenderung";
+            queryParameter += ", :letzte_aenderung";
+        }
+        if (probe.getMedia() != null) {
+            queryColumns += ", media";
+            queryParameter += ", :media";
+        }
+        if (probe.getMediaDesk() != null) {
+            queryColumns += ", media_desk";
+            queryParameter += ", :media_desk";
+        }
+        if (probe.getMittelungsdauer() != null) {
+            queryColumns += ", mittelungsdauer";
+            queryParameter += ", :mittelungsdauer";
+        }
+        if (probe.getMpKat() != null) {
+            queryColumns += ", mp_kat";
+            queryParameter += ", mp_kat";
+        }
+        if (probe.getMplId() != null) {
+            queryColumns += ", mpl_id";
+            queryParameter += ", :mpl_id";
+        }
+        if (probe.getMprId() != null) {
+            queryColumns += ", mpr_id";
+            queryParameter += ", :mpr_id";
+        }
+        if (probe.getProbeNehmerId() != null) {
+            queryColumns += ", probe_nehmer_id";
+            queryParameter += ", :probe_nehmer_id";
+        }
+        if (probe.getProbeentnahmeBeginn() != null) {
+            queryColumns += ", probeentnahme_beginn";
+            queryParameter += ", :probeentnahme_beginn";
+        }
+        if (probe.getProbeentnahmeEnde() != null) {
+            queryColumns += ", probeentnahme_ende";
+            queryParameter += ", :probeentnahme_ende";
+        }
+        if (probe.getSolldatumBeginn() != null) {
+            queryColumns += ", solldatum_beginn";
+            queryParameter += ", :solldatum_beginn";
+        }
+        if (probe.getSolldatumEnde() != null) {
+            queryColumns += ", solldatum_ende";
+            queryParameter += ", :solldatum_ende";
+        }
+        queryColumns += ") " + queryParameter + ")";
+        
+        Query insert = em.createNativeQuery(queryColumns);
+        insert.setParameter("probe_id", probe.getProbeId());
+        insert.setParameter("ba_id", probe.getBaId());
+        insert.setParameter("datenbasis_id", probe.getDatenbasisId());
+        insert.setParameter("mst_id", probe.getMstId());
+        insert.setParameter("netzbetreiber_id", probe.getNetzbetreiberId());
+        insert.setParameter("probenart_id", probe.getProbenartId());
+        insert.setParameter("test", probe.isTest());
+        insert.setParameter("umw_id", probe.getUmwId());
+        if (probe.getErzeugerId() != null) {
+            insert.setParameter("erzeuger_id", probe.getErzeugerId());
+        }
+        if (probe.getHauptprobenNr() != null) {
+            insert.setParameter("hauptproben_nr", probe.getHauptprobenNr());
+        }
+        if (probe.getLetzteAenderung() != null) {
+            insert.setParameter("letzte_aenderung", probe.getLetzteAenderung());
+        }
+        if (probe.getMedia() != null) {
+            insert.setParameter("media", probe.getMedia());
+        }
+        if (probe.getMediaDesk() != null) {
+            insert.setParameter("media_desk", probe.getMediaDesk());
+        }
+        if (probe.getMittelungsdauer() != null) {
+            insert.setParameter("mittelungsdauer", probe.getMittelungsdauer());
+        }
+        if (probe.getMpKat() != null) {
+            insert.setParameter("mp_kat", probe.getMpKat());
+        }
+        if (probe.getMplId() != null) {
+            insert.setParameter("mpl_id", probe.getMplId());
+        }
+        if (probe.getMprId() != null) {
+            insert.setParameter("mpr_id", probe.getMprId());
+        }
+        if (probe.getProbeNehmerId() != null) {
+            insert.setParameter("probe_nehmer_id", probe.getProbeNehmerId());
+        }
+        if (probe.getProbeentnahmeBeginn() != null) {
+            insert.setParameter("probeentnahme_beginn", probe.getProbeentnahmeBeginn());
+        }
+        if (probe.getProbeentnahmeEnde() != null) {
+            insert.setParameter("probeentnahme_ende", probe.getProbeentnahmeEnde());
+        }
+        if (probe.getSolldatumBeginn() != null) {
+            insert.setParameter("solldatum_beginn", probe.getSolldatumBeginn());
+        }
+        if (probe.getSolldatumEnde() != null) {
+            insert.setParameter("solldatum_ende", probe.getSolldatumEnde());
+        }
+        int res = insert.executeUpdate();
+        int i = res;
     }
 }
--- a/src/main/java/de/intevation/lada/data/importer/LAFParser.java	Fri Aug 23 11:32:21 2013 +0200
+++ b/src/main/java/de/intevation/lada/data/importer/LAFParser.java	Fri Aug 23 11:35:24 2013 +0200
@@ -12,6 +12,7 @@
 import de.intevation.lada.model.LMesswert;
 import de.intevation.lada.model.LOrt;
 import de.intevation.lada.model.LProbe;
+import de.intevation.lada.model.LProbe;
 
 
 public class LAFParser {
@@ -20,8 +21,8 @@
 
     private boolean dryRun;
 
-    //@Inject
-    //@Named("lafproducer")
+    @Inject
+    @Named("lafproducer")
     private Producer producer;
 
     List<LProbe> proben;
@@ -33,7 +34,7 @@
 
     public LAFParser() {
         this.setDryRun(false);
-        this.producer = new LAFProducer();
+        //this.producer = new LAFProducer();
         this.proben = new ArrayList<LProbe>();
         this.messungen = new ArrayList<LMessung>();
         this.orte = new ArrayList<LOrt>();
@@ -45,6 +46,13 @@
     public boolean parse(String laf)
     throws LAFParserException
     {
+        this.proben.clear();
+        this.messungen.clear();
+        this.orte.clear();
+        this.messwerte.clear();
+        this.probeKommentare.clear();
+        this.messungKommentare.clear();
+
         if (!laf.startsWith("%PROBE%\n")) {
             throw new LAFParserException("No %PROBE% at the begining.");
         }
@@ -131,6 +139,13 @@
                         producer.newOrt();
                     }
                 }
+                if (headerString.contains("%ENDE%")) {
+                    if (!dryRun) {
+                        this.producer.newMessung();
+                        this.producer.newOrt();
+                    }
+                    return;
+                }
                 continue;
             }
             else if (current == '"' && !value) {
@@ -178,4 +193,46 @@
     public void setDryRun(boolean dryRun) {
         this.dryRun = dryRun;
     }
+
+    /**
+     * @return the proben
+     */
+    public List<LProbe> getProben() {
+        return proben;
+    }
+
+    /**
+     * @return the messungen
+     */
+    public List<LMessung> getMessungen() {
+        return messungen;
+    }
+
+    /**
+     * @return the orte
+     */
+    public List<LOrt> getOrte() {
+        return orte;
+    }
+
+    /**
+     * @return the messwerte
+     */
+    public List<LMesswert> getMesswerte() {
+        return messwerte;
+    }
+
+    /**
+     * @return the probeKommentare
+     */
+    public List<LKommentarP> getProbeKommentare() {
+        return probeKommentare;
+    }
+
+    /**
+     * @return the messungKommentare
+     */
+    public List<LKommentarM> getMessungKommentare() {
+        return messungKommentare;
+    }
 }
--- a/src/main/java/de/intevation/lada/data/importer/LAFProducer.java	Fri Aug 23 11:32:21 2013 +0200
+++ b/src/main/java/de/intevation/lada/data/importer/LAFProducer.java	Fri Aug 23 11:35:24 2013 +0200
@@ -2,11 +2,12 @@
 
 import java.util.ArrayList;
 import java.util.List;
-import java.util.regex.Pattern;
 
+import javax.inject.Inject;
 import javax.inject.Named;
 
 import de.intevation.lada.model.LKommentarM;
+import de.intevation.lada.model.LKommentarMId;
 import de.intevation.lada.model.LKommentarP;
 import de.intevation.lada.model.LMessung;
 import de.intevation.lada.model.LMessungId;
@@ -14,6 +15,7 @@
 import de.intevation.lada.model.LMesswertId;
 import de.intevation.lada.model.LOrt;
 import de.intevation.lada.model.LProbe;
+import de.intevation.lada.model.LProbe;
 
 @Named("lafproducer")
 public class LAFProducer
@@ -34,17 +36,17 @@
     private List<EntryFormat> messungFormat;
     private List<EntryFormat> ortFormat;
 
+    @Inject
     private AttributeMapper mapper;
 
     public LAFProducer() {
-        this.mapper = new AttributeMapper();
         this.probe = new LProbe();
         this.pKommentare = new ArrayList<LKommentarP>();
         this.mKommentare = new ArrayList<LKommentarM>();
         this.messungen = new ArrayList<LMessung>();
         this.orte = new ArrayList<LOrt>();
         this.messwerte = new ArrayList<LMesswert>();
-        String fileName = System.getProperty("de.intevation.lada.import");
+        String fileName = System.getProperty("de.intevation.lada.importconfig");
         LAFFormat format = new LAFFormat();
         format.readConfigFile(fileName);
         probenFormat = format.getFormat("probe");
@@ -56,12 +58,21 @@
     public void addData(String key, Object values) {
         String lKey = key.toLowerCase();
         if(lKey.equals("probenkommentar")) {
+            LKommentarP kommentar = new LKommentarP();
+            kommentar.setProbeId(this.probe.getProbeId());
             this.pKommentare.add(
-                mapper.addAttribute(lKey, values, new LKommentarP()));
+                mapper.addAttribute(lKey, values, kommentar));
         }
         else if (lKey.equals("kommentar")) {
+            LKommentarMId id = new LKommentarMId();
+            id.setMessungsId(this.messung.getMessungsId());
+            id.setProbeId(this.probe.getProbeId());
+            LKommentarM kommentar = new LKommentarM();
+            kommentar.setId(id);
+            kommentar.setMessungsId(this.messung.getMessungsId());
+            kommentar.setProbeId(this.probe.getProbeId());
             this.mKommentare.add(
-                mapper.addAttribute(lKey, values, new LKommentarM()));
+                mapper.addAttribute(lKey, values, kommentar));
         }
         else if (lKey.equals("probenzusatzbeschreibung")) {
             //TODO: implement this!
@@ -75,8 +86,12 @@
             id.setMessungsId(this.messung.getId().getMessungsId());
             LMesswert m = new LMesswert();
             m.setId(id);
-            this.messwerte.add(
-                mapper.addAttribute(lKey, values, m));
+            m.setMessungsId(this.messung.getMessungsId());
+            m.setProbeId(this.probe.getProbeId());
+            LMesswert wert = mapper.addAttribute(lKey, values, m);
+            if (wert != null) {
+                this.messwerte.add(wert);
+            }
         }
         else if (isValidMessung(lKey, values.toString())) {
             this.messung = mapper.addAttribute(lKey, values, this.messung);
@@ -171,6 +186,7 @@
         LMessungId id = new LMessungId();
         id.setProbeId(this.probe.getProbeId());
         this.messung = new LMessung();
+        this.messung.setProbeId(this.probe.getProbeId());
         this.messung.setId(id);
     }
 
--- a/src/main/java/de/intevation/lada/rest/LProbeService.java	Fri Aug 23 11:32:21 2013 +0200
+++ b/src/main/java/de/intevation/lada/rest/LProbeService.java	Fri Aug 23 11:35:24 2013 +0200
@@ -1,6 +1,10 @@
 package de.intevation.lada.rest;
 
+import java.io.IOException;
+import java.io.InputStream;
 import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
 import java.util.logging.Logger;
@@ -34,6 +38,7 @@
 import de.intevation.lada.data.LProbeRepository;
 import de.intevation.lada.data.QueryBuilder;
 import de.intevation.lada.data.Repository;
+import de.intevation.lada.data.importer.Importer;
 import de.intevation.lada.model.LProbe;
 import de.intevation.lada.model.LProbeInfo;
 import de.intevation.lada.utils.QueryTools;
@@ -70,6 +75,10 @@
     @Named("dataauthorization")
     private Authorization authorization;
 
+    @Inject
+    @Named("lafimporter")
+    private Importer importer;
+
     /**
      * The logger for this class.
      */
@@ -261,6 +270,45 @@
     public Response upload(MultipartFormDataInput input, @Context HttpHeaders header) {
         try {
             AuthenticationResponse auth = authentication.authorizedGroups(header);
+            if (!authentication.isAuthorizedUser(header)) {
+                return new Response(false, 698, null);
+            }
+
+            String name = "";
+            String content = "";
+            Map<String, List<InputPart>> data = input.getFormDataMap();
+            try {
+                List<InputPart> parts = input.getParts();
+                for (InputPart part: parts) {
+                    InputStream inStream = part.getBody(InputStream.class, null);
+                    MultivaluedMap<String, String> headers = part.getHeaders();
+                    String[] cDisp = headers.getFirst("content-disposition").split(";");
+                    for (String fName : cDisp) {
+                        if (fName.trim().startsWith("filename")) {
+                            String[] fileName = fName.split("=");
+                            name = fileName[1].trim().replace("\"", "");
+                        }
+                    }
+                    content = IOUtils.toString(inStream);
+                }
+            }
+            catch (IOException e) {
+                return new Response(false, 603, null);
+            }
+
+            boolean success = importer.importData(content, auth);
+            List<Object> respData = new LinkedList<Object>();
+            respData.add(importer.getErrors());
+            respData.add(importer.getWarnings());
+            Map<String, String> fileData = new HashMap<String, String>();
+            fileData.put("filename", name);
+            respData.add(fileData);
+            int code = 200;
+            if (!success) {
+                code = 660;
+            }
+            Response response = new Response(success, code, respData);
+            return response;
             // TODO: Check Authorisation. How should we check the
             // authorisation while importing? I think we must differ between
             // updating already existing proben and creating new proben. (ti)
@@ -272,7 +320,6 @@
             //}
             // TODO: Response must contain a "file" attribute with the name of
             // the uploaded file.(ti) <2013-08-13 16:23> 
-            return new Response(true, 200, null);
             //return new Response(false, 698, null);
         }
         catch(AuthenticationException ae) {
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)