/*
 *    GeoTools - The Open Source Java GIS Toolkit
 *    http://geotools.org
 *
 *    (C) 2002-2008, Open Source Geospatial Foundation (OSGeo)
 *
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU Lesser General Public
 *    License as published by the Free Software Foundation;
 *    version 2.1 of the License.
 *
 *    This library 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
 *    Lesser General Public License for more details.
 */
package org.geotools.data.oracle;

import java.sql.Date;
import java.util.List;

import org.geotools.data.DataUtilities;
import org.geotools.data.DefaultTransaction;
import org.geotools.data.Transaction;
import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.data.simple.SimpleFeatureSource;
import org.geotools.data.simple.SimpleFeatureStore;
import org.geotools.feature.simple.SimpleFeatureBuilder;
import org.geotools.feature.simple.SimpleFeatureTypeBuilder;
import org.geotools.jdbc.JDBCDataStoreOnlineTest;
import org.geotools.jdbc.JDBCTestSetup;
import org.geotools.referencing.CRS;
import org.geotools.referencing.crs.DefaultGeographicCRS;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
import org.opengis.filter.identity.FeatureId;
import org.opengis.referencing.crs.CoordinateReferenceSystem;

import com.vividsolutions.jts.geom.Geometry;

/**
 * 
 *
 * @source $URL$
 */
public class OracleDataStoreOnlineTest extends JDBCDataStoreOnlineTest {

    @Override
    protected JDBCTestSetup createTestSetup() {
        return new OracleTestSetup();
    }
    
    public void testCreateSchemaOSGBCrs() throws Exception {
        SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder();
        builder.setName(tname("ft2"));
        builder.setNamespaceURI(dataStore.getNamespaceURI());
        CoordinateReferenceSystem crs = CRS.decode("EPSG:27700");
        builder.setCRS(crs);
        builder.add(aname("geometry"), Geometry.class);
        builder.add(aname("intProperty"), Integer.class);
        builder.add(aname("dateProperty"), Date.class);

        SimpleFeatureType featureType = builder.buildFeatureType();
        // used to fail here - with index creation error
        dataStore.createSchema(featureType);
    }
    
    public void testCreateSchemaWktCrs() throws Exception {
        SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder();
        builder.setName(tname("ft2"));
        builder.setNamespaceURI(dataStore.getNamespaceURI());
        CoordinateReferenceSystem crs = CRS.parseWKT("GEOGCS[\"NAD83\", \n" + 
                "  DATUM[\"North American Datum 1983\", \n" + 
                "    SPHEROID[\"GRS 1980\", 6378137.0, 298.257222101, AUTHORITY[\"EPSG\",\"7019\"]], \n" + 
                "    TOWGS84[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], \n" + 
                "    AUTHORITY[\"EPSG\",\"6269\"]], \n" + 
                "  PRIMEM[\"Greenwich\", 0.0, AUTHORITY[\"EPSG\",\"8901\"]], \n" + 
                "  UNIT[\"degree\", 0.017453292519943295], \n" + 
                "  AXIS[\"Geodetic longitude\", EAST], \n" + 
                "  AXIS[\"Geodetic latitude\", NORTH], \n" + 
                "  AUTHORITY[\"EPSG\",\"4269\"]]");
        builder.setCRS(crs);
        builder.add(aname("geometry"), Geometry.class);
        builder.add(aname("intProperty"), Integer.class);
        builder.add(aname("dateProperty"), Date.class);

        SimpleFeatureType featureType = builder.buildFeatureType();
        // used to fail here
        dataStore.createSchema(featureType);
    }
    
    public void testCreateSpatialIndexNameTooLong() throws Exception {
        SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder();
        builder.setName(tname("ft2"));
        builder.setNamespaceURI(dataStore.getNamespaceURI());
        builder.setCRS(DefaultGeographicCRS.WGS84);
        builder.add(aname("geometry_one_two_three_four"), Geometry.class);
        builder.add(aname("intProperty"), Integer.class);
        builder.add(aname("dateProperty"), Date.class);

        SimpleFeatureType featureType = builder.buildFeatureType();
        // used to fail here
        dataStore.createSchema(featureType);
    }
    
	public void testCreateLongVarChar() throws Exception {
		SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder();
		builder.setName(tname("longvar"));
		builder.setNamespaceURI(dataStore.getNamespaceURI());
		builder.setCRS(DefaultGeographicCRS.WGS84);
		builder.add(aname("geometry_one_two_three_four"), Geometry.class);
		builder.add(aname("longvar"), String.class);

		SimpleFeatureType featureType = builder.buildFeatureType();

		dataStore.createSchema(featureType);
		SimpleFeatureBuilder fBuilder = new SimpleFeatureBuilder(featureType);
		fBuilder.add(null);
		StringBuffer vBuffer = new StringBuffer(4000);
		// to be honest I can't tell from the oracle docs if 4000 or 3999 is the
		// actual limit but anything over 255 used to fail
		for (int i = 0; i < 3999; i++) {
			vBuffer.append("x");
		}

		fBuilder.add(vBuffer.toString());
		SimpleFeature f = fBuilder.buildFeature(null);
		// used to fail here
		Transaction transaction = new DefaultTransaction("create");
		SimpleFeatureSource featureSource = dataStore.getFeatureSource(featureType.getName().getLocalPart());
		SimpleFeatureCollection collection = DataUtilities.collection(f);
		SimpleFeatureStore outStore = (SimpleFeatureStore) featureSource;
		outStore.setTransaction(transaction);

		try {
			List<FeatureId> ids = outStore.addFeatures(collection);

			transaction.commit();
		} catch (Exception problem) {
			problem.printStackTrace();
			transaction.rollback();
		} finally {
			transaction.close();
		}

	}

}

@@ -1672,16 +1672,62 @@
 Test %7B%0A%0A
+    private OracleTestSetup oracleTestSetup;%0A%0A
     @Ove
@@ -1792,14 +1792,25 @@
     
-return
+oracleTestSetup =
  new
@@ -1825,24 +1825,56 @@
 estSetup();%0A
+        return oracleTestSetup;%0A
     %7D%0A    %0A 
@@ -5986,17 +5986,245 @@
 se();%0A%09%09
-%7D%0A%0A%09
+%09dataStore.removeSchema(tname(%22longvar%22));%0A%09%09%7D%0A%0A%09%7D%0A%0A    @Override%0A    protected void tearDownInternal() throws Exception %7B%0A        %0A        super.tearDownInternal();%0A        oracleTestSetup.deleteSpatialTable(tname(%22longvar%22));%0A    
 %7D%0A%0A%7D%0A

delete table after use in LongVar test