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@185: mschaefer@8974: import java.util.List; mschaefer@8974: teichmann@5829: import org.dive4elements.river.model.Attribute; mschaefer@8974: import org.hibernate.Query; sascha@187: import org.hibernate.Session; sascha@187: sascha@185: public class ImportAttribute sascha@186: implements Comparable sascha@185: { sascha@185: protected String value; sascha@185: sascha@187: protected Attribute peer; sascha@187: sascha@185: public ImportAttribute() { sascha@185: } sascha@185: mschaefer@8974: public ImportAttribute(final String value) { sascha@186: this.value = value; sascha@186: } sascha@186: sascha@185: public String getValue() { mschaefer@8974: return this.value; sascha@185: } sascha@185: mschaefer@8974: public void setValue(final String value) { sascha@185: this.value = value; sascha@185: } sascha@185: mschaefer@8974: @Override mschaefer@8974: public int compareTo(final ImportAttribute other) { mschaefer@8974: return this.value.compareTo(other.value); sascha@186: } sascha@186: sascha@185: @Override mschaefer@8974: public boolean equals(final Object other) { mschaefer@8974: if (other == this) mschaefer@8974: return true; mschaefer@8974: if (!(other instanceof ImportAttribute)) mschaefer@8974: return false; mschaefer@8974: return this.value.equals(((ImportAttribute) other).value); sascha@185: } sascha@185: sascha@185: @Override sascha@185: public int hashCode() { mschaefer@8974: return this.value.hashCode(); sascha@185: } sascha@187: sascha@187: public Attribute getPeer() { mschaefer@8974: if (this.peer != null) mschaefer@8974: return this.peer; mschaefer@8974: final Session session = ImporterSession.getInstance().getDatabaseSession(); mschaefer@8974: final Query query = session.createQuery("FROM Attribute WHERE lower(value)=:value"); mschaefer@8974: query.setString("value", this.value.trim().toLowerCase()); mschaefer@8974: final List attributes = query.list(); mschaefer@8974: if (attributes.isEmpty()) { mschaefer@8974: this.peer = new Attribute(this.value); mschaefer@8974: session.save(this.peer); sascha@187: } mschaefer@8974: else { mschaefer@8974: this.peer = attributes.get(0); mschaefer@8974: } mschaefer@8974: return this.peer; sascha@187: } sascha@185: } sascha@185: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :