view backend/src/main/java/org/dive4elements/river/importer/sinfo/importitem/CollisionTypeImport.java @ 9658:d86c7cb68b41

Importer (s/u-info) extensions: daily discharge: detecting, logging and skipping lines with missing date or q, or duplicate date, detecting wrong column titles and cancelling the import, specific error message if gauge not found
author mschaefer
date Mon, 23 Mar 2020 15:33:40 +0100
parents 50416a0df385
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.model.sinfo.CollisionType;
import org.hibernate.Query;
import org.hibernate.Session;

/**
 * Imported collision type
 *
 * @author Matthias Schäfer
 *
 */
public class CollisionTypeImport implements Comparable<CollisionTypeImport> {

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

    private static final Logger log = Logger.getLogger(CollisionTypeImport.class);

    protected String name;

    protected CollisionType peer;

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

    public CollisionTypeImport() {
    }

    public CollisionTypeImport(final String name) {
        this.name = name;
    }

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

    @Override
    public int compareTo(final CollisionTypeImport other) {
        return this.name.compareTo(other.name);
    }

    @Override
    public int hashCode() {
        return this.name.hashCode();
    }

    public String getName() {
        return this.name;
    }

    public void setName(final String name) {
        this.name = name;
    }

    public CollisionType getPeer() {
        if (this.peer != null)
            return this.peer;
        final Session session = ImporterSession.getInstance().getDatabaseSession();
        final Query query = session.createQuery("FROM CollisionType WHERE lower(name)=:name");
        query.setParameter("name", this.name.trim().toLowerCase());
        final List<CollisionType> types = query.list();
        if (types.isEmpty()) {
            this.peer = new CollisionType(this.name);
            session.save(this.peer);
            log.info(String.format("Create new database instance: %d, '%s'", this.peer.getId(), this.name));
        }
        else {
            this.peer = types.get(0);
        }
        return this.peer;
    }
}

http://dive4elements.wald.intevation.org