view backend/src/main/java/org/dive4elements/river/importer/ImportAttribute.java @ 8974:a275ddf7a3a1

Added some trim and lowercase in the where clauses of the selects of existing recordsets; added AnnotationType select
author mschaefer
date Tue, 03 Apr 2018 10:37:30 +0200
parents 5e38e2924c07
children c347512a07bd
line wrap: on
line source
/* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde
 * Software engineering by Intevation GmbH
 *
 * 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;

import java.util.List;

import org.dive4elements.river.model.Attribute;
import org.hibernate.Query;
import org.hibernate.Session;

public class ImportAttribute
implements   Comparable<ImportAttribute>
{
    protected String value;

    protected Attribute peer;

    public ImportAttribute() {
    }

    public ImportAttribute(final String value) {
        this.value = value;
    }

    public String getValue() {
        return this.value;
    }

    public void setValue(final String value) {
        this.value = value;
    }

    @Override
    public int compareTo(final ImportAttribute other) {
        return this.value.compareTo(other.value);
    }

    @Override
    public boolean equals(final Object other) {
        if (other == this)
            return true;
        if (!(other instanceof ImportAttribute))
            return false;
        return this.value.equals(((ImportAttribute) other).value);
    }

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

    public Attribute getPeer() {
        if (this.peer != null)
            return this.peer;
        final Session session = ImporterSession.getInstance().getDatabaseSession();
        final Query query = session.createQuery("FROM Attribute WHERE lower(value)=:value");
        query.setString("value", this.value.trim().toLowerCase());
        final List<Attribute> attributes = query.list();
        if (attributes.isEmpty()) {
            this.peer = new Attribute(this.value);
            session.save(this.peer);
        }
        else {
            this.peer = attributes.get(0);
        }
        return this.peer;
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org