Mercurial > dive4elements > river
diff backend/src/main/java/org/dive4elements/river/model/River.java @ 8971:50416a0df385
Importer for the Schifffahrt (S-INFO) and Oekologie (U-INFO) files
author | mschaefer |
---|---|
date | Tue, 03 Apr 2018 10:18:30 +0200 |
parents | c4ce25093953 |
children | 7c8d62867876 |
line wrap: on
line diff
--- a/backend/src/main/java/org/dive4elements/river/model/River.java Tue Apr 03 10:02:01 2018 +0200 +++ b/backend/src/main/java/org/dive4elements/river/model/River.java Tue Apr 03 10:18:30 2018 +0200 @@ -8,8 +8,6 @@ package org.dive4elements.river.model; -import org.dive4elements.river.backend.SessionHolder; - import java.io.Serializable; import java.math.BigDecimal; import java.math.MathContext; @@ -29,12 +27,12 @@ import javax.persistence.SequenceGenerator; import javax.persistence.Table; +import org.apache.log4j.Logger; +import org.dive4elements.river.backend.SessionHolder; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.annotations.Type; -import org.apache.log4j.Logger; - @Entity @Table(name = "rivers") public class River @@ -51,8 +49,8 @@ public static final Comparator<Double> KM_CMP = new Comparator<Double>() { @Override - public int compare(Double a, Double b) { - double diff = a - b; + public int compare(final Double a, final Double b) { + final double diff = a - b; if (diff < -EPSILON) return -1; if (diff > EPSILON) return +1; return 0; @@ -77,46 +75,46 @@ @Id @SequenceGenerator( - name = "SEQUENCE_RIVERS_ID_SEQ", - sequenceName = "RIVERS_ID_SEQ", - allocationSize = 1) + name = "SEQUENCE_RIVERS_ID_SEQ", + sequenceName = "RIVERS_ID_SEQ", + allocationSize = 1) @GeneratedValue( - strategy = GenerationType.SEQUENCE, - generator = "SEQUENCE_RIVERS_ID_SEQ") + strategy = GenerationType.SEQUENCE, + generator = "SEQUENCE_RIVERS_ID_SEQ") @Column(name = "id") public Integer getId() { - return id; + return this.id; } - public void setId(Integer id) { + public void setId(final Integer id) { this.id = id; } @Column(name = "official_number") public Long getOfficialNumber() { - return officialNumber; + return this.officialNumber; } - public void setOfficialNumber(Long officialNumber) { + public void setOfficialNumber(final Long officialNumber) { this.officialNumber = officialNumber; } @Column(name = "name") public String getName() { - return name; + return this.name; } - public void setName(String name) { + public void setName(final String name) { this.name = name; } @Type(type="numeric_boolean") @Column(name = "km_up") public boolean getKmUp() { - return kmUp; + return this.kmUp; } - public void setKmUp(boolean kmUp) { + public void setKmUp(final boolean kmUp) { this.kmUp = kmUp; } @@ -125,14 +123,14 @@ return this.modelUuid; } - public void setModelUuid(String modelUuid) { + public void setModelUuid(final String modelUuid) { this.modelUuid = modelUuid; } public River() { } - public River(String name, Unit wstUnit, String modelUuid) { + public River(final String name, final Unit wstUnit, final String modelUuid) { this.name = name; this.modelUuid = modelUuid; this.wstUnit = wstUnit; @@ -141,20 +139,20 @@ @OneToMany @JoinColumn(name="river_id") public List<Gauge> getGauges() { - return gauges; + return this.gauges; } - public void setGauges(List<Gauge> gauges) { + public void setGauges(final List<Gauge> gauges) { this.gauges = gauges; } @OneToOne @JoinColumn(name = "wst_unit_id" ) public Unit getWstUnit() { - return wstUnit; + return this.wstUnit; } - public void setWstUnit(Unit wstUnit) { + public void setWstUnit(final Unit wstUnit) { this.wstUnit = wstUnit; } @@ -168,7 +166,7 @@ * @return The name River in the seddb. */ public String nameForSeddb() { - SeddbName alt = getSeddbName(); + final SeddbName alt = getSeddbName(); if (alt == null) { return getName(); } @@ -179,16 +177,16 @@ @OneToOne @JoinColumn(name = "seddb_name_id" ) public SeddbName getSeddbName() { - return seddbName; + return this.seddbName; } - public void setSeddbName(SeddbName name) { + public void setSeddbName(final SeddbName name) { this.seddbName = name; } @Override public String toString() { - return name != null ? name : ""; + return this.name != null ? this.name : ""; } @@ -202,16 +200,16 @@ * @return the intersecting gauges. */ public List<Gauge> determineGauges(double a, double b) { - Session session = SessionHolder.HOLDER.get(); - - if (a > b) { double t = a; a = b; b = t; } + final Session session = SessionHolder.HOLDER.get(); - Query query = session.createQuery( - "from Gauge where river=:river " + - "and not " + - "((:b < least(range.a, range.b)) or" + - " (:a > greatest(range.a, range.b)))" + - "order by a"); + if (a > b) { final double t = a; a = b; b = t; } + + final Query query = session.createQuery( + "from Gauge where river=:river " + + "and not " + + "((:b < least(range.a, range.b)) or" + + " (:a > greatest(range.a, range.b)))" + + "order by a"); query.setParameter("river", this); query.setParameter("a", new BigDecimal(a, PRECISION)); query.setParameter("b", new BigDecimal(b, PRECISION)); @@ -220,28 +218,28 @@ } public Gauge maxOverlap(double a, double b) { - List<Gauge> gauges = determineGauges(a, b); + final List<Gauge> gauges = determineGauges(a, b); if (gauges == null) { return null; } - if (a > b) { double t = a; a = b; b = t; } + if (a > b) { final double t = a; a = b; b = t; } double max = -Double.MAX_VALUE; Gauge result = null; - for (Gauge gauge: gauges) { - Range r = gauge.getRange(); + for (final Gauge gauge: gauges) { + final Range r = gauge.getRange(); double c = r.getA().doubleValue(); double d = r.getB().doubleValue(); - if (c > d) { double t = c; c = d; d = t; } + if (c > d) { final double t = c; c = d; d = t; } - double start = c >= a ? c : a; - double stop = d <= b ? d : b; + final double start = c >= a ? c : a; + final double stop = d <= b ? d : b; - double length = stop - start; + final double length = stop - start; if (length > max) { max = length; @@ -252,17 +250,17 @@ return result; } - public Gauge determineGaugeByName(String name) { - Session session = SessionHolder.HOLDER.get(); - Query query = session.createQuery( - "from Gauge where river=:river and name=:name"); + public Gauge determineGaugeByName(final String name) { + final Session session = SessionHolder.HOLDER.get(); + final Query query = session.createQuery( + "from Gauge where river=:river and name=:name"); query.setParameter("river", this); query.setParameter("name", name); - List<Gauge> gauges = query.list(); + final List<Gauge> gauges = query.list(); return gauges.isEmpty() ? null : gauges.get(0); } - public Gauge determineGaugeByPosition(double p) { + public Gauge determineGaugeByPosition(final double p) { // Per default, we prefer the gauge downstream return determineGaugeByPosition(p, getKmUp()); } @@ -272,16 +270,16 @@ * @param kmLower At boundary of two gauge ranges, should gauge at lower * km be returned? */ - public Gauge determineGaugeByPosition(double p, boolean kmLower) { - Session session = SessionHolder.HOLDER.get(); - Query query = session.createQuery( - "from Gauge g where river=:river " + - "and :p between " + - "least(g.range.a, g.range.b) and " + - "greatest(g.range.a, g.range.b)"); + public Gauge determineGaugeByPosition(final double p, final boolean kmLower) { + final Session session = SessionHolder.HOLDER.get(); + final Query query = session.createQuery( + "from Gauge g where river=:river " + + "and :p between " + + "least(g.range.a, g.range.b) and " + + "greatest(g.range.a, g.range.b)"); query.setParameter("river", this); query.setParameter("p", new BigDecimal(p, PRECISION)); - List<Gauge> gauges = query.list(); + final List<Gauge> gauges = query.list(); if (gauges.isEmpty()) { return null; } @@ -291,19 +289,19 @@ if (gauges.size() > 2) { // TODO: database schema should prevent this. log.warn("More than two gauge ranges overlap km " + p + - ". Returning arbitrary result."); + ". Returning arbitrary result."); } - Gauge g0 = gauges.get(0); - Gauge g1 = gauges.get(1); + final Gauge g0 = gauges.get(0); + final Gauge g1 = gauges.get(1); if (kmLower) { return - g0.getStation().doubleValue() < g1.getStation().doubleValue() - ? g0 - : g1; + g0.getStation().doubleValue() < g1.getStation().doubleValue() + ? g0 + : g1; } return g0.getStation().doubleValue() > g1.getStation().doubleValue() - ? g0 - : g1; + ? g0 + : g1; } @@ -311,46 +309,46 @@ * @param s station at which the gauge is requested. * @return Gauge within tolerance at given station. null if there is none. */ - public Gauge determineGaugeAtStation(double s) { - Session session = SessionHolder.HOLDER.get(); + public Gauge determineGaugeAtStation(final double s) { + final Session session = SessionHolder.HOLDER.get(); - Query query = session.createQuery( - "from Gauge where river.id=:river " + - "and station between :a and :b"); + final Query query = session.createQuery( + "from Gauge where river.id=:river " + + "and station between :a and :b"); query.setParameter("river", getId()); query.setParameter("a", new BigDecimal(s - GAUGE_EPSILON)); query.setParameter("b", new BigDecimal(s + GAUGE_EPSILON)); - List<Gauge> gauges = query.list(); + final List<Gauge> gauges = query.list(); if (gauges.size() > 1) { log.warn("More than one gauge found at km " + s + - " within +-" + GAUGE_EPSILON + - ". Returning arbitrary result."); + " within +-" + GAUGE_EPSILON + + ". Returning arbitrary result."); } return gauges.isEmpty() ? null : gauges.get(0); } public double[] determineMinMaxQ() { - Session session = SessionHolder.HOLDER.get(); + final Session session = SessionHolder.HOLDER.get(); - Query query = session.createQuery( - "select min(wqr.q) as min, max(wqr.q) as max " + - "from Wst as w " + - "join w.columns as wc " + - "join wc.columnQRanges as wcqr " + - "join wcqr.wstQRange as wqr " + - "where w.kind = 0 and river_id = :river"); + final Query query = session.createQuery( + "select min(wqr.q) as min, max(wqr.q) as max " + + "from Wst as w " + + "join w.columns as wc " + + "join wc.columnQRanges as wcqr " + + "join wcqr.wstQRange as wqr " + + "where w.kind = 0 and river_id = :river"); query.setParameter("river", getId()); - double minmax[] = new double[] { Double.MAX_VALUE, -Double.MAX_VALUE }; + final double minmax[] = new double[] { Double.MAX_VALUE, -Double.MAX_VALUE }; - List<Object> results = query.list(); + final List<Object> results = query.list(); if (!results.isEmpty()) { - Object[] arr = (Object[]) results.get(0); - BigDecimal minq = (BigDecimal)arr[0]; - BigDecimal maxq = (BigDecimal)arr[1]; + final Object[] arr = (Object[]) results.get(0); + final BigDecimal minq = (BigDecimal)arr[0]; + final BigDecimal maxq = (BigDecimal)arr[1]; minmax[0] = minq.doubleValue(); minmax[1] = maxq.doubleValue(); } @@ -362,11 +360,11 @@ * Determine reference gauge dependent on direction of calculation * for a range calculation, otherwise dependent on flow direction. */ - public Gauge determineRefGauge(double[] range, boolean isRange) { + public Gauge determineRefGauge(final double[] range, final boolean isRange) { if (isRange) { return determineGaugeByPosition( - range[0], - range[0] > range[1]); + range[0], + range[0] > range[1]); } else { return determineGaugeByPosition(range[0]); @@ -381,21 +379,21 @@ * @return the min and max distance of this river. */ public double[] determineMinMaxDistance() { - Session session = SessionHolder.HOLDER.get(); + final Session session = SessionHolder.HOLDER.get(); - Query query = session.createQuery( - "select min(range.a), max(range.b) from Gauge " - + "where river=:river " - + "and range is not null"); + final Query query = session.createQuery( + "select min(range.a), max(range.b) from Gauge " + + "where river=:river " + + "and range is not null"); query.setParameter("river", this); - List<Object[]> result = query.list(); + final List<Object[]> result = query.list(); if (!result.isEmpty()) { - Object[] minMax = result.get(0); + final Object[] minMax = result.get(0); if (minMax[0] != null && minMax[1] != null) { return new double[] { ((BigDecimal)minMax[0]).doubleValue(), - ((BigDecimal)minMax[1]).doubleValue() }; + ((BigDecimal)minMax[1]).doubleValue() }; } } @@ -403,12 +401,12 @@ } public Map<Double, Double> queryGaugeDatumsKMs() { - List<Gauge> gauges = getGauges(); - Map<Double, Double> result = new TreeMap<Double, Double>(KM_CMP); + final List<Gauge> gauges = getGauges(); + final Map<Double, Double> result = new TreeMap<>(KM_CMP); - for (Gauge gauge: gauges) { - BigDecimal km = gauge.getStation(); - BigDecimal datum = gauge.getDatum(); + for (final Gauge gauge: gauges) { + final BigDecimal km = gauge.getStation(); + final BigDecimal datum = gauge.getDatum(); if (km != null && datum != null) { result.put(km.doubleValue(), datum.doubleValue()); } @@ -417,5 +415,17 @@ return result; } + /** + * Searches the gauges list of the river for a gauge number or a gauge name + */ + public Gauge findGauge(final long number, final String name) { + for (final Gauge gauge : getGauges()) { + if (gauge.getOfficialNumber().longValue() == number) + return gauge; + if (gauge.getName().equalsIgnoreCase(name)) + return gauge; + } + return null; + } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :