Mercurial > dive4elements > river
changeset 198:d980e545ccab
Added import code for importing discharge tables.
flys-backend/trunk@1537 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Ingo Weinzierl <ingo.weinzierl@intevation.de> |
---|---|
date | Tue, 22 Mar 2011 12:15:18 +0000 |
parents | c0dcc2357106 |
children | ed38839a6b08 |
files | flys-backend/ChangeLog flys-backend/src/main/java/de/intevation/flys/importer/AtFileParser.java flys-backend/src/main/java/de/intevation/flys/importer/ImportDischargeTable.java flys-backend/src/main/java/de/intevation/flys/importer/ImportDischargeTableValue.java flys-backend/src/main/java/de/intevation/flys/importer/ImportGauge.java flys-backend/src/main/java/de/intevation/flys/model/DischargeTable.java flys-backend/src/main/java/de/intevation/flys/model/DischargeTableValue.java |
diffstat | 7 files changed, 299 insertions(+), 1 deletions(-) [+] |
line wrap: on
line diff
--- a/flys-backend/ChangeLog Tue Mar 22 11:10:01 2011 +0000 +++ b/flys-backend/ChangeLog Tue Mar 22 12:15:18 2011 +0000 @@ -1,3 +1,19 @@ +2011-03-22 Ingo Weinzierl <ingo@intevation.de> + + * src/main/java/de/intevation/flys/model/DischargeTableValue.java, + src/main/java/de/intevation/flys/model/DischargeTable.java: Added new + constructors. + + * src/main/java/de/intevation/flys/importer/AtFileParser.java: New. This + parser is used to '*.at' files. + + * src/main/java/de/intevation/flys/importer/ImportGauge.java: Added code to + import discharge tables. + + * src/main/java/de/intevation/flys/importer/ImportDischargeTableValue.java, + src/main/java/de/intevation/flys/importer/ImportDischargeTable.java: New. + Helper models for import discharge tables. + 2011-03-22 Sascha L. Teichmann <sascha.teichmann@intevation.de> * src/main/java/de/intevation/flys/importer/WstParser.java,
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/flys-backend/src/main/java/de/intevation/flys/importer/AtFileParser.java Tue Mar 22 12:15:18 2011 +0000 @@ -0,0 +1,117 @@ +package de.intevation.flys.importer; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileInputStream; +import java.io.InputStreamReader; +import java.io.IOException; +import java.math.BigDecimal; +import java.text.NumberFormat; +import java.text.ParseException; + +import org.apache.log4j.Logger; + +import de.intevation.flys.importer.ImportDischargeTable; +import de.intevation.flys.importer.ImportDischargeTableValue; + + +public class AtFileParser { + + public static final String ENCODING = "ISO-8859-1"; + + private static Logger logger = Logger.getLogger(AtFileParser.class); + + private static NumberFormat nf = NumberFormat.getInstance(); + + + public AtFileParser() { + } + + + public ImportDischargeTable parse(ImportGauge gauge) throws IOException { + + File file = gauge.getAtFile(); + + logger.info("parsing AT file: " + file); + + BufferedReader br = null; + + String line = null; + + boolean beginning = true; + + ImportDischargeTable dischargeTable = new ImportDischargeTable(); + + try { + br = new BufferedReader( + new InputStreamReader( + new FileInputStream(file), ENCODING)); + + while ((line = br.readLine()) != null) { + String tmp = line.trim(); + + if (tmp.length() == 0) { + continue; + } + + if (tmp.startsWith("#! name=")) { + // XXX Skip the name, because we don't know where to save + // it at the moment + + //String name = tmp.substring(8); + continue; + } + + if (tmp.startsWith("#") || tmp.startsWith("*")) { + continue; + } + + String[] splits = tmp.replace(',', '.').split("\\s+"); + + if ((splits.length < 2) || (splits.length > 11)) { + logger.warn("Found an invalid row in the AT file."); + continue; + } + + String strW = splits[0].trim(); + double W = nf.parse(strW).doubleValue(); + + /* shift is used to differenciate between lines with + * exactly 10 Qs and lines with less than 10 Qs. The shift + * is only modified when it is the first line. + */ + int shift = 0; + + if (splits.length != 11 && beginning) { + shift = 11 - splits.length; + } + + + for (int i = 1; i < splits.length; i++) { + double iW = W + shift + i; + double iQ = nf.parse(splits[i].trim()).doubleValue(); + + dischargeTable.addDischargeTableValue( + new ImportDischargeTableValue( + new BigDecimal(iQ/100.0), + new BigDecimal(iW/100.0))); + } + + beginning = false; + } + } + catch (ParseException pe) { + logger.warn(pe.getMessage()); + } + finally { + if (br != null) { + br.close(); + } + } + + logger.info("Finished parsing AT file: " + file); + + return dischargeTable; + } +} +// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/flys-backend/src/main/java/de/intevation/flys/importer/ImportDischargeTable.java Tue Mar 22 12:15:18 2011 +0000 @@ -0,0 +1,75 @@ +package de.intevation.flys.importer; + +import java.util.ArrayList; +import java.util.List; + +import org.hibernate.Session; +import org.hibernate.Query; + +import de.intevation.flys.model.DischargeTable; +import de.intevation.flys.model.Gauge; + + +public class ImportDischargeTable +{ + protected DischargeTable peer; + + protected List<ImportDischargeTableValue> dischargeTableValues; + + + public ImportDischargeTable() { + dischargeTableValues = new ArrayList<ImportDischargeTableValue>(); + } + + + public void addDischargeTableValue(ImportDischargeTableValue value) { + dischargeTableValues.add(value); + } + + + public void setDischargeTableValues(List<ImportDischargeTableValue> values){ + this.dischargeTableValues = values; + } + + + public List<ImportDischargeTableValue> getDischargeTableValues() { + return dischargeTableValues; + } + + + public DischargeTable getPeer(Gauge gauge) { + if (peer == null) { + Session session = Importer.sessionHolder.get(); + + Query query = session.createQuery( + "from DischargeTable where gauge.id=:gauge"); + query.setParameter("gauge", gauge.getId()); + + List<DischargeTable> dischargeTables = query.list(); + if (dischargeTables.isEmpty()) { + peer = new DischargeTable(gauge); + session.save(peer); + } + else { + peer = dischargeTables.get(0); + } + } + + return peer; + } + + + public void storeDependencies(Gauge gauge) { + storeDischargeTableValues(gauge); + } + + + public void storeDischargeTableValues(Gauge gauge) { + DischargeTable dischargeTable = getPeer(gauge); + + for (ImportDischargeTableValue value: dischargeTableValues) { + value.getPeer(dischargeTable); + } + } +} +// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/flys-backend/src/main/java/de/intevation/flys/importer/ImportDischargeTableValue.java Tue Mar 22 12:15:18 2011 +0000 @@ -0,0 +1,59 @@ +package de.intevation.flys.importer; + +import java.math.BigDecimal; +import java.util.List; + +import org.hibernate.Session; +import org.hibernate.Query; + +import de.intevation.flys.model.DischargeTable; +import de.intevation.flys.model.DischargeTableValue; + + +public class ImportDischargeTableValue +{ + private Integer id; + + private BigDecimal q; + private BigDecimal w; + + private ImportDischargeTable dischargeTable; + + private DischargeTableValue peer; + + + public ImportDischargeTableValue() { + } + + + public ImportDischargeTableValue(BigDecimal q, BigDecimal w) { + this.dischargeTable = dischargeTable; + this.q = q; + this.w = w; + } + + + public DischargeTableValue getPeer(DischargeTable dischargeTable) { + if (peer == null) { + Session session = Importer.sessionHolder.get(); + + Query query = session.createQuery( + "from DischargeTableValue where " + + "dischargeTable.id=:tableId and q=:q and w=:w"); + query.setParameter("tableId", dischargeTable.getId()); + query.setParameter("q", q); + query.setParameter("w", w); + + List<DischargeTableValue> dischargeTableValues = query.list(); + if (dischargeTableValues.isEmpty()) { + peer = new DischargeTableValue(dischargeTable, q, w); + session.save(peer); + } + else { + peer = dischargeTableValues.get(0); + } + } + + return peer; + } +}
--- a/flys-backend/src/main/java/de/intevation/flys/importer/ImportGauge.java Tue Mar 22 11:10:01 2011 +0000 +++ b/flys-backend/src/main/java/de/intevation/flys/importer/ImportGauge.java Tue Mar 22 12:15:18 2011 +0000 @@ -28,6 +28,8 @@ protected Gauge peer; + protected ImportDischargeTable dischargeTable; + protected List<ImportMainValueType> mainValueTypes; protected List<ImportNamedMainValue> namedMainValues; protected List<ImportMainValue> mainValues; @@ -93,6 +95,14 @@ this.station = station; } + public ImportDischargeTable getDischargeTable() { + return dischargeTable; + } + + public void setDischargeTable(ImportDischargeTable dischargeTable) { + this.dischargeTable = dischargeTable; + } + public List<ImportMainValueType> getMainValueTypes() { return mainValueTypes; } @@ -120,6 +130,9 @@ public void parseDependencies() throws IOException { StaFileParser sfp = new StaFileParser(); sfp.parse(this); + + AtFileParser afp = new AtFileParser(); + setDischargeTable(afp.parse(this)); } public void storeDependencies(River river) { @@ -138,7 +151,13 @@ mainValue.getPeer(river); } - // TODO: Implement ATs + storeDischargeTable(gauge); + } + + + public void storeDischargeTable(Gauge gauge) { + dischargeTable.getPeer(gauge); + dischargeTable.storeDependencies(gauge); } public Gauge getPeer(River river) {
--- a/flys-backend/src/main/java/de/intevation/flys/model/DischargeTable.java Tue Mar 22 11:10:01 2011 +0000 +++ b/flys-backend/src/main/java/de/intevation/flys/model/DischargeTable.java Tue Mar 22 12:15:18 2011 +0000 @@ -29,6 +29,10 @@ public DischargeTable() { } + public DischargeTable(Gauge gauge) { + this.gauge = gauge; + } + @Id @SequenceGenerator( name = "SEQUENCE_DISCHARGE_TABLES_ID_SEQ",
--- a/flys-backend/src/main/java/de/intevation/flys/model/DischargeTableValue.java Tue Mar 22 11:10:01 2011 +0000 +++ b/flys-backend/src/main/java/de/intevation/flys/model/DischargeTableValue.java Tue Mar 22 12:15:18 2011 +0000 @@ -27,6 +27,14 @@ public DischargeTableValue() { } + public DischargeTableValue( + DischargeTable dischargeTable, BigDecimal q, BigDecimal w) + { + this.dischargeTable = dischargeTable; + this.q = q; + this.w = w; + } + @Id @SequenceGenerator( name = "SEQUENCE_DISCHARGE_TABLE_VALUES_ID_SEQ",