sascha@167: package de.intevation.flys.model; sascha@167: sascha@167: import java.io.Serializable; ingo@468: import java.math.BigDecimal; ingo@468: import java.util.List; sascha@167: sascha@171: import javax.persistence.Entity; sascha@171: import javax.persistence.Id; sascha@171: import javax.persistence.Table; sascha@171: import javax.persistence.GeneratedValue; sascha@171: import javax.persistence.Column; sascha@171: import javax.persistence.SequenceGenerator; sascha@171: import javax.persistence.GenerationType; sascha@174: import javax.persistence.JoinColumn; sascha@174: import javax.persistence.OneToOne; sascha@475: import javax.persistence.OneToMany; sascha@171: ingo@468: import org.apache.log4j.Logger; ingo@468: ingo@468: import org.hibernate.Session; ingo@468: import org.hibernate.Query; ingo@468: ingo@468: import de.intevation.flys.backend.SessionHolder; ingo@468: ingo@468: sascha@171: @Entity sascha@171: @Table(name = "wsts") sascha@167: public class Wst sascha@167: implements Serializable sascha@167: { ingo@468: private static Logger logger = Logger.getLogger(Wst.class); ingo@468: sascha@171: private Integer id; sascha@171: private River river; sascha@171: private String description; sascha@467: private Integer kind; sascha@167: sascha@475: private List columns; sascha@475: sascha@167: public Wst() { sascha@167: } sascha@167: sascha@201: public Wst(River river, String description) { sascha@467: this(river, description, 0); sascha@467: } sascha@467: sascha@467: public Wst(River river, String description, Integer kind) { sascha@201: this.river = river; sascha@201: this.description = description; sascha@467: this.kind = kind; sascha@201: } sascha@201: sascha@171: @Id sascha@171: @SequenceGenerator( sascha@171: name = "SEQUENCE_WSTS_ID_SEQ", sascha@171: sequenceName = "WSTS_ID_SEQ", sascha@171: allocationSize = 1) sascha@171: @GeneratedValue( sascha@171: strategy = GenerationType.SEQUENCE, sascha@171: generator = "SEQUENCE_WSTS_ID_SEQ") sascha@171: @Column(name = "id") sascha@171: public Integer getId() { sascha@171: return id; sascha@171: } sascha@171: sascha@168: public void setId(Integer id) { sascha@167: this.id = id; sascha@167: } sascha@167: sascha@174: @OneToOne sascha@174: @JoinColumn(name = "river_id" ) sascha@174: public River getRiver() { sascha@174: return river; sascha@174: } sascha@174: sascha@167: public void setRiver(River river) { sascha@167: this.river = river; sascha@167: } sascha@167: sascha@172: @Column(name = "description") sascha@172: public String getDescription() { sascha@172: return description; sascha@172: } sascha@172: sascha@167: public void setDescription(String description) { sascha@167: this.description = description; sascha@167: } sascha@467: sascha@467: @Column(name = "kind") sascha@467: public Integer getKind() { sascha@467: return kind; sascha@467: } sascha@467: sascha@467: public void setKind(Integer kind) { sascha@467: this.kind = kind; sascha@467: } ingo@468: sascha@475: @OneToMany sascha@475: @JoinColumn(name="wst_id") sascha@475: public List getColumns() { sascha@475: return columns; sascha@475: } sascha@475: sascha@475: public void setColumns(List columns) { sascha@475: this.columns = columns; sascha@475: } ingo@468: ingo@468: /** ingo@468: * Determines the min and max Q values of this WST. The min value is placed ingo@468: * in the first field of the resulting array - the max value is placed in ingo@468: * the second field. ingo@468: * ingo@468: * @return the min and max Q values of this WST. ingo@468: */ ingo@468: public double[] determineMinMaxQ() { ingo@468: double[] ab = river.determineMinMaxDistance(); ingo@468: return determineMinMaxQ(new Range(ab[0], ab[1], river)); ingo@468: } ingo@468: ingo@468: ingo@468: /** ingo@468: * Determines the min and max Q values of this WST in the given range. The ingo@468: * min value is placed in the first field of the resulting array - the max ingo@468: * value is placed in the second field. ingo@468: * ingo@468: * @param range The range used for querying the Q values. ingo@468: * ingo@468: * @return the min and max Q values of this WST. ingo@468: */ ingo@468: public double[] determineMinMaxQ(Range range) { ingo@468: Session session = SessionHolder.HOLDER.get(); ingo@468: ingo@468: Query query = session.createQuery( ingo@468: "select min(q), max(q) from WstQRange where " + ingo@468: " id in " + ingo@468: " (select wstQRange.id from WstColumnQRange where " + ingo@468: " wstColumn.id in (select id from WstColumn where wst.id = :wst)) " + ingo@468: " and range.id in " + ingo@468: " (select id from Range where not (a > :end or b < :start))"); ingo@468: sascha@469: query.setParameter("wst", getId()); ingo@468: query.setParameter("start", range.getA()); sascha@469: query.setParameter("end", range.getB()); ingo@468: sascha@469: List results = query.list(); ingo@468: sascha@469: if (results.isEmpty()) { sascha@469: return null; sascha@469: } sascha@469: sascha@469: Object [] result = results.get(0); sascha@469: sascha@469: return new double [] { sascha@469: ((BigDecimal)result[0]).doubleValue(), sascha@469: ((BigDecimal)result[1]).doubleValue() }; ingo@468: } sascha@167: } sascha@167: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :