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; ingo@2346: ingo@2346: import java.util.List; ingo@2346: ingo@2346: import org.apache.log4j.Logger; ingo@2346: ingo@2346: import org.hibernate.Session; ingo@2346: import org.hibernate.Query; ingo@2346: teichmann@5829: import org.dive4elements.river.model.Unit; ingo@2346: ingo@2346: ingo@2346: public class ImportUnit ingo@2346: { ingo@2346: private static final Logger log = Logger.getLogger(ImportUnit.class); ingo@2346: ingo@2346: protected String name; ingo@2346: ingo@2346: protected Unit peer; ingo@2346: ingo@2346: ingo@2346: public ImportUnit(String name) { ingo@2346: this.name = name; ingo@2346: } ingo@2346: ingo@2346: ingo@2346: public String getName() { ingo@2346: return name; ingo@2346: } ingo@2346: ingo@2346: ingo@2347: public Unit getPeer() { ingo@2346: if (peer == null) { tom@8856: Session session = ImporterSession.getInstance() tom@8856: .getDatabaseSession(); ingo@2346: Query query = session.createQuery("from Unit where name=:name"); ingo@2346: query.setParameter("name", name); ingo@2346: ingo@2346: List units = query.list(); ingo@2346: if (units.isEmpty()) { ingo@2347: log.info("Store new unit '" + name + "'"); ingo@2347: ingo@2346: peer = new Unit(name); ingo@2346: session.save(peer); ingo@2346: } ingo@2346: else { ingo@2346: peer = units.get(0); ingo@2346: } ingo@2346: } ingo@2346: return peer; ingo@2346: } ingo@2346: } ingo@2346: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :