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; felix@1233: import de.intevation.flys.model.MainValue; ingo@468: sascha@171: @Entity sascha@171: @Table(name = "gauges") sascha@167: public class Gauge sascha@167: implements Serializable sascha@167: { ingo@480: public static final int DEFAULT_SCALE = 100; ingo@480: 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@2371: private Long officialNumber; sascha@167: private Range range; sascha@167: sascha@203: private List dischargeTables; sascha@203: felix@1233: /** MainValues at this Gauge. */ felix@1233: protected List mainValues; felix@1233: 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@2371: Long officialNumber, sascha@192: Range range sascha@192: ) { sascha@2371: this.name = name; sascha@2371: this.river = river; sascha@2371: this.station = station; sascha@2371: this.aeo = aeo; sascha@2371: this.datum = datum; sascha@2371: this.officialNumber = officialNumber; sascha@2371: 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@2371: @Column(name = "official_number") sascha@2371: public Long getOfficialNumber() { sascha@2371: return officialNumber; sascha@2371: } sascha@2371: sascha@2371: public void setOfficialNumber(Long officialNumber) { sascha@2371: this.officialNumber = officialNumber; sascha@2371: } sascha@2371: 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@480: * Returns min and max W values of this gauge based with a DEFAULT_SCALE. ingo@480: * ingo@480: * @return min and max W value of this gauge [min,max]. ingo@480: */ ingo@480: public double[] determineMinMaxW() { ingo@480: return determineMinMaxW(DEFAULT_SCALE); ingo@480: } ingo@480: ingo@480: ingo@480: /** ingo@468: * Returns min and max W values of this gauge. ingo@468: * felix@1233: * @return the min and max W value of this gauge [min,max]. ingo@468: */ ingo@480: public double[] determineMinMaxW(int scale) { 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@480: ((BigDecimal) result[0]).doubleValue() * scale, ingo@480: ((BigDecimal) result[1]).doubleValue() * scale} ingo@468: : null; ingo@468: } felix@1233: felix@1233: @OneToMany felix@1233: @JoinColumn(name = "gauge_id") felix@1233: public List getMainValues() { felix@1233: return mainValues; felix@1233: } felix@1233: felix@1233: public void setMainValues(List mainValues) { felix@1233: this.mainValues = mainValues; felix@1233: } sascha@167: } sascha@167: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :