view backend/src/main/java/org/dive4elements/river/importer/ImportAttribute.java @ 9488:c347512a07bd

Fixed: finding attributes record first case sensitive, insensitive then as fallback
author mschaefer
date Tue, 18 Sep 2018 17:27:09 +0200
parents a275ddf7a3a1
children
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 value=:value");
        query.setString("value", this.value.trim());
        final List<Attribute> attributes = query.list();
        if (attributes.isEmpty()) {
            this.peer = getPeerCaseInsensitive();
            if (this.peer == null) {
                this.peer = new Attribute(this.value);
                session.save(this.peer);
            }
        }
        else {
            this.peer = attributes.get(0);
        }
        return this.peer;
    }

    private Attribute getPeerCaseInsensitive() {
        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