view flys-backend/src/main/java/de/intevation/flys/model/CrossSection.java @ 5407:db5def5456ce

Only match strings vor field values
author Andre Heinecke <aheinecke@intevation.de>
date Mon, 25 Mar 2013 17:00:37 +0100
parents 2ae732e2c65c
children
line wrap: on
line source
package de.intevation.flys.model;

import java.io.Serializable;

import java.util.List;
import java.util.ArrayList;

import java.awt.geom.Point2D;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.GeneratedValue;
import javax.persistence.Column;
import javax.persistence.SequenceGenerator;
import javax.persistence.GenerationType;
import javax.persistence.OneToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.JoinColumn;

import java.math.MathContext;
import java.math.BigDecimal;

import org.hibernate.Session;
import org.hibernate.SQLQuery;
import org.hibernate.Query;

import org.hibernate.type.StandardBasicTypes;

import de.intevation.flys.backend.SessionHolder;

@Entity
@Table(name = "cross_sections")
public class CrossSection
implements   Serializable
{
    public static final MathContext PRECISION = new MathContext(6);

    public static final String SQL_FAST_CROSS_SECTION_LINES =
        "SELECT km, x, y, csl.id AS csl_id " +
        "FROM cross_section_lines csl JOIN cross_section_points csp " +
        "ON csp.cross_section_line_id = csl.id " +
        "WHERE csl.cross_section_id = :cs_id AND " +
        "km between :from_km AND :to_km " +
        "ORDER BY csl.id, csp.col_pos";

    private Integer                id;
    private River                  river;
    private TimeInterval           timeInterval;
    private String                 description;
    private List<CrossSectionLine> lines;

    public CrossSection() {
    }

    public CrossSection(
        River        river,
        TimeInterval timeInterval,
        String       description
    ) {
        this.river        = river;
        this.timeInterval = timeInterval;
        this.description  = description;
    }

    @Id
    @SequenceGenerator(
        name           = "SEQUENCE_CROSS_SECTIONS_ID_SEQ",
        sequenceName   = "CROSS_SECTIONS_ID_SEQ",
        allocationSize = 1)
    @GeneratedValue(
        strategy  = GenerationType.SEQUENCE,
        generator = "SEQUENCE_CROSS_SECTIONS_ID_SEQ")
    @Column(name = "id")
    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    @OneToOne
    @JoinColumn(name = "river_id")
    public River getRiver() {
        return river;
    }

    public void setRiver(River river) {
        this.river = river;
    }

    @OneToOne
    @JoinColumn(name = "time_interval_id")
    public TimeInterval getTimeInterval() {
        return timeInterval;
    }

    public void setTimeInterval(TimeInterval timeInterval) {
        this.timeInterval = timeInterval;
    }

    @Column(name = "description")
    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    @OneToMany
    @OrderBy("km")
    @JoinColumn(name="cross_section_id")
    public List<CrossSectionLine> getLines() {
        return lines;
    }

    public void setLines(List<CrossSectionLine> lines) {
        this.lines = lines;
    }

    public List<CrossSectionLine> getLines(double startKm, double endKm) {
        Session session = SessionHolder.HOLDER.get();
        Query query = session.createQuery(
            "from CrossSectionLine where crossSection=:crossSection " +
            "and km between :startKm and :endKm order by km");
        query.setParameter("crossSection", this);
        query.setParameter("startKm", new BigDecimal(startKm, PRECISION));
        query.setParameter("endKm", new BigDecimal(endKm, PRECISION));

        return query.list();
    }

    public List<FastCrossSectionLine> getFastLines(
        double startKm,
        double endKm
    ) {
        Session session = SessionHolder.HOLDER.get();

        SQLQuery sqlQuery = session.createSQLQuery(SQL_FAST_CROSS_SECTION_LINES)
            .addScalar("km",     StandardBasicTypes.DOUBLE)
            .addScalar("x",      StandardBasicTypes.DOUBLE)
            .addScalar("y",      StandardBasicTypes.DOUBLE)
            .addScalar("csl_id", StandardBasicTypes.INTEGER);

        sqlQuery
            .setInteger("cs_id",  getId())
            .setDouble("from_km", startKm)
            .setDouble("to_km",   endKm);

        List<Object []> results = sqlQuery.list();

        ArrayList<Point2D> points = new ArrayList<Point2D>(500);
        ArrayList<FastCrossSectionLine> lines =
            new ArrayList<FastCrossSectionLine>();

        Integer lastId = null;
        Double  lastKm = null;

        for (Object [] result: results) {
            Double  km = (Double)result[0];
            Double  x  = (Double)result[1];
            Double  y  = (Double)result[2];
            Integer id = (Integer)result[3];

            if (lastId != null && !lastId.equals(id)) {
                points.trimToSize();
                FastCrossSectionLine line =
                    new FastCrossSectionLine(lastKm, points);
                lines.add(line);
                points = new ArrayList<Point2D>(500);
            }

            Point2D p = new Point2D.Double(x, y);

            if (CrossSectionLine.isValid(p)) {
                points.add(p);
            }

            lastKm = km;
            lastId = id;
        }

        if (lastId != null) {
            points.trimToSize();
            FastCrossSectionLine line =
                new FastCrossSectionLine(lastKm, points);
            lines.add(line);
        }

        lines.trimToSize();

        return lines;
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org