sascha@167: package de.intevation.flys.model; sascha@167: sascha@167: import java.io.Serializable; sascha@167: ingo@465: import java.math.BigDecimal; ingo@465: sascha@168: import javax.persistence.Entity; sascha@168: import javax.persistence.Id; sascha@168: import javax.persistence.Table; sascha@168: import javax.persistence.GeneratedValue; sascha@168: import javax.persistence.Column; sascha@168: import javax.persistence.SequenceGenerator; sascha@174: import javax.persistence.OneToMany; sascha@174: import javax.persistence.JoinColumn; sascha@168: import javax.persistence.GenerationType; sascha@168: sascha@174: import java.util.List; sascha@174: ingo@472: import org.hibernate.Session; ingo@472: import org.hibernate.Query; ingo@472: ingo@472: import de.intevation.flys.backend.SessionHolder; ingo@472: ingo@472: sascha@168: @Entity sascha@168: @Table(name = "rivers") sascha@167: public class River sascha@167: implements Serializable sascha@167: { sascha@168: private Integer id; sascha@167: sascha@168: private String name; sascha@167: sascha@174: private List gauges; sascha@174: sascha@168: @Id sascha@168: @SequenceGenerator( sascha@171: name = "SEQUENCE_RIVERS_ID_SEQ", sascha@169: sequenceName = "RIVERS_ID_SEQ", sascha@169: allocationSize = 1) sascha@168: @GeneratedValue( sascha@168: strategy = GenerationType.SEQUENCE, sascha@171: generator = "SEQUENCE_RIVERS_ID_SEQ") sascha@168: @Column(name = "id") sascha@168: public Integer getId() { sascha@168: return id; sascha@168: } sascha@168: sascha@168: public void setId(Integer id) { sascha@167: this.id = id; sascha@167: } sascha@167: sascha@168: @Column(name = "name") sascha@168: public String getName() { sascha@168: return name; sascha@167: } sascha@167: sascha@167: public void setName(String name) { sascha@167: this.name = name; sascha@167: } sascha@167: sascha@167: public River() { sascha@167: } sascha@169: sascha@169: public River(String name) { sascha@169: this.name = name; sascha@169: } sascha@174: sascha@174: @OneToMany sascha@174: @JoinColumn(name="river_id") sascha@174: public List getGauges() { sascha@174: return gauges; sascha@174: } sascha@174: sascha@174: public void setGauges(List gauges) { sascha@174: this.gauges = gauges; sascha@174: } sascha@188: sascha@188: public String toString() { sascha@188: return name != null ? name : ""; sascha@188: } ingo@465: ingo@472: ingo@472: /** ingo@472: * This method returns the first gauge that is intersected by a and ingo@472: * b, ingo@472: * ingo@472: * @param a A start point. ingo@472: * @param b An end point. ingo@472: * ingo@472: * @return the first intersecting gauge. ingo@472: */ ingo@472: public Gauge determineGauge(double a, double b) { ingo@472: Session session = SessionHolder.HOLDER.get(); ingo@472: ingo@472: Query query = session.createQuery( ingo@472: "from Gauge where river=:river " + ingo@472: "and not (range.a > :b or range.b < :a) order by a"); ingo@472: query.setParameter("river", this); ingo@472: query.setParameter("a", new BigDecimal(a)); ingo@472: query.setParameter("b", new BigDecimal(b)); ingo@472: ingo@472: List gauges = query.list(); ingo@472: ingo@472: return gauges != null && gauges.size() > 0 ? gauges.get(0) : null; ingo@472: } ingo@472: ingo@465: /** ingo@465: * Returns the min and max distance of this river. The first position in the ingo@465: * resulting array contains the min distance, the second position the max ingo@465: * distance. ingo@465: * ingo@465: * @return the min and max distance of this river. ingo@465: */ ingo@465: public double[] determineMinMaxDistance() { ingo@465: if (gauges == null) { ingo@465: return null; ingo@465: } ingo@465: ingo@465: double minmax[] = new double[] { Double.MAX_VALUE, Double.MIN_VALUE }; ingo@465: ingo@465: for (Gauge g: gauges) { ingo@465: Range r = g.getRange(); ingo@465: ingo@465: double a = r.getA().doubleValue(); ingo@465: minmax[0] = minmax[0] < a ? minmax[0] : a; ingo@465: ingo@465: BigDecimal bigB = r.getB(); ingo@465: if (bigB != null) { ingo@465: double b = bigB.doubleValue(); ingo@465: minmax[1] = minmax[1] > b ? minmax[1] : b; ingo@465: } ingo@465: } ingo@465: ingo@465: return minmax; ingo@465: } sascha@167: } sascha@167: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :