Mercurial > dive4elements > river
diff backend/src/main/java/org/dive4elements/river/importer/common/AbstractSeriesImport.java @ 8971:50416a0df385
Importer for the Schifffahrt (S-INFO) and Oekologie (U-INFO) files
author | mschaefer |
---|---|
date | Tue, 03 Apr 2018 10:18:30 +0200 |
parents | |
children | ae76f618d990 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/backend/src/main/java/org/dive4elements/river/importer/common/AbstractSeriesImport.java Tue Apr 03 10:18:30 2018 +0200 @@ -0,0 +1,170 @@ +/* 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 comment; + + protected final List<KMLINE> values; + + protected SERIES peer; + + protected Map<StoreMode, Integer> valueStoreCount; + + protected StoreMode seriesStoreMode; + + + /***** 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)); + } + + 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 getComment() { + return this.comment; + } + + public void setComment(final String comment) { + this.comment = comment; + } + + 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.seriesStoreMode)); + } + } + ImporterSession.getInstance().getDatabaseSession().flush(); + return this.seriesStoreMode; + } + + /** + * 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 + */ + public SERIES getPeer(final River river) { + if (this.peer != null) { + this.seriesStoreMode = StoreMode.NONE; + 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.seriesStoreMode = StoreMode.INSERT; + } else { + this.peer = rows.get(0); + this.seriesStoreMode = 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); +} \ No newline at end of file