import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import org.geotools.data.DataStore;
import org.geotools.data.DataUtilities;
import org.geotools.data.DefaultTransaction;
import org.geotools.data.FeatureStore;
import org.geotools.data.Transaction;
import org.geotools.data.oracle.OracleNGDataStoreFactory;
import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.feature.FeatureIterator;
import org.geotools.feature.FeatureTypes;
import org.geotools.feature.simple.SimpleFeatureBuilder;
import org.geotools.feature.simple.SimpleFeatureTypeBuilder;
import org.geotools.jdbc.JDBCDataStoreFactory;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
import org.opengis.feature.type.AttributeDescriptor;
import org.opengis.filter.Filter;


public class OracleImporter {

    public static void main(String[] args) throws IOException {
        String path = "/home/aaime/devel/gisData/naturalEarth/10m_cultural/10m_populated_places.shp";
        ShapefileDataStore shp = new ShapefileDataStore(new File(path).toURL());
        
        Map<Serializable, Object> params = new HashMap<Serializable, Object>();
        params.put(JDBCDataStoreFactory.USER.key, "geoserver");
        params.put(JDBCDataStoreFactory.PASSWD.key, "postgis");
        params.put(JDBCDataStoreFactory.HOST.key, "localhost");
        params.put(JDBCDataStoreFactory.PORT.key, OracleNGDataStoreFactory.PORT.sample);
        params.put(JDBCDataStoreFactory.DATABASE.key, "xe");
        params.put(JDBCDataStoreFactory.DBTYPE.key, "oracle");
        
        DataStore oracle = new OracleNGDataStoreFactory().createDataStore(params);
        if(oracle != null && oracle.getTypeNames() != null)
            System.out.println("Oracle connected");
        
        String typeName = "PLACES"; // shp.getTypeNames()[0].toUpperCase();
        if(!Arrays.asList(oracle.getTypeNames()).contains(typeName)) {
            SimpleFeatureTypeBuilder tb = new SimpleFeatureTypeBuilder();
            tb.init(shp.getSchema());
            tb.setName(typeName);
            SimpleFeatureType oraType = tb.buildFeatureType();
            oracle.createSchema(oraType);
        }
        
        FeatureStore oraStore = (FeatureStore) oracle.getFeatureSource(typeName);
        oraStore.removeFeatures(Filter.INCLUDE);
        
        SimpleFeatureType targetSchema = (SimpleFeatureType) oraStore.getSchema();
        SimpleFeatureBuilder builder = new SimpleFeatureBuilder(targetSchema);
        
        FeatureIterator fi = shp.getFeatureSource().getFeatures().features();
        SimpleFeatureType sourceSchema = shp.getSchema();
        
        Transaction t = new DefaultTransaction();
        oraStore.setTransaction(t);
        while(fi.hasNext()) {
            SimpleFeature source = (SimpleFeature) fi.next();
        
            for(AttributeDescriptor ad : sourceSchema.getAttributeDescriptors()) {
                String attribute = ad.getLocalName();
                builder.set(attribute.toUpperCase(), source.getAttribute(attribute));
            }
            
            try {
                oraStore.addFeatures(DataUtilities.collection(builder.buildFeature(null)));
            } catch(Exception e) {
                e.printStackTrace();
            }
        }
        t.commit();
        t.close();
        
    }
}
