teichmann@5844: /* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde teichmann@5844: * Software engineering by Intevation GmbH teichmann@5844: * teichmann@5992: * This file is Free Software under the GNU AGPL (>=v3) teichmann@5844: * and comes with ABSOLUTELY NO WARRANTY! Check out the teichmann@5992: * documentation coming with Dive4Elements River for details. teichmann@5844: */ teichmann@5844: teichmann@5829: package org.dive4elements.river.importer; sascha@759: teichmann@5829: import org.dive4elements.river.model.Edge; sascha@759: sascha@759: import org.hibernate.Session; sascha@759: import org.hibernate.Query; sascha@759: sascha@759: import java.util.List; sascha@759: sascha@759: import java.math.BigDecimal; sascha@759: sascha@759: public class ImportEdge sascha@759: implements Comparable sascha@759: { sascha@759: protected BigDecimal top; sascha@759: protected BigDecimal bottom; sascha@759: sascha@759: protected Edge peer; sascha@759: sascha@759: public ImportEdge() { sascha@759: } sascha@759: sascha@759: public ImportEdge(BigDecimal top, BigDecimal bottom) { sascha@759: this.top = top; sascha@759: this.bottom = bottom; sascha@759: } sascha@759: sascha@759: public BigDecimal getTop() { sascha@759: return top; sascha@759: } sascha@759: sascha@759: public void setTop(BigDecimal top) { sascha@759: this.top = top; sascha@759: } sascha@759: sascha@759: public BigDecimal getBottom() { sascha@759: return bottom; sascha@759: } sascha@759: sascha@759: public void setBottom(BigDecimal bottom) { sascha@759: this.bottom = bottom; sascha@759: } sascha@759: sascha@759: private static final int compare(BigDecimal a, BigDecimal b) { sascha@759: if (a == null && b != null) return -1; sascha@759: if (a != null && b == null) return +1; sascha@759: if (a == null && b == null) return 0; sascha@759: return a.compareTo(b); sascha@759: } sascha@759: sascha@759: public int compareTo(ImportEdge other) { sascha@759: int cmp = compare(top, other.top); sascha@759: return cmp != 0 ? cmp : compare(bottom, other.bottom); sascha@759: } sascha@759: sascha@759: public Edge getPeer() { sascha@759: if (peer == null) { tom@8856: Session session = ImporterSession.getInstance() tom@8856: .getDatabaseSession(); sascha@759: Query query = session.createQuery( sascha@759: "from Edge where top=:top and bottom=:bottom"); sascha@759: query.setParameter("top", top); sascha@759: query.setParameter("bottom", bottom); sascha@759: List edges = query.list(); sascha@759: if (edges.isEmpty()) { sascha@759: peer = new Edge(top, bottom); sascha@759: session.save(peer); sascha@759: } sascha@759: else { sascha@759: peer = edges.get(0); sascha@759: } sascha@759: } sascha@759: return peer; sascha@759: } sascha@759: } sascha@759: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :