sascha@185: package de.intevation.flys.importer; sascha@185: sascha@188: import de.intevation.flys.model.Annotation; sascha@188: import de.intevation.flys.model.Range; sascha@188: import de.intevation.flys.model.Position; sascha@188: import de.intevation.flys.model.Attribute; sascha@188: import de.intevation.flys.model.River; sascha@759: import de.intevation.flys.model.Edge; sascha@188: sascha@188: import org.hibernate.Session; sascha@188: import org.hibernate.Query; sascha@188: sascha@188: import java.util.List; sascha@188: sascha@185: public class ImportAnnotation sascha@186: implements Comparable sascha@185: { sascha@185: protected ImportAttribute attribute; sascha@185: protected ImportPosition position; sascha@185: protected ImportRange range; sascha@759: protected ImportEdge edge; sascha@185: sascha@188: protected Annotation peer; sascha@188: sascha@185: public ImportAnnotation() { sascha@185: } sascha@185: sascha@186: public ImportAnnotation( sascha@186: ImportAttribute attribute, sascha@186: ImportPosition position, sascha@759: ImportRange range, sascha@759: ImportEdge edge sascha@186: ) { sascha@186: this.attribute = attribute; sascha@186: this.position = position; sascha@186: this.range = range; sascha@759: this.edge = edge; sascha@186: } sascha@186: sascha@186: public int compareTo(ImportAnnotation other) { sascha@186: int d = attribute.compareTo(other.attribute); sascha@186: if (d != 0) { sascha@186: return d; sascha@186: } sascha@186: sascha@186: if ((d = position.compareTo(other.position)) != 0) { sascha@186: return d; sascha@186: } sascha@186: sascha@186: if ((d = range.compareTo(other.range)) != 0) { sascha@186: return d; sascha@186: } sascha@186: sascha@759: if (edge == null && other.edge != null) return -1; sascha@759: if (edge != null && other.edge == null) return +1; sascha@759: if (edge == null && other.edge == null) return 0; sascha@759: sascha@759: return edge.compareTo(other.edge); sascha@186: } sascha@186: sascha@185: public ImportAttribute getAttribute() { sascha@185: return attribute; sascha@185: } sascha@185: sascha@185: public void setAttribute(ImportAttribute attribute) { sascha@185: this.attribute = attribute; sascha@185: } sascha@185: sascha@185: public ImportPosition getPosition() { sascha@185: return position; sascha@185: } sascha@185: sascha@185: public void setPosition(ImportPosition position) { sascha@185: this.position = position; sascha@185: } sascha@185: sascha@185: public ImportRange getRange() { sascha@185: return range; sascha@185: } sascha@185: sascha@185: public void setRange(ImportRange range) { sascha@185: this.range = range; sascha@185: } sascha@188: sascha@188: public Annotation getPeer(River river) { sascha@188: if (peer == null) { sascha@188: Range r = range.getPeer(river); sascha@188: Attribute a = attribute.getPeer(); sascha@188: Position p = position.getPeer(); sascha@759: Edge e = edge != null ? edge.getPeer() : null; sascha@497: Session session = ImporterSession.getInstance().getDatabaseSession(); sascha@188: Query query = session.createQuery( sascha@759: "from Annotation where " + sascha@759: "range=:range and " + sascha@759: "attribute=:attribute and " + sascha@759: "position=:position and " + sascha@759: "edge=:edge"); sascha@188: query.setParameter("range", r); sascha@188: query.setParameter("attribute", a); sascha@188: query.setParameter("position", p); sascha@759: query.setParameter("edge", e); sascha@188: List annotations = query.list(); sascha@188: if (annotations.isEmpty()) { sascha@188: peer = new Annotation(r, a, p); sascha@188: session.save(peer); sascha@188: } sascha@188: else { sascha@188: peer = annotations.get(0); sascha@188: } sascha@188: } sascha@188: return peer; sascha@188: } sascha@185: } sascha@185: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :