/*
 * Copyright (C) 2011-2013 B3Partners B.V.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package nl.b3p.viewer.config.services;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.*;
import org.json.JSONException;
import org.json.JSONObject;

/**
 *
 * @author Matthijs Laan
 */
@Entity
public class AttributeDescriptor {
    public static final String TYPE_STRING = "string";
    public static final String TYPE_DOUBLE = "double";
    public static final String TYPE_INTEGER = "integer";
    public static final String TYPE_BOOLEAN = "boolean";
    public static final String TYPE_GEOMETRY = "geometry";    
    public static final String TYPE_DATE = "date";
    public static final String TYPE_TIMESTAMP = "timestamp";
    public static final String TYPE_GEOMETRY_POINT = "point";
    public static final String TYPE_GEOMETRY_MPOINT = "multipoint";
    public static final String TYPE_GEOMETRY_LINESTRING = "linestring";
    public static final String TYPE_GEOMETRY_MLINESTRING = "multilinestring";
    public static final String TYPE_GEOMETRY_POLYGON = "polygon";
    public static final String TYPE_GEOMETRY_MPOLYGON = "multipolygon";
    
    public static final Set<String> GEOMETRY_TYPES = Collections.unmodifiableSet(new HashSet(Arrays.asList(new String[] {
        TYPE_GEOMETRY,
        TYPE_GEOMETRY_POINT,
        TYPE_GEOMETRY_MPOINT,
        TYPE_GEOMETRY_LINESTRING,
        TYPE_GEOMETRY_MLINESTRING,
        TYPE_GEOMETRY_POLYGON,
        TYPE_GEOMETRY_MPOLYGON
    })));

    @Id
    private Long id;

    @Basic(optional=false)
    private String name;

    @Column(name="name_alias")
    private String alias;

    private String type;

    //<editor-fold defaultstate="collapsed" desc="getters en setters">
    public Long getId() {
        return id;
    }

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

    public String getAlias() {
        return alias;
    }

    public void setAlias(String alias) {
        this.alias = alias;
    }

    public String getName() {
        return name;
    }

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

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }
    //</editor-fold>
    
    public JSONObject toJSONObject() throws JSONException {
        JSONObject j = new JSONObject();
        j.put("id", id);
        j.put("name", name);
        j.put("alias", alias);
        j.put("type", type);
        return j;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 97 * hash + (this.name != null ? this.name.hashCode() : 0);
        hash = 97 * hash + (this.alias != null ? this.alias.hashCode() : 0);
        hash = 97 * hash + (this.type != null ? this.type.hashCode() : 0);
        return hash;
    }
    
    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final AttributeDescriptor other = (AttributeDescriptor) obj;
        if ((this.name == null) ? (other.name != null) : !this.name.equals(other.name)) {
            return false;
        }
        if ((this.alias == null) ? (other.alias != null) : !this.alias.equals(other.alias)) {
            return false;
        }
        if ((this.type == null) ? (other.type != null) : !this.type.equals(other.type)) {
            return false;
        }
        return true;
    }
}

@@ -21,17 +21,17 @@
 2011-201
-3
+5
  B3Partn
@@ -2335,16 +2335,954 @@
 ng type;
+%0A    %0A    /**%0A     * Returns the ExtJS%0A     * %7B@link https://docs.sencha.com/extjs/5.1/5.1.0-apidocs/#!/api/Ext.data.field.Field field%7D%0A     * type for this attribute.%0A     *%0A     * @return a string representing the field type (one of%0A     * auto,string,int,number,boolean,date)%0A     */%0A    public String getExtJSType() %7B%0A        // default to %22auto%22 field type%0A        String ext_type = %22auto%22;%0A%0A        if (this.type.equals(TYPE_STRING)) %7B%0A            ext_type = %22string%22;%0A        %7D else if (this.type.equals(TYPE_INTEGER)) %7B%0A            ext_type = %22int%22;%0A        %7D else if (this.type.equals(TYPE_DOUBLE)) %7B%0A            ext_type = %22number%22;%0A        %7D else if (this.type.equals(TYPE_BOOLEAN)) %7B%0A            ext_type = %22boolean%22;%0A        %7D else if (this.type.equals(TYPE_DATE)) %7B%0A            ext_type = %22date%22;%0A        %7D else if (this.type.equals(TYPE_TIMESTAMP)) %7B%0A            ext_type = %22date%22;%0A        %7D%0A%0A        return ext_type;%0A    %7D
 %0A%0A    //

add a method to return the proper ExtJS Field type for this attribute