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@763: mschaefer@8974: import java.util.List; mschaefer@8974: mschaefer@8974: import org.apache.log4j.Logger; teichmann@5829: import org.dive4elements.river.model.AnnotationType; mschaefer@8974: import org.hibernate.Query; sascha@763: import org.hibernate.Session; sascha@763: sascha@763: public class ImportAnnotationType sascha@763: implements Comparable sascha@763: { mschaefer@8974: private static final Logger log = Logger.getLogger(ImportAnnotationType.class); sascha@763: protected String name; sascha@763: protected AnnotationType peer; sascha@763: sascha@763: public ImportAnnotationType() { sascha@763: } sascha@763: mschaefer@8974: public ImportAnnotationType(final String name) { sascha@763: this.name = name; sascha@763: } sascha@763: mschaefer@8974: @Override mschaefer@8974: public int compareTo(final ImportAnnotationType other) { mschaefer@8974: return this.name.compareTo(other.name); sascha@763: } sascha@763: sascha@766: public String getName() { mschaefer@8974: return this.name; sascha@766: } sascha@766: mschaefer@8974: public void setName(final String name) { sascha@766: this.name = name; sascha@766: } sascha@766: sascha@766: sascha@763: public AnnotationType 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 AnnotationType WHERE lower(name)=:name"); mschaefer@8974: query.setParameter("name", this.name.trim().toLowerCase()); mschaefer@8974: final List types = query.list(); mschaefer@8974: if (types.isEmpty()) { mschaefer@8974: this.peer = new AnnotationType(this.name); mschaefer@8974: session.save(this.peer); mschaefer@8974: log.info(String.format("Create new database instance: %d, '%s'", this.peer.getId(), this.name)); sascha@763: } mschaefer@8974: else { mschaefer@8974: this.peer = types.get(0); mschaefer@8974: } mschaefer@8974: return this.peer; sascha@763: } sascha@763: } sascha@763: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :