view backend/src/main/java/org/dive4elements/river/model/Attribute.java @ 9176:1614cb14308f

Work on calculations for S-Info flood duration workflow
author mschaefer
date Mon, 25 Jun 2018 19:21:11 +0200
parents 4c3ccf2b0304
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.model;

import java.io.Serializable;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.dive4elements.river.backend.SessionHolder;
import org.hibernate.Query;
import org.hibernate.Session;

@Entity
@Table(name = "attributes")
public class Attribute
implements   Serializable
{

    /***** TYPES *****/

    /**
     * River attribute (river side or range type)
     *
     */
    public enum AttributeKey {
        NONE(""), STRECKE(">>>>>>>>>>>>>>>"), LEFT("links"), RIGHT("rechts"), UNKNOWN("?");

        private final String name;
        private int id;
        private boolean ready;

        AttributeKey(final String name) {
            this.name = name;
            this.id = 0;
            this.ready = false;
        }

        /**
         * Type name in the database
         */
        public String getName() {
            return this.name;
        }

        /**
         * Type id in the database
         */
        public int getId() {
            initFromDatabase();
            return this.id;
        }

        /**
         * Set the type id
         */
        public void setId(final int id) {
            this.id = id;
        }

        protected boolean getReady() {
            return this.ready;
        }

        protected void setReady(final boolean ready) {
            this.ready = ready;
        }

        /**
         * Main value type key for a database name value
         */
        public static AttributeKey forDbName(final String dbname) {
            initFromDatabase();
            for (final AttributeKey k : AttributeKey.values()) {
                if (k.getName().equalsIgnoreCase(dbname))
                    return k;
            }
            return UNKNOWN;
        }

        /**
         * Main value type key for a database id value
         */
        public static AttributeKey forDbId(final int dbid) {
            initFromDatabase();
            for (final AttributeKey k : AttributeKey.values()) {
                if (k.getId() == dbid)
                    return k;
            }
            return UNKNOWN;
        }

        /**
         * Initially queries the database ids
         */
        private static void initFromDatabase() {
            if (STRECKE.getReady())
                return;
            // Avoid recursion
            for (final AttributeKey k : AttributeKey.values())
                k.setReady(true);
            // Select database ids
            final Session session = SessionHolder.HOLDER.get();
            final Query query = session.createQuery("FROM Attribute");
            final List<Attribute> rows = query.list();
            if (!rows.isEmpty()) {
                for (int i = 0; i <= rows.size() - 1; i++) {
                    if (forDbName(rows.get(i).getValue()) != UNKNOWN)
                        forDbName(rows.get(i).getValue()).setId(rows.get(i).getId());
                }
            }
        }
    }

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

    private Integer id;

    private String  value;

    public Attribute() {
    }

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

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

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

    @Id
    @SequenceGenerator(
            name           = "SEQUENCE_ATTRIBUTES_ID_SEQ",
            sequenceName   = "ATTRIBUTES_ID_SEQ",
            allocationSize = 1)
    @GeneratedValue(
            strategy  = GenerationType.SEQUENCE,
            generator = "SEQUENCE_ATTRIBUTES_ID_SEQ")
    @Column(name = "id")
    public Integer getId() {
        return this.id;
    }

    public void setId(final Integer id) {
        this.id = id;
    }

    @Column(name = "value")
    public String getValue() {
        return this.value;
    }

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

    @Transient
    public AttributeKey getKey() {
        return AttributeKey.forDbId(this.getId());
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org