# HG changeset patch # User Ingo Weinzierl # Date 1334819725 0 # Node ID 75446c47ef77608082d53ba10c51fb99b3a250f6 # Parent 8b910e3277b0fa7c7601e598e22c3a91fbb6e1c2 Added new importer classes for importing sediment yield data. flys-backend/trunk@4271 c6561f87-3c4e-4783-a992-168aeb5c3f6f diff -r 8b910e3277b0 -r 75446c47ef77 flys-backend/ChangeLog --- a/flys-backend/ChangeLog Wed Apr 18 12:54:51 2012 +0000 +++ b/flys-backend/ChangeLog Thu Apr 19 07:15:25 2012 +0000 @@ -1,3 +1,10 @@ +2012-04-19 Ingo Weinzierl + + * src/main/java/de/intevation/flys/importer/ImportGrainFraction.java, + src/main/java/de/intevation/flys/importer/ImportSedimentYieldValue.java, + src/main/java/de/intevation/flys/importer/ImportSedimentYield.java: New + importer classes for importing sediment yield data. + 2012-04-18 Ingo Weinzierl * src/main/java/de/intevation/flys/model/SedimentYield.java, diff -r 8b910e3277b0 -r 75446c47ef77 flys-backend/src/main/java/de/intevation/flys/importer/ImportGrainFraction.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/flys-backend/src/main/java/de/intevation/flys/importer/ImportGrainFraction.java Thu Apr 19 07:15:25 2012 +0000 @@ -0,0 +1,84 @@ +package de.intevation.flys.importer; + +import java.util.List; + +import org.apache.log4j.Logger; + +import org.hibernate.Session; +import org.hibernate.Query; + +import de.intevation.flys.model.GrainFraction; +import de.intevation.flys.model.Unit; + + +public class ImportGrainFraction { + + private static final Logger log = + Logger.getLogger(ImportGrainFraction.class); + + private String name; + + private Double lower; + private Double upper; + + private ImportUnit unit; + + private GrainFraction peer; + + + public ImportGrainFraction( + String name, + Double lower, + Double upper, + ImportUnit unit + ) { + this.name = name; + this.lower = lower; + this.upper = upper; + this.unit = unit; + } + + + public void storeDependencies() { + log.debug("store dependencies"); + + getPeer(); + } + + + public GrainFraction getPeer() { + log.debug("get peer"); + + Unit u = unit != null ? unit.getPeer() : null; + + if (peer == null) { + Session session = ImporterSession.getInstance().getDatabaseSession(); + Query query = session.createQuery( + "from GrainFraction where " + + " name=:name and " + + " lower=:lower and " + + " upper=:upper and " + + " unit=:unit" + ); + + query.setParameter("name", name); + query.setParameter("lower", lower); + query.setParameter("upper", upper); + query.setParameter("unit", u); + + List fractions = query.list(); + if (fractions.isEmpty()) { + log.info("create new GrainFraction"); + + peer = new GrainFraction(name, lower, upper, u); + session.save(peer); + } + else { + peer = fractions.get(0); + } + } + + return peer; + } +} +// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : diff -r 8b910e3277b0 -r 75446c47ef77 flys-backend/src/main/java/de/intevation/flys/importer/ImportSedimentYield.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/flys-backend/src/main/java/de/intevation/flys/importer/ImportSedimentYield.java Thu Apr 19 07:15:25 2012 +0000 @@ -0,0 +1,124 @@ +package de.intevation.flys.importer; + +import java.util.ArrayList; +import java.util.List; + +import org.apache.log4j.Logger; + +import org.hibernate.Session; +import org.hibernate.Query; + +import de.intevation.flys.model.GrainFraction; +import de.intevation.flys.model.River; +import de.intevation.flys.model.SedimentYield; +import de.intevation.flys.model.TimeInterval; +import de.intevation.flys.model.Unit; + + +public class ImportSedimentYield { + + private static Logger log = Logger.getLogger(ImportSedimentYield.class); + + private ImportGrainFraction grainFraction; + + private ImportUnit unit; + + private ImportTimeInterval timeInterval; + + private List values; + + private SedimentYield peer; + + + public ImportSedimentYield() { + this.values = new ArrayList(); + } + + + public void setTimeInterval(ImportTimeInterval timeInterval) { + this.timeInterval = timeInterval; + } + + + public void setUnit(ImportUnit unit) { + this.unit = unit; + } + + + public void setGrainFraction(ImportGrainFraction grainFraction) { + this.grainFraction = grainFraction; + } + + + public void addValue(ImportSedimentYieldValue value) { + this.values.add(value); + } + + + public void storeDependencies(River river) { + log.debug("store dependencies"); + + if (grainFraction != null) { + grainFraction.storeDependencies(); + } + + SedimentYield peer = getPeer(river); + + int i = 0; + + for (ImportSedimentYieldValue value: values) { + value.storeDependencies(peer); + i++; + } + + log.info("stored " + i + " sediment yield values."); + } + + + public SedimentYield getPeer(River river) { + log.debug("get peer"); + + GrainFraction gf = grainFraction != null + ? grainFraction.getPeer() + : null; + + Unit u = unit != null ? unit.getPeer() : null; + + TimeInterval ti = timeInterval != null ? timeInterval.getPeer() : null; + + if (ti == null || u == null) { + log.warn("Skip invalid SedimentYield: time interval or unit null!"); + return null; + } + + if (peer == null) { + Session session = ImporterSession.getInstance().getDatabaseSession(); + Query query = session.createQuery( + "from SedimentYield where " + + " river=:river and " + + " grainFraction=:grainFraction and " + + " unit=:unit and " + + " timeInterval=:timeInterval" + ); + + query.setParameter("river", river); + query.setParameter("grainFraction", gf); + query.setParameter("unit", u); + query.setParameter("timeInterval", ti); + + List yields = query.list(); + if (yields.isEmpty()) { + log.debug("create new SedimentYield"); + + peer = new SedimentYield(river, u, ti, gf); + session.save(peer); + } + else { + peer = yields.get(0); + } + } + + return peer; + } +} +// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : diff -r 8b910e3277b0 -r 75446c47ef77 flys-backend/src/main/java/de/intevation/flys/importer/ImportSedimentYieldValue.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/flys-backend/src/main/java/de/intevation/flys/importer/ImportSedimentYieldValue.java Thu Apr 19 07:15:25 2012 +0000 @@ -0,0 +1,58 @@ +package de.intevation.flys.importer; + +import java.util.List; + +import org.hibernate.Session; +import org.hibernate.Query; + +import de.intevation.flys.model.SedimentYield; +import de.intevation.flys.model.SedimentYieldValue; + + +public class ImportSedimentYieldValue { + + private Double station; + private Double value; + + private SedimentYieldValue peer; + + + public ImportSedimentYieldValue(Double station, Double value) { + this.station = station; + this.value = value; + } + + + public void storeDependencies(SedimentYield sedimentYield) { + getPeer(sedimentYield); + } + + + public SedimentYieldValue getPeer(SedimentYield sedimentYield) { + if (peer == null) { + Session session = ImporterSession.getInstance().getDatabaseSession(); + Query query = session.createQuery( + "from SedimentYieldValue where " + + " sedimentYield=:sedimentYield and " + + " station=:station and " + + " value=:value" + ); + + query.setParameter("sedimentYield", sedimentYield); + query.setParameter("station", station); + query.setParameter("value", value); + + List values = query.list(); + if (values.isEmpty()) { + peer = new SedimentYieldValue(sedimentYield, station, value); + session.save(peer); + } + else { + peer = values.get(0); + } + } + + return peer; + } +} +// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :