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@2808: ingo@2809: import java.util.List; ingo@2809: ingo@2809: import org.apache.log4j.Logger; mschaefer@8974: import org.dive4elements.river.model.ElevationModel; mschaefer@8974: import org.hibernate.Query; ingo@2809: import org.hibernate.Session; ingo@2808: ingo@2808: ingo@2808: public class ImportElevationModel { ingo@2808: ingo@2809: private static final Logger log = mschaefer@8974: Logger.getLogger(ImportElevationModel.class); ingo@2809: ingo@2808: protected String name; ingo@2808: ingo@2808: protected ImportUnit unit; ingo@2808: ingo@2809: protected ElevationModel peer; ingo@2809: ingo@2808: mschaefer@8974: public ImportElevationModel(final String name, final ImportUnit unit) { ingo@2808: this.name = name; ingo@2808: this.unit = unit; ingo@2808: } ingo@2809: ingo@2809: ingo@2809: public void storeDependencies() { mschaefer@8974: final ElevationModel model = getPeer(); ingo@2809: } ingo@2809: ingo@2809: public ElevationModel getPeer() { mschaefer@8974: if (this.unit == null) { ingo@3949: log.warn("No elevation model specified."); ingo@3949: return null; ingo@3949: } mschaefer@8974: if (this.peer != null) mschaefer@8974: return this.peer; ingo@2809: mschaefer@8974: final Session session = ImporterSession.getInstance().getDatabaseSession(); mschaefer@8974: final Query query = session.createQuery("FROM ElevationModel WHERE (trim(name)=:name) AND (unit=:unit)"); mschaefer@8974: query.setParameter("name", this.name); mschaefer@8974: query.setParameter("unit", this.unit.getPeer()); mschaefer@8974: final List models = query.list(); ingo@2809: mschaefer@8974: if (models.isEmpty()) { mschaefer@8974: log.info("Create new ElevationModel DB instance."); mschaefer@8974: mschaefer@8974: this.peer = new ElevationModel(this.name, this.unit.getPeer()); mschaefer@8974: session.save(this.peer); ingo@2809: } mschaefer@8974: else { mschaefer@8974: this.peer = models.get(0); mschaefer@8974: } mschaefer@8974: return this.peer; ingo@2809: } ingo@2808: } ingo@2808: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :