# HG changeset patch # User Sascha L. Teichmann # Date 1300450233 0 # Node ID 003ac16812dd5b9f525428247d2312bda2e74563 # Parent ecf90018563b75f725e0a1dc6648abcbcbdd497d Store annotations in backend. flys-backend/trunk@1518 c6561f87-3c4e-4783-a992-168aeb5c3f6f diff -r ecf90018563b -r 003ac16812dd flys-backend/ChangeLog --- a/flys-backend/ChangeLog Thu Mar 17 18:07:13 2011 +0000 +++ b/flys-backend/ChangeLog Fri Mar 18 12:10:33 2011 +0000 @@ -1,3 +1,29 @@ +2011-03-17 Sascha L. Teichmann + + Store annotations in backend. + + * src/main/java/de/intevation/flys/model/Annotation.java: + New convinience constructor. + + * src/main/java/de/intevation/flys/model/River.java: + Added toString() method. + + * src/main/java/de/intevation/flys/model/Range.java: + Fixed nasty mistake in @OneToOne annotatation. + New convinience constructors. + + * src/main/java/de/intevation/flys/importer/ImportPosition.java + src/main/java/de/intevation/flys/importer/ImportAnnotation.java, + src/main/java/de/intevation/flys/importer/ImportRange.java + src/main/java/de/intevation/flys/importer/ImportAttribute.java: + Make storing to backend work. It's a bit too slow. :-/ + + * src/main/java/de/intevation/flys/importer/ImportRiver.java: + Fetch peer from backend. Added method to store annotations. + * src/main/java/de/intevation/flys/importer/Importer.java: + Stored annotations into backend. More eloquent SQL exception + handling. + 2011-03-17 Sascha L. Teichmann * src/main/java/de/intevation/flys/model/Attribute.java, diff -r ecf90018563b -r 003ac16812dd flys-backend/src/main/java/de/intevation/flys/importer/ImportAnnotation.java --- a/flys-backend/src/main/java/de/intevation/flys/importer/ImportAnnotation.java Thu Mar 17 18:07:13 2011 +0000 +++ b/flys-backend/src/main/java/de/intevation/flys/importer/ImportAnnotation.java Fri Mar 18 12:10:33 2011 +0000 @@ -1,5 +1,16 @@ package de.intevation.flys.importer; +import de.intevation.flys.model.Annotation; +import de.intevation.flys.model.Range; +import de.intevation.flys.model.Position; +import de.intevation.flys.model.Attribute; +import de.intevation.flys.model.River; + +import org.hibernate.Session; +import org.hibernate.Query; + +import java.util.List; + public class ImportAnnotation implements Comparable { @@ -7,6 +18,8 @@ protected ImportPosition position; protected ImportRange range; + protected Annotation peer; + public ImportAnnotation() { } @@ -60,5 +73,29 @@ public void setRange(ImportRange range) { this.range = range; } + + public Annotation getPeer(River river) { + if (peer == null) { + Range r = range.getPeer(river); + Attribute a = attribute.getPeer(); + Position p = position.getPeer(); + Session session = Importer.sessionHolder.get(); + Query query = session.createQuery( + "from Annotation where " + + "range=:range and attribute=:attribute and position=:position"); + query.setParameter("range", r); + query.setParameter("attribute", a); + query.setParameter("position", p); + List annotations = query.list(); + if (annotations.isEmpty()) { + peer = new Annotation(r, a, p); + session.save(peer); + } + else { + peer = annotations.get(0); + } + } + return peer; + } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : diff -r ecf90018563b -r 003ac16812dd flys-backend/src/main/java/de/intevation/flys/importer/ImportAttribute.java --- a/flys-backend/src/main/java/de/intevation/flys/importer/ImportAttribute.java Thu Mar 17 18:07:13 2011 +0000 +++ b/flys-backend/src/main/java/de/intevation/flys/importer/ImportAttribute.java Fri Mar 18 12:10:33 2011 +0000 @@ -46,7 +46,7 @@ } public Attribute getPeer() { - if (peer != null) { + if (peer == null) { Session session = Importer.sessionHolder.get(); Query query = session.createQuery("from Attribute where value=:value"); query.setString("value", value); diff -r ecf90018563b -r 003ac16812dd flys-backend/src/main/java/de/intevation/flys/importer/ImportPosition.java --- a/flys-backend/src/main/java/de/intevation/flys/importer/ImportPosition.java Thu Mar 17 18:07:13 2011 +0000 +++ b/flys-backend/src/main/java/de/intevation/flys/importer/ImportPosition.java Fri Mar 18 12:10:33 2011 +0000 @@ -34,7 +34,7 @@ } public Position getPeer() { - if (peer != null) { + if (peer == null) { Session session = Importer.sessionHolder.get(); Query query = session.createQuery("from Position where value=:value"); query.setString("value", value); diff -r ecf90018563b -r 003ac16812dd flys-backend/src/main/java/de/intevation/flys/importer/ImportRange.java --- a/flys-backend/src/main/java/de/intevation/flys/importer/ImportRange.java Thu Mar 17 18:07:13 2011 +0000 +++ b/flys-backend/src/main/java/de/intevation/flys/importer/ImportRange.java Fri Mar 18 12:10:33 2011 +0000 @@ -1,10 +1,26 @@ package de.intevation.flys.importer; +import de.intevation.flys.model.Range; +import de.intevation.flys.model.River; + +import org.hibernate.Session; +import org.hibernate.Query; + +import java.util.List; + +import java.math.BigDecimal; + +import org.apache.log4j.Logger; + public class ImportRange implements Comparable { - public Double from; - public Double to; + private static Logger log = Logger.getLogger(ImportRange.class); + + protected Double from; + protected Double to; + + protected Range peer; public ImportRange() { } @@ -48,5 +64,27 @@ public void setTo(Double to) { this.to = to; } + + public Range getPeer(River river) { + if (peer == null) { + 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()); + List ranges = query.list(); + if (ranges.isEmpty()) { + peer = new Range(a, b, river); + session.save(peer); + } + else { + peer = ranges.get(0); + } + } + return peer; + } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : diff -r ecf90018563b -r 003ac16812dd flys-backend/src/main/java/de/intevation/flys/importer/ImportRiver.java --- a/flys-backend/src/main/java/de/intevation/flys/importer/ImportRiver.java Thu Mar 17 18:07:13 2011 +0000 +++ b/flys-backend/src/main/java/de/intevation/flys/importer/ImportRiver.java Fri Mar 18 12:10:33 2011 +0000 @@ -7,8 +7,13 @@ import org.apache.log4j.Logger; +import de.intevation.flys.model.River; + import de.intevation.flys.utils.FileTools; +import org.hibernate.Session; +import org.hibernate.Query; + public class ImportRiver { private static Logger log = Logger.getLogger(ImportRiver.class); @@ -25,6 +30,8 @@ protected List annotations; + protected River peer; + public ImportRiver() { } @@ -85,5 +92,29 @@ annotations = aparser.getAnnotations(); } + + public void storeAnnotations() { + River river = getPeer(); + for (ImportAnnotation annotation: annotations) { + annotation.getPeer(river); + } + } + + public River getPeer() { + if (peer == null) { + Session session = Importer.sessionHolder.get(); + Query query = session.createQuery("from River where name=:name"); + query.setString("name", name); + List rivers = query.list(); + if (rivers.isEmpty()) { + peer = new River(name); + session.save(peer); + } + else { + peer = rivers.get(0); + } + } + return peer; + } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : diff -r ecf90018563b -r 003ac16812dd flys-backend/src/main/java/de/intevation/flys/importer/Importer.java --- a/flys-backend/src/main/java/de/intevation/flys/importer/Importer.java Thu Mar 17 18:07:13 2011 +0000 +++ b/flys-backend/src/main/java/de/intevation/flys/importer/Importer.java Fri Mar 18 12:10:33 2011 +0000 @@ -5,12 +5,15 @@ import java.util.List; +import java.sql.SQLException; + import org.apache.log4j.Logger; 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; @@ -42,18 +45,15 @@ public void writeRivers() { log.debug("write rivers started"); + Session session = sessionHolder.get(); - Query query = session.createQuery("from River where name=:name"); - for (ImportRiver iriver: rivers) { - log.debug("writing river '" + iriver.getName() + "'"); - query.setString("name", iriver.getName()); - List dbrivers = (List)query.list(); - if (dbrivers.isEmpty()) { - River dbriver = new River(iriver.getName()); - session.save(dbriver); - } + for (ImportRiver river: rivers) { + log.debug("writing river '" + river.getName() + "'"); + river.storeAnnotations(); + session.flush(); } + log.debug("write rivers finished"); } @@ -70,7 +70,18 @@ try { tx = session.beginTransaction(); - writeRivers(); + try { + writeRivers(); + } + catch (HibernateException he) { + Throwable t = he.getCause(); + while (t instanceof SQLException) { + SQLException sqle = (SQLException)t; + log.error("SQL exeception chain:", sqle); + t = sqle.getNextException(); + } + throw he; + } tx.commit(); } diff -r ecf90018563b -r 003ac16812dd flys-backend/src/main/java/de/intevation/flys/model/Annotation.java --- a/flys-backend/src/main/java/de/intevation/flys/model/Annotation.java Thu Mar 17 18:07:13 2011 +0000 +++ b/flys-backend/src/main/java/de/intevation/flys/model/Annotation.java Fri Mar 18 12:10:33 2011 +0000 @@ -25,6 +25,12 @@ public Annotation() { } + public Annotation(Range range, Attribute attribute, Position position) { + this.range = range; + this.attribute = attribute; + this.position = position; + } + @Id @SequenceGenerator( name = "SEQUENCE_ANNOTATIONS_ID_SEQ", diff -r ecf90018563b -r 003ac16812dd flys-backend/src/main/java/de/intevation/flys/model/Range.java --- a/flys-backend/src/main/java/de/intevation/flys/model/Range.java Thu Mar 17 18:07:13 2011 +0000 +++ b/flys-backend/src/main/java/de/intevation/flys/model/Range.java Fri Mar 18 12:10:33 2011 +0000 @@ -45,6 +45,19 @@ 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; + this.river = river; + } + @Column(name = "a") // FIXME: type mapping needed? public BigDecimal getA() { return a; @@ -63,14 +76,14 @@ this.b = b; } + @OneToOne + @JoinColumn(name = "river_id") + public River getRiver() { + return river; + } + public void setRiver(River river) { this.river = river; } - - @OneToOne - @JoinColumn(name = "id") - public River getRiver() { - return river; - } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : diff -r ecf90018563b -r 003ac16812dd flys-backend/src/main/java/de/intevation/flys/model/River.java --- a/flys-backend/src/main/java/de/intevation/flys/model/River.java Thu Mar 17 18:07:13 2011 +0000 +++ b/flys-backend/src/main/java/de/intevation/flys/model/River.java Fri Mar 18 12:10:33 2011 +0000 @@ -67,5 +67,9 @@ public void setGauges(List gauges) { this.gauges = gauges; } + + public String toString() { + return name != null ? name : ""; + } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :