view backend/src/main/java/org/dive4elements/river/importer/common/AbstractKmLineImport.java @ 9650:a2a42a6bac6b

Importer (s/u-info) extensions: outer try/catch for parse and log of line no, catching parsing exception if not enough value fields, parsing error and warning log messages with line number, detecting and rejecting duplicate data series, better differentiation between error and warning log messages
author mschaefer
date Mon, 23 Mar 2020 14:57:03 +0100
parents ddebd4c2fe93
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