view backend/src/main/java/org/dive4elements/river/importer/common/AbstractSeriesImport.java @ 9038:4c5eeaff554c

Database column "comment" renamed to "notes" (restrictions in Oracle)
author mschaefer
date Wed, 02 May 2018 06:56:21 +0200
parents ae76f618d990
children a2a42a6bac6b
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 java.util.ArrayList;
import java.util.EnumMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.dive4elements.river.importer.ImporterSession;
import org.dive4elements.river.model.River;
import org.hibernate.Session;

/**
 * Abstract base class of a km bound data series of a river importing from a file
 *
 * @author Matthias Schäfer
 *
 */
public abstract class AbstractSeriesImport<SERIES, KMTUPLE, KMLINE extends AbstractKmLineImport<SERIES, KMTUPLE>>
{
    /***** FIELDS *****/

    /**
     * Name of the imported file without type extension
     */
    protected String filename;

    protected String kmrange_info;

    protected String notes;

    protected final List<KMLINE> values;

    protected SERIES peer;

    protected Map<StoreMode, Integer> valueStoreCount;

    protected StoreMode storeMode;


    /***** CONSTRUCTORS *****/

    AbstractSeriesImport() {
        this.values = new ArrayList<>();
        this.valueStoreCount = new EnumMap<>(StoreMode.class);
        for (final StoreMode mode : StoreMode.values())
            this.valueStoreCount.put(mode, Integer.valueOf(0));
        this.storeMode = StoreMode.NONE;
    }

    public AbstractSeriesImport(final String filename) {
        this();
        setFilename(filename);
    }


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

    /**
     * Gets the class's logger
     */
    public abstract Logger getLog();

    void setFilename(final String filename) {
        this.filename = filename;
    }

    public String getFilename() {
        return this.filename;
    }

    public String getKmrange_info() {
        return this.kmrange_info;
    }

    public void setKmrange_info(final String kmrange_info) {
        this.kmrange_info = kmrange_info;
    }

    public String getNotes() {
        return this.notes;
    }

    public void setNotes(final String notes) {
        this.notes = notes;
    }

    public int getValueCount() {
        return this.values.size();
    }

    public void addValue(final KMLINE value) {
        this.values.add(value);
    }

    public int getValueStoreCount(final StoreMode mode) {
        return this.valueStoreCount.get(mode).intValue();
    }

    /**
     * Stores the data series and their values in the database
     *
     * @param river
     */
    public StoreMode store(final River river) {
        logStoreInfo();
        for (final StoreMode mode : StoreMode.values())
            this.valueStoreCount.put(mode, Integer.valueOf(0));
        final SERIES peer = getPeer(river);
        if (peer != null) {
            for (final KMLINE value : this.values) {
                incrementValueStoreCount(value.store(peer, this.storeMode));
            }
        }
        ImporterSession.getInstance().getDatabaseSession().flush();
        return this.storeMode;
    }

    /**
     * Writes the store start info to the log
     */
    protected void logStoreInfo() {
        getLog().info("Store series '" + getFilename() + "'");
    }

    private void incrementValueStoreCount(final StoreMode mode) {
        this.valueStoreCount.put(mode, Integer.valueOf(this.valueStoreCount.get(mode).intValue() + 1));
    }

    /**
     * Gets the model object of the data series, inserting it into the database if not already existing
     */
    protected SERIES getPeer(final River river) {
        if (this.peer != null)
            return this.peer;
        final Session session = ImporterSession.getInstance().getDatabaseSession();
        final List<SERIES> rows = querySeriesItem(session, river);
        if (rows.isEmpty()) {
            getLog().info("Create new database instance");
            this.peer = createSeriesItem(river);
            session.save(this.peer);
            this.storeMode = StoreMode.INSERT;
        } else {
            this.peer = rows.get(0);
            this.storeMode = StoreMode.UPDATE;
        }
        return this.peer;
    }

    /**
     * Queries the series item(s) from the database
     */
    public abstract List<SERIES> querySeriesItem(final Session session, final River river);

    /**
     * Creates a new value item
     */
    public abstract SERIES createSeriesItem(final River river);
}

http://dive4elements.wald.intevation.org