view flys-backend/src/main/java/de/intevation/flys/importer/ImportRange.java @ 188:003ac16812dd

Store annotations in backend. flys-backend/trunk@1518 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Fri, 18 Mar 2011 12:10:33 +0000
parents cf8cbcb6a10d
children bc3747a371cc
line wrap: on
line source
package de.intevation.flys.importer;

import de.intevation.flys.model.Range;
import de.intevation.flys.model.River;

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

import java.util.List;

import java.math.BigDecimal;

import org.apache.log4j.Logger;

public class ImportRange
implements   Comparable<ImportRange>
{
    private static Logger log = Logger.getLogger(ImportRange.class);

    protected Double from;
    protected Double to;

    protected Range peer;

    public ImportRange() {
    }

    public ImportRange(Double from, Double to) {
        this.from = from;
        this.to   = to;
    }

    private static final int compare(Double a, Double b) {
        if (a == null && b == null) {
            return 0;
        }
        if (a == null && b != null) {
            return -1;
        }
        if (a != null && b == null) {
            return +1;
        }
        return a.compareTo(b);
    }

    public int compareTo(ImportRange other) {
        int cmp = compare(from, other.from);
        if (cmp != 0) return cmp;
        return compare(to, other.to);
    }

    public Double getFrom() {
        return from;
    }

    public void setFrom(Double from) {
        this.from = from;
    }

    public Double getTo() {
        return to;
    }

    public void setTo(Double to) {
        this.to = to;
    }

    public Range getPeer(River river) {
        if (peer == null) {
            Session session = Importer.sessionHolder.get();
            Query query = session.createQuery(
                "from Range where a=:a and b=:b and river.id=:river");
            BigDecimal a = from != null ? BigDecimal.valueOf(from) : null;
            BigDecimal b = to   != null ? BigDecimal.valueOf(to) : null;
            query.setParameter("a", a);
            query.setParameter("b", b);
            query.setParameter("river", river.getId());
            List<Range> ranges = query.list();
            if (ranges.isEmpty()) {
                peer = new Range(a, b, river);
                session.save(peer);
            }
            else {
                peer = ranges.get(0);
            }
        }
        return peer;
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org