teichmann@6344: /* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde teichmann@6344: * Software engineering by Intevation GmbH teichmann@6344: * teichmann@6344: * This file is Free Software under the GNU AGPL (>=v3) teichmann@6344: * and comes with ABSOLUTELY NO WARRANTY! Check out the teichmann@6344: * documentation coming with Dive4Elements River for details. teichmann@6344: */ teichmann@6344: teichmann@6344: package org.dive4elements.river.importer; teichmann@6344: teichmann@6344: import java.util.List; teichmann@6344: teichmann@6344: import org.dive4elements.river.model.NamedMainValue; teichmann@6344: import org.dive4elements.river.model.OfficialLine; teichmann@6344: import org.dive4elements.river.model.River; teichmann@6344: import org.dive4elements.river.model.WstColumn; teichmann@6344: import org.hibernate.Query; teichmann@6344: import org.hibernate.Session; teichmann@6344: teichmann@6344: public class ImportOfficialLine teichmann@6344: { teichmann@6344: protected String name; teichmann@6344: protected ImportWstColumn wstColumn; teichmann@6344: teichmann@6344: protected OfficialLine peer; teichmann@6344: teichmann@6344: public ImportOfficialLine() { teichmann@6344: } teichmann@6344: teichmann@6344: public ImportOfficialLine(String name, ImportWstColumn wstColumn) { teichmann@6344: this.name = name; teichmann@6344: this.wstColumn = wstColumn; teichmann@6344: } teichmann@6344: teichmann@6347: public String getName() { teichmann@6347: return name; teichmann@6347: } teichmann@6347: teichmann@6347: public void setName(String name) { teichmann@6347: this.name = name; teichmann@6347: } teichmann@6347: teichmann@6344: public OfficialLine getPeer(River river) { teichmann@6344: if (peer == null) { teichmann@6344: // XXX: This is a bit odd. We do not have not enough infos here teichmann@6344: // to create a new NamedMainValue. So we just look for existing ones. teichmann@6344: Session session = ImporterSession.getInstance().getDatabaseSession(); teichmann@6344: NamedMainValue nmv = NamedMainValue.fetchByName(name, session); teichmann@6344: if (nmv == null) { teichmann@6344: // failed -> failed to create OfficialLine teichmann@6344: return null; teichmann@6344: } teichmann@6344: WstColumn wc = wstColumn.getPeer(river); teichmann@6344: Query query = session.createQuery( teichmann@6344: "from OfficialLine " + teichmann@6344: "where namedMainValue = :nmv and wstColumn = :wc"); teichmann@6344: query.setParameter("nmv", nmv); teichmann@6344: query.setParameter("wc", wc); teichmann@6344: List lines = query.list(); teichmann@6344: if (lines.isEmpty()) { teichmann@6344: peer = new OfficialLine(wc, nmv); teichmann@6344: session.save(peer); teichmann@6344: } teichmann@6344: else { teichmann@6344: peer = lines.get(0); teichmann@6344: } teichmann@6344: teichmann@6344: } teichmann@6344: return peer; teichmann@6344: } teichmann@6344: } teichmann@6344: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : teichmann@6344: