/*
 * Copyright (C) 2012 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.geotools.data.arcims;

import java.io.IOException;
import org.geotools.data.FeatureReader;
import org.geotools.data.Query;
import org.geotools.data.QueryCapabilities;
import org.geotools.data.store.ContentEntry;
import org.geotools.data.store.ContentFeatureSource;
import org.geotools.feature.simple.SimpleFeatureTypeBuilder;
import org.geotools.geometry.jts.ReferencedEnvelope;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
import org.opengis.filter.sort.SortBy;
import nl.b3p.geotools.data.arcims.axl.*;

/**
 *
 * @author Matthijs Laan
 */
public class ArcIMSFeatureSource extends ContentFeatureSource {
    
    public ArcIMSFeatureSource(ContentEntry ce) {
        super(ce, null);
    }
    
    @Override
    public QueryCapabilities buildQueryCapabilities() {        
        return new QueryCapabilities() {
            @Override
            public boolean isJoiningSupported() {
                return false;
            }
            
            @Override
            public boolean isOffsetSupported() {
                return true;
            }
            
            @Override
            public boolean isReliableFIDSupported() {
                return true;
            }
            
            @Override
            public boolean isVersionSupported() {
                return false;
            }
            
            @Override 
            public boolean supportsSorting(SortBy[] attributes) {
                return false;
            }                    
        };       
    }
    
    @Override
    protected boolean canFilter() {
        // TODO: implement filter
        return false;
    }        
    
    @Override
    protected boolean canLimit() {
        return true;
    }
    
    @Override
    protected boolean canOffset() {
        return true;
    }
    
    @Override
    protected ReferencedEnvelope getBoundsInternal(Query query) throws IOException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override   
    protected int getCountInternal(Query query) throws IOException {
        return new ArcIMSFeatureReader(this, query).getCount();
    }

    @Override
    protected FeatureReader<SimpleFeatureType, SimpleFeature> getReaderInternal(Query query) throws IOException {
        return new ArcIMSFeatureReader(this, query);
    }

    @Override
    protected SimpleFeatureType buildFeatureType() throws IOException {       
        AxlLayerInfo al = ((ArcIMSDataStore)getDataStore()).getAxlLayerInfo(entry.getTypeName());
        AxlFClass fc = al.getFclass();
        
        SimpleFeatureTypeBuilder b = new SimpleFeatureTypeBuilder();
        b.setName(al.getId());       
        
        // Add AxlField.TYPE_ROW_ID first, default sort column
        for(AxlFieldInfo f: fc.getFields()) {
            b.add(f.getName(), f.getBinding(fc));
        }
             
        return b.buildFeatureType();
    }
    
    protected String findRowIdAttribute() throws IOException {
        AxlLayerInfo al = ((ArcIMSDataStore)getDataStore()).getAxlLayerInfo(entry.getTypeName());
        AxlFClass fc = al.getFclass();
        for(AxlFieldInfo f: fc.getFields()) {
            if(AxlField.TYPE_ROW_ID == f.getType()) {
                return f.getName();
            }
        }
        return null;
    }    
}

@@ -2554,32 +2554,144 @@
 rue;%0A    %7D%0A    %0A
+    protected ArcIMSDataStore getArcIMSDataStore() %7B%0A        return (ArcIMSDataStore)getDataStore();%0A    %7D%0A    %0A
     @Override%0A  
@@ -2680,32 +2680,32 @@
  %0A    @Override%0A
-
     protected Re
@@ -3307,34 +3307,35 @@
 lLayerInfo al = 
-((
+get
 ArcIMSDataStore)
@@ -3329,39 +3329,25 @@
 IMSDataStore
-)getDataStore()
+(
 ).getAxlLaye
@@ -3707,37 +3707,81 @@
       %7D%0A        
-     
+%0A        b.setCRS(getArcIMSDataStore().getCRS());
 %0A        return 
@@ -3906,10 +3906,11 @@
 l = 
-((
+get
 ArcI
@@ -3920,31 +3920,17 @@
 ataStore
-)getDataStore()
+(
 ).getAxl

set CRS on SimpleFeatureTypeBuilder