# HG changeset patch # User Raimund Renkert # Date 1429192144 -7200 # Node ID 374a2e78cec5319b5883a9b277547299ab6bc40a # Parent 093bfdcdb09c705044ed2085ed1e9b3173551d2f Added importer impl for laf file format. diff -r 093bfdcdb09c -r 374a2e78cec5 src/main/java/de/intevation/lada/importer/laf/AttributeMapper.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/de/intevation/lada/importer/laf/AttributeMapper.java Thu Apr 16 15:49:04 2015 +0200 @@ -0,0 +1,555 @@ +package de.intevation.lada.importer.laf; + +import java.math.BigInteger; +import java.sql.Timestamp; +import java.text.DateFormat; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +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.persistence.Query; + +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; +import de.intevation.lada.model.land.LMessung; +import de.intevation.lada.model.land.LMesswert; +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.util.annotation.RepositoryConfig; +import de.intevation.lada.util.data.QueryBuilder; +import de.intevation.lada.util.data.Repository; +import de.intevation.lada.util.data.RepositoryType; +import de.intevation.lada.util.rest.Response; + +/** + * The AttributeMapper is used to set object attributes via string based + * key value pairs. The key represents a member of an entity object. + * + * @author Raimund Renkert + */ +@Stateless +public class AttributeMapper +{ + + @Inject + private Logger logger; + + @Inject + @RepositoryConfig(type=RepositoryType.RW) + private Repository repository; + + private List warnings; + private List errors; + + /** + * Default constructor to create a new AttributeMapper object. + */ + public AttributeMapper() { + this.warnings = new ArrayList(); + this.errors = new ArrayList(); + } + + /** + * Add an attribute to the given LProbe object. + * + * @param key The key mapping to a object member. + * @param value The value to set. + * @param probe The entity object. + * @return The updated entity object. + */ + public LProbe addAttribute(String key, Object value, LProbe probe) { + DateFormat format = new SimpleDateFormat("yyyyMMdd HHmm"); + if ("datenbasis_s".equals(key) && probe.getDatenbasisId() == null) { + Integer v = Integer.valueOf(value.toString()); + probe.setDatenbasisId(v); + } + else if ("datenbasis_s".equals(key) && probe.getDatenbasisId() != null){ + this.warnings.add(new ReportItem(key, value.toString(), 672)); + } + + if ("datenbasis".equals(key) && probe.getDatenbasisId() == null) { + String nativeQuery = "select id from stammdaten.datenbasis where datenbasis = '"; + nativeQuery += value.toString() + "'"; + Query query = repository.entityManager("land").createNativeQuery(nativeQuery); + List result = query.getResultList(); + + Integer v = Integer.valueOf(result.get(0)[0].toString()); + probe.setDatenbasisId(v); + } + else if ("datenbasis".equals(key) && probe.getDatenbasisId() != null){ + this.warnings.add(new ReportItem(key, value.toString(), 672)); + } + + if ("probe_id".equals(key)) { + QueryBuilder builder = + new QueryBuilder( + repository.entityManager("land"), ProbeTranslation.class); + builder.and("probeIdAlt", value); + Response response = + repository.filter(builder.getQuery(), "land"); + List info = (List)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()); + } + + if ("mpr_id".equals(key)) { + Integer v = Integer.valueOf(value.toString()); + probe.setMprId(v); + } + + if ("netzkennung".equals(key)) { + probe.setNetzbetreiberId(value.toString()); + } + + if ("messstelle".equals(key)) { + probe.setMstId(value.toString()); + } + + if ("messprogramm_s".equals(key) && probe.getBaId() == null) { + probe.setBaId(value.toString()); + } + else if ("messprogramm_s".equals(key) && probe.getBaId() != null){ + this.warnings.add(new ReportItem(key, value.toString(), 672)); + } + + if ("soll_datum_uhrzeit_a".equals(key)) { + try { + Date d = format.parse(value.toString()); + probe.setSolldatumBeginn(new Timestamp(d.getTime())); + } + catch (ParseException e) { + this.warnings.add(new ReportItem(key, value.toString(), 674)); + } + } + if ("soll_datum_uhrzeit_e".equals(key)) { + try { + Date d = format.parse(value.toString()); + probe.setSolldatumEnde(new Timestamp(d.getTime())); + } + catch (ParseException e) { + this.warnings.add(new ReportItem(key, value.toString(), 674)); + } + } + if ("probenahme_datum_uhrzeit_a".equals(key)) { + try { + Date d = format.parse(value.toString()); + probe.setProbeentnahmeBeginn(new Timestamp(d.getTime())); + } + catch (ParseException e) { + this.warnings.add(new ReportItem(key, value.toString(), 674)); + } + } + if ("probenahme_datum_uhrzeit_e".equals(key)) { + try { + Date d = format.parse(value.toString()); + probe.setProbeentnahmeEnde(new Timestamp(d.getTime())); + } + catch (ParseException e) { + this.warnings.add(new ReportItem(key, value.toString(), 674)); + } + } + + if ("umweltbereich_s".equals(key) && probe.getUmwId() == null) { + probe.setUmwId(value.toString()); + } + else if ("umweltbereich_s".equals(key) && probe.getUmwId() != null){ + this.warnings.add(new ReportItem(key, value.toString(), 672)); + } + if ("umweltbereich_c".equals(key) && probe.getUmwId() == null) { + String nativeQuery = "select id from stammdaten.umwelt where umwelt_bereich= '"; + int length = value.toString().length() > 80 ? 80 : value.toString().length(); + nativeQuery += value.toString().substring(0, length) + "'"; + Query query = repository.entityManager("land").createNativeQuery(nativeQuery); + List result = query.getResultList(); + probe.setUmwId(result.get(0)[0].toString()); + } + else if ("umweltbereich_c".equals(key) && probe.getUmwId() != null){ + this.warnings.add(new ReportItem(key, value.toString(), 672)); + } + + if ("deskriptoren".equals(key)) { + probe.setMediaDesk(value.toString()); + } + + if ("testdaten".equals(key)) { + if (!value.toString().equals("0")) { + probe.setTest(true); + } + else { + probe.setTest(false); + } + } + + if ("medium".equals(key)) { + probe.setMedia(value.toString()); + } + + if ("probenart".equals(key)) { + String nativeQuery = "select id from stammdaten.probenart where probenart = '"; + nativeQuery += value.toString() + "'"; + Query query = repository.entityManager("land").createNativeQuery(nativeQuery); + List result = query.getResultList(); + probe.setProbenartId(Integer.valueOf(result.get(0).toString())); + } + return probe; + } + + /** + * Add an attribute to the given LKommentarP object. + * + * @param key The key mapping to a object member. + * @param value The value to set. + * @param kommentar The entity object. + * @return The updated entity object. + */ + public LKommentarP addAttribute( + String key, + Object value, + LKommentarP kommentar + ) { + 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 { + d = format.parse(date); + kommentar.setDatum(new Timestamp(d.getTime())); + } + catch (ParseException e) { + this.warnings.add(new ReportItem(key, value.toString(), 674)); + } + String text = v.substring(23, v.length() -1); + kommentar.setErzeuger(erzeuger); + kommentar.setText(text); + return kommentar; + } + + /** + * Add an attribute to the given LKommentarM object. + * + * @param key The key mapping to a object member. + * @param value The value to set. + * @param kommentar The entity object. + * @return The updated entity object. + */ + public LKommentarM addAttribute( + String key, + Object value, + LKommentarM kommentar + ) { + DateFormat format = new SimpleDateFormat("yyyyMMdd HHmm"); + String v = value.toString(); + String erzeuger = v.substring(1, 6); + String date = v.substring(8, 21); + Date d; + try { + d = format.parse(date); + kommentar.setDatum(new Timestamp(d.getTime())); + } + catch (ParseException e) { + this.warnings.add(new ReportItem(key, value.toString(), 674)); + } + String text = v.substring(23, v.length() -1); + kommentar.setErzeuger(erzeuger); + kommentar.setText(text); + return kommentar; + } + + /** + * Add an attribute to the given LMessung object. + * + * @param key The key mapping to a object member. + * @param value The value to set. + * @param messung The entity object. + * @return The updated entity object. + */ + public LMessung addAttribute( + String key, + Object value, + LMessung messung + ) { + DateFormat format = new SimpleDateFormat("yyyyMMdd HHmm"); + if ("nebenprobennummer".equals(key)) { + messung.setNebenprobenNr(value.toString()); + } + else if ("mess_datum_uhrzeit".equals(key)) { + try { + Date d = format.parse(value.toString()); + messung.setMesszeitpunkt(new Timestamp(d.getTime())); + } + catch (ParseException e) { + this.warnings.add(new ReportItem(key, value.toString(), 674)); + } + } + else if ("messzeit_sekunden".equals(key)) { + Integer i = Integer.valueOf(value.toString()); + messung.setMessdauer(i); + } + else if ("messmethode_s".equals(key)) { + messung.setMmtId(value.toString()); + } + else if ("bearbeitungsstatus".equals(key)) { + //ignored.!? + } + else if ("erfassung_abgeschlossen".equals(key)) { + if(!value.toString().equals("0")) { + messung.setFertig(true); + } + else { + messung.setFertig(false); + } + } + return messung; + } + + public MessungTranslation addAttribute( + String key, + 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; + } + + /** + * Add an attribute to the given LMesswert object. + * + * @param key The key mapping to a object member. + * @param value The value to set. + * @param messwert The entity object. + * @return The updated entity object. + */ + public LMesswert addAttribute( + String key, + Object value, + LMesswert messwert + ) { + Pattern p = Pattern.compile( + "(\".+\")( .+ )(\".+\")( .*)( .{1,12})( .{1,9})(.{0,9})(.{0,3})"); + //TODO Does not perfectly match... Use better matching for floats. + Matcher m = p.matcher(value.toString()); + if (m.matches()) { + String messgroesse = m.group(1).substring(1, m.group(1).length() - 1); + String wert = m.group(2); + String einheit = m.group(3).substring(1, m.group(3).length() - 1); + if (wert.startsWith(" <")) { + wert = wert.substring(2); + messwert.setGrenzwertueberschreitung(false); + } + else if (wert.startsWith(" >")) { + wert = wert.substring(2); + messwert.setGrenzwertueberschreitung(true); + } + float fWert = Float.valueOf(wert); + messwert.setMesswert(fWert); + + String nativeQuery = "select id from stammdaten.mess_einheit where einheit = '"; + nativeQuery += einheit + "'"; + Query query = repository.entityManager("land").createNativeQuery(nativeQuery); + List result = query.getResultList(); + + if (result.isEmpty()) { + this.errors.add(new ReportItem("messeinheit", "null", 673)); + return null; + } + else { + messwert.setMehId((Integer)result.get(0)); + } + + String nativeQuery2 = "select id from stammdaten.messgroesse where messgroesse = '"; + nativeQuery2 += messgroesse + "'"; + Query query2 = repository.entityManager("land").createNativeQuery(nativeQuery2); + List result2 = query2.getResultList(); + + if (result2.isEmpty()) { + this.errors.add(new ReportItem("messgroesse", "null", 673)); + return null; + } + else { + messwert.setMessgroesseId((Integer)result2.get(0)); + } + } + return messwert; + } + + /** + * Add an attribute to the OrtCreator. The creator is used to build the + * two objects Ort and LOrt. + * + * @param key The key mapping to a object member. + * @param value The value to set. + * @param ort The creator object. + * @return The updated creator object. + */ + public OrtCreator addAttribute( + String key, + Object value, + OrtCreator ort + ) { + if ("ort_code".equals(key)) { + ort.setOrtCode(value.toString()); + } + if ("ort_typ".equals(key)) { + ort.setOrtTyp(value.toString()); + } + if ("ort_zusatz".equals(key)) { + ort.setZusatztext(value.toString()); + } + if ("ort_land_lang".equals(key)) { + ort.setLandLang(value.toString()); + } + if ("ort_land_kurz".equals(key)) { + ort.setLandKurz(value.toString()); + } + if ("ort_land_s".equals(key)) { + ort.setLandS(value.toString()); + } + if ("ort_gemeindeschlüssel".equals(key)) { + ort.setGemSchluessel(value.toString()); + } + if ("ort_bezeichnung".equals(key)) { + ort.setBezeichnung(value.toString()); + } + if ("ort_beschreibung".equals(key)) { + ort.setBeschreibung(value.toString()); + } + if ("ort_nuts_code".equals(key)) { + ort.setNuts(value.toString()); + } + if ("ort_hoehe_land".equals(key)) { + ort.setHoehe(value.toString()); + } + if ("ort_koordinaten".equals(key)) { + ort.setKoordinaten(value.toString()); + } + if ("ort_koordinaten_s".equals(key)) { + ort.setKoordinatenS(value.toString()); + } + return ort; + } + + /** + * Add an attribute to the given LZusatzwert object. + * + * @param lKey The key mapping to a object member. + * @param value The value to set. + * @param wert The entity object. + * @return The updated entity object. + */ + public LZusatzWert addAttribute( + String lKey, + Object value, + LZusatzWert wert + ) { + String v = value.toString().substring(1); + int ndx = v.indexOf("\""); + String groesse = v.substring(0, ndx); + v = v.substring(ndx + 2); + ndx = v.indexOf(" "); + String w = v.substring(0, ndx); + v = v.substring(ndx + 2); + ndx = v.indexOf("\""); + String einheit = v.substring(0, ndx); + String fehler = v.substring(ndx + 2); + + String nativeQuery = "select id from stammdaten.probenzusatz where zusatzwert = '"; + nativeQuery += groesse + "'"; + Query query = repository.entityManager("land").createNativeQuery(nativeQuery); + List result = query.getResultList(); + + if (result == null || result.isEmpty()) { + this.errors.add(new ReportItem(lKey, "zusatzwert", 673)); + return null; + } + wert.setPzsId(result.get(0)[0].toString()); + wert.setMesswertPzs(Float.valueOf(w)); + wert.setMessfehler(Float.valueOf(fehler)); + return wert; + } + + /** + * Add an attribute to the given LZusatzwert object. + * + * @param lKey The key mapping to a object member. + * @param value The value to set. + * @param wert The entity object. + * @return The updated entity object. + */ + public LZusatzWert addAttributeS( + String lKey, + Object value, + LZusatzWert wert + ) { + String v = value.toString().substring(1); + int ndx = v.indexOf("\""); + String groesse = v.substring(0, ndx); + v = v.substring(ndx + 2); + ndx = v.indexOf(" "); + String w = v.substring(0, ndx); + v = v.substring(ndx + 2); + ndx = v.indexOf(" "); + String einheit = v.substring(0, ndx); + String fehler = v.substring(ndx + 2); + wert.setPzsId(groesse); + wert.setMesswertPzs(Float.valueOf(w)); + wert.setMessfehler(Float.valueOf(fehler)); + return wert; + } + + /** + * @return the warnings + */ + public List getWarnings() { + return warnings; + } + + /** + * @return the errors + */ + public List getErrors() { + return errors; + } + + public void reset() { + errors = new ArrayList(); + warnings = new ArrayList(); + } +} diff -r 093bfdcdb09c -r 374a2e78cec5 src/main/java/de/intevation/lada/importer/laf/EntryFormat.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/de/intevation/lada/importer/laf/EntryFormat.java Thu Apr 16 15:49:04 2015 +0200 @@ -0,0 +1,66 @@ +package de.intevation.lada.importer.laf; + +import java.util.regex.Pattern; + +/** + * An EntryFormat describes the internal structure of LAF-based key-value pairs. + * The pattern is a regular expression used to match the value in the LAF + * importer. + * The entry formats are defined in a config file + * (see wiki-doc: https://bfs-intern.intevation.de/Server/Importer). + * @author Raimund Renkert + */ +public class EntryFormat +{ + private String key; + private Pattern pattern; + private Object defaultValue; + + /** + * Default constructor to create a new EntryFormat object. + */ + public EntryFormat() { + } + + /** + * @return the key. + */ + public String getKey() { + return key; + } + + /** + * @param key The key to set. + */ + public void setKey(String key) { + this.key = key; + } + + /** + * @return the pattern + */ + public Pattern getPattern() { + return pattern; + } + + /** + * @param pattern The pattern to set. + */ + public void setPattern(Pattern pattern) { + this.pattern = pattern; + } + + /** + * @return the default value. + */ + public Object getDefaultValue() { + return defaultValue; + } + + /** + * @param defaultValue the default value to set. + */ + public void setDefaultValue(Object defaultValue) { + this.defaultValue = defaultValue; + } +} diff -r 093bfdcdb09c -r 374a2e78cec5 src/main/java/de/intevation/lada/importer/laf/LafFormat.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/de/intevation/lada/importer/laf/LafFormat.java Thu Apr 16 15:49:04 2015 +0200 @@ -0,0 +1,87 @@ +package de.intevation.lada.importer.laf; + +import java.io.IOException; +import java.io.InputStream; +import java.io.StringReader; +import java.util.LinkedList; +import java.util.List; +import java.util.regex.Pattern; + +import javax.json.Json; +import javax.json.JsonArray; +import javax.json.JsonException; +import javax.json.JsonObject; +import javax.json.JsonReader; + +/** + * The LAFFormat reads the config file + * (https://bfs-intern.intevation.de/Server/Importer) and creates format + * objects for each entry. + * + * @author Raimund Renkert + */ +public class LafFormat +{ + private JsonObject fileContent; + + /** + * Reads the config file. + * + * @param fileName Path to the config file. + * @return success + */ + public boolean readConfigFile(String fileName) { + try { + InputStream inputStream = + LafFormat.class.getResourceAsStream(fileName); + int ch; + StringBuilder builder = new StringBuilder(); + while((ch = inputStream.read()) != -1) { + builder.append((char)ch); + } + JsonReader reader = + Json.createReader(new StringReader(builder.toString())); + fileContent = reader.readObject(); + return true; + } + catch (IOException ioe) { + return false; + } + catch (JsonException je) { + return false; + } + } + + /** + * Returns a List of EntryFormat for the requested entity type. + * The Entity type can be one of: + * * "probe" + * * "messung" + * * "ort" + * + * @param dataType The entity type + * @return List of entry formats defined for the requested type. + */ + public List getFormat(String dataType) { + List formats = new LinkedList(); + try { + JsonArray block = fileContent.getJsonArray(dataType); + for (int i = 0; i < block.size(); i++) { + JsonObject jEntry = block.getJsonObject(i); + EntryFormat entry = new EntryFormat(); + entry.setKey(jEntry.getString("key")); + Pattern pattern = + Pattern.compile( + jEntry.getString("regex"), + Pattern.MULTILINE); + entry.setPattern(pattern); + entry.setDefaultValue(jEntry.get("default")); + formats.add(entry); + } + return formats; + } + catch (JsonException e) { + return null; + } + } +} diff -r 093bfdcdb09c -r 374a2e78cec5 src/main/java/de/intevation/lada/importer/laf/LafImporter.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/de/intevation/lada/importer/laf/LafImporter.java Thu Apr 16 15:49:04 2015 +0200 @@ -0,0 +1,84 @@ +package de.intevation.lada.importer.laf; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import javax.ejb.Stateless; +import javax.inject.Inject; + +import org.apache.log4j.Logger; + +import de.intevation.lada.importer.ImportConfig; +import de.intevation.lada.importer.ImportFormat; +import de.intevation.lada.importer.Importer; +import de.intevation.lada.importer.ReportItem; +import de.intevation.lada.util.auth.UserInfo; + +@ImportConfig(format=ImportFormat.LAF) +@Stateless +public class LafImporter implements Importer { + + @Inject + private Logger logger; + + @Inject + private LafParser parser; + + private Map> warnings; + private Map> errors; + + /** + * Default constructor. + */ + public LafImporter() { + warnings = new HashMap>(); + errors = new HashMap>(); + } + + /** + * @return the warnings + */ + @Override + public Map> getWarnings() { + return warnings; + } + + /** + * @return the errors + */ + @Override + public Map> getErrors() { + return errors; + } + + /** + * Reset the errors and warnings. Use this before calling doImport() + * to have a clean error and warning report. + */ + @Override + public void reset() { + parser.reset(); + warnings = new HashMap>(); + errors = new HashMap>(); + } + + @Override + public void doImport(String content, UserInfo userInfo) { + this.warnings.clear(); + this.errors.clear(); + this.parser.reset(); + logger.debug("doing import"); + boolean success = parser.parse(userInfo, content); + logger.debug("import success: " + success); + if (!success) { + List report = new ArrayList(); + report.add(new ReportItem("parser", "no success", 660)); + errors.put("parser", report); + warnings.put("parser", new ArrayList()); + } + this.warnings.putAll(this.parser.getWarnings()); + this.errors.putAll(this.parser.getErrors()); + } +} diff -r 093bfdcdb09c -r 374a2e78cec5 src/main/java/de/intevation/lada/importer/laf/LafParser.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/de/intevation/lada/importer/laf/LafParser.java Thu Apr 16 15:49:04 2015 +0200 @@ -0,0 +1,368 @@ +package de.intevation.lada.importer.laf; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import javax.inject.Inject; + +import org.apache.log4j.Logger; + +import de.intevation.lada.importer.ReportItem; +import de.intevation.lada.model.land.LMessung; +import de.intevation.lada.model.land.LMesswert; +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.util.auth.UserInfo; +import de.intevation.lada.validation.Validator; +import de.intevation.lada.validation.annotation.ValidationConfig; + +/** + * This parser is used to read data in LAF based key-value pair structure. + * + * @author Raimund Renkert + */ +public class LafParser { + + @Inject + private Logger logger; + + private static final String PROBE_NEXT = "\n%PROBE%"; + + private boolean dryRun; + + @Inject + private LafProducer producer; + + @Inject + private LafWriter writer; + + @Inject + @ValidationConfig(type="Probe") + private Validator probeValidator; + + @Inject + @ValidationConfig(type="Messung") + private Validator messungValidator; + + //@Inject + //@ValidationConfig(type="Messwert") + //private Validator messwertValidator; + + //@Inject + //@ValidationConfig(type="Ort") + //private Validator ortValidator; + + private Map> warnings; + private Map> errors; + + /** + * Default constructor. + */ + public LafParser() { + this.warnings = new HashMap>(); + this.errors = new HashMap>(); + this.setDryRun(false); + } + + /** + * Read and parse the data and write the objects to the database. + * + * @param auth Authentication information + * @param laf The LAF formated data. + * @return success + * @throws LafParserException + */ + public boolean parse(UserInfo userInfo, String laf) { + if (!laf.startsWith("%PROBE%\n")) { + logger.debug("no %PROBE% tag found!"); + return false; + } + boolean parsed = false; + while (laf.startsWith("%PROBE%\n")) { + parsed = true; + int nextPos = laf.indexOf(PROBE_NEXT); + String single = ""; + if (nextPos > 0) { + single = laf.substring(0, nextPos + 1); + laf = laf.substring(nextPos + 1); + try { + logger.debug("parsing probe"); + readAll(single); + this.warnings.putAll(producer.getWarnings()); + this.errors.putAll(producer.getErrors()); + logger.debug("writing to database"); + writeAll(userInfo); + this.producer.reset(); + this.writer.reset(); + } + catch (LafParserException lpe) { + Map> pErr = producer.getErrors(); + if (pErr.isEmpty()) { + List err = new ArrayList(); + err.add(new ReportItem("parser", lpe.getMessage(), 673)); + this.errors.put("parser", err); + this.warnings.put("parser", new ArrayList()); + } + else { + this.errors.putAll(pErr); + this.warnings.putAll(producer.getWarnings()); + } + this.producer.reset(); + continue; + } + } + else { + try { + logger.debug("parsing single probe"); + readAll(laf); + this.warnings.putAll(producer.getWarnings()); + this.errors.putAll(producer.getErrors()); + logger.debug("writing single to database"); + writeAll(userInfo); + this.producer.reset(); + this.writer.reset(); + laf = ""; + } + catch (LafParserException lpe) { + Map> pErr = producer.getErrors(); + if (pErr.isEmpty()) { + List err = new ArrayList(); + err.add(new ReportItem("parser", lpe.getMessage(), 673)); + this.errors.put("parser", err); + this.warnings.put("parser", new ArrayList()); + } + else { + this.errors.putAll(pErr); + this.warnings.putAll(producer.getWarnings()); + } + this.producer.reset(); + laf = ""; + continue; + } + } + } + return parsed; + } + + /** + * Write all created objects to the database. + * + * @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); + 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()); + } + 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()); + } + + private void validateProbe(LProbe probe) { + } + + private void validateMessungen(Map messungen) { + } + + private void validateMesswerte(Map> werte) { + } + + private void validateLOrte(List orte) { + } + + /** + * Read all attributes from a single probe block and create entity objects. + * + * @param content Single probe block enclosed by %PROBE% + * @throws LafParserException + */ + private void readAll(String content) + throws LafParserException + { + boolean key = false; + boolean value = false; + boolean header = false; + boolean white = false; + boolean string = false; + boolean multiValue = false; + String keyString = ""; + String valueString = ""; + String headerString = ""; + for (int i = 0; i < content.length(); i++) { + char current = content.charAt(i); + + if ((current == '"' || (current == ' ' && !string)) && + value && + i < content.length() - 1 && + (content.charAt(i + 1) != '\n' && + content.charAt(i + 1) != '\r')) { + multiValue = true; + } + + if (current == '"' && !string) { + string = true; + } + else if (current == '"' && string) { + string = false; + } + + if (current == ' ' && !value) { + key = false; + white = true; + continue; + } + else if (current != ' ' && + current != '\n' && + current != '\r' && + white) { + value = true; + white = false; + } + else if (current == '%' && !header && !value) { + headerString = ""; + producer.finishOrt(); + key = false; + header = true; + } + else if ((current == '\n' || current == '\r') && header) { + header = false; + key = true; + if (!dryRun) { + if (headerString.contains("MESSUNG")) { + producer.newMessung(); + } + if (headerString.contains("ORT")) { + producer.newOrt(); + } + } + if (headerString.contains("%ENDE%")) { + if (!dryRun) { + this.producer.newMessung(); + this.producer.newOrt(); + } + return; + } + continue; + } + else if (current == '"' && !value) { + value = true; + } + else if ((current == '\n' || current == '\r') && value && !string) { + if (!multiValue && valueString.startsWith("\"")) { + valueString = + valueString.substring(1, valueString.length() - 1); + } + value = false; + multiValue = false; + key = true; + if (!this.dryRun) { + producer.addData(keyString, valueString); + } + keyString = ""; + valueString = ""; + continue; + } + if ((current == '\n' || current == '\r') && (key || white)) { + //TODO error!!! + return; + } + + if (key) { + keyString += current; + } + else if (value) { + valueString += current; + } + else if (header) { + headerString += current; + } + } + if (!dryRun) { + this.producer.newMessung(); + this.producer.newOrt(); + } + } + + /** + * @return if objects are or not. + */ + public boolean isDryRun() { + return dryRun; + } + + /** + * If set to true, no objects will be created and written to database. + * + * @param dryRun + */ + public void setDryRun(boolean dryRun) { + this.dryRun = dryRun; + } + + /** + * @return the warnings + */ + public Map> getWarnings() { + return warnings; + } + + /** + * @return the errors + */ + public Map> getErrors() { + return errors; + } + + /** + * Reset errors and warnings. + */ + public void reset() { + producer.reset(); + this.errors = new HashMap>(); + this.warnings = new HashMap>(); + } + + private void appendErrors(String probeId, List errs) { + List 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 warns) { + List warn = this.warnings.get(probeId); + if (warn == null) { + this.warnings.put(probeId, warns); + } + else { + warn.addAll(warns); + this.warnings.put(probeId, warn); + } + } +} diff -r 093bfdcdb09c -r 374a2e78cec5 src/main/java/de/intevation/lada/importer/laf/LafParserException.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/de/intevation/lada/importer/laf/LafParserException.java Thu Apr 16 15:49:04 2015 +0200 @@ -0,0 +1,19 @@ +package de.intevation.lada.importer.laf; + +/** + * Exception thrown in the LAF import process. + * + * @author Raimund Renkert + */ +public class LafParserException +extends Exception +{ + /** + * + */ + private static final long serialVersionUID = 1L; + + public LafParserException(String message) { + super(message); + } +} diff -r 093bfdcdb09c -r 374a2e78cec5 src/main/java/de/intevation/lada/importer/laf/LafProducer.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/de/intevation/lada/importer/laf/LafProducer.java Thu Apr 16 15:49:04 2015 +0200 @@ -0,0 +1,405 @@ +package de.intevation.lada.importer.laf; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import javax.inject.Inject; + +import org.apache.log4j.Logger; + +import de.intevation.lada.importer.ReportItem; +import de.intevation.lada.model.Ort; +import de.intevation.lada.model.land.LKommentarM; +import de.intevation.lada.model.land.LKommentarP; +import de.intevation.lada.model.land.LMessung; +import de.intevation.lada.model.land.LMesswert; +import de.intevation.lada.model.land.LOrt; +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.stamm.SOrt; + +/** + * The LAFProducer creates entity objects form key-value pairs using the + * AttributeMapper. + * + * @author Raimund Renkert + */ +public class LafProducer +{ + @Inject + private Logger logger; + + private LProbe probe; + private LMessung messung; + private MessungTranslation messungTranslation; + + @Inject + private OrtCreator ort; + + private List pKommentare; + private Map> mKommentare; + private Map messungen; + private List lorte; + private List orte; + private Map> messwerte; + private List zusatzwerte; + + private List probenFormat; + private List messungFormat; + private List ortFormat; + + private Map> warnings; + private Map> errors; + + @Inject + private AttributeMapper mapper; + + /** + * Default contructor. Initializes the producer and reads the config file + * using the systemproperty "de.intevation.lada.importconfig". + */ + public LafProducer() { + this.warnings = new HashMap>(); + this.errors = new HashMap>(); + this.probe = new LProbe(); + this.pKommentare = new ArrayList(); + this.mKommentare = new HashMap>(); + this.messungen = new HashMap(); + this.lorte = new ArrayList(); + this.messwerte = new HashMap>(); + String fileName = "/import.json"; + LafFormat format = new LafFormat(); + format.readConfigFile(fileName); + probenFormat = format.getFormat("probe"); + messungFormat = format.getFormat("messung"); + ortFormat = format.getFormat("ort"); + } + + /** + * Add data to the producer. + * This triggers the producer to create a new object or add data to + * existing objects. + * + * @param key The key. + * @param values The value + * @throws LafParserException + */ + public void addData(String key, Object values) + throws LafParserException { + logger.debug("adding " + key + ": " + values); + String lKey = key.toLowerCase(); + if(lKey.equals("probenkommentar")) { + LKommentarP kommentar = new LKommentarP(); + kommentar.setProbeId(this.probe.getId()); + this.pKommentare.add( + mapper.addAttribute(lKey, values, kommentar)); + } + else if (lKey.equals("kommentar")) { + LKommentarM kommentar = new LKommentarM(); + kommentar.setMessungsId(this.messung.getId()); + if (this.mKommentare.get(this.messung) == null) { + this.mKommentare.put(this.messung, new ArrayList()); + } + this.mKommentare.get(this.messung).add( + mapper.addAttribute(lKey, values, kommentar)); + } + else if (lKey.equals("probenzusatzbeschreibung")) { + LZusatzWert wert = new LZusatzWert(); + LZusatzWert zusatzWert = mapper.addAttribute(lKey, values, wert); + if (zusatzWert != null) { + zusatzWert.setProbeId(probe.getId()); + this.zusatzwerte.add(zusatzWert); + } + else { + String ekey = probe.getId() == null ? + "probeId" : probe.getId().toString(); + List err = this.errors.get(ekey); + if (err == null) { + this.errors.put(ekey, mapper.getErrors()); + } + else { + err.addAll(mapper.getErrors()); + } + } + } + else if (lKey.equals("pzb_s")) { + LZusatzWert wert = new LZusatzWert(); + LZusatzWert zusatzWert = mapper.addAttributeS(lKey, values, wert); + if (zusatzWert != null) { + zusatzWert.setProbeId(probe.getId()); + this.zusatzwerte.add(zusatzWert); + } + else { + String ekey = probe.getId() == null ? + "probeId" : probe.getId().toString(); + List err = this.errors.get(ekey); + if (err == null) { + this.errors.put(ekey, mapper.getErrors()); + } + else { + err.addAll(mapper.getErrors()); + } + } + } + else if (lKey.equals("messwert")) { + LMesswert m = new LMesswert(); + m.setMessungsId(this.messung.getId()); + LMesswert wert = mapper.addAttribute(lKey, values, m); + if (wert != null) { + if (this.messwerte.get(this.messung) == null) { + this.messwerte.put(this.messung, new ArrayList()); + } + this.messwerte.get(this.messung).add( + mapper.addAttribute(lKey, values, wert)); + } + else { + String ekey = probe.getId() == null ? + "probeId" : probe.getId().toString(); + List err = this.errors.get(ekey); + if (err == null) { + this.errors.put(ekey, mapper.getErrors()); + } + else { + err.addAll(mapper.getErrors()); + } + } + } + else if (isValidMessung(lKey, values.toString())) { + this.messung = mapper.addAttribute(lKey, values, this.messung); + logger.debug("####### " + lKey + ": " + values); + this.messungTranslation = + mapper.addAttribute(lKey, values, this.messungTranslation); + } + else if (isValidProbe(lKey, values.toString())) { + this.probe = mapper.addAttribute(lKey, values, this.probe); + if (this.probe == null) { + this.errors.put(values.toString(), mapper.getErrors()); + throw new LafParserException(values.toString() + " exists"); + } + } + else if (isValidOrt(lKey, values.toString())) { + this.ort.addAttribute(lKey, values); + } + } + + /** + * Check if the key is defined in the config file and validate the value + * using the pattern defined for this key. + * + * @param key The key. + * @param value The value. + * @return valid or not. + */ + private boolean isValidOrt(String key, String value) { + for (EntryFormat ef: ortFormat) { + if (ef.getKey().equals(key.toLowerCase())) { + if (ef.getPattern().matcher(value).matches()) { + return true; + } + } + } + return false; + } + + /** + * Check if the key is defined in the config file and validate the value + * using the pattern defined for this key. + * + * @param key The key. + * @param value The value. + * @return valid or not. + */ + private boolean isValidMessung(String key, String value) { + for (EntryFormat ef: messungFormat) { + if (ef.getKey().equals(key.toLowerCase())) { + if (ef.getPattern().matcher(value).matches()) { + return true; + } + } + } + return false; + } + + /** + * Check if the key is defined in the config file and validate the value + * using the pattern defined for this key. + * + * @param key The key. + * @param value The value. + * @return valid or not. + */ + private boolean isValidProbe(String key, String value) { + for (EntryFormat ef: probenFormat) { + if (ef.getKey().equals(key.toLowerCase())) { + if (ef.getPattern().matcher(value).matches()) { + return true; + } + } + } + return false; + } + + /** + * @return the {@link LProbe} entity. + */ + public LProbe getProbe() { + return this.probe; + } + + /** + * @return List of {@link LMessung} entities. + */ + public Map getMessungen() { + return this.messungen; + } + + /** + * @return List of {@link Ort} entities. + */ + public List getOrte() { + return this.orte; + } + + /** + * @return List of {@link LOrt} entities. + */ + public List getLOrte() { + return this.lorte; + } + + /** + * @return List of {@link LKommentarP} entities. + */ + public List getProbenKommentare() { + return this.pKommentare; + } + + /** + * @return List of {@link LKommentarM} entities. + */ + public Map> getMessungsKommentare() { + return this.mKommentare; + } + + /** + * @return List of {@link LMesswert} entities. + */ + public Map> getMesswerte() { + return this.messwerte; + } + + /** + * @return List of {@link LZusatzWert} entities. + */ + public List getZusatzwerte() { + return this.zusatzwerte; + } + + /** + * Reset errors and warnings. + */ + public void reset() { + this.errors = new HashMap>(); + this.warnings = new HashMap>(); + this.probe = new LProbe(); + this.messungen = new HashMap(); + this.messung = null; + this.lorte = new ArrayList(); + this.orte = new ArrayList(); + this.ort.reset(); + this.messwerte = new HashMap>(); + this.mKommentare = new HashMap>(); + this.pKommentare = new ArrayList(); + mapper.reset(); + } + + /** + * Add the current {@link LMessung} entity to the List and create a new one. + */ + public void newMessung() { + if (this.messung != null && this.messungTranslation != null) { + if (this.messung.getFertig() == null) { + this.messung.setFertig(false); + } + if (this.messung.getGeplant() == null) { + this.messung.setGeplant(false); + } + this.messungen.put(this.messung, this.messungTranslation); + } + this.messung = new LMessung(); + this.messungTranslation = new MessungTranslation(); + this.messung.setProbeId(this.probe.getId()); + } + + /** + * Add the {@link Ort} and {@link LOrt} entities to the lists and create + * a new {@link OrtCreator}. + */ + public void newOrt() { + if (this.ort != null) { + SOrt o = this.ort.toOrt(); + if (o != null) { + this.orte.add(o); + } + logger.debug("ort: " + this.ort.getProbeId()); + LOrt lo = this.ort.toLOrt(); + logger.debug("lo is " + lo != null); + if (lo != null) { + this.lorte.add(lo); + } + } + OrtCreator creator = this.ort; + creator.reset(); + logger.debug(this.probe.getId()); + logger.debug(creator.hashCode()); + creator.setProbeId(this.probe.getId()); + logger.debug("ort: " + creator.getProbeId()); + logger.debug(creator.hashCode()); + } + + /** + * @return the warnings + */ + public Map> getWarnings() { + if (this.probe == null) { + return this.warnings; + } + String key = probe.getId() == null ? "probeId" : probe.getId().toString(); + List warn = this.warnings.get(key); + if (warn == null) { + this.warnings.put(key, mapper.getWarnings()); + } + else { + warn.addAll(mapper.getWarnings()); + } + return this.warnings; + } + + /** + * @return the errors + */ + public Map> getErrors() { + if (this.probe == null) { + return this.errors; + } + String key = probe.getId() == null ? + "probeId" : probe.getId().toString(); + List err = this.errors.get(key); + if (err == null) { + this.errors.put(key, mapper.getErrors()); + } + else { + err.addAll(mapper.getErrors()); + } + return this.errors; + } + + public void finishOrt() { + if (orte.isEmpty()) { + return; + } + } +} diff -r 093bfdcdb09c -r 374a2e78cec5 src/main/java/de/intevation/lada/importer/laf/LafWriter.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/de/intevation/lada/importer/laf/LafWriter.java Thu Apr 16 15:49:04 2015 +0200 @@ -0,0 +1,210 @@ +package de.intevation.lada.importer.laf; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +import javax.ejb.Stateless; +import javax.inject.Inject; +import javax.persistence.PersistenceException; + +import de.intevation.lada.importer.ReportItem; +import de.intevation.lada.model.land.LKommentarM; +import de.intevation.lada.model.land.LKommentarP; +import de.intevation.lada.model.land.LMessung; +import de.intevation.lada.model.land.LMesswert; +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.stamm.SOrt; +import de.intevation.lada.util.annotation.AuthorizationConfig; +import de.intevation.lada.util.annotation.RepositoryConfig; +import de.intevation.lada.util.auth.Authorization; +import de.intevation.lada.util.auth.AuthorizationType; +import de.intevation.lada.util.auth.UserInfo; +import de.intevation.lada.util.data.Repository; +import de.intevation.lada.util.data.RepositoryType; + +/** + * Writer to persist new entities in the database. + * + * @author Raimund Renkert + */ +@Stateless +public class LafWriter { + + @Inject + @RepositoryConfig(type=RepositoryType.RW) + private Repository repository; + + @Inject + @AuthorizationConfig(type=AuthorizationType.OPEN_ID) + private Authorization authorization; + + private List errors; + private List warnings; + + /** + * Default constructor. + */ + public LafWriter() { + errors = new ArrayList(); + warnings = new ArrayList(); + } + + /** + * Write a new {@link LProbe} object to the database using + * authorization and validation. + * + * @param auth The authentication information. + * @param probe The new {@link LProbe} object. + * @return success + */ + public boolean writeProbe(UserInfo userInfo, LProbe probe) { + if (!authorization.isAuthorized(userInfo, probe)) { + errors.add(new ReportItem("auth", "not authorized", 699)); + return false; + } + if (probe.getId() == null) { + errors.add(new ReportItem("probeId", "missing", 673)); + return false; + } + try { + repository.update(probe, "land"); + } + catch (PersistenceException e) { + errors.add(new ReportItem("probe", "writing", 670)); + return false; + } + return true; + } + + /** + * Write new {@link LMessung} objects to the database using + * authorization and validation. + * + * @param auth The authentication information. + * @param messungen The new {@link LMessung} objects. + * @return success + */ + public boolean writeMessungen( + UserInfo userInfo, + Map messungen + ) { + for(LMessung messung: messungen.keySet()) { + repository.create(messung, "land"); + MessungTranslation mt = messungen.get(messung); + mt.setMessungsId(messung); + repository.create(mt, "land"); + } + return true; + } + + /** + * Write new {@link SOrt} objects to the database. + * + * @param auth The authentication information. + * @param orte List of {@link SOrt} objects. + * @return success + */ + public boolean writeOrte(UserInfo userInfo, List orte) { + for (SOrt ort :orte) { + //TODO create the SOrt!!! + //repository.create(ort, "land"); + } + return true; + } + + /** + * Write new {@link LOrt} objects to the database using validation. + * + * @param auth The authentication information. + * @param orte List of {@link LOrt} objects. + * @return success + */ + public boolean writeLOrte(UserInfo userInfo, List orte) { + for(LOrt ort: orte) { + repository.create(ort, "land"); + } + return true; + } + + /** + * Write new {@link LKommentarP} objects to the database. + * + * @param auth The authentication information. + * @param kommentare List of {@link LKommentarP} objects. + * @return success + */ + public boolean writeProbenKommentare( + UserInfo userInfo, + List kommentare + ) { + for(LKommentarP kommentar: kommentare) { + repository.create(kommentar, "land"); + } + return true; + } + + /** + * Write new {@link LKommentarM} objects to the database. + * + * @param auth The authentication information. + * @param kommentare List of {@link LKommentarM} objects. + * @return success + */ + public boolean writeMessungKommentare( + UserInfo userInfo, + Map> kommentare + ) { + for (LMessung messung : kommentare.keySet()) { + for (LKommentarM kommentar: kommentare.get(messung)) { + kommentar.setMessungsId(messung.getId()); + repository.create(kommentar, "land"); + } + } + return true; + } + + /** + * Write new {@link LMesswert} objects to the database using validation. + * + * @param auth The authentication information. + * @param werte List of {@link LMesswert} objects. + * @return success + */ + public boolean writeMesswerte( + UserInfo userInfo, + Map> werte + ) { + for (LMessung messung : werte.keySet()) { + for(LMesswert messwert: werte.get(messung)) { + messwert.setMessungsId(messung.getId()); + repository.create(messwert, "land"); + } + } + return true; + } + + /** + * @return the errors + */ + public List getErrors() { + return errors; + } + + /** + * @return the warnings + */ + public List getWarnings() { + return warnings; + } + + /** + * Reset the errors and warnings. + */ + public void reset() { + this.warnings = new ArrayList(); + this.errors = new ArrayList(); + } +} diff -r 093bfdcdb09c -r 374a2e78cec5 src/main/java/de/intevation/lada/importer/laf/OrtCreator.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/de/intevation/lada/importer/laf/OrtCreator.java Thu Apr 16 15:49:04 2015 +0200 @@ -0,0 +1,716 @@ +package de.intevation.lada.importer.laf; + +import java.math.BigInteger; +import java.util.List; + +import javax.ejb.Stateless; +import javax.inject.Inject; +import javax.persistence.Query; + +import org.apache.log4j.Logger; + +import de.intevation.lada.model.land.LOrt; +import de.intevation.lada.model.stamm.SOrt; +import de.intevation.lada.util.annotation.RepositoryConfig; +import de.intevation.lada.util.data.Repository; +import de.intevation.lada.util.data.RepositoryType; + +/** + * Class to produce Ort/LOrt objects from a given set of attributes. + * + * @author Raimund Renkert + */ +public class OrtCreator +{ + @Inject + private Logger logger; + + @Inject + @RepositoryConfig(type=RepositoryType.RW) + private Repository repository; + + private Integer probeId; + private Integer ortId; + private String ortCode; + private String ortTyp; + private String zusatztext; + private String landLang; + private String landKurz; + private String landS; + private String gemSchluessel; + private String gemName; + private String koordArt; + private String koord; + private String koordS; + private String bezeichnung; + private String beschreibung; + private String nuts; + private String hoehe; + private String koordinaten; + private String koordinatenS; + + public OrtCreator() { + this.ortId = null; + } + + /** + * @return the probeId + */ + public Integer getProbeId() { + return this.probeId; + } + + /** + * @param probeId the probeId to set + */ + public void setProbeId(Integer probeId) { + this.probeId = probeId; + } + + /** + * @return the ortCode + */ + public String getOrtCode() { + return ortCode; + } + + /** + * @param ortCode the ortCode to set + */ + public void setOrtCode(String ortCode) { + logger.debug("ort code is " + ortCode); + this.ortCode = ortCode; + } + + /** + * @return the ortTyp + */ + public String getOrtTyp() { + return ortTyp; + } + + /** + * @param ortTyp the ortTyp to set + */ + public void setOrtTyp(String ortTyp) { + this.ortTyp = ortTyp; + } + + /** + * @return the zusatztext + */ + public String getZusatztext() { + return zusatztext; + } + + /** + * @param zusatztext the zusatztext to set + */ + public void setZusatztext(String zusatztext) { + this.zusatztext = zusatztext; + } + + /** + * @return the landLang + */ + public String getLandLang() { + return landLang; + } + + /** + * @param landLang the landLang to set + */ + public void setLandLang(String landLang) { + this.landLang = landLang; + } + + /** + * @return the landKurz + */ + public String getLandKurz() { + return landKurz; + } + + /** + * @param landKurz the landKurz to set + */ + public void setLandKurz(String landKurz) { + this.landKurz = landKurz; + } + + /** + * @return the landS + */ + public String getLandS() { + return landS; + } + + /** + * @param landS the landS to set + */ + public void setLandS(String landS) { + this.landS = landS; + } + + /** + * @return the gemSchluessel + */ + public String getGemSchluessel() { + return gemSchluessel; + } + + /** + * @param gemSchluessel the gemSchluessel to set + */ + public void setGemSchluessel(String gemSchluessel) { + this.gemSchluessel = gemSchluessel; + } + + /** + * @return the koordArt + */ + public String getKoordArt() { + return koordArt; + } + + /** + * @param koordArt the koordArt to set + */ + public void setKoordArt(String koordArt) { + this.koordArt = koordArt; + } + + /** + * @return the koord + */ + public String getKoord() { + return koord; + } + + /** + * @param koord the koord to set + */ + public void setKoord(String koord) { + this.koord = koord; + } + + /** + * @return the koordS + */ + public String getKoordS() { + return koordS; + } + + /** + * @param koordS the koordS to set + */ + public void setKoordS(String koordS) { + this.koordS = koordS; + } + + /** + * @return the nuts + */ + public String getNuts() { + return nuts; + } + + /** + * @param nuts the nuts to set + */ + public void setNuts(String nuts) { + this.nuts = nuts; + } + + /** + * @return the hoehe + */ + public String getHoehe() { + return hoehe; + } + + /** + * @param hoehe the hoehe to set + */ + public void setHoehe(String hoehe) { + this.hoehe = hoehe; + } + + /** + * @return the bezeichung + */ + public String getBezeichnung() { + return bezeichnung; + } + + /** + * @param bezeichnung the bezeichnung to set + */ + public void setBezeichnung(String bezeichnung) { + this.bezeichnung = bezeichnung; + } + + /** + * @return the gemName + */ + public String getGemName() { + return gemName; + } + + /** + * @param gemName the gemName to set + */ + public void setGemName(String gemName) { + this.gemName = gemName; + } + + /** + * @return the beschreibung + */ + public String getBeschreibung() { + return beschreibung; + } + + /** + * @param beschreibung the beschreibung to set + */ + public void setBeschreibung(String beschreibung) { + this.beschreibung = beschreibung; + } + + /** + * @return the koordinaten + */ + public String getKoordinaten() { + return koordinaten; + } + + /** + * @param koordinaten the koordinaten to set + */ + public void setKoordinaten(String koordinaten) { + this.koordinaten = koordinaten; + } + + /** + * @return the koordinatenS + */ + public String getKoordinatenS() { + return koordinatenS; + } + + /** + * @param koordinatenS the koordinatenS to set + */ + public void setKoordinatenS(String koordinatenS) { + this.koordinatenS = koordinatenS; + } + + /** + * Add an attribute to the OrtCreator. The creator is used to build the + * two objects Ort and LOrt. + * + * @param key The key mapping to a object member. + * @param value The value to set. + * @param ort The creator object. + * @return The updated creator object. + */ + public void addAttribute( + String key, + Object value + ) { + logger.debug("# adding " + key + ": " + value); + if ("ort_code".equals(key)) { + this.setOrtCode(value.toString()); + } + if ("ort_typ".equals(key)) { + this.setOrtTyp(value.toString()); + } + if ("ort_zusatz".equals(key)) { + this.setZusatztext(value.toString()); + } + if ("ort_land_lang".equals(key)) { + this.setLandLang(value.toString()); + } + if ("ort_land_kurz".equals(key)) { + this.setLandKurz(value.toString()); + } + if ("ort_land_s".equals(key)) { + this.setLandS(value.toString()); + } + if ("ort_gemeindeschluessel".equals(key)) { + this.setGemSchluessel(value.toString()); + } + if ("ort_bezeichnung".equals(key)) { + this.setBezeichnung(value.toString()); + } + if ("ort_beschreibung".equals(key)) { + this.setBeschreibung(value.toString()); + } + if ("ort_nuts_code".equals(key)) { + this.setNuts(value.toString()); + } + if ("ort_hoehe_land".equals(key)) { + this.setHoehe(value.toString()); + } + if ("ort_koordinaten".equals(key)) { + this.setKoordinaten(value.toString()); + } + if ("ort_koordinaten_s".equals(key)) { + this.setKoordinatenS(value.toString()); + } + } + + + /** + * Create the Ort object. + * + * @return The new Ort. + */ + public SOrt toOrt() { + if (this.ortCode != null && this.ortCode.length() > 0) { + return null; + } + SOrt ort = new SOrt(); + // TODO USE NATIVE QUERY... + //repository.create(ort, "land"); + this.ortId = ort.getId(); + boolean koord = true; + if (this.koordinatenS != null && this.koordinatenS.length() > 0) { + ort = setKoordinatenS(ort); + koord = false; + if (this.koordinaten != null && this.koordinaten.length() > 0) { + //TODO: add warning. + } + } + else if (this.koordinaten != null && this.koordinaten.length() > 0) { + ort = setKoordinaten(ort); + koord = false; + } + if (this.gemSchluessel != null && + this.gemSchluessel.length() > 0){ + ort = setGemeindeS(ort, koord); + koord = false; + if(this.gemName != null && this.gemName.length() > 0) { + //TODO: add warning. + } + } + else if (this.gemName != null && this.gemName.length() > 0) { + ort = setGemeinde(ort, koord); + koord = false; + } + if(this.landS != null && this.landS.length() > 0) { + ort = setLandS(ort, koord); + koord = false; + if (this.landLang != null && this.landLang.length() > 0) { + //TODO: add warning. + } + if (this.landKurz != null && this.landKurz.length() > 0) { + //TODO: add warning. + } + } + else if (this.landKurz != null && this.landKurz.length() > 0) { + ort = setLandKurz(ort, koord); + koord = false; + if (this.landLang != null && this.landLang.length() > 0) { + //TODO: add warning. + } + } + else if (this.landLang != null && this.landLang.length() > 0) { + ort = setLandLang(ort, koord); + } + if (koord) { + //TODO: add warning. + return null; + } + if (this.nuts != null && this.nuts.length() > 0) { + ort.setNutsCode(nuts); + } + else if (ort.getVerwaltungseinheitId() != null && + ort.getVerwaltungseinheitId().length() > 0) + { + String nativeQuery = "select nuts from stammdaten.verwaltungseinheit where gem_id = '"; + nativeQuery += ort.getVerwaltungseinheitId() + "'"; + Query query = repository.entityManager("land").createNativeQuery(nativeQuery); + List result = query.getResultList(); + + ort.setNutsCode(result.get(0)[0].toString()); + } + ort.setBeschreibung(beschreibung); + if (this.hoehe != null) { + ort.setHoeheLand(Float.valueOf(hoehe)); + } + //TODO USE NATIVE QUERY... + //repository.update(ort, "land"); + return ort; + } + + /** + * Find the SStaat object identified by the land_lang string and set a + * reference to the ort object. + * + * @param ort The ort object. + * @param koord Set the coordinates or not. + * @return The Ort object. + */ + private SOrt setLandLang(SOrt ort, boolean koord) { + String nativeQuery = "select id, koord_x_extern, koord_y_extern from stammdaten.staat where staat = '"; + nativeQuery += this.landLang + "'"; + Query query = repository.entityManager("land").createNativeQuery(nativeQuery); + List result = query.getResultList(); + + if (result.isEmpty()) { + //TODO: add warning. + return ort; + } + ort.setStaatId(Integer.valueOf(result.get(0)[0].toString())); + if (koord) { + ort.setKoordXExtern(result.get(0)[1].toString()); + ort.setKoordYExtern(result.get(0)[2].toString()); + } + return ort; + } + + /** + * Find the SStaat object identified by the land_kurz string and set a + * reference to the ort object. + * + * @param ort The ort object. + * @param koord Set the coordinates or not. + * @return The Ort object. + */ + private SOrt setLandKurz(SOrt ort, boolean koord) { + String nativeQuery = "select id, koord_x_extern, koord_y_extern from stammdaten.staat where staat_kurz = '"; + nativeQuery += this.landKurz + "'"; + Query query = repository.entityManager("land").createNativeQuery(nativeQuery); + List result = query.getResultList(); + + if (result.isEmpty()) { + //TODO add warning. + return ort; + } + ort.setStaatId((Integer)result.get(0)[0]); + if (koord) { + ort.setKoordXExtern(result.get(0)[1].toString()); + ort.setKoordYExtern(result.get(0)[2].toString()); + } + return ort; + } + + /** + * Find the SStaat object identified by the land_s id and set a + * reference to the ort object. + * + * @param ort The ort object. + * @param koord Set the coordinates or not. + * @return The Ort object. + */ + private SOrt setLandS(SOrt ort, boolean koord) { + ort.setStaatId(Integer.valueOf(this.landS)); + if (koord) { + String nativeQuery = "select koord_x_extern, koord_y_extern from stammdaten.staat where id = '"; + nativeQuery += this.landS + "'"; + Query query = repository.entityManager("land").createNativeQuery(nativeQuery); + List result = query.getResultList(); + if (result.isEmpty()) { + //TODO: add warning. + return ort; + } + ort.setKoordXExtern(result.get(0)[0].toString()); + ort.setLongitude(Double.valueOf(result.get(0)[0].toString())); + ort.setKoordYExtern(result.get(0)[1].toString()); + ort.setLatitude(Double.valueOf(result.get(0)[1].toString())); + } + return ort; + } + + /** + * Find the SVerwaltungseinheit object identified by the gem_name string + * and set a reference to the ort object. + * + * @param ort The ort object. + * @param koord Set the coordinates or not. + * @return The Ort object. + */ + private SOrt setGemeinde(SOrt ort, boolean koord) { + String nativeQuery = "select id, koord_x_extern, koord_y_extern from stammdaten.verwaltungseinheit where bezeichnung = '"; + nativeQuery += this.gemName + "'"; + Query query = repository.entityManager("land").createNativeQuery(nativeQuery); + List result = query.getResultList(); + if (result.isEmpty()) { + //TODO: add warning. + return ort; + } + ort.setVerwaltungseinheitId(result.get(0)[0].toString()); + if (koord) { + ort.setKoordXExtern(result.get(0)[1].toString()); + ort.setKoordYExtern(result.get(0)[2].toString()); + } + return ort; + } + + /** + * Find the SVerwaltungseinheit object identified by the gem_schluessel id + * and set a reference to the ort object. + * + * @param ort The ort object. + * @param koord Set the coordinates or not. + * @return The Ort object. + */ + private SOrt setGemeindeS(SOrt ort, boolean koord) { + ort.setVerwaltungseinheitId(this.gemSchluessel); + if (koord) { + String nativeQuery = "select koord_x_extern, koord_y_extern from stammdaten.verwaltungseinheit where bezeichnung = '"; + nativeQuery += this.gemName + "'"; + Query query = repository.entityManager("land").createNativeQuery(nativeQuery); + List result = query.getResultList(); + if (result.isEmpty()) { + // TODO: add warning. + return ort; + } + ort.setKoordXExtern(result.get(0)[0].toString()); + ort.setKoordYExtern(result.get(0)[1].toString()); + } + return ort; + } + + /** + * Parse the coordinates and the the attributes to the new ort object. + * + * @param ort The ort object. + * @return The Ort object. + */ + private SOrt setKoordinaten(SOrt ort) { + String art = ""; + String x = ""; + String y = ""; + String tmp = ""; + if (this.koordinaten.startsWith("\"")) { + tmp = this.koordinaten.substring(1); + art = tmp.substring(0, tmp.indexOf("\"")); + tmp = tmp.substring(tmp.indexOf("\"") + 2); + } + else { + art = this.koordinaten.substring(0, this.koordinaten.indexOf(" ")); + tmp = this.koordinaten.substring( + 0, this.koordinaten.indexOf(" ") + 1); + } + if (tmp.startsWith("\"")) { + tmp = tmp.substring(1); + x = tmp.substring(0, tmp.indexOf("\"")); + tmp = tmp.substring(0, tmp.indexOf("\"") + 2); + } + else { + x = tmp.substring(0, tmp.indexOf(" ")); + tmp = tmp.substring(0, tmp.indexOf(" ") + 1); + } + if (tmp.startsWith("\"")) { + tmp = tmp.substring(1); + y = tmp.substring(0, tmp.indexOf("\"")); + } + else { + y = tmp; + } + ort.setKoordXExtern(x); + ort.setKoordYExtern(y); + return ort; + } + + /** + * Parse the coordinates and the the attributes to the new ort object. + * + * @param ort The ort object. + * @return The Ort object. + */ + private SOrt setKoordinatenS(SOrt ort) { + String art = ""; + String x = ""; + String y = ""; + String tmp = ""; + if (this.koordinatenS.startsWith("\"")) { + tmp = this.koordinatenS.substring(1); + art = tmp.substring(0, tmp.indexOf("\"")); + tmp = tmp.substring(tmp.indexOf("\"") + 2); + } + else { + art = this.koordinatenS.substring(0, this.koordinatenS.indexOf(" ")); + tmp = this.koordinatenS.substring( + this.koordinatenS.indexOf(" ") + 1); + } + if (tmp.startsWith("\"")) { + tmp = tmp.substring(1); + x = tmp.substring(0, tmp.indexOf("\"")); + tmp = tmp.substring(tmp.indexOf("\"") + 2); + } + else { + x = tmp.substring(0, tmp.indexOf(" ")); + tmp = tmp.substring(tmp.indexOf(" ") + 1); + } + if (tmp.startsWith("\"")) { + tmp = tmp.substring(1); + y = tmp.substring(0, tmp.indexOf("\"")); + } + else { + y = tmp; + } + ort.setKoordXExtern(x); + ort.setLongitude(Double.valueOf(x)); + ort.setKoordYExtern(y); + ort.setLatitude(Double.valueOf(y)); + return ort; + } + + /** + * Create the new LOrt object from the given attributes. + * + * @return The new LOrt object. + */ + 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); + return null; + } + if(this.ortCode != null && this.ortCode.length() > 0) { + String nativeQuery = "select id from stammdaten.ort where bezeichnung = '"; + nativeQuery += this.ortCode + "'"; + Query query = repository.entityManager("land").createNativeQuery(nativeQuery); + List result = query.getResultList(); + if (result != null && !result.isEmpty()) { + this.ortId = result.get(0); + } + } + LOrt ort = new LOrt(); + ort.setOrt(BigInteger.valueOf(this.ortId)); + ort.setProbeId(this.probeId); + ort.setOrtsTyp(this.ortTyp); + ort.setOrtszusatztext(this.zusatztext); + logger.debug("yeah a new ort"); + return ort; + } + + public void reset() { + logger.debug("resetting ortcreator"); + this.beschreibung = null; + this.bezeichnung = null; + this.gemName = null; + this.gemSchluessel = null; + this.hoehe = null; + this.koord = null; + this.koordArt = null; + this.koordinaten = null; + this.koordinatenS = null; + this.koordS = null; + this.landKurz = null; + this.landLang = null; + this.landS = null; + this.nuts = null; + this.ortCode = null; + this.ortId = null; + this.ortTyp = null; + this.probeId = null; + this.zusatztext = null; + } +}