mschaefer@8971: /* Copyright (C) 2017 by Bundesanstalt für Gewässerkunde mschaefer@8971: * Software engineering by mschaefer@8971: * Björnsen Beratende Ingenieure GmbH mschaefer@8971: * Dr. Schumacher Ingenieurbüro für Wasser und Umwelt mschaefer@8971: * mschaefer@8971: * This file is Free Software under the GNU AGPL (>=v3) mschaefer@8971: * and comes with ABSOLUTELY NO WARRANTY! Check out the mschaefer@8971: * documentation coming with Dive4Elements River for details. mschaefer@8971: */ mschaefer@8971: mschaefer@8971: package org.dive4elements.river.importer.common; mschaefer@8971: mschaefer@8971: import org.dive4elements.river.importer.ImporterSession; mschaefer@8971: import org.hibernate.Session; mschaefer@8971: mschaefer@8971: /** mschaefer@8971: * Abstract base class of a river station with associated data importing from a file mschaefer@8971: * mschaefer@8971: * @author Matthias Schäfer mschaefer@8971: * mschaefer@8971: */ mschaefer@8971: public abstract class AbstractKmLineImport { mschaefer@8971: mschaefer@8971: /***** FIELDS *****/ mschaefer@8971: mschaefer@8971: protected double station; mschaefer@8971: mschaefer@8971: private KMTUPLE peer; mschaefer@8971: mschaefer@8971: protected StoreMode storeMode; mschaefer@8971: mschaefer@8988: mschaefer@8971: /***** CONSTRUCTOR *****/ mschaefer@8971: mschaefer@8971: public AbstractKmLineImport(final double km) { mschaefer@8971: this.station = km; mschaefer@8988: this.storeMode = StoreMode.NONE; mschaefer@8971: } mschaefer@8971: mschaefer@8971: mschaefer@8971: /***** METHODS *****/ mschaefer@8971: mschaefer@8971: /** mschaefer@8971: * Stores the station value record in the database mschaefer@8971: */ mschaefer@8971: public StoreMode store(final SERIES parent, final StoreMode parentStoreMode) { mschaefer@8971: getPeer(parent, parentStoreMode); mschaefer@8971: return this.storeMode; mschaefer@8971: } mschaefer@8971: mschaefer@8971: mschaefer@8971: /** mschaefer@8971: * Gets the station value record from the database if existing, or creates a database record from this object and adds mschaefer@8971: * it mschaefer@8971: */ mschaefer@8971: protected KMTUPLE getPeer(final SERIES parent, final StoreMode parentStoreMode) { mschaefer@8988: if (this.peer != null) mschaefer@8971: return this.peer; mschaefer@8971: final Session session = ImporterSession.getInstance().getDatabaseSession(); mschaefer@8971: KMTUPLE value = null; mschaefer@8971: if (parentStoreMode != StoreMode.INSERT) { mschaefer@8971: value = queryValueItem(session, parent); mschaefer@8971: } mschaefer@8971: if (value == null) { mschaefer@8971: this.peer = createValueItem(parent); mschaefer@8971: session.save(this.peer); mschaefer@8971: this.storeMode = StoreMode.INSERT; mschaefer@8971: } else { mschaefer@8971: this.peer = value; mschaefer@8971: this.storeMode = StoreMode.UPDATE; mschaefer@8971: } mschaefer@8971: return this.peer; mschaefer@8971: } mschaefer@8971: mschaefer@8971: /** mschaefer@8971: * Queries the (first matching) value item from the database mschaefer@8971: * mschaefer@8971: * @return first matching database value item, or null mschaefer@8971: */ mschaefer@8971: protected abstract KMTUPLE queryValueItem(final Session session, final SERIES parent); mschaefer@8971: mschaefer@8971: /** mschaefer@8971: * Creates a new value item mschaefer@8971: */ mschaefer@8971: protected abstract KMTUPLE createValueItem(final SERIES parent); mschaefer@8971: }