sascha@167: package de.intevation.flys.model; sascha@167: sascha@167: import java.math.BigDecimal; sascha@167: sascha@167: import java.io.Serializable; sascha@167: sascha@203: import java.util.List; sascha@203: 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@203: import javax.persistence.OneToMany; sascha@171: 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 = "gauges") sascha@167: public class Gauge sascha@167: implements Serializable sascha@167: { sascha@171: private Integer id; sascha@167: private String name; sascha@167: private River river; sascha@167: private BigDecimal station; sascha@167: private BigDecimal aeo; sascha@167: private BigDecimal datum; sascha@167: private Range range; sascha@167: sascha@203: private List dischargeTables; sascha@203: sascha@167: public Gauge() { sascha@167: } sascha@167: sascha@192: public Gauge( sascha@192: String name, sascha@192: River river, sascha@192: BigDecimal station, sascha@192: BigDecimal aeo, sascha@192: BigDecimal datum, sascha@192: Range range sascha@192: ) { sascha@192: this.name = name; sascha@192: this.river = river; sascha@192: this.station = station; sascha@192: this.aeo = aeo; sascha@192: this.datum = datum; sascha@192: this.range = range; sascha@192: } sascha@192: sascha@171: @Id sascha@171: @SequenceGenerator( sascha@171: name = "SEQUENCE_GAUGES_ID_SEQ", sascha@171: sequenceName = "GAUGES_ID_SEQ", sascha@171: allocationSize = 1) sascha@171: @GeneratedValue( sascha@171: strategy = GenerationType.SEQUENCE, sascha@171: generator = "SEQUENCE_GAUGES_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@193: @OneToOne sascha@193: @JoinColumn(name = "river_id" ) sascha@193: public River getRiver() { sascha@193: return river; sascha@193: } sascha@193: sascha@193: public void setRiver(River river) { sascha@193: this.river = river; sascha@193: } sascha@193: sascha@172: @Column(name = "name") sascha@172: public String getName() { sascha@172: return name; sascha@172: } sascha@172: sascha@167: public void setName(String name) { sascha@167: this.name = name; sascha@167: } sascha@167: sascha@172: @Column(name = "station") // FIXME: type mapping needed sascha@172: public BigDecimal getStation() { sascha@172: return station; sascha@167: } sascha@167: sascha@167: public void setStation(BigDecimal station) { sascha@167: this.station = station; sascha@167: } sascha@167: sascha@172: @Column(name = "aeo") // FIXME: type mapping needed sascha@172: public BigDecimal getAeo() { sascha@172: return aeo; sascha@167: } sascha@167: sascha@167: public void setAeo(BigDecimal aeo) { sascha@167: this.aeo = aeo; sascha@167: } sascha@167: sascha@172: @Column(name = "datum") // FIXME: type mapping needed sascha@172: public BigDecimal getDatum() { sascha@172: return datum; sascha@167: } sascha@167: sascha@167: public void setDatum(BigDecimal datum) { sascha@167: this.datum = datum; sascha@167: } sascha@167: sascha@174: @OneToOne sascha@174: @JoinColumn(name = "range_id" ) sascha@172: public Range getRange() { sascha@172: return range; sascha@167: } sascha@167: sascha@167: public void setRange(Range range) { sascha@167: this.range = range; sascha@167: } sascha@203: sascha@203: @OneToMany sascha@203: @JoinColumn(name = "gauge_id") sascha@203: public List getDischargeTables() { sascha@203: return dischargeTables; sascha@203: } sascha@203: sascha@203: public void setDischargeTables(List dischargeTables) { sascha@203: this.dischargeTables = dischargeTables; sascha@203: } ingo@468: ingo@468: ingo@468: /** ingo@468: * Returns min and max W values of this gauge. ingo@468: * ingo@468: * @return the min and max W value of this gauge [min,max]. ingo@468: */ ingo@468: public double[] determineMinMaxW() { ingo@468: Session session = SessionHolder.HOLDER.get(); ingo@468: ingo@468: List tables = getDischargeTables(); ingo@468: DischargeTable dischargeTable = null; ingo@468: ingo@468: for (DischargeTable tmp: tables) { ingo@468: if (tmp.getKind() == 0) { ingo@468: dischargeTable = tmp; ingo@468: break; ingo@468: } ingo@468: } ingo@468: ingo@468: if (dischargeTable == null) { ingo@468: return null; ingo@468: } ingo@468: ingo@468: Query query = session.createQuery( ingo@468: "select min(w) as min, max(w) as max from DischargeTableValue " + ingo@468: "where table_id =:table"); ingo@468: query.setParameter("table", dischargeTable.getId()); ingo@468: ingo@468: List results = query.list(); ingo@468: Object[] result = (Object[]) results.get(0); ingo@468: ingo@468: return result != null ingo@468: ? new double[] { ingo@468: ((BigDecimal) result[0]).doubleValue(), ingo@468: ((BigDecimal) result[1]).doubleValue() } ingo@468: : null; ingo@468: } sascha@167: } sascha@167: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :