Mercurial > dive4elements > river
changeset 8986:392bbcd8a88b
Database inserts accelerated by suppressing unnecessary database queries for new data series
line wrap: on
line diff
--- a/backend/src/main/java/org/dive4elements/river/importer/ImportBedHeight.java Fri Apr 06 14:13:14 2018 +0200 +++ b/backend/src/main/java/org/dive4elements/river/importer/ImportBedHeight.java Sun Apr 08 18:07:06 2018 +0200 @@ -12,6 +12,7 @@ import java.util.List; import org.apache.log4j.Logger; +import org.dive4elements.river.importer.common.StoreMode; import org.dive4elements.river.model.BedHeight; import org.dive4elements.river.model.BedHeightType; import org.dive4elements.river.model.ElevationModel; @@ -40,12 +41,15 @@ protected List<ImportBedHeightValue> values; + protected StoreMode storeMode; + protected BedHeight peer; public ImportBedHeight(final String description) { this.description = description; this.values = new ArrayList<>(); + this.storeMode = StoreMode.NONE; } @@ -178,9 +182,11 @@ (this.oldElevationModel != null) ? this.oldElevationModel.getPeer() : null, this.range.getPeer(river), this.evaluationBy, this.description, this.sounding_width_info, this.comment); session.save(this.peer); + this.storeMode = StoreMode.INSERT; } else { this.peer = bedHeights.get(0); + this.storeMode = StoreMode.UPDATE; } return this.peer;
--- a/backend/src/main/java/org/dive4elements/river/importer/ImportBedHeightValue.java Fri Apr 06 14:13:14 2018 +0200 +++ b/backend/src/main/java/org/dive4elements/river/importer/ImportBedHeightValue.java Sun Apr 08 18:07:06 2018 +0200 @@ -11,6 +11,7 @@ import java.util.List; import org.apache.log4j.Logger; +import org.dive4elements.river.importer.common.StoreMode; import org.dive4elements.river.model.BedHeight; import org.dive4elements.river.model.BedHeightValue; import org.hibernate.Query; @@ -61,13 +62,18 @@ if (this.peer != null) return this.peer; + List<BedHeightValue> values; final Session session = ImporterSession.getInstance().getDatabaseSession(); - final Query query = session.createQuery("FROM BedHeightValue WHERE (bedHeight=:bedHeight)" - + " AND (station BETWEEN (:station-0.0001) AND (:station+0.0001))"); - query.setParameter("bedHeight", bedHeight); - query.setParameter("station", this.station); - final List<BedHeightValue> values = query.list(); - if (values.isEmpty()) { + if (this.bedHeight.storeMode == StoreMode.INSERT) + values = null; + else { + final Query query = session.createQuery("FROM BedHeightValue WHERE (bedHeight=:bedHeight)" + + " AND (station BETWEEN (:station-0.0001) AND (:station+0.0001))"); + query.setParameter("bedHeight", bedHeight); + query.setParameter("station", this.station); + values = query.list(); + } + if ((values == null) || values.isEmpty()) { this.peer = new BedHeightValue(bedHeight, this.station, this.height, this.uncertainty, this.dataGap, this.soundingWidth, this.minHeight, this.maxHeight); session.save(this.peer);
--- a/backend/src/main/java/org/dive4elements/river/importer/ImportCrossSection.java Fri Apr 06 14:13:14 2018 +0200 +++ b/backend/src/main/java/org/dive4elements/river/importer/ImportCrossSection.java Sun Apr 08 18:07:06 2018 +0200 @@ -8,16 +8,15 @@ package org.dive4elements.river.importer; -import org.dive4elements.river.model.River; -import org.dive4elements.river.model.CrossSection; -import org.dive4elements.river.model.TimeInterval; - -import org.hibernate.Session; -import org.hibernate.Query; - import java.util.List; import org.apache.log4j.Logger; +import org.dive4elements.river.importer.common.StoreMode; +import org.dive4elements.river.model.CrossSection; +import org.dive4elements.river.model.River; +import org.dive4elements.river.model.TimeInterval; +import org.hibernate.Query; +import org.hibernate.Session; /** CrossSection to be imported, holds list of ImportCrossSectionLines. */ public class ImportCrossSection @@ -28,6 +27,7 @@ protected String description; protected ImportTimeInterval timeInterval; protected List<ImportCrossSectionLine> lines; + protected StoreMode storeMode; protected CrossSection peer; @@ -35,94 +35,97 @@ } public ImportCrossSection( - ImportRiver river, - String description, - ImportTimeInterval timeInterval, - List<ImportCrossSectionLine> lines - ) { + final ImportRiver river, + final String description, + final ImportTimeInterval timeInterval, + final List<ImportCrossSectionLine> lines + ) { this.river = river; this.description = description; this.timeInterval = timeInterval; this.lines = lines; + this.storeMode = StoreMode.NONE; wireWithLines(); } public void wireWithLines() { - for (ImportCrossSectionLine line: lines) { + for (final ImportCrossSectionLine line: this.lines) { line.setCrossSection(this); } } public ImportRiver getRiver() { - return river; + return this.river; } - public void setRiver(ImportRiver river) { + public void setRiver(final ImportRiver river) { this.river = river; } public String getDescription() { - return description; + return this.description; } - public void setDescription(String description) { + public void setDescription(final String description) { this.description = description; } public ImportTimeInterval getTimeInterval() { - return timeInterval; + return this.timeInterval; } - public void setTimeInterval(ImportTimeInterval timeInterval) { + public void setTimeInterval(final ImportTimeInterval timeInterval) { this.timeInterval = timeInterval; } public void storeDependencies() { - log.info("store cross section '" + description + "'"); + log.info("store cross section '" + this.description + "'"); getPeer(); - int i = 1, N = lines.size(); + // int i = 1; + // final int N = this.lines.size(); - for (ImportCrossSectionLine line: lines) { + for (final ImportCrossSectionLine line: this.lines) { line.storeDependencies(); - log.info(" stored " + i + " lines. remaining: " + (N-i)); - ++i; + // log.info(" stored " + i + " lines. remaining: " + (N-i)); + // ++i; } + log.info(" lines stored: " + this.lines.size()); } public CrossSection getPeer() { - if (peer == null) { - River r = river.getPeer(); - TimeInterval t = timeInterval != null - ? timeInterval.getPeer() - : null; + if (this.peer == null) { + final River r = this.river.getPeer(); + final TimeInterval t = (this.timeInterval != null) ? this.timeInterval.getPeer() : null; - Session session = - ImporterSession.getInstance().getDatabaseSession(); + final Session session = + ImporterSession.getInstance().getDatabaseSession(); - Query query = session.createQuery( - "from CrossSection where " + - "river=:r and " + - "timeInterval=:t and " + - "description=:d"); + final Query query = session.createQuery( + "from CrossSection where " + + "river=:r and " + + "timeInterval=:t and " + + "description=:d"); query.setParameter("r", r); query.setParameter("t", t); - query.setParameter("d", description); + query.setParameter("d", this.description); - List<CrossSection> crossSections = query.list(); + final List<CrossSection> crossSections = query.list(); if (crossSections.isEmpty()) { - peer = new CrossSection(r, t, description); - session.save(peer); + this.peer = new CrossSection(r, t, this.description); + session.save(this.peer); + this.storeMode = StoreMode.INSERT; } else { - peer = crossSections.get(0); + this.peer = crossSections.get(0); + this.storeMode = StoreMode.UPDATE; } } - return peer; + return this.peer; } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/backend/src/main/java/org/dive4elements/river/importer/ImportCrossSectionLine.java Fri Apr 06 14:13:14 2018 +0200 +++ b/backend/src/main/java/org/dive4elements/river/importer/ImportCrossSectionLine.java Sun Apr 08 18:07:06 2018 +0200 @@ -8,18 +8,18 @@ package org.dive4elements.river.importer; -import org.dive4elements.river.model.CrossSection; -import org.dive4elements.river.model.CrossSectionPoint; -import org.dive4elements.river.model.CrossSectionLine; - -import org.hibernate.Session; -import org.hibernate.Query; - +import java.util.Comparator; import java.util.List; -import java.util.Comparator; import java.util.Map; import java.util.TreeMap; +import org.dive4elements.river.importer.common.StoreMode; +import org.dive4elements.river.model.CrossSection; +import org.dive4elements.river.model.CrossSectionLine; +import org.dive4elements.river.model.CrossSectionPoint; +import org.hibernate.Query; +import org.hibernate.Session; + /** * A CrossSectionLine (containing points) ready to be transformed into a mapped * object and written to db (used in importer). @@ -27,39 +27,42 @@ public class ImportCrossSectionLine { public static final Comparator<CrossSectionPoint> INDEX_CMP = - new Comparator<CrossSectionPoint>() { - public int compare(CrossSectionPoint a, CrossSectionPoint b) { - return a.getColPos().compareTo(b.getColPos()); - } - }; + new Comparator<CrossSectionPoint>() { + @Override + public int compare(final CrossSectionPoint a, final CrossSectionPoint b) { + return a.getColPos().compareTo(b.getColPos()); + } + }; protected Double km; protected ImportCrossSection crossSection; protected List<XY> points; + protected StoreMode storeMode; protected CrossSectionLine peer; public ImportCrossSectionLine() { } - public ImportCrossSectionLine(Double km, List<XY> points) { + public ImportCrossSectionLine(final Double km, final List<XY> points) { this.km = km; this.points = points; + this.storeMode = StoreMode.NONE; } public ImportCrossSection getCrossSection() { - return crossSection; + return this.crossSection; } - public void setCrossSection(ImportCrossSection crossSection) { + public void setCrossSection(final ImportCrossSection crossSection) { this.crossSection = crossSection; } public Double getKm() { - return km; + return this.km; } - public void setKm(Double km) { + public void setKm(final Double km) { this.km = km; } @@ -70,35 +73,37 @@ /** Write a line and its points. */ protected void storePoints() { - CrossSectionLine csl = getPeer(); - - Map<CrossSectionPoint, CrossSectionPoint> map = - new TreeMap<CrossSectionPoint, CrossSectionPoint>(INDEX_CMP); + final CrossSectionLine csl = getPeer(); + if (this.storeMode == StoreMode.INSERT) { + insertPoints(); + return; + } + final Map<CrossSectionPoint, CrossSectionPoint> map = + new TreeMap<>(INDEX_CMP); // Build index for faster (index) collision lookup. - List<CrossSectionPoint> ps = csl.getPoints(); + final List<CrossSectionPoint> ps = csl.getPoints(); if (ps != null) { - for (CrossSectionPoint point: ps) { + for (final CrossSectionPoint point: ps) { map.put(point, point); } } - Session session = - ImporterSession.getInstance().getDatabaseSession(); + final Session session = ImporterSession.getInstance().getDatabaseSession(); - CrossSectionPoint key = new CrossSectionPoint(); + final CrossSectionPoint key = new CrossSectionPoint(); // Somehow it looks as if even with the map it is still possible that // multiple points with same id enter hibernate (and then violate a // constraint). -> TODO - for (XY xy: points) { + for (final XY xy: this.points) { key.setColPos(xy.getIndex()); CrossSectionPoint csp = map.get(key); if (csp == null) { // create new csp = new CrossSectionPoint( - csl, key.getColPos(), - Double.valueOf(xy.getX()), - Double.valueOf(xy.getY())); + csl, key.getColPos(), + Double.valueOf(xy.getX()), + Double.valueOf(xy.getY())); } else { // update old csp.setX(Double.valueOf(xy.getX())); @@ -108,29 +113,41 @@ } } + /** + * Insert the points of a new line into the database without previously querying the database + */ + private void insertPoints() { + final Session session = ImporterSession.getInstance().getDatabaseSession(); + for (final XY xy : this.points) { + session.save(new CrossSectionPoint(getPeer(), xy.getIndex(), xy.getX(), xy.getY())); + } + } + /** Pull database-mapped object from db, or create (and save) one. */ public CrossSectionLine getPeer() { - if (peer == null) { - CrossSection cs = crossSection.getPeer(); - - Session session = - ImporterSession.getInstance().getDatabaseSession(); - - Query query = session.createQuery( - "from CrossSectionLine where crossSection=:cs and km=:km"); - query.setParameter("cs", cs); - query.setParameter("km", km); - - List<CrossSectionLine> lines = query.list(); - if (lines.isEmpty()) { - peer = new CrossSectionLine(cs, km); - session.save(peer); + if (this.peer == null) { + final CrossSection cs = this.crossSection.getPeer(); + final Session session = ImporterSession.getInstance().getDatabaseSession(); + List<CrossSectionLine> lines; + if (this.crossSection.storeMode == StoreMode.INSERT) + lines = null; + else { + final Query query = session.createQuery("from CrossSectionLine where crossSection=:cs and km=:km"); + query.setParameter("cs", cs); + query.setParameter("km", this.km); + lines = query.list(); + } + if ((lines == null) || lines.isEmpty()) { + this.peer = new CrossSectionLine(cs, this.km); + session.save(this.peer); + this.storeMode = StoreMode.INSERT; } else { - peer = lines.get(0); + this.peer = lines.get(0); + this.storeMode = StoreMode.UPDATE; } } - return peer; + return this.peer; } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/backend/src/main/java/org/dive4elements/river/importer/ImportFlowVelocityModel.java Fri Apr 06 14:13:14 2018 +0200 +++ b/backend/src/main/java/org/dive4elements/river/importer/ImportFlowVelocityModel.java Sun Apr 08 18:07:06 2018 +0200 @@ -12,75 +12,75 @@ import java.util.List; import org.apache.log4j.Logger; - -import org.hibernate.Session; -import org.hibernate.Query; - +import org.dive4elements.river.importer.common.StoreMode; import org.dive4elements.river.model.DischargeZone; import org.dive4elements.river.model.FlowVelocityModel; import org.dive4elements.river.model.River; +import org.hibernate.Query; +import org.hibernate.Session; public class ImportFlowVelocityModel { private static final Logger log = Logger - .getLogger(ImportFlowVelocityModel.class); + .getLogger(ImportFlowVelocityModel.class); private String description; private ImportDischargeZone dischargeZone; - private List<ImportFlowVelocityModelValue> values; + private final List<ImportFlowVelocityModelValue> values; + + protected StoreMode storeMode; private FlowVelocityModel peer; public ImportFlowVelocityModel() { - values = new ArrayList<ImportFlowVelocityModelValue>(); + this.values = new ArrayList<>(); + this.storeMode = StoreMode.NONE; } - public ImportFlowVelocityModel(String description) { + public ImportFlowVelocityModel(final String description) { this(); - this.description = description; } - public ImportFlowVelocityModel(ImportDischargeZone dischargeZone, - String description) { + public ImportFlowVelocityModel(final ImportDischargeZone dischargeZone, + final String description) { this(); - this.dischargeZone = dischargeZone; this.description = description; } - public void setDischargeZone(ImportDischargeZone dischargeZone) { + public void setDischargeZone(final ImportDischargeZone dischargeZone) { this.dischargeZone = dischargeZone; } - public void setDescription(String description) { + public void setDescription(final String description) { this.description = description; } - public void addValue(ImportFlowVelocityModelValue value) { + public void addValue(final ImportFlowVelocityModelValue value) { this.values.add(value); } - public void storeDependencies(River river) { + public void storeDependencies(final River river) { log.debug("store dependencies"); - if (dischargeZone == null) { + if (this.dischargeZone == null) { log.warn("skip flow velocity model: No discharge zone specified."); return; } - dischargeZone.storeDependencies(river); + this.dischargeZone.storeDependencies(river); - FlowVelocityModel peer = getPeer(river); + final FlowVelocityModel peer = getPeer(river); if (peer != null) { int i = 0; - for (ImportFlowVelocityModelValue value : values) { - value.storeDependencies(peer); + for (final ImportFlowVelocityModelValue value : this.values) { + value.storeDependencies(peer, this.storeMode); i++; } @@ -88,30 +88,32 @@ } } - public FlowVelocityModel getPeer(River river) { - if (peer == null) { - Session session = ImporterSession.getInstance() - .getDatabaseSession(); + public FlowVelocityModel getPeer(final River river) { + if (this.peer == null) { + final Session session = ImporterSession.getInstance() + .getDatabaseSession(); - DischargeZone zone = dischargeZone.getPeer(river); + final DischargeZone zone = this.dischargeZone.getPeer(river); - Query query = session.createQuery("from FlowVelocityModel where " - + " dischargeZone=:dischargeZone"); + final Query query = session.createQuery("from FlowVelocityModel where " + + " dischargeZone=:dischargeZone"); query.setParameter("dischargeZone", zone); - List<FlowVelocityModel> model = query.list(); + final List<FlowVelocityModel> model = query.list(); if (model.isEmpty()) { - peer = new FlowVelocityModel(zone, description); - session.save(peer); + this.peer = new FlowVelocityModel(zone, this.description); + session.save(this.peer); + this.storeMode = StoreMode.INSERT; } else { - peer = model.get(0); + this.peer = model.get(0); + this.storeMode = StoreMode.UPDATE; } } - return peer; + return this.peer; } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/backend/src/main/java/org/dive4elements/river/importer/ImportFlowVelocityModelValue.java Fri Apr 06 14:13:14 2018 +0200 +++ b/backend/src/main/java/org/dive4elements/river/importer/ImportFlowVelocityModelValue.java Sun Apr 08 18:07:06 2018 +0200 @@ -9,34 +9,33 @@ package org.dive4elements.river.importer; import java.math.BigDecimal; - import java.util.List; -import org.hibernate.Session; -import org.hibernate.Query; - +import org.dive4elements.river.importer.common.StoreMode; import org.dive4elements.river.model.FlowVelocityModel; import org.dive4elements.river.model.FlowVelocityModelValue; +import org.hibernate.Query; +import org.hibernate.Session; public class ImportFlowVelocityModelValue { - private BigDecimal station; - private BigDecimal q; - private BigDecimal totalChannel; - private BigDecimal mainChannel; - private BigDecimal shearStress; + private final BigDecimal station; + private final BigDecimal q; + private final BigDecimal totalChannel; + private final BigDecimal mainChannel; + private final BigDecimal shearStress; private FlowVelocityModelValue peer; public ImportFlowVelocityModelValue( - BigDecimal station, - BigDecimal q, - BigDecimal totalChannel, - BigDecimal mainChannel, - BigDecimal shearStress - ) { + final BigDecimal station, + final BigDecimal q, + final BigDecimal totalChannel, + final BigDecimal mainChannel, + final BigDecimal shearStress + ) { this.station = station; this.q = q; this.totalChannel = totalChannel; @@ -45,39 +44,41 @@ } - public void storeDependencies(FlowVelocityModel model) { - getPeer(model); + public void storeDependencies(final FlowVelocityModel model, final StoreMode parentStoreMode) { + getPeer(model, parentStoreMode); } - public FlowVelocityModelValue getPeer(FlowVelocityModel model) { - if (peer == null) { - Session session = ImporterSession.getInstance() - .getDatabaseSession(); + public FlowVelocityModelValue getPeer(final FlowVelocityModel model, final StoreMode parentStoreMode) { + if (this.peer == null) { + List<FlowVelocityModelValue> values; + final Session session = ImporterSession.getInstance().getDatabaseSession(); + if (parentStoreMode == StoreMode.INSERT) + values = null; + else { + final Query query = session.createQuery( + "from FlowVelocityModelValue where " + + " flowVelocity=:model and " + + " station between :station - 0.00001 and :station + 0.00001" + ); - Query query = session.createQuery( - "from FlowVelocityModelValue where " + - " flowVelocity=:model and " + - " station between :station - 0.00001 and :station + 0.00001" - ); + query.setParameter("model", model); + query.setParameter("station", this.station.doubleValue()); - query.setParameter("model", model); - query.setParameter("station", station.doubleValue()); - - List<FlowVelocityModelValue> values = query.list(); + values = query.list(); + } + if ((values == null) || values.isEmpty()) { + this.peer = new FlowVelocityModelValue( + model, this.station, this.q, this.totalChannel, this.mainChannel, this.shearStress); - if (values.isEmpty()) { - peer = new FlowVelocityModelValue( - model, station, q, totalChannel, mainChannel, shearStress); - - session.save(peer); + session.save(this.peer); } else { - peer = values.get(0); + this.peer = values.get(0); } } - return peer; + return this.peer; } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/backend/src/main/java/org/dive4elements/river/importer/ImportHYK.java Fri Apr 06 14:13:14 2018 +0200 +++ b/backend/src/main/java/org/dive4elements/river/importer/ImportHYK.java Sun Apr 08 18:07:06 2018 +0200 @@ -8,16 +8,15 @@ package org.dive4elements.river.importer; +import java.util.ArrayList; +import java.util.List; + +import org.apache.log4j.Logger; +import org.dive4elements.river.importer.common.StoreMode; import org.dive4elements.river.model.HYK; import org.dive4elements.river.model.River; - -import java.util.List; -import java.util.ArrayList; - +import org.hibernate.Query; import org.hibernate.Session; -import org.hibernate.Query; - -import org.apache.log4j.Logger; public class ImportHYK { @@ -28,61 +27,66 @@ protected List<ImportHYKEntry> entries; + protected StoreMode storeMode; + protected HYK peer; public ImportHYK() { - entries = new ArrayList<ImportHYKEntry>(); + this.entries = new ArrayList<>(); + this.storeMode = StoreMode.NONE; } - public ImportHYK(ImportRiver river, String description) { + public ImportHYK(final ImportRiver river, final String description) { this(); this.river = river; this.description = description; } public ImportRiver getRiver() { - return river; + return this.river; } - public void setRiver(ImportRiver river) { + public void setRiver(final ImportRiver river) { this.river = river; } - public void addEntry(ImportHYKEntry entry) { - entries.add(entry); + public void addEntry(final ImportHYKEntry entry) { + this.entries.add(entry); entry.setHYK(this); } public void storeDependencies() { - log.info("store HYK '" + description + "'"); + log.info("store HYK '" + this.description + "'"); getPeer(); - for (int i = 0, N = entries.size(); i < N; ++i) { - ImportHYKEntry entry = entries.get(i); + for (int i = 0, N = this.entries.size(); i < N; ++i) { + final ImportHYKEntry entry = this.entries.get(i); log.info(" store km " + entry.getKm() + - " (" + (i+1) + " of " + N + ")"); + " (" + (i+1) + " of " + N + ")"); entry.storeDependencies(); } } public HYK getPeer() { - if (peer == null) { - River r = river.getPeer(); - Session session = ImporterSession.getInstance() - .getDatabaseSession(); - Query query = session.createQuery( - "from HYK where river=:river and description=:description"); + if (this.peer == null) { + final River r = this.river.getPeer(); + final Session session = ImporterSession.getInstance() + .getDatabaseSession(); + final Query query = session.createQuery( + "from HYK where river=:river and description=:description"); query.setParameter("river", r); - query.setParameter("description", description); - List<HYK> hyks = query.list(); + query.setParameter("description", this.description); + final List<HYK> hyks = query.list(); if (hyks.isEmpty()) { - peer = new HYK(r, description); - session.save(peer); + this.peer = new HYK(r, this.description); + session.save(this.peer); + this.storeMode = StoreMode.INSERT; } else { - peer = hyks.get(0); + this.peer = hyks.get(0); + this.storeMode = StoreMode.UPDATE; } } - return peer; + return this.peer; } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/backend/src/main/java/org/dive4elements/river/importer/ImportHYKEntry.java Fri Apr 06 14:13:14 2018 +0200 +++ b/backend/src/main/java/org/dive4elements/river/importer/ImportHYKEntry.java Sun Apr 08 18:07:06 2018 +0200 @@ -8,17 +8,16 @@ package org.dive4elements.river.importer; -import org.dive4elements.river.model.HYKEntry; -import org.dive4elements.river.model.HYK; - +import java.math.BigDecimal; +import java.util.ArrayList; import java.util.Date; import java.util.List; -import java.util.ArrayList; -import java.math.BigDecimal; - +import org.dive4elements.river.importer.common.StoreMode; +import org.dive4elements.river.model.HYK; +import org.dive4elements.river.model.HYKEntry; +import org.hibernate.Query; import org.hibernate.Session; -import org.hibernate.Query; public class ImportHYKEntry { @@ -28,17 +27,20 @@ protected List<ImportHYKFormation> formations; + protected StoreMode storeMode; + protected HYKEntry peer; public ImportHYKEntry() { - formations = new ArrayList<ImportHYKFormation>(); + this.formations = new ArrayList<>(); + this.storeMode = StoreMode.NONE; } public ImportHYKEntry( - ImportHYK hyk, - BigDecimal km, - Date measure - ) { + final ImportHYK hyk, + final BigDecimal km, + final Date measure + ) { this(); this.hyk = hyk; this.km = km; @@ -46,56 +48,63 @@ } public ImportHYK getHYK() { - return hyk; + return this.hyk; } - public void setHYK(ImportHYK hyk) { + public void setHYK(final ImportHYK hyk) { this.hyk = hyk; } public BigDecimal getKm() { - return km; + return this.km; } - public void setKm(BigDecimal km) { + public void setKm(final BigDecimal km) { this.km = km; } - public void addFormation(ImportHYKFormation formation) { - int numFormation = formations.size(); - formations.add(formation); + public void addFormation(final ImportHYKFormation formation) { + final int numFormation = this.formations.size(); + this.formations.add(formation); formation.setFormationNum(numFormation); formation.setEntry(this); } public void storeDependencies() { getPeer(); - for (ImportHYKFormation formation: formations) { + for (final ImportHYKFormation formation: this.formations) { formation.storeDependencies(); } } public HYKEntry getPeer() { - if (peer == null) { - HYK h = hyk.getPeer(); - Session session = ImporterSession.getInstance() - .getDatabaseSession(); - Query query = session.createQuery( - "from HYKEntry where HYK=:hyk " + - "and km=:km and measure=:measure"); - query.setParameter("hyk", h); - query.setParameter("km", km); - query.setParameter("measure", measure); - List<HYKEntry> entries = query.list(); - if (entries.isEmpty()) { - peer = new HYKEntry(h, km, measure); - session.save(peer); + if (this.peer == null) { + final HYK h = this.hyk.getPeer(); + final Session session = ImporterSession.getInstance() + .getDatabaseSession(); + List<HYKEntry> entries; + if (this.hyk.storeMode == StoreMode.INSERT) + entries = null; + else { + final Query query = session.createQuery( + "from HYKEntry where HYK=:hyk " + + "and km=:km and measure=:measure"); + query.setParameter("hyk", h); + query.setParameter("km", this.km); + query.setParameter("measure", this.measure); + entries = query.list(); + } + if ((entries == null) || entries.isEmpty()) { + this.peer = new HYKEntry(h, this.km, this.measure); + session.save(this.peer); + this.storeMode = StoreMode.INSERT; } else { - peer = entries.get(0); + this.peer = entries.get(0); + this.storeMode = StoreMode.UPDATE; } } - return peer; + return this.peer; } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/backend/src/main/java/org/dive4elements/river/importer/ImportHYKFlowZone.java Fri Apr 06 14:13:14 2018 +0200 +++ b/backend/src/main/java/org/dive4elements/river/importer/ImportHYKFlowZone.java Sun Apr 08 18:07:06 2018 +0200 @@ -8,16 +8,15 @@ package org.dive4elements.river.importer; -import org.dive4elements.river.model.HYKFormation; +import java.math.BigDecimal; +import java.util.List; + +import org.dive4elements.river.importer.common.StoreMode; import org.dive4elements.river.model.HYKFlowZone; import org.dive4elements.river.model.HYKFlowZoneType; - -import org.hibernate.Session; +import org.dive4elements.river.model.HYKFormation; import org.hibernate.Query; - -import java.util.List; - -import java.math.BigDecimal; +import org.hibernate.Session; public class ImportHYKFlowZone { @@ -32,11 +31,11 @@ } public ImportHYKFlowZone( - ImportHYKFormation formation, - ImportHYKFlowZoneType type, - BigDecimal a, - BigDecimal b - ) { + final ImportHYKFormation formation, + final ImportHYKFlowZoneType type, + final BigDecimal a, + final BigDecimal b + ) { this.formation = formation; this.type = type; this.a = a; @@ -44,10 +43,10 @@ } public ImportHYKFormation getFormation() { - return formation; + return this.formation; } - public void setFormation(ImportHYKFormation formation) { + public void setFormation(final ImportHYKFormation formation) { this.formation = formation; } @@ -56,29 +55,34 @@ } public HYKFlowZone getPeer() { - if (peer == null) { - HYKFormation f = formation.getPeer(); - HYKFlowZoneType t = type.getPeer(); - Session session = ImporterSession.getInstance() - .getDatabaseSession(); - Query query = session.createQuery( - "from HYKFlowZone where formation=:formation " + - "and type=:type and a=:a and b=:b"); - query.setParameter("formation", f); - query.setParameter("type", t); - query.setParameter("a", a); - query.setParameter("b", b); - List<HYKFlowZone> zones = query.list(); - if (zones.isEmpty()) { - peer = new HYKFlowZone(f, t, a, b); - session.save(peer); + if (this.peer == null) { + final HYKFormation f = this.formation.getPeer(); + final HYKFlowZoneType t = this.type.getPeer(); + final Session session = ImporterSession.getInstance() + .getDatabaseSession(); + List<HYKFlowZone> zones; + if (this.formation.storeMode == StoreMode.INSERT) + zones = null; + { + final Query query = session.createQuery( + "from HYKFlowZone where formation=:formation " + + "and type=:type and a=:a and b=:b"); + query.setParameter("formation", f); + query.setParameter("type", t); + query.setParameter("a", this.a); + query.setParameter("b", this.b); + zones = query.list(); + } + if ((zones == null) || zones.isEmpty()) { + this.peer = new HYKFlowZone(f, t, this.a, this.b); + session.save(this.peer); } else { - peer = zones.get(0); + this.peer = zones.get(0); } } - return peer; + return this.peer; } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/backend/src/main/java/org/dive4elements/river/importer/ImportHYKFormation.java Fri Apr 06 14:13:14 2018 +0200 +++ b/backend/src/main/java/org/dive4elements/river/importer/ImportHYKFormation.java Sun Apr 08 18:07:06 2018 +0200 @@ -8,16 +8,15 @@ package org.dive4elements.river.importer; -import org.dive4elements.river.model.HYKFormation; -import org.dive4elements.river.model.HYKEntry; - +import java.math.BigDecimal; import java.util.ArrayList; import java.util.List; -import org.hibernate.Session; +import org.dive4elements.river.importer.common.StoreMode; +import org.dive4elements.river.model.HYKEntry; +import org.dive4elements.river.model.HYKFormation; import org.hibernate.Query; - -import java.math.BigDecimal; +import org.hibernate.Session; public class ImportHYKFormation { @@ -31,21 +30,24 @@ protected List<ImportHYKFlowZone> zones; + protected StoreMode storeMode; + protected HYKFormation peer; public ImportHYKFormation() { - zones = new ArrayList<ImportHYKFlowZone>(); + this.zones = new ArrayList<>(); + this.storeMode = StoreMode.NONE; } public ImportHYKFormation( - int formationNum, - ImportHYKEntry entry, - BigDecimal top, - BigDecimal bottom, - BigDecimal distanceVL, - BigDecimal distanceHF, - BigDecimal distanceVR - ) { + final int formationNum, + final ImportHYKEntry entry, + final BigDecimal top, + final BigDecimal bottom, + final BigDecimal distanceVL, + final BigDecimal distanceHF, + final BigDecimal distanceVR + ) { this(); this.formationNum = formationNum; this.entry = entry; @@ -56,103 +58,110 @@ this.distanceVR = distanceVR; } - public void addFlowZone(ImportHYKFlowZone zone) { - zones.add(zone); + public void addFlowZone(final ImportHYKFlowZone zone) { + this.zones.add(zone); zone.setFormation(this); } public int getFormationNum() { - return formationNum; + return this.formationNum; } - public void setFormationNum(int formationNum) { + public void setFormationNum(final int formationNum) { this.formationNum = formationNum; } public ImportHYKEntry getEntry() { - return entry; + return this.entry; } - public void setEntry(ImportHYKEntry entry) { + public void setEntry(final ImportHYKEntry entry) { this.entry = entry; } public BigDecimal getTop() { - return top; + return this.top; } - public void setTop(BigDecimal top) { + public void setTop(final BigDecimal top) { this.top = top; } public BigDecimal getBottom() { - return bottom; + return this.bottom; } - public void setBottom(BigDecimal bottom) { + public void setBottom(final BigDecimal bottom) { this.bottom = bottom; } public BigDecimal getDistanceVL() { - return distanceVL; + return this.distanceVL; } - public void setDistanceVL(BigDecimal distanceVL) { + public void setDistanceVL(final BigDecimal distanceVL) { this.distanceVL = distanceVL; } public BigDecimal getDistanceHF() { - return distanceHF; + return this.distanceHF; } - public void setDistanceHF(BigDecimal distanceHF) { + public void setDistanceHF(final BigDecimal distanceHF) { this.distanceHF = distanceHF; } public BigDecimal getDistanceVR() { - return distanceVR; + return this.distanceVR; } - public void setDistanceVR(BigDecimal distanceVR) { + public void setDistanceVR(final BigDecimal distanceVR) { this.distanceVR = distanceVR; } public void storeDependencies() { getPeer(); - for (ImportHYKFlowZone zone: zones) { + for (final ImportHYKFlowZone zone: this.zones) { zone.storeDependencies(); } } public HYKFormation getPeer() { - if (peer == null) { - HYKEntry e = entry.getPeer(); - Session session = ImporterSession.getInstance() - .getDatabaseSession(); - Query query = session.createQuery( - "from HYKFormation where formationNum=:formationNum " + - "and entry=:entry and top=:top and bottom=:bottom " + - "and distanceVL=:distanceVL and distanceHF=:distanceHF " + - "and distanceVR=:distanceVR"); - query.setParameter("formationNum", formationNum); - query.setParameter("entry", e); - query.setParameter("top", top); - query.setParameter("bottom", bottom); - query.setParameter("distanceVL", distanceVL); - query.setParameter("distanceHF", distanceHF); - query.setParameter("distanceVR", distanceVR); - List<HYKFormation> formations = query.list(); - if (formations.isEmpty()) { - peer = new HYKFormation( - formationNum, e, top, bottom, - distanceVL, distanceHF, distanceVR); - session.save(peer); + if (this.peer == null) { + final HYKEntry e = this.entry.getPeer(); + final Session session = ImporterSession.getInstance() + .getDatabaseSession(); + List<HYKFormation> formations; + if (this.entry.storeMode == StoreMode.INSERT) + formations = null; + else { + final Query query = session.createQuery( + "from HYKFormation where formationNum=:formationNum " + + "and entry=:entry and top=:top and bottom=:bottom " + + "and distanceVL=:distanceVL and distanceHF=:distanceHF " + + "and distanceVR=:distanceVR"); + query.setParameter("formationNum", this.formationNum); + query.setParameter("entry", e); + query.setParameter("top", this.top); + query.setParameter("bottom", this.bottom); + query.setParameter("distanceVL", this.distanceVL); + query.setParameter("distanceHF", this.distanceHF); + query.setParameter("distanceVR", this.distanceVR); + formations = query.list(); + } + if ((formations == null) || formations.isEmpty()) { + this.peer = new HYKFormation( + this.formationNum, e, this.top, this.bottom, + this.distanceVL, this.distanceHF, this.distanceVR); + session.save(this.peer); + this.storeMode = StoreMode.INSERT; } else { - peer = formations.get(0); + this.peer = formations.get(0); + this.storeMode = StoreMode.UPDATE; } } - return peer; + return this.peer; } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/backend/src/main/java/org/dive4elements/river/importer/ImportPorosity.java Fri Apr 06 14:13:14 2018 +0200 +++ b/backend/src/main/java/org/dive4elements/river/importer/ImportPorosity.java Sun Apr 08 18:07:06 2018 +0200 @@ -12,12 +12,12 @@ import java.util.List; import org.apache.log4j.Logger; +import org.dive4elements.river.importer.common.StoreMode; +import org.dive4elements.river.model.Porosity; +import org.dive4elements.river.model.River; import org.hibernate.Query; import org.hibernate.Session; -import org.dive4elements.river.model.River; -import org.dive4elements.river.model.Porosity; - public class ImportPorosity { @@ -33,82 +33,88 @@ protected List<ImportPorosityValue> values; - public ImportPorosity(String description) { + protected StoreMode storeMode; + + public ImportPorosity(final String description) { this.description = description; - this.values = new ArrayList<ImportPorosityValue>(); + this.values = new ArrayList<>(); + this.storeMode = StoreMode.NONE; } public String getDescription() { - return description; + return this.description; } - public void setDepth(ImportDepth depth) { + public void setDepth(final ImportDepth depth) { this.depth = depth; } - public void setTimeInterval(ImportTimeInterval importTimeInterval) { + public void setTimeInterval(final ImportTimeInterval importTimeInterval) { this.timeInterval = importTimeInterval; } - public void addValue(ImportPorosityValue value) { - values.add(value); + public void addValue(final ImportPorosityValue value) { + this.values.add(value); } - public void storeDependencies(River river) { - log.info("store dependencies"); + public void storeDependencies(final River river) { + log.info("store dependencies for '" + getDescription() + "'"); - if (depth != null) { - depth.storeDependencies(); + if (this.depth != null) { + this.depth.storeDependencies(); } - Porosity peer = getPeer(river); + final Porosity peer = getPeer(river); if (peer != null) { - log.info("store porosity values."); - for (ImportPorosityValue value : values) { - value.storeDependencies(peer); + // log.info("store porosity values."); + for (final ImportPorosityValue value : this.values) { + value.storeDependencies(peer, this.storeMode); } + log.info("Porosity values processed: " + this.values.size()); } } - public Porosity getPeer(River river) { - log.info("get peer"); + public Porosity getPeer(final River river) { + // log.info("get peer"); - if (depth == null) { - log.warn("cannot store porosity '" + description - + "': no depth"); + if (this.depth == null) { + log.warn("cannot store porosity '" + this.description + + "': no depth"); return null; } - if (peer == null) { - Session session = ImporterSession.getInstance() - .getDatabaseSession(); + if (this.peer == null) { + final Session session = ImporterSession.getInstance() + .getDatabaseSession(); - Query query = session.createQuery("from Porosity where " - + " river=:river and " - + " depth=:depth and " - + " description=:description"); + final Query query = session.createQuery("from Porosity where " + + " river=:river and " + + " depth=:depth and " + + " description=:description"); query.setParameter("river", river); - query.setParameter("depth", depth.getPeer()); - query.setParameter("description", description); + query.setParameter("depth", this.depth.getPeer()); + query.setParameter("description", this.description); - List<Porosity> porosity = query.list(); + final List<Porosity> porosity = query.list(); if (porosity.isEmpty()) { log.debug("Create new Porosity DB instance."); - peer = new Porosity(river, depth.getPeer(), - description, timeInterval.getPeer()); + this.peer = new Porosity(river, this.depth.getPeer(), + this.description, this.timeInterval.getPeer()); - session.save(peer); + session.save(this.peer); + this.storeMode = StoreMode.INSERT; } else { - peer = porosity.get(0); + this.peer = porosity.get(0); + this.storeMode = StoreMode.UPDATE; } } - return peer; + return this.peer; } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/backend/src/main/java/org/dive4elements/river/importer/ImportPorosityValue.java Fri Apr 06 14:13:14 2018 +0200 +++ b/backend/src/main/java/org/dive4elements/river/importer/ImportPorosityValue.java Sun Apr 08 18:07:06 2018 +0200 @@ -9,22 +9,20 @@ package org.dive4elements.river.importer; import java.math.BigDecimal; - import java.util.List; import org.apache.log4j.Logger; - -import org.hibernate.Session; -import org.hibernate.Query; - +import org.dive4elements.river.importer.common.StoreMode; import org.dive4elements.river.model.Porosity; import org.dive4elements.river.model.PorosityValue; +import org.hibernate.Query; +import org.hibernate.Session; public class ImportPorosityValue { private static final Logger log = - Logger.getLogger(ImportPorosityValue.class); + Logger.getLogger(ImportPorosityValue.class); protected PorosityValue peer; @@ -39,11 +37,11 @@ public ImportPorosityValue( - BigDecimal station, - BigDecimal shoreOffset, - BigDecimal porosity, - String description - ) { + final BigDecimal station, + final BigDecimal shoreOffset, + final BigDecimal porosity, + final String description + ) { this.station = station; this.shoreOffset = shoreOffset; this.porosity = porosity; @@ -51,53 +49,57 @@ } - public void storeDependencies(Porosity porosity) { - log.info("store dependencies"); + public void storeDependencies(final Porosity porosity, final StoreMode parentStoreMode) { + // log.info("store dependencies"); - getPeer(porosity); + getPeer(porosity, parentStoreMode); } - public PorosityValue getPeer(Porosity porosity) { + public PorosityValue getPeer(final Porosity porosity, final StoreMode parentStoreMode) { log.info("get peer"); - if (peer == null) { - Session session = ImporterSession.getInstance() - .getDatabaseSession(); - - Query query = session.createQuery( - "from PorosityValue " - + "where porosity=:porosity " - + "and station between :station - 0.0001f " - + " and :station + 0.0001f " - + "and porosityValue between :poros -0.0001f " - + " and :poros + 0.0001f " - + "and description=:description"); + if (this.peer == null) { + final Session session = ImporterSession.getInstance() + .getDatabaseSession(); + List<PorosityValue> values; + if (parentStoreMode == StoreMode.INSERT) + values = null; + else { + final Query query = session.createQuery( + "from PorosityValue " + + "where porosity=:porosity " + + "and station between :station - 0.0001f " + + " and :station + 0.0001f " + + "and porosityValue between :poros -0.0001f " + + " and :poros + 0.0001f " + + "and description=:description"); - query.setParameter("porosity", porosity); - query.setParameter("station", station.floatValue()); - query.setParameter("poros", this.porosity.floatValue()); - query.setParameter("description", description); + query.setParameter("porosity", porosity); + query.setParameter("station", this.station.floatValue()); + query.setParameter("poros", this.porosity.floatValue()); + query.setParameter("description", this.description); - List<PorosityValue> values = query.list(); - if (values.isEmpty()) { - log.debug("Create new PorosityValue DB instance."); + values = query.list(); + } + if ((values == null) || values.isEmpty()) { + // log.debug("Create new PorosityValue DB instance."); - peer = new PorosityValue( - porosity, - station, - shoreOffset, - this.porosity, - description); + this.peer = new PorosityValue( + porosity, + this.station, + this.shoreOffset, + this.porosity, + this.description); - session.save(peer); + session.save(this.peer); } else { - peer = values.get(0); + this.peer = values.get(0); } } - return peer; + return this.peer; } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/backend/src/main/java/org/dive4elements/river/importer/ImportSedimentLoad.java Fri Apr 06 14:13:14 2018 +0200 +++ b/backend/src/main/java/org/dive4elements/river/importer/ImportSedimentLoad.java Sun Apr 08 18:07:06 2018 +0200 @@ -8,9 +8,11 @@ package org.dive4elements.river.importer; +import java.util.ArrayList; import java.util.List; -import java.util.ArrayList; +import org.apache.log4j.Logger; +import org.dive4elements.river.importer.common.StoreMode; import org.dive4elements.river.model.GrainFraction; import org.dive4elements.river.model.SedimentLoad; import org.dive4elements.river.model.TimeInterval; @@ -19,6 +21,8 @@ public class ImportSedimentLoad { + private static Logger log = Logger.getLogger(ImportSedimentLoad.class); + private SedimentLoad peer; private ImportGrainFraction grainFraction; @@ -27,92 +31,98 @@ private String description; private Integer kind; - private List<ImportSedimentLoadValue> values; + private final List<ImportSedimentLoadValue> values; + + protected StoreMode storeMode; public ImportSedimentLoad() { - this.values = new ArrayList<ImportSedimentLoadValue>(); + this.values = new ArrayList<>(); } public ImportSedimentLoad( - ImportGrainFraction grainFraction, - ImportTimeInterval timeInterval, - ImportTimeInterval sqTimeInterval, - String description, - Integer kind - ) { + final ImportGrainFraction grainFraction, + final ImportTimeInterval timeInterval, + final ImportTimeInterval sqTimeInterval, + final String description, + final Integer kind + ) { this.grainFraction = grainFraction; this.timeInterval = timeInterval; this.sqTimeInterval = sqTimeInterval; this.description = description; this.kind = kind; - this.values = new ArrayList<ImportSedimentLoadValue>(); + this.values = new ArrayList<>(); + this.storeMode = StoreMode.NONE; } - public void addValue(ImportSedimentLoadValue value) { - values.add(value); + public void addValue(final ImportSedimentLoadValue value) { + this.values.add(value); } public void storeDependencies() { - grainFraction.getPeer(); - timeInterval.getPeer(); + log.info("store dependencies for '" + this.description + "'"); + this.grainFraction.getPeer(); + this.timeInterval.getPeer(); - if (sqTimeInterval != null) { - sqTimeInterval.getPeer(); + if (this.sqTimeInterval != null) { + this.sqTimeInterval.getPeer(); } getPeer(); - for (ImportSedimentLoadValue value : values) { - value.storeDependencies(peer); + for (final ImportSedimentLoadValue value : this.values) { + value.storeDependencies(this.peer, this.storeMode); } - + log.info("Values processed: " + this.values.size()); } public SedimentLoad getPeer() { - if (peer == null) { - Session session = ImporterSession.getInstance() - .getDatabaseSession(); - - String sqtquery = sqTimeInterval == null - ? "sq_time_interval_id is null" - : "sqTimeInterval = :sqTimeInterval"; - Query query = session.createQuery( - "from SedimentLoad where " + - " grainFraction = :grainFraction and " + - " timeInterval = :timeInterval and " + - " description = :description and " + - " kind = :kind and " + - sqtquery); - - GrainFraction gf = grainFraction.getPeer(); - TimeInterval ti = timeInterval.getPeer(); + if (this.peer == null) { + final Session session = ImporterSession.getInstance() + .getDatabaseSession(); - TimeInterval sqti = sqTimeInterval != null - ? sqTimeInterval.getPeer() - : null; - - query.setParameter("grainFraction", gf); - query.setParameter("timeInterval", ti); + final String sqtquery = this.sqTimeInterval == null + ? "sq_time_interval_id is null" + : "sqTimeInterval = :sqTimeInterval"; + final Query query = session.createQuery( + "from SedimentLoad where " + + " grainFraction = :grainFraction and " + + " timeInterval = :timeInterval and " + + " description = :description and " + + " kind = :kind and " + + sqtquery); - if (sqti != null) { - query.setParameter("sqTimeInterval", sqti); - } - query.setParameter("description", description); - query.setParameter("kind", kind); + final GrainFraction gf = this.grainFraction.getPeer(); + final TimeInterval ti = this.timeInterval.getPeer(); - List<SedimentLoad> loads = query.list(); - if (loads.isEmpty()) { - peer = new SedimentLoad(gf, ti, sqti, description, kind); - session.save(peer); - } - else { - peer = loads.get(0); - } + final TimeInterval sqti = this.sqTimeInterval != null + ? this.sqTimeInterval.getPeer() + : null; + + query.setParameter("grainFraction", gf); + query.setParameter("timeInterval", ti); + + if (sqti != null) { + query.setParameter("sqTimeInterval", sqti); + } + query.setParameter("description", this.description); + query.setParameter("kind", this.kind); + + final List<SedimentLoad> loads = query.list(); + if (loads.isEmpty()) { + this.peer = new SedimentLoad(gf, ti, sqti, this.description, this.kind); + session.save(this.peer); + this.storeMode = StoreMode.INSERT; + } + else { + this.peer = loads.get(0); + this.storeMode = StoreMode.UPDATE; + } } - return peer; + return this.peer; } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/backend/src/main/java/org/dive4elements/river/importer/ImportSedimentLoadLS.java Fri Apr 06 14:13:14 2018 +0200 +++ b/backend/src/main/java/org/dive4elements/river/importer/ImportSedimentLoadLS.java Sun Apr 08 18:07:06 2018 +0200 @@ -12,15 +12,14 @@ import java.util.List; import org.apache.log4j.Logger; - -import org.hibernate.Session; -import org.hibernate.Query; - +import org.dive4elements.river.importer.common.StoreMode; import org.dive4elements.river.model.GrainFraction; import org.dive4elements.river.model.River; import org.dive4elements.river.model.SedimentLoadLS; import org.dive4elements.river.model.TimeInterval; import org.dive4elements.river.model.Unit; +import org.hibernate.Query; +import org.hibernate.Session; public class ImportSedimentLoadLS { @@ -35,53 +34,56 @@ private ImportTimeInterval sqTimeInterval; - private String description; + private final String description; private Integer kind; - private List<ImportSedimentLoadLSValue> values; + private final List<ImportSedimentLoadLSValue> values; + + protected StoreMode storeMode; private SedimentLoadLS peer; - public ImportSedimentLoadLS(String description) { - this.values = new ArrayList<ImportSedimentLoadLSValue>(); + public ImportSedimentLoadLS(final String description) { + this.values = new ArrayList<>(); this.description = description; + this.storeMode = StoreMode.NONE; } - public void setTimeInterval(ImportTimeInterval timeInterval) { + public void setTimeInterval(final ImportTimeInterval timeInterval) { this.timeInterval = timeInterval; } - public void setSQTimeInterval(ImportTimeInterval sqTimeInterval) { + public void setSQTimeInterval(final ImportTimeInterval sqTimeInterval) { this.sqTimeInterval = sqTimeInterval; } - public void setUnit(ImportUnit unit) { + public void setUnit(final ImportUnit unit) { this.unit = unit; } - public void setGrainFraction(ImportGrainFraction grainFraction) { + public void setGrainFraction(final ImportGrainFraction grainFraction) { this.grainFraction = grainFraction; } - public void setKind(Integer kind) { + public void setKind(final Integer kind) { this.kind = kind; } - public void addValue(ImportSedimentLoadLSValue value) { + public void addValue(final ImportSedimentLoadLSValue value) { this.values.add(value); } - public void storeDependencies(River river) { - log.debug("store dependencies"); + public void storeDependencies(final River river) { + log.info("store dependencies for '" + this.description + "'"); - SedimentLoadLS peer = getPeer(river); + final SedimentLoadLS peer = getPeer(river); if (peer != null) { int i = 0; - for (ImportSedimentLoadLSValue value : values) { - value.storeDependencies(peer); + for (final ImportSedimentLoadLSValue value : this.values) { + value.storeDependencies(peer, this.storeMode); i++; } @@ -89,40 +91,38 @@ } } - public SedimentLoadLS getPeer(River river) { + public SedimentLoadLS getPeer(final River river) { log.debug("get peer"); - GrainFraction gf = grainFraction != null ? grainFraction.getPeer() - : null; + final GrainFraction gf = this.grainFraction != null ? this.grainFraction.getPeer() + : null; - Unit u = unit != null ? unit.getPeer() : null; + final Unit u = this.unit != null ? this.unit.getPeer() : null; - TimeInterval ti = timeInterval != null ? timeInterval.getPeer() : null; - TimeInterval sqti = sqTimeInterval != null - ? sqTimeInterval.getPeer() - : null; + final TimeInterval ti = (this.timeInterval != null) ? this.timeInterval.getPeer() : null; + final TimeInterval sqti = (this.sqTimeInterval != null) ? this.sqTimeInterval.getPeer() : null; if (ti == null || u == null) { log.warn( - "Skip invalid SedimentLoadLS: time interval or unit null!"); + "Skip invalid SedimentLoadLS: time interval or unit null!"); return null; } - if (peer == null) { - Session session = ImporterSession.getInstance() - .getDatabaseSession(); + if (this.peer == null) { + final Session session = ImporterSession.getInstance() + .getDatabaseSession(); - String sqtquery = sqTimeInterval == null ? - "sq_time_interval_id is null" : - "sqTimeInterval = :sqTimeInterval"; - Query query = session.createQuery("from SedimentLoadLS where " - + " river=:river and " - + " grainFraction=:grainFraction and " - + " unit=:unit and " - + " timeInterval=:timeInterval and " - + " description=:description and " - + " kind = :kind and " + - sqtquery); + final String sqtquery = this.sqTimeInterval == null ? + "sq_time_interval_id is null" : + "sqTimeInterval = :sqTimeInterval"; + final Query query = session.createQuery("from SedimentLoadLS where " + + " river=:river and " + + " grainFraction=:grainFraction and " + + " unit=:unit and " + + " timeInterval=:timeInterval and " + + " description=:description and " + + " kind = :kind and " + + sqtquery); query.setParameter("river", river); query.setParameter("grainFraction", gf); @@ -131,23 +131,25 @@ if (sqti != null) { query.setParameter("sqTimeInterval", sqti); } - query.setParameter("description", description); - query.setParameter("kind", kind); + query.setParameter("description", this.description); + query.setParameter("kind", this.kind); - List<SedimentLoadLS> loads = query.list(); + final List<SedimentLoadLS> loads = query.list(); if (loads.isEmpty()) { log.debug("create new SedimentLoadLS"); - peer = new SedimentLoadLS(river, u, ti, sqti, gf, description); - peer.setKind(this.kind); - session.save(peer); + this.peer = new SedimentLoadLS(river, u, ti, sqti, gf, this.description); + this.peer.setKind(this.kind); + session.save(this.peer); + this.storeMode = StoreMode.INSERT; } else { - peer = loads.get(0); + this.peer = loads.get(0); + this.storeMode = StoreMode.UPDATE; } } - return peer; + return this.peer; } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/backend/src/main/java/org/dive4elements/river/importer/ImportSedimentLoadLSValue.java Fri Apr 06 14:13:14 2018 +0200 +++ b/backend/src/main/java/org/dive4elements/river/importer/ImportSedimentLoadLSValue.java Sun Apr 08 18:07:06 2018 +0200 @@ -10,58 +10,60 @@ import java.util.List; -import org.hibernate.Session; -import org.hibernate.Query; - +import org.dive4elements.river.importer.common.StoreMode; import org.dive4elements.river.model.SedimentLoadLS; import org.dive4elements.river.model.SedimentLoadLSValue; +import org.hibernate.Query; +import org.hibernate.Session; public class ImportSedimentLoadLSValue { - private Double station; - private Double value; + private final Double station; + private final Double value; private SedimentLoadLSValue peer; - public ImportSedimentLoadLSValue(Double station, Double value) { + public ImportSedimentLoadLSValue(final Double station, final Double value) { this.station = station; this.value = value; } - public void storeDependencies(SedimentLoadLS sedimentLoadLS) { - getPeer(sedimentLoadLS); + public void storeDependencies(final SedimentLoadLS sedimentLoadLS, final StoreMode parentStoreMode) { + getPeer(sedimentLoadLS, parentStoreMode); } - public SedimentLoadLSValue getPeer(SedimentLoadLS sedimentLoadLS) { - if (peer == null) { - Session session = ImporterSession.getInstance() - .getDatabaseSession(); - Query query = session.createQuery( - "from SedimentLoadLSValue where " + - " sedimentLoadLS=:sedimentLoadLS and " + - " station=:station and " + - " value=:value" - ); - - query.setParameter("sedimentLoadLS", sedimentLoadLS); - query.setParameter("station", station); - query.setParameter("value", value); - - List<SedimentLoadLSValue> values = query.list(); - if (values.isEmpty()) { - peer = new SedimentLoadLSValue(sedimentLoadLS, station, value); - session.save(peer); + public SedimentLoadLSValue getPeer(final SedimentLoadLS sedimentLoadLS, final StoreMode parentStoreMode) { + if (this.peer == null) { + List<SedimentLoadLSValue> values; + final Session session = ImporterSession.getInstance().getDatabaseSession(); + if (parentStoreMode == StoreMode.INSERT) + values = null; + else { + final Query query = session.createQuery( + "from SedimentLoadLSValue where " + + " sedimentLoadLS=:sedimentLoadLS and " + + " station=:station and " + + " value=:value" + ); + query.setParameter("sedimentLoadLS", sedimentLoadLS); + query.setParameter("station", this.station); + query.setParameter("value", this.value); + values = query.list(); + } + if ((values == null) || values.isEmpty()) { + this.peer = new SedimentLoadLSValue(sedimentLoadLS, this.station, this.value); + session.save(this.peer); } else { - peer = values.get(0); + this.peer = values.get(0); } } - return peer; + return this.peer; } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/backend/src/main/java/org/dive4elements/river/importer/ImportSedimentLoadValue.java Fri Apr 06 14:13:14 2018 +0200 +++ b/backend/src/main/java/org/dive4elements/river/importer/ImportSedimentLoadValue.java Sun Apr 08 18:07:06 2018 +0200 @@ -10,6 +10,7 @@ import java.util.List; +import org.dive4elements.river.importer.common.StoreMode; import org.dive4elements.river.model.MeasurementStation; import org.dive4elements.river.model.SedimentLoad; import org.dive4elements.river.model.SedimentLoadValue; @@ -27,43 +28,48 @@ } public ImportSedimentLoadValue( - MeasurementStation station, - Double value - ) { + final MeasurementStation station, + final Double value + ) { this.station = station; this.value = value; } - protected SedimentLoadValue getPeer(SedimentLoad sedimentLoad) { + protected SedimentLoadValue getPeer(final SedimentLoad sedimentLoad, final StoreMode parentStoreMode) { - if (peer == null) { - Session session = ImporterSession.getInstance() - .getDatabaseSession(); - Query query = session.createQuery( - "from SedimentLoadValue where " + - " measurementStation = :station and " + - " sedimentLoad = :sedimentLoad and " + - " value = :value"); + if (this.peer == null) { + final Session session = ImporterSession.getInstance() + .getDatabaseSession(); + List<SedimentLoadValue> values; + if (parentStoreMode == StoreMode.INSERT) + values = null; + else { + final Query query = session.createQuery( + "from SedimentLoadValue where " + + " measurementStation = :station and " + + " sedimentLoad = :sedimentLoad and " + + " value = :value"); - query.setParameter("station", station); - query.setParameter("sedimentLoad", sedimentLoad); - query.setParameter("value", value); + query.setParameter("station", this.station); + query.setParameter("sedimentLoad", sedimentLoad); + query.setParameter("value", this.value); - List<SedimentLoadValue> values = query.list(); - if (values.isEmpty()) { - peer = new SedimentLoadValue(sedimentLoad, station, value); - session.save(peer); + values = query.list(); + } + if ((values == null) || values.isEmpty()) { + this.peer = new SedimentLoadValue(sedimentLoad, this.station, this.value); + session.save(this.peer); } else { - peer = values.get(0); + this.peer = values.get(0); } } - return peer; + return this.peer; } - public void storeDependencies(SedimentLoad sedimentLoad) { - getPeer(sedimentLoad); + public void storeDependencies(final SedimentLoad sedimentLoad, final StoreMode parentStoreMode) { + getPeer(sedimentLoad, parentStoreMode); } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/backend/src/main/java/org/dive4elements/river/importer/ImportWst.java Fri Apr 06 14:13:14 2018 +0200 +++ b/backend/src/main/java/org/dive4elements/river/importer/ImportWst.java Sun Apr 08 18:07:06 2018 +0200 @@ -8,16 +8,15 @@ package org.dive4elements.river.importer; -import org.dive4elements.river.model.Wst; -import org.dive4elements.river.model.River; +import java.util.ArrayList; +import java.util.List; import org.apache.log4j.Logger; - -import org.hibernate.Session; +import org.dive4elements.river.importer.common.StoreMode; +import org.dive4elements.river.model.River; +import org.dive4elements.river.model.Wst; import org.hibernate.Query; - -import java.util.ArrayList; -import java.util.List; +import org.hibernate.Session; /** Not (yet) db-mapped WST object. */ public class ImportWst @@ -29,12 +28,12 @@ } public static final ImportWstColumnFactory COLUMN_FACTORY = - new ImportWstColumnFactory() { - @Override - public ImportWstColumn create(ImportWst importWst, int position) { - return new ImportWstColumn(importWst, null, null, position); - } - }; + new ImportWstColumnFactory() { + @Override + public ImportWstColumn create(final ImportWst importWst, final int position) { + return new ImportWstColumn(importWst, null, null, position); + } + }; protected String description; @@ -48,6 +47,8 @@ protected boolean kmUp; + protected StoreMode storeMode; + /** Wst as in db. */ protected Wst peer; @@ -55,123 +56,126 @@ this(COLUMN_FACTORY); } - public ImportWst(ImportWstColumnFactory columnFactory) { + public ImportWst(final ImportWstColumnFactory columnFactory) { this.columnFactory = columnFactory; - kind = 0; - columns = new ArrayList<ImportWstColumn>(); + this.kind = 0; + this.columns = new ArrayList<>(); + this.storeMode = StoreMode.NONE; } - public ImportWst(String description) { + public ImportWst(final String description) { this(description, COLUMN_FACTORY); } public ImportWst( - String description, - ImportWstColumnFactory columnFactory - ) { + final String description, + final ImportWstColumnFactory columnFactory + ) { this(columnFactory); this.description = description; } public String getDescription() { - return description; + return this.description; } public Integer getKind() { - return kind; + return this.kind; } - public void setKind(Integer kind) { + public void setKind(final Integer kind) { this.kind = kind; } public boolean getKmUp() { - return kmUp; + return this.kmUp; } - public void setKmUp(boolean kmUp) { + public void setKmUp(final boolean kmUp) { this.kmUp = kmUp; } - public void setDescription(String description) { + public void setDescription(final String description) { this.description = description; } /** Create columns that can be accessed with getColumn. */ - public void setNumberColumns(int numColumns) { + public void setNumberColumns(final int numColumns) { for (int i = 0; i < numColumns; ++i) { - columns.add(columnFactory.create(this, i)); + this.columns.add(this.columnFactory.create(this, i)); } } public int getNumberColumns() { - return columns.size(); + return this.columns.size(); } - public ImportWstColumn getColumn(int index) { - return columns.get(index); + public ImportWstColumn getColumn(final int index) { + return this.columns.get(index); } public List<ImportWstColumn> getColumns() { - return columns; + return this.columns; } /** Adds a column. Assumes that columns wst is this instance. */ - public void addColumn(ImportWstColumn column) { - columns.add(column); + public void addColumn(final ImportWstColumn column) { + this.columns.add(column); } public ImportUnit getUnit() { - return unit; + return this.unit; } - public void setUnit(ImportUnit unit) { + public void setUnit(final ImportUnit unit) { this.unit = unit; } - public void storeDependencies(River river) { + public void storeDependencies(final River river) { - log.info("store '" + description + "'"); + log.info("store '" + this.description + "'"); getPeer(river); - for (ImportWstColumn column: columns) { + for (final ImportWstColumn column: this.columns) { column.storeDependencies(river); } - Session session = ImporterSession.getInstance().getDatabaseSession(); + final Session session = ImporterSession.getInstance().getDatabaseSession(); session.flush(); } public boolean guessWaterLevelIncreasing() { int up = 0; - for (ImportWstColumn column: columns) { + for (final ImportWstColumn column: this.columns) { if (column.guessWaterLevelIncreasing()) ++up; } - return up > columns.size() - up; + return up > this.columns.size() - up; } /** Get corresponding mapped wst (from database). */ - public Wst getPeer(River river) { - if (peer == null) { - Session session = ImporterSession.getInstance() - .getDatabaseSession(); - Query query = session.createQuery( - "from Wst where " + - "river=:river and description=:description and kind=:kind"); + public Wst getPeer(final River river) { + if (this.peer == null) { + final Session session = ImporterSession.getInstance() + .getDatabaseSession(); + final Query query = session.createQuery( + "from Wst where " + + "river=:river and description=:description and kind=:kind"); query.setParameter("river", river); - query.setParameter("description", description); - query.setParameter("kind", kind); - List<Wst> wsts = query.list(); + query.setParameter("description", this.description); + query.setParameter("kind", this.kind); + final List<Wst> wsts = query.list(); if (wsts.isEmpty()) { - peer = new Wst(river, description, kind); - session.save(peer); + this.peer = new Wst(river, this.description, this.kind); + session.save(this.peer); + this.storeMode = StoreMode.INSERT; } else { - peer = wsts.get(0); + this.peer = wsts.get(0); + this.storeMode = StoreMode.UPDATE; } } - return peer; + return this.peer; } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/backend/src/main/java/org/dive4elements/river/importer/ImportWstColumn.java Fri Apr 06 14:13:14 2018 +0200 +++ b/backend/src/main/java/org/dive4elements/river/importer/ImportWstColumn.java Sun Apr 08 18:07:06 2018 +0200 @@ -8,21 +8,19 @@ package org.dive4elements.river.importer; -import org.dive4elements.river.model.Wst; -import org.dive4elements.river.model.WstColumn; +import java.math.BigDecimal; +import java.util.ArrayList; +import java.util.List; +import java.util.Random; + +import org.apache.log4j.Logger; +import org.dive4elements.river.importer.common.StoreMode; import org.dive4elements.river.model.River; import org.dive4elements.river.model.TimeInterval; - -import org.hibernate.Session; +import org.dive4elements.river.model.Wst; +import org.dive4elements.river.model.WstColumn; import org.hibernate.Query; - -import java.util.List; -import java.util.ArrayList; -import java.util.Random; - -import java.math.BigDecimal; - -import org.apache.log4j.Logger; +import org.hibernate.Session; /** Unmapped column of a WST. */ @@ -41,20 +39,23 @@ protected List<ImportWstColumnQRange> columnQRanges; protected List<ImportWstColumnValue> columnValues; + protected StoreMode storeMode; + protected WstColumn peer; public ImportWstColumn() { - columnQRanges = new ArrayList<ImportWstColumnQRange>(); - columnValues = new ArrayList<ImportWstColumnValue>(); + this.columnQRanges = new ArrayList<>(); + this.columnValues = new ArrayList<>(); + this.storeMode = StoreMode.NONE; } public ImportWstColumn( - ImportWst wst, - String name, - String description, - Integer position, - String source - ) { + final ImportWst wst, + final String name, + final String description, + final Integer position, + final String source + ) { this(); this.wst = wst; this.name = name; @@ -63,114 +64,114 @@ } public ImportWstColumn( - ImportWst wst, - String name, - String description, - Integer position - ) { + final ImportWst wst, + final String name, + final String description, + final Integer position + ) { this(wst, name, description, position, null); } public ImportWst getWst() { - return wst; + return this.wst; } - public void setWst(ImportWst wst) { + public void setWst(final ImportWst wst) { this.wst = wst; } public String getName() { - return name; + return this.name; } - public void setName(String name) { + public void setName(final String name) { this.name = name; } public String getDescription() { - return description; + return this.description; } - public void setDescription(String description) { + public void setDescription(final String description) { this.description = description; } public Integer getPosition() { - return position; + return this.position; } - public void setPosition(Integer position) { + public void setPosition(final Integer position) { this.position = position; } public String getSource() { - return source; + return this.source; } - public void setSource(String source) { + public void setSource(final String source) { this.source = source; } - public void addColumnValue(BigDecimal position, BigDecimal w) { - columnValues.add( - new ImportWstColumnValue(this, position, w)); + public void addColumnValue(final BigDecimal position, final BigDecimal w) { + this.columnValues.add( + new ImportWstColumnValue(this, position, w)); } - public void addColumnQRange(ImportWstQRange columnQRange) { - columnQRanges.add( - new ImportWstColumnQRange(this, columnQRange)); + public void addColumnQRange(final ImportWstQRange columnQRange) { + this.columnQRanges.add( + new ImportWstColumnQRange(this, columnQRange)); } /** Get the Column Values stored in this column. */ public List<ImportWstColumnValue> getColumnValues() { - return columnValues; + return this.columnValues; } - public void storeDependencies(River river) { - log.info("store column '" + name + "'"); + public void storeDependencies(final River river) { + log.info("store column '" + this.name + "'"); getPeer(river); - for (ImportWstColumnQRange columnQRange: columnQRanges) { + for (final ImportWstColumnQRange columnQRange: this.columnQRanges) { columnQRange.getPeer(river); } - for (ImportWstColumnValue columnValue: columnValues) { + for (final ImportWstColumnValue columnValue: this.columnValues) { columnValue.getPeer(river); } } public ImportTimeInterval getTimeInterval() { - return timeInterval; + return this.timeInterval; } - public void setTimeInterval(ImportTimeInterval timeInterval) { + public void setTimeInterval(final ImportTimeInterval timeInterval) { this.timeInterval = timeInterval; } public boolean guessWaterLevelIncreasing() { - int N = columnValues.size(); + final int N = this.columnValues.size(); if (N < 2) { return true; } - Random r = new Random(); + final Random r = new Random(); int up = 0; - int S = N < 50 ? N : (int)(0.1f * N)+1; + final int S = N < 50 ? N : (int)(0.1f * N)+1; for (int s = 0; s < S; ++s) { int i1, i2; do { i1 = r.nextInt(N-1); i2 = r.nextInt(N-1); } while (i1 == i2); - ImportWstColumnValue b = columnValues.get(i1); - ImportWstColumnValue a = columnValues.get(i2); + ImportWstColumnValue b = this.columnValues.get(i1); + ImportWstColumnValue a = this.columnValues.get(i2); if (b.getPosition().compareTo(a.getPosition()) < 0) { - ImportWstColumnValue t = a; a = b; b = t; + final ImportWstColumnValue t = a; a = b; b = t; } if (a.getW().compareTo(b.getW()) < 0) ++up; @@ -180,37 +181,42 @@ } /** Get corresponding mapped wst-column (from database). */ - public WstColumn getPeer(River river) { - if (peer == null) { - Wst w = wst.getPeer(river); - Session session = ImporterSession.getInstance() - .getDatabaseSession(); - Query query = session.createQuery( - "from WstColumn where" + - " wst=:wst and name=:name" + - " and source=:source" + - " and position=:position"); - query.setParameter("wst", w); - query.setParameter("name", name); - query.setParameter("position", position); - query.setParameter("source", source); + public WstColumn getPeer(final River river) { + if (this.peer == null) { + final Wst w = this.wst.getPeer(river); + List<WstColumn> columns; + final Session session = ImporterSession.getInstance() + .getDatabaseSession(); + if (this.wst.storeMode == StoreMode.INSERT) + columns = null; + else { + final Query query = session.createQuery( + "from WstColumn where" + + " wst=:wst and name=:name" + + " and source=:source" + + " and position=:position"); + query.setParameter("wst", w); + query.setParameter("name", this.name); + query.setParameter("position", this.position); + query.setParameter("source", this.source); + columns = query.list(); + } - TimeInterval ti = timeInterval != null - ? timeInterval.getPeer() - : null; + final TimeInterval ti = (this.timeInterval != null) ? this.timeInterval.getPeer() : null; - List<WstColumn> columns = query.list(); - if (columns.isEmpty()) { - log.debug("source: " + source); - peer = new WstColumn( - w, name, description, source, position, ti); - session.save(peer); + if ((columns == null) || columns.isEmpty()) { + log.debug("source: " + this.source); + this.peer = new WstColumn( + w, this.name, this.description, this.source, this.position, ti); + session.save(this.peer); + this.storeMode = StoreMode.INSERT; } else { - peer = columns.get(0); + this.peer = columns.get(0); + this.storeMode = StoreMode.UPDATE; } } - return peer; + return this.peer; } }
--- a/backend/src/main/java/org/dive4elements/river/importer/ImportWstColumnQRange.java Fri Apr 06 14:13:14 2018 +0200 +++ b/backend/src/main/java/org/dive4elements/river/importer/ImportWstColumnQRange.java Sun Apr 08 18:07:06 2018 +0200 @@ -8,71 +8,81 @@ package org.dive4elements.river.importer; +import java.util.List; + +import org.dive4elements.river.importer.common.StoreMode; +import org.dive4elements.river.model.River; +import org.dive4elements.river.model.WstColumn; import org.dive4elements.river.model.WstColumnQRange; import org.dive4elements.river.model.WstQRange; -import org.dive4elements.river.model.WstColumn; -import org.dive4elements.river.model.River; - +import org.hibernate.Query; import org.hibernate.Session; -import org.hibernate.Query; - -import java.util.List; public class ImportWstColumnQRange { protected ImportWstColumn wstColumn; protected ImportWstQRange qRange; + protected StoreMode storeMode; + protected WstColumnQRange peer; public ImportWstColumnQRange() { } public ImportWstColumnQRange( - ImportWstColumn wstColumn, - ImportWstQRange qRange - ) { + final ImportWstColumn wstColumn, + final ImportWstQRange qRange + ) { this.wstColumn = wstColumn; this.qRange = qRange; + this.storeMode = StoreMode.NONE; } public ImportWstColumn getWstColumn() { - return wstColumn; + return this.wstColumn; } - public void setWstColumn(ImportWstColumn wstColumn) { + public void setWstColumn(final ImportWstColumn wstColumn) { this.wstColumn = wstColumn; } public ImportWstQRange getQRange() { - return qRange; + return this.qRange; } - public void setQRange(ImportWstQRange qRange) { + public void setQRange(final ImportWstQRange qRange) { this.qRange = qRange; } - public WstColumnQRange getPeer(River river) { - if (peer == null) { - WstColumn c = wstColumn.getPeer(river); - WstQRange q = qRange.getPeer(river); - Session session = ImporterSession.getInstance() - .getDatabaseSession(); - Query query = session.createQuery( - "from WstColumnQRange where " + - "wstColumn=:c and wstQRange=:q"); - query.setParameter("c", c); - query.setParameter("q", q); - List<WstColumnQRange> cols = query.list(); - if (cols.isEmpty()) { - peer = new WstColumnQRange(c, q); - session.save(peer); + public WstColumnQRange getPeer(final River river) { + if (this.peer == null) { + final WstColumn c = this.wstColumn.getPeer(river); + final WstQRange q = this.qRange.getPeer(river); + List<WstColumnQRange> cols; + final Session session = ImporterSession.getInstance() + .getDatabaseSession(); + if (this.wstColumn.storeMode == StoreMode.INSERT) + cols = null; + else { + final Query query = session.createQuery( + "from WstColumnQRange where " + + "wstColumn=:c and wstQRange=:q"); + query.setParameter("c", c); + query.setParameter("q", q); + cols = query.list(); + } + if ((cols == null) || cols.isEmpty()) { + this.peer = new WstColumnQRange(c, q); + session.save(this.peer); + this.storeMode = StoreMode.INSERT; } else { - peer = cols.get(0); + this.peer = cols.get(0); + this.storeMode = StoreMode.UPDATE; } } - return peer; + return this.peer; } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :