# HG changeset patch # User gernotbelger # Date 1535560450 -7200 # Node ID 80800fe126cdcad0b071ec902827238b04b254e4 # Parent e28303971812517bdbcee4b6a38e703261759630 Niemals Objekte mit == vergleichen!!! diff -r e28303971812 -r 80800fe126cd backend/src/main/java/org/dive4elements/river/model/CrossSection.java --- a/backend/src/main/java/org/dive4elements/river/model/CrossSection.java Wed Aug 29 17:58:02 2018 +0200 +++ b/backend/src/main/java/org/dive4elements/river/model/CrossSection.java Wed Aug 29 18:34:10 2018 +0200 @@ -8,152 +8,120 @@ package org.dive4elements.river.model; -import java.io.Serializable; - -import java.util.List; -import java.util.ArrayList; - import java.awt.geom.Point2D; +import java.io.Serializable; +import java.math.BigDecimal; +import java.math.MathContext; +import java.util.ArrayList; +import java.util.List; +import javax.persistence.Column; import javax.persistence.Entity; -import javax.persistence.Id; -import javax.persistence.Table; import javax.persistence.GeneratedValue; -import javax.persistence.Column; -import javax.persistence.SequenceGenerator; import javax.persistence.GenerationType; -import javax.persistence.OneToOne; -import javax.persistence.OneToMany; -import javax.persistence.OrderBy; +import javax.persistence.Id; import javax.persistence.JoinColumn; - -import java.math.MathContext; -import java.math.BigDecimal; - -import org.hibernate.Session; -import org.hibernate.SQLQuery; -import org.hibernate.Query; - -import org.hibernate.type.StandardBasicTypes; - -import org.dive4elements.river.backend.SessionHolder; +import javax.persistence.OneToMany; +import javax.persistence.OneToOne; +import javax.persistence.OrderBy; +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.SQLQuery; +import org.hibernate.Session; +import org.hibernate.type.StandardBasicTypes; @Entity @Table(name = "cross_sections") -public class CrossSection -implements Serializable -{ - private static Logger log = - Logger.getLogger(CrossSection.class); +public class CrossSection implements Serializable { + private static Logger log = Logger.getLogger(CrossSection.class); private static final MathContext PRECISION = new MathContext(6); - private static final String SQL_FAST_CROSS_SECTION_LINES = - "SELECT km, x, y, csl.id AS csl_id " + - "FROM cross_section_lines csl JOIN cross_section_points csp " + - "ON csp.cross_section_line_id = csl.id " + - "WHERE csl.cross_section_id = :cs_id AND " + - "km between :from_km AND :to_km " + - "ORDER BY csl.km, csl.id, csp.col_pos"; + private static final String SQL_FAST_CROSS_SECTION_LINES = "SELECT km, x, y, csl.id AS csl_id " + + "FROM cross_section_lines csl JOIN cross_section_points csp " + "ON csp.cross_section_line_id = csl.id " + + "WHERE csl.cross_section_id = :cs_id AND " + "km between :from_km AND :to_km " + "ORDER BY csl.km, csl.id, csp.col_pos"; - private static final String SQL_MIN_MAX = - "SELECT * FROM ( "+ - "SELECT cross_section_id, MIN(km) AS minkm, MAX(km) AS maxkm " + - "FROM cross_section_lines " + - "WHERE cross_section_id IN " + - " (SELECT id FROM cross_sections WHERE river_id = :river_id) " + - " GROUP BY cross_section_id" + - ") cs_ranges " + - "JOIN cross_sections cs ON cs_ranges.cross_section_id = cs.id " + - "LEFT OUTER JOIN time_intervals " + - " ON cs.time_interval_id = time_intervals.id " + - "WHERE :km BETWEEN minkm AND maxkm " + - "ORDER BY stop_time desc, start_time desc, :km - minkm"; + private static final String SQL_MIN_MAX = "SELECT * FROM ( " + "SELECT cross_section_id, MIN(km) AS minkm, MAX(km) AS maxkm " + "FROM cross_section_lines " + + "WHERE cross_section_id IN " + " (SELECT id FROM cross_sections WHERE river_id = :river_id) " + " GROUP BY cross_section_id" + ") cs_ranges " + + "JOIN cross_sections cs ON cs_ranges.cross_section_id = cs.id " + "LEFT OUTER JOIN time_intervals " + + " ON cs.time_interval_id = time_intervals.id " + "WHERE :km BETWEEN minkm AND maxkm " + + "ORDER BY stop_time desc, start_time desc, :km - minkm"; // Order by time interval missing. - private Integer id; - private River river; - private TimeInterval timeInterval; - private String description; + private Integer id; + private River river; + private TimeInterval timeInterval; + private String description; private List lines; public CrossSection() { } - public CrossSection( - River river, - TimeInterval timeInterval, - String description - ) { - this.river = river; + public CrossSection(final River river, final TimeInterval timeInterval, final String description) { + this.river = river; this.timeInterval = timeInterval; - this.description = description; + this.description = description; } @Id - @SequenceGenerator( - name = "SEQUENCE_CROSS_SECTIONS_ID_SEQ", - sequenceName = "CROSS_SECTIONS_ID_SEQ", - allocationSize = 1) - @GeneratedValue( - strategy = GenerationType.SEQUENCE, - generator = "SEQUENCE_CROSS_SECTIONS_ID_SEQ") + @SequenceGenerator(name = "SEQUENCE_CROSS_SECTIONS_ID_SEQ", sequenceName = "CROSS_SECTIONS_ID_SEQ", allocationSize = 1) + @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQUENCE_CROSS_SECTIONS_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; } @OneToOne @JoinColumn(name = "river_id") public River getRiver() { - return river; + return this.river; } - public void setRiver(River river) { + public void setRiver(final River river) { this.river = river; } @OneToOne @JoinColumn(name = "time_interval_id") public TimeInterval getTimeInterval() { - return timeInterval; + return this.timeInterval; } - public void setTimeInterval(TimeInterval timeInterval) { + public void setTimeInterval(final TimeInterval timeInterval) { this.timeInterval = timeInterval; } @Column(name = "description") public String getDescription() { - return description; + return this.description; } - public void setDescription(String description) { + public void setDescription(final String description) { this.description = description; } @OneToMany @OrderBy("km") - @JoinColumn(name="cross_section_id") + @JoinColumn(name = "cross_section_id") public List getLines() { - return lines; + return this.lines; } - public void setLines(List lines) { + public void setLines(final List lines) { this.lines = lines; } - public List getLines(double startKm, double endKm) { - Session session = SessionHolder.HOLDER.get(); - Query query = session.createQuery( - "from CrossSectionLine where crossSection=:crossSection " + - "and km between :startKm and :endKm order by km"); + public List getLines(final double startKm, final double endKm) { + final Session session = SessionHolder.HOLDER.get(); + final Query query = session.createQuery("from CrossSectionLine where crossSection=:crossSection " + "and km between :startKm and :endKm order by km"); query.setParameter("crossSection", this); query.setParameter("startKm", new BigDecimal(startKm, PRECISION)); query.setParameter("endKm", new BigDecimal(endKm, PRECISION)); @@ -162,45 +130,33 @@ } /** Get Lines from startkm to endkm, fast because direct usage of sql. */ - public List getFastLines( - double startKm, - double endKm - ) { - Session session = SessionHolder.HOLDER.get(); + public List getFastLines(final double startKm, final double endKm) { + final Session session = SessionHolder.HOLDER.get(); - SQLQuery sqlQuery = session.createSQLQuery( - SQL_FAST_CROSS_SECTION_LINES) - .addScalar("km", StandardBasicTypes.DOUBLE) - .addScalar("x", StandardBasicTypes.DOUBLE) - .addScalar("y", StandardBasicTypes.DOUBLE) - .addScalar("csl_id", StandardBasicTypes.INTEGER); + final SQLQuery sqlQuery = session.createSQLQuery(SQL_FAST_CROSS_SECTION_LINES).addScalar("km", StandardBasicTypes.DOUBLE) + .addScalar("x", StandardBasicTypes.DOUBLE).addScalar("y", StandardBasicTypes.DOUBLE).addScalar("csl_id", StandardBasicTypes.INTEGER); - sqlQuery - .setInteger("cs_id", getId()) - .setDouble("from_km", startKm) - .setDouble("to_km", endKm); + sqlQuery.setInteger("cs_id", getId()).setDouble("from_km", startKm).setDouble("to_km", endKm); - List results = sqlQuery.list(); + final List results = sqlQuery.list(); - ArrayList points = new ArrayList(500); - ArrayList lines = - new ArrayList(); + ArrayList points = new ArrayList<>(500); + final ArrayList lines = new ArrayList<>(); Integer lastId = null; - Double lastKm = null; + Double lastKm = null; - for (Object [] result: results) { - Double km = (Double)result[0]; - Double x = (Double)result[1]; - Double y = (Double)result[2]; - Integer id = (Integer)result[3]; + for (final Object[] result : results) { + final Double km = (Double) result[0]; + final Double x = (Double) result[1]; + final Double y = (Double) result[2]; + final Integer id = (Integer) result[3]; if (lastId != null && !lastId.equals(id)) { points.trimToSize(); - FastCrossSectionLine line = - new FastCrossSectionLine(lastKm, points); + final FastCrossSectionLine line = new FastCrossSectionLine(lastKm, points); lines.add(line); - points = new ArrayList(500); + points = new ArrayList<>(500); } points.add(new Point2D.Double(x, y)); @@ -211,8 +167,7 @@ if (lastId != null) { points.trimToSize(); - FastCrossSectionLine line = - new FastCrossSectionLine(lastKm, points); + final FastCrossSectionLine line = new FastCrossSectionLine(lastKm, points); lines.add(line); } @@ -224,34 +179,33 @@ /** * True if the given section is the "newest" for that river * and has values at km. - * @param km Given station. + * + * @param km + * Given station. * @return true if the section has the most advanced end of its validity * interval or the most advanced start of its validity interval. */ - public boolean shouldBeMaster(double km) { - Session session = SessionHolder.HOLDER.get(); - - SQLQuery sqlQuery = session.createSQLQuery(SQL_MIN_MAX) - .addScalar("cross_section_id", StandardBasicTypes.INTEGER); - - sqlQuery - .setInteger("river_id", getRiver().getId()) - .setDouble("km", km); + public boolean shouldBeMaster(final double km) { + final Session session = SessionHolder.HOLDER.get(); - List results = sqlQuery.list(); - final StringBuilder builder = new StringBuilder(); - - + final SQLQuery sqlQuery = session.createSQLQuery(SQL_MIN_MAX).addScalar("cross_section_id", StandardBasicTypes.INTEGER); + + sqlQuery.setInteger("river_id", getRiver().getId()).setDouble("km", km); + + final List results = sqlQuery.list(); + if (results.size() >= 1) { - final String resultIds = results.toString(); - builder.append("CrossSection line 243 ID = ").append(getId()).append(", Result SIZE = ").append(results.size()).append( ", Result_Ids = ").append(resultIds); + final String resultIds = results.toArray().toString(); + final StringBuilder builder = new StringBuilder(); + builder.append("CrossSection line 243 ID = ").append(getId()).append(", Result SIZE = ").append(results.size()).append(", Result_Ids = ") + .append(resultIds); log.debug(builder.toString()); - Integer result = results.get(0); - if (result == getId()) { + + final Integer result = results.get(0); + if (result.equals(getId())) { return true; } - } - else { + } else { log.warn("No CS found that could be master."); }