# HG changeset patch # User Raimund Renkert # Date 1377250524 -7200 # Node ID 5844d7457dde003e79b69824b26b2fe083c3aed7 # Parent 6621f7345c0611bca534cc16658729535cb16497 Completed importer for LAF format. Ort objects still need some attention. diff -r 6621f7345c06 -r 5844d7457dde pom.xml --- 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 @@ maven-compiler-plugin 2.3.1 - 1.6 - 1.6 + 1.7 + 1.7 diff -r 6621f7345c06 -r 5844d7457dde src/main/java/de/intevation/lada/data/importer/AttributeMapper.java --- 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 einheiten = + (List)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 messgroessen = + (List)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; diff -r 6621f7345c06 -r 5844d7457dde src/main/java/de/intevation/lada/data/importer/Importer.java --- 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> getErrors(); + public Map> getWarnings(); } diff -r 6621f7345c06 -r 5844d7457dde src/main/java/de/intevation/lada/data/importer/LAFFormat.java --- 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; diff -r 6621f7345c06 -r 5844d7457dde src/main/java/de/intevation/lada/data/importer/LAFImporter.java --- 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> warnings; + private Map> errors; + + public LAFImporter() { + warnings = new HashMap>(); + errors = new HashMap>(); + } + + /** + * @return the warnings + */ + public Map> getWarnings() { + return warnings; + } + + /** + * @return the errors + */ + public Map> 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 proben = parser.getProben(); + List messungen = parser.getMessungen(); + List orte = parser.getOrte(); + List pKommentare = parser.getProbeKommentare(); + List mKommentare = parser.getMessungKommentare(); + List messwerte = parser.getMesswerte(); + writeProben(auth, proben); + writeMessungen(auth, messungen); + //writeOrte(auth, orte); + writePKommentare(auth, pKommentare); + writeMKommentare(auth, mKommentare); + writeMesswerte(auth, messwerte); + } + else { + Map err = new HashMap(); + err.put("no success", 660); + errors.put("parser", err); + return false; + } + } + catch (LAFParserException e) { + Map err = new HashMap(); + err.put(e.getMessage(), 660); + errors.put("parser", err); + return false; + } + Map>> data = + new HashMap>>(); + data.put("warnings", warnings); + data.put("errors", errors); + return true; + } + + private void writeMessungen( + AuthenticationResponse auth, + List messungen + ) { + for(LMessung messung: messungen) { + try { + + Map 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 messwerte + ) { + for(LMesswert messwert: messwerte) { + try { + Map 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 kommentare + ) { + for(LKommentarP kommentar: kommentare) { + try { + pKommentarRepository.create(kommentar); + } + catch(Exception e) { + Map err = new HashMap(); + err.put( + kommentar.getProbeId() + " - " + + kommentar.getkId(), 661); + errors.put("lkommentarp", err); + } + } + } + + @TransactionAttribute(TransactionAttributeType.REQUIRED) + private void writeMKommentare( + AuthenticationResponse auth, + List 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 orte + ) { + for(LOrt ort: orte) { + try { + Map 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 proben) { + for (LProbe probe: proben) { + if (!authorized(probe, auth)) { + Map err = new HashMap(); + err.put("not authorized", 699); + errors.put(probe.getProbeId(), err); + continue; + } + try { + Map 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; } } diff -r 6621f7345c06 -r 5844d7457dde src/main/java/de/intevation/lada/data/importer/LAFParser.java --- 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 proben; @@ -33,7 +34,7 @@ public LAFParser() { this.setDryRun(false); - this.producer = new LAFProducer(); + //this.producer = new LAFProducer(); this.proben = new ArrayList(); this.messungen = new ArrayList(); this.orte = new ArrayList(); @@ -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 getProben() { + return proben; + } + + /** + * @return the messungen + */ + public List getMessungen() { + return messungen; + } + + /** + * @return the orte + */ + public List getOrte() { + return orte; + } + + /** + * @return the messwerte + */ + public List getMesswerte() { + return messwerte; + } + + /** + * @return the probeKommentare + */ + public List getProbeKommentare() { + return probeKommentare; + } + + /** + * @return the messungKommentare + */ + public List getMessungKommentare() { + return messungKommentare; + } } diff -r 6621f7345c06 -r 5844d7457dde src/main/java/de/intevation/lada/data/importer/LAFProducer.java --- 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 messungFormat; private List ortFormat; + @Inject private AttributeMapper mapper; public LAFProducer() { - this.mapper = new AttributeMapper(); this.probe = new LProbe(); this.pKommentare = new ArrayList(); this.mKommentare = new ArrayList(); this.messungen = new ArrayList(); this.orte = new ArrayList(); this.messwerte = new ArrayList(); - 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); } diff -r 6621f7345c06 -r 5844d7457dde src/main/java/de/intevation/lada/rest/LProbeService.java --- 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> data = input.getFormDataMap(); + try { + List parts = input.getParts(); + for (InputPart part: parts) { + InputStream inStream = part.getBody(InputStream.class, null); + MultivaluedMap 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 respData = new LinkedList(); + respData.add(importer.getErrors()); + respData.add(importer.getWarnings()); + Map fileData = new HashMap(); + 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) {