Mercurial > dive4elements > river
changeset 189:bc3747a371cc
First part of parsing main values.
flys-backend/trunk@1520 c6561f87-3c4e-4783-a992-168aeb5c3f6f
line wrap: on
line diff
--- a/flys-backend/ChangeLog Fri Mar 18 12:10:33 2011 +0000 +++ b/flys-backend/ChangeLog Fri Mar 18 16:12:24 2011 +0000 @@ -1,3 +1,53 @@ +2011-03-17 Sascha L. Teichmann <sascha.teichmann@intevation.de> + + First part of parsing main values. + + * src/main/java/de/intevation/flys/App.java: Commented out + creation of dummy rivers. + + * src/main/java/de/intevation/flys/model/NamedMainValues.java: + Moved to NamedMainValue. + + * src/main/java/de/intevation/flys/model/NamedMainValue.java: + New. Formerly NamedMainValues. + + * src/main/java/de/intevation/flys/model/MainValue.java: + New. Forgotten part of the model. + + * src/main/java/de/intevation/flys/model/MainValueType.java: + Data is String not BigDecimal + + * src/main/java/de/intevation/flys/model/Range.java: Removed + contructor with double arguments. Using BigDecimal now. + + * src/main/java/de/intevation/flys/importer/PegelGltParser.java: + Propagate BigDecimal usage. + + * src/main/java/de/intevation/flys/importer/Importer.java: + Removed needless import. Added TODO + + * src/main/java/de/intevation/flys/importer/ImportRiver.java: + Parse the dependencies of the gauges, too. + + * src/main/java/de/intevation/flys/importer/StaFileParser.java: + New. Parser for STA files. + + * src/main/java/de/intevation/flys/importer/ImportGauge.java: + Call STA file parser. + + * src/main/java/de/intevation/flys/importer/AnnotationsParser.java, + src/main/java/de/intevation/flys/importer/ImportRange.java: + Uses BigDecimal now. + + * src/main/java/de/intevation/flys/importer/ImportAttribute.java: + Fixed wrong type cast in equals. + + * src/main/java/de/intevation/flys/importer/ImportMainValueType.java: + New. Helper model for importing main value types. + + * src/main/java/de/intevation/flys/backend/SessionFactoryProvider.java: + Register forgotten MainValue model. + 2011-03-17 Sascha L. Teichmann <sascha.teichmann@intevation.de> Store annotations in backend.
--- a/flys-backend/src/main/java/de/intevation/flys/App.java Fri Mar 18 12:10:33 2011 +0000 +++ b/flys-backend/src/main/java/de/intevation/flys/App.java Fri Mar 18 16:12:24 2011 +0000 @@ -10,7 +10,8 @@ import de.intevation.flys.model.DischargeTableValue; import de.intevation.flys.model.Gauge; import de.intevation.flys.model.MainValueType; -import de.intevation.flys.model.NamedMainValues; +import de.intevation.flys.model.NamedMainValue; +import de.intevation.flys.model.MainValue; import de.intevation.flys.model.Position; import de.intevation.flys.model.Range; import de.intevation.flys.model.River; @@ -62,7 +63,8 @@ cfg.addAnnotatedClass(DischargeTableValue.class); cfg.addAnnotatedClass(Gauge.class); cfg.addAnnotatedClass(MainValueType.class); - cfg.addAnnotatedClass(NamedMainValues.class); + cfg.addAnnotatedClass(NamedMainValue.class); + cfg.addAnnotatedClass(MainValue.class); cfg.addAnnotatedClass(Position.class); cfg.addAnnotatedClass(Range.class); cfg.addAnnotatedClass(River.class); @@ -102,6 +104,8 @@ cfg.mergeProperties(props); + /* + SessionFactory sessionFactory = cfg.buildSessionFactory(); Session session = sessionFactory.openSession(); @@ -115,6 +119,7 @@ session.getTransaction().commit(); session.close(); + */ } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/flys-backend/src/main/java/de/intevation/flys/backend/SessionFactoryProvider.java Fri Mar 18 12:10:33 2011 +0000 +++ b/flys-backend/src/main/java/de/intevation/flys/backend/SessionFactoryProvider.java Fri Mar 18 16:12:24 2011 +0000 @@ -15,7 +15,8 @@ import de.intevation.flys.model.DischargeTableValue; import de.intevation.flys.model.Gauge; import de.intevation.flys.model.MainValueType; -import de.intevation.flys.model.NamedMainValues; +import de.intevation.flys.model.NamedMainValue; +import de.intevation.flys.model.MainValue; import de.intevation.flys.model.Position; import de.intevation.flys.model.Range; import de.intevation.flys.model.River; @@ -117,7 +118,8 @@ cfg.addAnnotatedClass(DischargeTableValue.class); cfg.addAnnotatedClass(Gauge.class); cfg.addAnnotatedClass(MainValueType.class); - cfg.addAnnotatedClass(NamedMainValues.class); + cfg.addAnnotatedClass(NamedMainValue.class); + cfg.addAnnotatedClass(MainValue.class); cfg.addAnnotatedClass(Position.class); cfg.addAnnotatedClass(Range.class); cfg.addAnnotatedClass(River.class);
--- a/flys-backend/src/main/java/de/intevation/flys/importer/AnnotationsParser.java Fri Mar 18 12:10:33 2011 +0000 +++ b/flys-backend/src/main/java/de/intevation/flys/importer/AnnotationsParser.java Fri Mar 18 16:12:24 2011 +0000 @@ -11,6 +11,8 @@ import java.io.InputStreamReader; import java.io.FileInputStream; +import java.math.BigDecimal; + import org.apache.log4j.Logger; import de.intevation.flys.utils.FileTools; @@ -74,13 +76,13 @@ String [] r = parts[2].replace(",", ".").split("\\s*#\\s*"); - Double from, to; + BigDecimal from, to; try { - from = Double.valueOf(r[0]); - to = r.length < 2 ? null : Double.valueOf(r[1]); - if (to != null && from > to) { - Double t = from; from = to; to = t; + from = new BigDecimal(r[0]); + to = r.length < 2 ? null : new BigDecimal(r[1]); + if (to != null && from.compareTo(to) > 0) { + BigDecimal t = from; from = to; to = t; } } catch (NumberFormatException nfe) {
--- a/flys-backend/src/main/java/de/intevation/flys/importer/ImportAttribute.java Fri Mar 18 12:10:33 2011 +0000 +++ b/flys-backend/src/main/java/de/intevation/flys/importer/ImportAttribute.java Fri Mar 18 16:12:24 2011 +0000 @@ -36,8 +36,8 @@ @Override public boolean equals(Object other) { if (other == this) return true; - if (!(other instanceof ImportPosition)) return false; - return value.equals(((ImportPosition)other).value); + if (!(other instanceof ImportAttribute)) return false; + return value.equals(((ImportAttribute)other).value); } @Override
--- a/flys-backend/src/main/java/de/intevation/flys/importer/ImportGauge.java Fri Mar 18 12:10:33 2011 +0000 +++ b/flys-backend/src/main/java/de/intevation/flys/importer/ImportGauge.java Fri Mar 18 16:12:24 2011 +0000 @@ -2,12 +2,25 @@ import java.io.File; +import java.math.BigDecimal; + +import de.intevation.flys.model.River; +import de.intevation.flys.model.Gauge; + +import java.io.IOException; + public class ImportGauge { protected ImportRange range; - protected File staFile; - protected File atFile; + protected File staFile; + protected File atFile; + + protected String name; + protected BigDecimal aeo; + protected BigDecimal datum; + + protected Gauge peer; public ImportGauge() { } @@ -37,5 +50,40 @@ public File getAtFile() { return atFile; } + + public BigDecimal getAeo() { + return aeo; + } + + public void setAeo(BigDecimal aeo) { + this.aeo = aeo; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public BigDecimal getDatum() { + return datum; + } + + public void setDatum(BigDecimal datum) { + this.datum = datum; + } + + public void parseDependencies() throws IOException { + StaFileParser sfp = new StaFileParser(); + sfp.parse(this); + } + + public Gauge getPeer(River river) { + if (peer == null) { + } + return peer; + } } // 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/ImportMainValueType.java Fri Mar 18 16:12:24 2011 +0000 @@ -0,0 +1,65 @@ +package de.intevation.flys.importer; + +import de.intevation.flys.model.MainValueType; + +import org.hibernate.Session; +import org.hibernate.Query; + +import java.util.List; + +public class ImportMainValueType +implements Comparable<ImportMainValueType> +{ + protected String value; + + protected MainValueType peer; + + public ImportMainValueType() { + } + + public ImportMainValueType(String value) { + this.value = value; + } + + public String getValue() { + return value; + } + + public void setValue(String value) { + this.value = value; + } + + public int compareTo(ImportMainValueType other) { + return value.compareTo(other.value); + } + + @Override + public boolean equals(Object other) { + if (other == this) return true; + if (!(other instanceof ImportMainValueType)) return false; + return value.equals(((ImportMainValueType)other).value); + } + + @Override + public int hashCode() { + return value.hashCode(); + } + + public MainValueType getPeer() { + if (peer == null) { + Session session = Importer.sessionHolder.get(); + Query query = session.createQuery("from MainValueType where value=:value"); + query.setString("value", value); + List<MainValueType> values = query.list(); + if (values.isEmpty()) { + peer = new MainValueType(value); + session.save(peer); + } + else { + peer = values.get(0); + } + } + return peer; + } +} +// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/flys-backend/src/main/java/de/intevation/flys/importer/ImportRange.java Fri Mar 18 12:10:33 2011 +0000 +++ b/flys-backend/src/main/java/de/intevation/flys/importer/ImportRange.java Fri Mar 18 16:12:24 2011 +0000 @@ -17,20 +17,20 @@ { private static Logger log = Logger.getLogger(ImportRange.class); - protected Double from; - protected Double to; + protected BigDecimal a; + protected BigDecimal b; protected Range peer; public ImportRange() { } - public ImportRange(Double from, Double to) { - this.from = from; - this.to = to; + public ImportRange(BigDecimal a, BigDecimal b) { + this.a = a; + this.b = b; } - private static final int compare(Double a, Double b) { + private static final int compare(BigDecimal a, BigDecimal b) { if (a == null && b == null) { return 0; } @@ -44,25 +44,25 @@ } public int compareTo(ImportRange other) { - int cmp = compare(from, other.from); + int cmp = compare(a, other.a); if (cmp != 0) return cmp; - return compare(to, other.to); - } - - public Double getFrom() { - return from; + return compare(b, other.b); } - public void setFrom(Double from) { - this.from = from; + public BigDecimal getA() { + return a; } - public Double getTo() { - return to; + public void setA(BigDecimal a) { + this.a = a; } - public void setTo(Double to) { - this.to = to; + public BigDecimal getB() { + return b; + } + + public void setB(BigDecimal b) { + this.b = b; } public Range getPeer(River river) { @@ -70,8 +70,6 @@ Session session = Importer.sessionHolder.get(); Query query = session.createQuery( "from Range where a=:a and b=:b and river.id=:river"); - BigDecimal a = from != null ? BigDecimal.valueOf(from) : null; - BigDecimal b = to != null ? BigDecimal.valueOf(to) : null; query.setParameter("a", a); query.setParameter("b", b); query.setParameter("river", river.getId());
--- a/flys-backend/src/main/java/de/intevation/flys/importer/ImportRiver.java Fri Mar 18 12:10:33 2011 +0000 +++ b/flys-backend/src/main/java/de/intevation/flys/importer/ImportRiver.java Fri Mar 18 16:12:24 2011 +0000 @@ -83,6 +83,10 @@ pgltp.parse(gltFile); gauges = pgltp.getGauges(); + + for (ImportGauge gauge: gauges) { + gauge.parseDependencies(); + } } public void parseAnnotations() throws IOException {
--- a/flys-backend/src/main/java/de/intevation/flys/importer/Importer.java Fri Mar 18 12:10:33 2011 +0000 +++ b/flys-backend/src/main/java/de/intevation/flys/importer/Importer.java Fri Mar 18 16:12:24 2011 +0000 @@ -12,13 +12,10 @@ import org.hibernate.SessionFactory; import org.hibernate.Session; import org.hibernate.Transaction; -import org.hibernate.Query; import org.hibernate.HibernateException; import de.intevation.flys.backend.SessionFactoryProvider; -import de.intevation.flys.model.River; - public class Importer { private static Logger log = Logger.getLogger(Importer.class); @@ -111,6 +108,7 @@ } } + // TODO: Add a dry run option new Importer(infoGewParser.getRivers()).writeToDatabase(); } }
--- a/flys-backend/src/main/java/de/intevation/flys/importer/PegelGltParser.java Fri Mar 18 12:10:33 2011 +0000 +++ b/flys-backend/src/main/java/de/intevation/flys/importer/PegelGltParser.java Fri Mar 18 16:12:24 2011 +0000 @@ -5,14 +5,13 @@ import java.util.List; import java.util.ArrayList; -import java.util.regex.Pattern; -import java.util.regex.Matcher; - import java.io.IOException; import java.io.LineNumberReader; import java.io.FileInputStream; import java.io.InputStreamReader; +import java.math.BigDecimal; + import org.apache.log4j.Logger; import de.intevation.flys.utils.FileTools; @@ -71,9 +70,11 @@ continue; } - double from = Double.parseDouble(parts[0].replace(",", ".")); - double to = Double.parseDouble(parts[1].replace(",", ".")); - if (to < from) { double t = from; from = to; to = t; } + BigDecimal from = new BigDecimal(parts[0].replace(",", ".")); + BigDecimal to = new BigDecimal(parts[1].replace(",", ".")); + if (to.compareTo(from) > 0) { + BigDecimal t = from; from = to; to = t; + } ImportRange range = new ImportRange(from, to); File staFile = FileTools.repair(new File(parent, parts[2])); File atFile = FileTools.repair(new File(parent, parts[3]));
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/flys-backend/src/main/java/de/intevation/flys/importer/StaFileParser.java Fri Mar 18 16:12:24 2011 +0000 @@ -0,0 +1,136 @@ +package de.intevation.flys.importer; + +import java.io.File; +import java.io.IOException; +import java.io.LineNumberReader; +import java.io.FileInputStream; +import java.io.InputStreamReader; + +import java.math.BigDecimal; + +import java.util.regex.Pattern; +import java.util.regex.Matcher; + +import java.util.HashMap; + +import org.apache.log4j.Logger; + +public class StaFileParser +{ + private static Logger log = Logger.getLogger(StaFileParser.class); + + public static final String ENCODING = "ISO-8859-1"; + + public static final Pattern QWTD_ = + Pattern.compile("\\s*([^\\s]+)\\s+([^\\s]+)\\s+([QWTD-]).*"); + + public StaFileParser() { + } + + public boolean parse(ImportGauge gauge) throws IOException { + + File file = gauge.getStaFile(); + + log.info("parsing STA file: " + file); + LineNumberReader in = null; + try { + in = + new LineNumberReader( + new InputStreamReader( + new FileInputStream(file), ENCODING)); + + String line = in.readLine(); + + if (line == null) { + log.warn("STA file is empty."); + return false; + } + + if (line.length() < 37) { + log.warn("first line in STA file is too short."); + return false; + } + + gauge.setName(line.substring(16, 37).trim()); + + String [] values = line.substring(38).trim().split("\\s+", 2); + + if (values.length < 2) { + log.warn("Not enough columns for aeo and datum"); + } + try { + gauge.setAeo(new BigDecimal(values[0].replace(",", "."))); + gauge.setDatum(new BigDecimal(values[1].replace(",", "."))); + } + catch (NumberFormatException nfe) { + log.warn("cannot parse aeo or datum"); + return false; + } + + line = in.readLine(); + + if (line == null) { + log.warn("STA file has not enough lines"); + return false; + } + + if (line.length() < 36) { + log.warn("second line is too short"); + return false; + } + + try { + gauge.setDatum( + new BigDecimal(line.substring(29, 36).trim())); + } + catch (NumberFormatException nfe) { + log.warn("parsing of the datum of the gauge failed"); + return false; + } + + // overread the next six lines + for (int i = 0; i < 6; ++i) { + if ((line = in.readLine()) == null) { + log.warn("STA file is too short"); + return false; + } + } + + HashMap<String, ImportMainValueType> types = + new HashMap<String, ImportMainValueType>(); + + while ((line = in.readLine()) != null) { + Matcher m = QWTD_.matcher(line); + if (m.matches()) { + BigDecimal value; + try { + value = new BigDecimal(m.group(2).replace(",", ".")); + } + catch (NumberFormatException nfe) { + log.warn("value not parseable in line " + + in.getLineNumber()); + continue; + } + String typeString = m.group(3); + log.debug("\t type: " + typeString); + ImportMainValueType type = types.get(typeString); + if (type == null) { + type = new ImportMainValueType(typeString); + types.put(typeString, type); + } + } + else { + // TODO: treat as a comment + } + } + } + finally { + if (in != null) { + in.close(); + } + } + log.info("finished parsing STA file: " + file); + return true; + } +} +// 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/model/MainValue.java Fri Mar 18 16:12:24 2011 +0000 @@ -0,0 +1,91 @@ +package de.intevation.flys.model; + +import java.io.Serializable; + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; +import javax.persistence.GeneratedValue; +import javax.persistence.Column; +import javax.persistence.SequenceGenerator; +import javax.persistence.OneToOne; +import javax.persistence.JoinColumn; +import javax.persistence.GenerationType; + +import java.math.BigDecimal; + +@Entity +@Table(name = "main_values") +public class MainValue +implements Serializable +{ + private Integer id; + + private Gauge gauge; + + private NamedMainValue mainValue; + + private BigDecimal value; + + private TimeInterval timeInterval; + + public MainValue() { + } + + @Id + @SequenceGenerator( + name = "SEQUENCE_MAIN_VALUES_ID_SEQ", + sequenceName = "MAIN_VALUES_ID_SEQ", + allocationSize = 1) + @GeneratedValue( + strategy = GenerationType.SEQUENCE, + generator = "SEQUENCE_MAIN_VALUES_ID_SEQ") + @Column(name = "id") + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + @OneToOne + @JoinColumn(name = "gauge_id") + public Gauge getGauge() { + return gauge; + } + + public void setGauge(Gauge gauge) { + this.gauge = gauge; + } + + @OneToOne + @JoinColumn(name = "named_value_id") + public NamedMainValue getMainValue() { + return mainValue; + } + + public void setMainValue(NamedMainValue mainValue) { + this.mainValue = mainValue; + } + + @Column(name = "value") // FIXME: type mapping needed? + public BigDecimal getValue() { + return value; + } + + public void setValue(BigDecimal value) { + this.value = value; + } + + @OneToOne + @JoinColumn(name = "time_interval_id") + public TimeInterval getTimeInterval() { + return timeInterval; + } + + public void setTimeInterval(TimeInterval timeInterval) { + this.timeInterval = timeInterval; + } +} +// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/flys-backend/src/main/java/de/intevation/flys/model/MainValueType.java Fri Mar 18 12:10:33 2011 +0000 +++ b/flys-backend/src/main/java/de/intevation/flys/model/MainValueType.java Fri Mar 18 16:12:24 2011 +0000 @@ -17,12 +17,16 @@ public class MainValueType implements Serializable { - private Integer id; - private BigDecimal value; + private Integer id; + private String name; public MainValueType() { } + public MainValueType(String name) { + this.name = name; + } + @Id @SequenceGenerator( name = "SEQUENCE_MAIN_VALUE_TYPES_ID_SEQ", @@ -40,13 +44,13 @@ this.id = id; } - @Column(name = "value") // FIXME: Type conversion needed? - public BigDecimal getValue() { - return value; + @Column(name = "name") // FIXME: Type conversion needed? + public String getName() { + return name; } - public void setValue(BigDecimal value) { - this.value = value; + public void setName(String name) { + this.name = name; } } // 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/model/NamedMainValue.java Fri Mar 18 16:12:24 2011 +0000 @@ -0,0 +1,63 @@ +package de.intevation.flys.model; + +import java.io.Serializable; + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; +import javax.persistence.GeneratedValue; +import javax.persistence.Column; +import javax.persistence.SequenceGenerator; +import javax.persistence.GenerationType; +import javax.persistence.OneToOne; +import javax.persistence.JoinColumn; + +@Entity +@Table(name = "named_main_values") +public class NamedMainValue +implements Serializable +{ + private Integer id; + private String name; + private MainValueType type; + + public NamedMainValue() { + } + + @Id + @SequenceGenerator( + name = "SEQUENCE_NAMED_MAIN_VALUES_ID_SEQ", + sequenceName = "NAMED_MAIN_VALUES_ID_SEQ", + allocationSize = 1) + @GeneratedValue( + strategy = GenerationType.SEQUENCE, + generator = "SEQUENCE_NAMED_MAIN_VALUES_ID_SEQ") + @Column(name = "id") + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + @Column(name = "name") + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + @OneToOne + @JoinColumn(name = "type_id" ) + public MainValueType getType() { + return type; + } + + public void setType(MainValueType type) { + this.type = type; + } +} +// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/flys-backend/src/main/java/de/intevation/flys/model/NamedMainValues.java Fri Mar 18 12:10:33 2011 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,63 +0,0 @@ -package de.intevation.flys.model; - -import java.io.Serializable; - -import javax.persistence.Entity; -import javax.persistence.Id; -import javax.persistence.Table; -import javax.persistence.GeneratedValue; -import javax.persistence.Column; -import javax.persistence.SequenceGenerator; -import javax.persistence.GenerationType; -import javax.persistence.OneToOne; -import javax.persistence.JoinColumn; - -@Entity -@Table(name = "named_main_values") -public class NamedMainValues -implements Serializable -{ - private Integer id; - private String name; - private MainValueType type; - - public NamedMainValues() { - } - - @Id - @SequenceGenerator( - name = "SEQUENCE_NAMED_MAIN_VALUES_ID_SEQ", - sequenceName = "NAMED_MAIN_VALUES_ID_SEQ", - allocationSize = 1) - @GeneratedValue( - strategy = GenerationType.SEQUENCE, - generator = "SEQUENCE_NAMED_MAIN_VALUES_ID_SEQ") - @Column(name = "id") - public Integer getId() { - return id; - } - - public void setId(Integer id) { - this.id = id; - } - - @Column(name = "name") - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - @OneToOne - @JoinColumn(name = "type_id" ) - public MainValueType getType() { - return type; - } - - public void setType(MainValueType type) { - this.type = type; - } -} -// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/flys-backend/src/main/java/de/intevation/flys/model/Range.java Fri Mar 18 12:10:33 2011 +0000 +++ b/flys-backend/src/main/java/de/intevation/flys/model/Range.java Fri Mar 18 16:12:24 2011 +0000 @@ -45,13 +45,6 @@ this.id = id; } - public Range(Double a, Double b, River river) { - this( - a != null ? BigDecimal.valueOf(a) : null, - b != null ? BigDecimal.valueOf(b) : null, - river); - } - public Range(BigDecimal a, BigDecimal b, River river) { this.a = a; this.b = b;