/*
 * Copyright (C) 2012 Expression organization is undefined on line 4, column 61 in Templates/Licenses/license-gpl30.txt.
 *
 * 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.geotools.data.arcims.axl;

import com.vividsolutions.jts.geom.*;
import java.io.IOException;
import java.math.BigInteger;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlTransient;
import nl.b3p.geotools.data.arcims.ArcXMLUtils;

/**
 *
 * @author matthijsln
 */
@XmlAccessorType(XmlAccessType.FIELD)
public class AxlField {
    public static final int TYPE_ROW_ID        = -99;
    public static final int TYPE_SHAPE         = -98;
    public static final int TYPE_BOOLEAN       = -7;
    public static final int TYPE_BIG_INTEGER   = -5; 
    public static final int TYPE_CHAR          = 1;
    public static final int TYPE_INTEGER       = 4;
    public static final int TYPE_SMALL_INTEGER = 5;
    public static final int TYPE_FLOAT         = 6;
    public static final int TYPE_DOUBLE        = 8;
    public static final int TYPE_STRING        = 12;
    public static final int TYPE_DATE          = 91;
   
    @XmlAttribute
    private String name;
    
    @XmlAttribute
    private String value;
    
    @XmlTransient
    private AxlFeature feature;

    public String getName() {
        return name;
    }

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

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }
    
    public static DateFormat createDateFormat() {
        return new SimpleDateFormat("{ts 'YYYY-MM-dd HH:mm:ss'}");
    }
    
    public Object getConvertedValue(Class binding, GeometryFactory geometryFactory) throws ParseException, IOException {
        if(value == null) {
            return null;
        }
        if(binding.equals(String.class)) {
            return value;
        } else if(binding.equals(Integer.class)) {
            return Integer.parseInt(value);
        } else if(binding.equals(Boolean.class)) {
            return "true".equals(value);
        } else if(binding.equals(BigInteger.class)) {
            return new BigInteger(value);
        } else if(binding.equals(Character.class)) {
            return value.charAt(0);
        } else if(binding.equals(Float.class)) {
            return Float.parseFloat(value.replace(',', '.'));
        } else if(binding.equals(Double.class)) {
            return Double.parseDouble(value.replace(',', '.'));
        } else if(binding.equals(Date.class)) {
            return new Date(Long.parseLong(value));
        } else if(binding.getName().startsWith("com.vividsolutions.jts.geom.")) {
            
            if(this.feature == null) {
                return null;
            } else {
                return ArcXMLUtils.convertToJTSGeometry(feature.getGeometry(), geometryFactory);
            }
        } 
           
        return null;
    }
    
    void afterUnmarshal(Unmarshaller u, Object parent) {
        if(parent instanceof AxlFeature) {
            this.feature = (AxlFeature)parent;
        }
    }
}

@@ -2816,23 +2816,23 @@
 .equals(
-Integer
+Boolean
 .class))
@@ -2857,24 +2857,21 @@
 urn 
-Integer.parseInt
+%22true%22.equals
 (val
@@ -2876,37 +2876,193 @@
 alue);%0A        %7D
- else
+%0A        %0A        // For the following bindings empty string is null%0A        if(value.trim().length() == 0) %7B%0A            return null;%0A        %7D%0A        %0A       
  if(binding.equa
@@ -3056,39 +3056,39 @@
 (binding.equals(
-Boolean
+Integer
 .class)) %7B%0A     
@@ -3097,37 +3097,40 @@
      return 
-%22true%22.equals
+Integer.parseInt
 (value);%0A   

don't try to convert empty string values to numbers