Mercurial > lada > lada-server
changeset 609:093bfdcdb09c
Added exporter impl. for laf file format.
author | Raimund Renkert <raimund.renkert@intevation.de> |
---|---|
date | Thu, 16 Apr 2015 15:48:27 +0200 |
parents | e35f047f019f |
children | 374a2e78cec5 |
files | src/main/java/de/intevation/lada/exporter/laf/LafCreator.java src/main/java/de/intevation/lada/exporter/laf/LafExporter.java |
diffstat | 2 files changed, 395 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/de/intevation/lada/exporter/laf/LafCreator.java Thu Apr 16 15:48:27 2015 +0200 @@ -0,0 +1,336 @@ +package de.intevation.lada.exporter.laf; + +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.List; + +import javax.inject.Inject; +import javax.inject.Named; +import javax.persistence.Query; + +import org.apache.log4j.Logger; + +import de.intevation.lada.exporter.Creator; +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.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; + +/** + * This creator produces a LAF conform String containing all information about + * a single {@link LProbe} object including subobjects like + * {@link LMessung}, {@link LMesswert}, {@link LKommentarP}... + * + * @author <a href="mailto:rrenkert@intevation.de">Raimund Renkert</a> + */ +@Named("lafcreator") +public class LafCreator +implements Creator +{ + @Inject + private Logger logger; + + @Inject + @RepositoryConfig(type=RepositoryType.RO) + private Repository repository; + + /** + * Create the LAF conform String. + * + * @param probeId The {@link LProbe} id. + */ + @Override + public String create(String probeId) { + String lafProbe = "%PROBE%\n"; + lafProbe += probeToLAF(probeId); + return lafProbe; + } + + /** + * Find the {@link LProbe} object and produce the LAF conform string. + * @param probeId The {@link LProbe} id. + * @return LAF conform string. + */ + private String probeToLAF(String probeId) { + Response found = repository.getById(LProbe.class, Integer.valueOf(probeId), "land"); + if (found.getData() == null) { + return null; + } + LProbe aProbe = (LProbe)found.getData(); + String lafProbe = writeAttributes(aProbe); + return lafProbe; + } + + /** + * Write the attributes and subobjects. + * + * @param probe The {@link LProbeInfo} object. + * @return LAF conform string. + */ + private String writeAttributes(LProbe probe) { + DateFormat format = new SimpleDateFormat("yyyyMMdd HHmm"); + QueryBuilder<LKommentarP> kommBuilder = + new QueryBuilder<LKommentarP>( + repository.entityManager("land"), LKommentarP.class); + kommBuilder.and("probeId", probe.getId()); + Response kommentar = repository.filter(kommBuilder.getQuery(), "land"); + List<LKommentarP> kommentare = (List<LKommentarP>)kommentar.getData(); + + String nativeQuery = "select probenart from stammdaten.probenart where id= '"; + nativeQuery += probe.getProbenartId() + "'"; + Query query = repository.entityManager("land").createNativeQuery(nativeQuery); + List<String> result = query.getResultList(); + String probenart = result.get(0);//[0].toString(); + + QueryBuilder<LZusatzWert> zusatzBuilder = + new QueryBuilder<LZusatzWert>( + repository.entityManager("land"), LZusatzWert.class); + zusatzBuilder.and("probeId", probe.getId()); + Response zusatz = repository.filter(zusatzBuilder.getQuery(), "land"); + List<LZusatzWert> zusatzwerte = (List<LZusatzWert>)zusatz.getData(); + + QueryBuilder<ProbeTranslation> transBuilder = + new QueryBuilder<ProbeTranslation>( + repository.entityManager("land"), ProbeTranslation.class); + transBuilder.and("probe", probe.getId()); + Response trans = repository.filter(transBuilder.getQuery(), "land"); + List<ProbeTranslation> translation = (List<ProbeTranslation>)trans.getData(); + String laf = ""; + laf += probe.getDatenbasisId() == null ? + "": lafLine("DATENBASIS_S", probe.getDatenbasisId().toString()); + laf += probe.getNetzbetreiberId() == null ? + "" : lafLine("NETZKENNUNG", probe.getNetzbetreiberId()); + laf += probe.getMstId() == null ? + "" : lafLine("MESSSTELLE", probe.getMstId()); + laf += lafLine("PROBE_ID", translation.get(0).getProbeIdAlt()); + laf += lafLine("HAUPTPROBENNUMMER", probe.getHauptprobenNr()); + laf += probe.getBaId() == null ? + "" : lafLine("MESSPROGRAMM_S", "\"" + probe.getBaId() + "\""); + laf += probe.getProbenartId() == null ? + "" : lafLine("PROBENART", + "\"" + probenart + "\""); + laf += probe.getSolldatumBeginn() == null ? + "" : lafLine("SOLL_DATUM_UHRZEIT_A", + format.format(probe.getSolldatumBeginn())); + laf += probe.getSolldatumEnde() == null ? + "" : lafLine("SOLL_DATUM_UHRZEIT_E", + format.format(probe.getSolldatumEnde())); + laf += probe.getProbeentnahmeBeginn() == null ? + "" : lafLine("PROBENAHME_DATUM_UHRZEIT_A", + format.format(probe.getProbeentnahmeBeginn())); + laf += probe.getProbeentnahmeEnde() == null ? + "" : lafLine("PROBENAHME_DATUM_UHRZEIT_E", + format.format(probe.getProbeentnahmeEnde())); + laf += probe.getUmwId() == null ? + "" : lafLine("UMWELTBEREICH_S", probe.getUmwId()); + laf += probe.getMedia() == null ? + "" : lafLine("MEDIUM", "\"" + probe.getMedia() + "\""); + laf += probe.getMediaDesk() == null ? + "" : lafLine("DESKRIPTOREN", "\"" + probe.getMediaDesk() + "\""); + laf += probe.getTest() == Boolean.TRUE ? + lafLine("TESTDATEN", "1") : lafLine("TESTDATEN", "0"); + for (LZusatzWert zw : zusatzwerte) { + laf += writeZusatzwert(zw); + } + for (LKommentarP kp : kommentare) { + laf += writeKommentar(kp); + } + laf += writeMessung(probe); + laf += writeOrt(probe); + return laf; + } + + /** + * Write {@link LZusatzWert} attributes. + * + * @param zw The {@link LZusatzWert}. + * @return Single LAF line. + */ + private String writeZusatzwert(LZusatzWert zw) { + String nativeQuery = "select beschreibung, meh_id from stammdaten.proben_zusatz where pzs_id = '"; + nativeQuery += zw.getPzsId() + "'"; + Query query = repository.entityManager("land").createNativeQuery(nativeQuery); + List<Object[]> result = query.getResultList(); + + String value = "\"" + result.get(0)[0].toString() + "\""; + value += " " + zw.getMesswertPzs(); + value += " " + result.get(0)[1].toString(); + value += " " + zw.getMessfehler(); + return lafLine("PZB_S", value); + } + + /** + * Write {@link LOrt} attributes. + * + * @param probe The {@link LProbeInfo} object. + * @return LAF conform string + */ + private String writeOrt(LProbe probe) { + QueryBuilder<LOrt> builder = + new QueryBuilder<LOrt>( + repository.entityManager("land"), + LOrt.class); + builder.and("probeId", probe.getId()); + Response objects = repository.filter(builder.getQuery(), "land"); + List<LOrt> orte = (List<LOrt>)objects.getData(); + + String laf = ""; + for(LOrt o : orte) { + laf += "%ORT%\n"; + String nativeQuery = "select bezeichnung, staat_id, longitude, latitude, gem_id from stammdaten.ort where id = '"; + nativeQuery += o.getOrt() + "'"; + Query query = repository.entityManager("land").createNativeQuery(nativeQuery); + List<Object[]> result = query.getResultList(); + + laf += lafLine("ORT_CODE", + "\"" + result.get(0)[0].toString() + "\""); + laf += lafLine("ORT_TYP", "\"" + o.getOrtsTyp() + "\""); + laf += o.getOrtszusatztext() == null ? "": + lafLine("ORT_ZUSATZTEXT", "\"" + o.getOrtszusatztext() + "\""); + laf += lafLine("ORT_LAND_S", String.valueOf(result.get(0)[1].toString())); + String koord = ""; + koord += "05 "; + koord += result.get(0)[2].toString() + " "; + koord += result.get(0)[3].toString(); + //TODO: use table koordinatenart and koord*extern! + laf += lafLine("ORT_KOORDINATEN_S", koord); + laf += lafLine("ORT_GEMEINDESCHLUESSEL", result.get(0)[4].toString()); + } + return laf; + } + + /** + * Write {@link LKommentarP} attributes. + * + * @param kp The {@link LKommentarP} object. + * @return Single LAF line. + */ + private String writeKommentar(LKommentarP kp) { + DateFormat format = new SimpleDateFormat("yyyyMMdd HHmm"); + String value = "\"" + kp.getErzeuger() + "\" " + + format.format(kp.getDatum()) + " " + + "\"" + kp.getText() + "\""; + return lafLine("PROBENKOMMENTAR", value); + } + + /** + * Write {@link LMessung} attributes. + * + * @param probe The {@link LProbeInfo} object. + * @return LAF conform string. + */ + private String writeMessung(LProbe probe) { + DateFormat format = new SimpleDateFormat("yyyyMMdd HHmm"); + // Get all messungen + QueryBuilder<LMessung> builder = + new QueryBuilder<LMessung>( + repository.entityManager("land"), + LMessung.class); + builder.and("probeId", probe.getId()); + Response objects = repository.filter(builder.getQuery(), "land"); + List<LMessung> mess = (List<LMessung>)objects.getData(); + + String laf = ""; + for(LMessung m : mess) { + laf += "%MESSUNG%\n"; + QueryBuilder<LMesswert> wertBuilder = + new QueryBuilder<LMesswert>( + repository.entityManager("land"), LMesswert.class); + wertBuilder.and("messungsId", m.getId()); + Response messw = repository.filter(wertBuilder.getQuery(), "land"); + List<LMesswert> werte = (List<LMesswert>)messw.getData(); + QueryBuilder<LKommentarM> kommBuilder = + new QueryBuilder<LKommentarM>( + repository.entityManager("land"), LKommentarM.class); + kommBuilder.and("messungsId", m.getId()); + Response kommentar = repository.filter(kommBuilder.getQuery(), "land"); + List<LKommentarM> kommentare = (List<LKommentarM>)kommentar.getData(); + laf += lafLine("MESSUNGS_ID", m.getId().toString()); + laf += lafLine("NEBENPROBENNUMMER", m.getNebenprobenNr()); + laf += m.getMesszeitpunkt() == null ? + "" : lafLine( + "MESS_DATUM_UHRZEIT", + format.format(m.getMesszeitpunkt())); + laf += m.getMessdauer() == null ? + "" : lafLine("MESSZEIT_SEKUNDEN", m.getMessdauer().toString()); + laf += m.getMmtId() == null ? + "" : lafLine("MESSMETHODE_S", m.getMmtId()); + for (LMesswert mw : werte) { + laf += writeMesswert(mw); + } + for (LKommentarM mk: kommentare) { + laf += writeKommentar(mk); + } + } + return laf; + } + + /** + * Write {@link LKommentarM} attributes. + * @param mk The {@link LKommentarM} object. + * @return Single LAF line. + */ + private String writeKommentar(LKommentarM mk) { + DateFormat format = new SimpleDateFormat("yyyyMMdd HHmm"); + String value = "\"" + mk.getErzeuger() + "\" " + + format.format(mk.getDatum()) + " " + + "\"" + mk.getText() + "\""; + return lafLine("KOMMENTAR", value); + } + + /** + * Write {@link LMesswert} attributes. + * @param mw The {@link LMesswert} object. + * @return Single LAF line. + */ + private String writeMesswert(LMesswert mw) { + String nativeQuery = "select messgroesse from stammdaten.messgroesse where id = '"; + nativeQuery += mw.getMessgroesseId() + "'"; + Query query = repository.entityManager("land").createNativeQuery(nativeQuery); + List<Object[]> result = query.getResultList(); + + String nativeQuery2 = "select einheit from stammdaten.mess_einheit where id = '"; + nativeQuery2 += mw.getMehId() + "'"; + Query query2 = repository.entityManager("land").createNativeQuery(nativeQuery2); + List<Object[]> result2 = query2.getResultList(); + + String value = "\"" + result.get(0) + "\""; + if (mw.getGrenzwertueberschreitung() != null && + !mw.getGrenzwertueberschreitung()) { + value += " <"; + } + else { + value += " "; + } + value += mw.getMesswert(); + value += " \"" + result2.get(0) + "\""; + value += mw.getMessfehler() == null ? " NULL" : " " + mw.getMessfehler(); + value += mw.getNwgZuMesswert() == null ? " NULL" : " " + mw.getNwgZuMesswert(); + value += mw.getGrenzwertueberschreitung() == null ? " N" : + mw.getGrenzwertueberschreitung() ? " Y" : " N"; + return lafLine("MESSWERT", value); + } + + /** + * Write a single LAF conform line from key and value. + * + * @param key The key. + * @param value The value. + * @return LAF conform line. + */ + private String lafLine(String key, String value) { + for (int i = key.length(); i < 30; i++) { + key += " "; + } + return key + value + "\n"; + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/de/intevation/lada/exporter/laf/LafExporter.java Thu Apr 16 15:48:27 2015 +0200 @@ -0,0 +1,59 @@ +package de.intevation.lada.exporter.laf; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.List; + +import javax.inject.Inject; + +import org.apache.log4j.Logger; + +import de.intevation.lada.exporter.ExportConfig; +import de.intevation.lada.exporter.ExportFormat; +import de.intevation.lada.exporter.Exporter; +import de.intevation.lada.model.land.LProbe; +import de.intevation.lada.util.auth.UserInfo; + +/** +* The LAF exporter implements {@link Exporter} to produce a LAF file. +* +* @author <a href="mailto:rrenkert@intevation.de">Raimund Renkert</a> +*/ +@ExportConfig(format=ExportFormat.LAF) +public class LafExporter +implements Exporter +{ + @Inject + private Logger logger; + + @Inject + private LafCreator creator; + + /** + * Export the {@link LProbe} objects. + * + * @param proben List of probe ids. + * @param auth The authentication information. + * @return InputStream with the LAF data. + */ + @Override + public InputStream export( + List<Integer> proben, + UserInfo userInfo + ) { + String laf = ""; + for (Integer probeId: proben) { + laf += creator.create(probeId.toString()); + } + laf += "%ENDE%"; + InputStream in = new ByteArrayInputStream(laf.getBytes()); + try { + in.close(); + } + catch (IOException e) { + //TODO Exception handling. + } + return in; + } +}