ingo@1230: package de.intevation.flys.model; ingo@1230: ingo@1230: import java.io.Serializable; ingo@1230: import java.math.BigDecimal; ingo@1235: import java.util.List; ingo@1230: ingo@1230: import javax.persistence.Column; ingo@1230: import javax.persistence.Entity; ingo@1230: import javax.persistence.Id; ingo@1230: import javax.persistence.JoinColumn; ingo@1230: import javax.persistence.OneToOne; ingo@1230: import javax.persistence.Table; ingo@1230: ingo@1235: import org.hibernate.Query; ingo@1235: import org.hibernate.Session; ingo@1230: import org.hibernate.annotations.Type; ingo@1230: ingo@1230: import com.vividsolutions.jts.geom.LineString; ingo@1230: ingo@1235: import de.intevation.flys.backend.SessionHolder; ingo@1235: ingo@1230: ingo@1230: @Entity ingo@1230: @Table(name = "cross_section_tracks") ingo@1230: public class CrossSectionTrack ingo@1230: implements Serializable ingo@1230: { ingo@1230: private Integer id; ingo@1230: private River river; ingo@3330: private String name; ingo@1230: private LineString geom; ingo@1230: private BigDecimal km; ingo@1230: private BigDecimal z; ingo@1230: ingo@1230: public CrossSectionTrack() { ingo@1230: } ingo@1230: ingo@1230: ingo@1230: @Id ingo@1230: @Column(name = "id") ingo@1230: public Integer getId() { ingo@1230: return id; ingo@1230: } ingo@1230: ingo@1230: ingo@1230: public void setId(Integer id) { ingo@1230: this.id = id; ingo@1230: } ingo@1230: ingo@1230: ingo@1230: @OneToOne ingo@1230: @JoinColumn(name = "river_id") ingo@1230: public River getRiver() { ingo@1230: return river; ingo@1230: } ingo@1230: ingo@1230: ingo@1230: public void setRiver(River river) { ingo@1230: this.river = river; ingo@1230: } ingo@1230: ingo@1230: ingo@3330: @Column(name = "name") ingo@3330: public String getName() { ingo@3330: return name; ingo@3330: } ingo@3330: ingo@3330: ingo@3330: public void setName(String name) { ingo@3330: this.name = name; ingo@3330: } ingo@3330: ingo@3330: ingo@1230: @Column(name = "geom") ingo@1230: @Type(type = "org.hibernatespatial.GeometryUserType") ingo@1230: public LineString getGeom() { ingo@1230: return geom; ingo@1230: } ingo@1230: ingo@1230: ingo@1230: public void setGeom(LineString geom) { ingo@1230: this.geom = geom; ingo@1230: } ingo@1230: ingo@1230: ingo@1230: @Column(name = "km") ingo@1230: public BigDecimal getKm() { ingo@1230: return km; ingo@1230: } ingo@1230: ingo@1230: ingo@1230: public void setKm(BigDecimal km) { ingo@1230: this.km = km; ingo@1230: } ingo@1230: ingo@1230: ingo@1230: @Column(name = "z") ingo@1230: public BigDecimal getZ() { ingo@1230: return z; ingo@1230: } ingo@1230: ingo@1230: ingo@1230: public void setZ(BigDecimal z) { ingo@1230: this.z = z; ingo@1230: } ingo@1235: ingo@1235: ingo@1235: public static List getCrossSectionTrack( ingo@1235: String river) ingo@1235: { ingo@1235: Session session = SessionHolder.HOLDER.get(); ingo@1235: ingo@1235: Query query = session.createQuery( ingo@1235: "from CrossSectionTrack where river.name =:river"); ingo@1235: query.setParameter("river", river); ingo@1235: ingo@1235: return query.list(); ingo@1235: } ingo@2341: ingo@2341: ingo@3330: public static List getCrossSectionTrack( ingo@3330: String river, ingo@3330: String name ingo@3330: ) { ingo@3330: Session session = SessionHolder.HOLDER.get(); ingo@3330: ingo@3330: Query query = session.createQuery( ingo@3330: "from CrossSectionTrack as cst " + ingo@3330: " where river.name =:river" + ingo@3330: " and cst.name=:name"); ingo@3330: query.setParameter("river", river); ingo@3330: query.setParameter("name", name); ingo@3330: ingo@3330: return query.list(); ingo@3330: } ingo@3330: ingo@3330: ingo@2341: /** ingo@2341: * Returns the nearest CrossSectionTrack of river to a given ingo@2341: * km. ingo@2341: * ingo@2341: * @param river The name of a river. ingo@2341: * @param km The kilometer value. ingo@2341: * ingo@2341: * @return the nearest CrossSectionTrack to km of river river. ingo@2341: */ ingo@2341: public static CrossSectionTrack getCrossSectionTrack( ingo@2341: String river, ingo@2341: double km ingo@2341: ) { ingo@2341: Session session = SessionHolder.HOLDER.get(); ingo@2341: ingo@2341: Query query = session.createQuery( ingo@2341: "from CrossSectionTrack where river.name =:river " + ingo@2341: "order by abs( km - :mykm)"); ingo@2341: query.setParameter("river", river); ingo@2341: query.setParameter("mykm", new BigDecimal(km)); ingo@2341: ingo@2341: List cst = query.list(); ingo@2341: ingo@2341: return cst != null && !cst.isEmpty() ? cst.get(0) : null; ingo@2341: } ingo@1230: } ingo@1230: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :