view backend/src/main/java/org/dive4elements/river/importer/common/AbstractKmLineImport.java @ 9056:ddebd4c2fe93

Corrected station parsing for infrastructure import
author mschaefer
date Fri, 04 May 2018 14:05:21 +0200
parents ae76f618d990
children
line wrap: on
line source
/* Copyright (C) 2017 by Bundesanstalt für Gewässerkunde
 * Software engineering by
 *  Björnsen Beratende Ingenieure GmbH
 *  Dr. Schumacher Ingenieurbüro für Wasser und Umwelt
 *
 * This file is Free Software under the GNU AGPL (>=v3)
 * and comes with ABSOLUTELY NO WARRANTY! Check out the
 * documentation coming with Dive4Elements River for details.
 */

package org.dive4elements.river.importer.common;

import org.dive4elements.river.importer.ImporterSession;
import org.hibernate.Session;

/**
 * Abstract base class of a river station with associated data importing from a file
 *
 * @author Matthias Schäfer
 *
 */
public abstract class AbstractKmLineImport<SERIES, KMTUPLE> {

    /***** FIELDS *****/

    protected double station;

    private KMTUPLE peer;

    protected StoreMode storeMode;


    /***** CONSTRUCTOR *****/

    public AbstractKmLineImport(final double km) {
        this.station = km;
        this.storeMode = StoreMode.NONE;
    }


    /***** METHODS *****/

    /**
     * Stores the station value record in the database
     */
    public StoreMode store(final SERIES parent, final StoreMode parentStoreMode) {
        getPeer(parent, parentStoreMode);
        return this.storeMode;
    }

    public double getStation() {
        return this.station;
    }

    /**
     * Gets the station value record from the database if existing, or creates a database record from this object and adds
     * it
     */
    protected KMTUPLE getPeer(final SERIES parent, final StoreMode parentStoreMode) {
        if (this.peer != null)
            return this.peer;
        final Session session = ImporterSession.getInstance().getDatabaseSession();
        KMTUPLE value = null;
        if (parentStoreMode != StoreMode.INSERT) {
            value = queryValueItem(session, parent);
        }
        if (value == null) {
            this.peer = createValueItem(parent);
            session.save(this.peer);
            this.storeMode = StoreMode.INSERT;
        } else {
            this.peer = value;
            this.storeMode = StoreMode.UPDATE;
        }
        return this.peer;
    }

    /**
     * Queries the (first matching) value item from the database
     *
     * @return first matching database value item, or null
     */
    protected abstract KMTUPLE queryValueItem(final Session session, final SERIES parent);

    /**
     * Creates a new value item
     */
    protected abstract KMTUPLE createValueItem(final SERIES parent);
}

http://dive4elements.wald.intevation.org