teichmann@5829: package org.dive4elements.river.importer; sascha@185: teichmann@5829: import org.dive4elements.river.model.Annotation; teichmann@5829: import org.dive4elements.river.model.AnnotationType; teichmann@5829: import org.dive4elements.river.model.Range; teichmann@5829: import org.dive4elements.river.model.Position; teichmann@5829: import org.dive4elements.river.model.Attribute; teichmann@5829: import org.dive4elements.river.model.River; teichmann@5829: import org.dive4elements.river.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@763: protected ImportAttribute attribute; sascha@763: protected ImportPosition position; sascha@763: protected ImportRange range; sascha@763: protected ImportEdge edge; sascha@763: protected ImportAnnotationType type; sascha@185: sascha@188: protected Annotation peer; sascha@188: sascha@185: public ImportAnnotation() { sascha@185: } sascha@185: sascha@186: public ImportAnnotation( sascha@763: ImportAttribute attribute, sascha@763: ImportPosition position, sascha@763: ImportRange range, sascha@763: ImportEdge edge, sascha@763: ImportAnnotationType type sascha@186: ) { sascha@186: this.attribute = attribute; sascha@186: this.position = position; sascha@186: this.range = range; sascha@759: this.edge = edge; sascha@763: this.type = type; 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@763: if ((d = edge.compareTo(other.edge)) != 0) { sascha@763: return d; sascha@763: } sascha@763: sascha@763: if (type == null && other.type != null) return -1; sascha@763: if (type != null && other.type == null) return +1; sascha@763: if (type == null && other.type == null) return 0; sascha@763: sascha@763: return type.compareTo(other.type); 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@763: public ImportEdge getEdge() { sascha@763: return edge; sascha@763: } sascha@763: sascha@763: public void setEdge(ImportEdge edge) { sascha@763: this.edge = edge; sascha@763: } sascha@763: sascha@763: public ImportAnnotationType getType() { sascha@763: return type; sascha@763: } sascha@763: sascha@763: public void setType(ImportAnnotationType type) { sascha@763: this.type = type; sascha@763: } sascha@763: sascha@188: public Annotation getPeer(River river) { sascha@188: if (peer == null) { sascha@763: Range r = range.getPeer(river); sascha@763: Attribute a = attribute.getPeer(); sascha@763: Position p = position.getPeer(); sascha@763: Edge e = edge != null ? edge.getPeer() : null; sascha@763: AnnotationType t = type != null ? type.getPeer() : null; sascha@763: 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@763: "edge=:edge and " + sascha@763: "type=:type"); 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@763: query.setParameter("type", t); sascha@188: List annotations = query.list(); sascha@188: if (annotations.isEmpty()) { sascha@763: peer = new Annotation(r, a, p, e, t); 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 :