view backend/src/main/java/org/dive4elements/river/importer/sinfo/importitem/FlowDepthSeriesImport.java @ 9657:a79881a892c9

Importer (s/u-info) extensions: depth-evolution: corrected directory name Bezug_aktueller_GlW, detecting and logging of wrong units, then cancelling, various checks of the plausibility of the meta data year values and cancelling in case of errors, detecting and logging missing change values, skipping those lines
author mschaefer
date Mon, 23 Mar 2020 15:26:50 +0100
parents 31549fdfaf4f
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.sinfo.importitem;

import java.util.List;

import org.apache.log4j.Logger;
import org.dive4elements.river.importer.ImporterSession;
import org.dive4elements.river.importer.common.StoreMode;
import org.dive4elements.river.model.River;
import org.dive4elements.river.model.sinfo.FlowDepth;
import org.hibernate.Query;
import org.hibernate.Session;

/**
 * Imported flow depth data series group of a river
 *
 * @author Matthias Schäfer
 *
 */
public class FlowDepthSeriesImport {

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

    private static Logger log = Logger.getLogger(FlowDepthSeriesImport.class);

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

    private String kmrange_info;

    private String notes;

    private int year;

    private String sounding_info;

    private String evaluation_by;

    private FlowDepth peer;

    private StoreMode storeMode;


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

    public FlowDepthSeriesImport(final String filename) {
        this.filename = filename;
    }


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

    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 getYear() {
        return this.year;
    }

    public void setYear(final int year) {
        this.year = year;
    }

    public String getSounding_info() {
        return this.sounding_info;
    }

    public void setSounding_info(final String sounding_info) {
        this.sounding_info = sounding_info;
    }

    public String getEvaluation_by() {
        return this.evaluation_by;
    }

    public void setEvaluation_by(final String evaluation_by) {
        this.evaluation_by = evaluation_by;
    }

    public StoreMode getStoreMode() {
        return this.storeMode;
    }

    /**
     * Gets the model object of the data series group, inserting it into the database if not already existing
     */
    public FlowDepth getPeer(final River river) {
        if (this.peer != null) {
            this.storeMode = StoreMode.NONE;
            return this.peer;
        }
        final Session session = ImporterSession.getInstance().getDatabaseSession();
        final List<FlowDepth> rows = querySeriesItem(session, river);
        if (rows.isEmpty()) {
            log.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;
    }

    public List<FlowDepth> querySeriesItem(final Session session, final River river) {
        final Query query = session.createQuery("FROM FlowDepth WHERE river=:river AND lower(filename)=:filename");
        query.setParameter("river", river);
        query.setParameter("filename", this.filename.toLowerCase());
        return query.list();
    }

    private FlowDepth createSeriesItem(final River river) {
        return new FlowDepth(river, this.filename, this.kmrange_info, this.notes, this.year, this.sounding_info, this.evaluation_by);
    }
}

http://dive4elements.wald.intevation.org