Mercurial > dive4elements > river
diff flys-backend/src/main/java/de/intevation/flys/model/Wst.java @ 3813:6aeee2250418 pre2.6-2011-12-05
merged flys-backend/pre2.6-2011-12-05
author | Thomas Arendsen Hein <thomas@intevation.de> |
---|---|
date | Fri, 28 Sep 2012 12:14:54 +0200 |
parents | 0acf28a3d28a |
children | 027736510a30 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/flys-backend/src/main/java/de/intevation/flys/model/Wst.java Fri Sep 28 12:14:54 2012 +0200 @@ -0,0 +1,159 @@ +package de.intevation.flys.model; + +import java.io.Serializable; +import java.math.BigDecimal; +import java.util.List; + +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.JoinColumn; +import javax.persistence.OneToOne; +import javax.persistence.OneToMany; + +import org.apache.log4j.Logger; + +import org.hibernate.Session; +import org.hibernate.Query; + +import de.intevation.flys.backend.SessionHolder; + + +@Entity +@Table(name = "wsts") +public class Wst +implements Serializable +{ + private static Logger logger = Logger.getLogger(Wst.class); + + private Integer id; + private River river; + private String description; + private Integer kind; + + private List<WstColumn> columns; + + public Wst() { + } + + public Wst(River river, String description) { + this(river, description, 0); + } + + public Wst(River river, String description, Integer kind) { + this.river = river; + this.description = description; + this.kind = kind; + } + + @Id + @SequenceGenerator( + name = "SEQUENCE_WSTS_ID_SEQ", + sequenceName = "WSTS_ID_SEQ", + allocationSize = 1) + @GeneratedValue( + strategy = GenerationType.SEQUENCE, + generator = "SEQUENCE_WSTS_ID_SEQ") + @Column(name = "id") + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + @OneToOne + @JoinColumn(name = "river_id" ) + public River getRiver() { + return river; + } + + public void setRiver(River river) { + this.river = river; + } + + @Column(name = "description") + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + @Column(name = "kind") + public Integer getKind() { + return kind; + } + + public void setKind(Integer kind) { + this.kind = kind; + } + + @OneToMany + @JoinColumn(name="wst_id") + public List<WstColumn> getColumns() { + return columns; + } + + public void setColumns(List<WstColumn> columns) { + this.columns = columns; + } + + + /** + * Determines the min and max Q values of this WST. The min value is placed + * in the first field of the resulting array - the max value is placed in + * the second field. + * + * @return the min and max Q values of this WST. + */ + public double[] determineMinMaxQ() { + double[] ab = river.determineMinMaxDistance(); + return determineMinMaxQ(new Range(ab[0], ab[1], river)); + } + + + /** + * Determines the min and max Q values of this WST in the given range. The + * min value is placed in the first field of the resulting array - the max + * value is placed in the second field. + * + * @param range The range used for querying the Q values. + * + * @return the min and max Q values of this WST. + */ + public double[] determineMinMaxQ(Range range) { + Session session = SessionHolder.HOLDER.get(); + + Query query = session.createQuery( + "select min(q), max(q) from WstQRange where " + + " id in " + + " (select wstQRange.id from WstColumnQRange where " + + " wstColumn.id in (select id from WstColumn where wst.id = :wst)) " + + " and range.id in " + + " (select id from Range where not (a > :end or b < :start))"); + + query.setParameter("wst", getId()); + query.setParameter("start", range.getA()); + query.setParameter("end", range.getB()); + + List<Object []> results = query.list(); + + if (results.isEmpty()) { + return null; + } + + Object [] result = results.get(0); + + return new double [] { + ((BigDecimal)result[0]).doubleValue(), + ((BigDecimal)result[1]).doubleValue() }; + } +} +// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :