/*
 *    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.io.IOException;
import java.util.Map;

import org.geotools.jdbc.JDBCDataStore;
import org.geotools.jdbc.JDBCDataStoreFactory;
import org.geotools.jdbc.SQLDialect;

/**
 * Oracle data store factory.
 * 
 * @author Justin Deoliveira, OpenGEO
 * @author Andrea Aime, OpenGEO
 *
 *
 *
 * @source $URL$
 */
public class OracleNGDataStoreFactory extends JDBCDataStoreFactory {
    private static final String JDBC_PATH = "jdbc:oracle:thin:@";
    
    /** parameter for database type */
    public static final Param DBTYPE = new Param("dbtype", String.class, "Type", true, "oracle");

    /** parameter for database port */
    public static final Param PORT = new Param("port", Integer.class, "Port", false, 1521);
    
    /** parameter for database host */
    public static final Param HOST = new Param("host", String.class, "Host", false, "localhost");
    
    /** parameter for database instance */
    public static final Param DATABASE = new Param("database", String.class, "Database", true);
    
    /** parameter that enables estimated extends instead of exact ones */ 
    public static final Param ESTIMATED_EXTENTS = new Param("Estimated extends", Boolean.class, "Use the spatial index information to quickly get an estimate of the data bounds", false, Boolean.TRUE);
    
    /** parameter for namespace of the datastore */
    public static final Param LOOSEBBOX = new Param("Loose bbox", Boolean.class, "Perform only primary filter on bbox", false, Boolean.TRUE);
    
    /** Metadata table providing information about primary keys **/
    public static final Param GEOMETRY_METADATA_TABLE = new Param("Geometry metadata table", String.class,
            "The optional table containing geometry metadata (geometry type and srid). Can be expressed as 'schema.name' or just 'name'", false);
    
    @Override
    protected SQLDialect createSQLDialect(JDBCDataStore dataStore) {
        return new OracleDialect(dataStore);
    }

    @Override
    protected String getDatabaseID() {
        return (String) DBTYPE.sample;
    }
    
    @Override
    public String getDisplayName() {
        return "Oracle NG";
    }

    public String getDescription() {
        return "Oracle Database";
    }
    
    @Override
    protected String getDriverClassName() {
        return "oracle.jdbc.driver.OracleDriver";
    }
    
    @Override
    protected boolean checkDBType(Map params) {
        if (super.checkDBType(params)) {
            try {
                //check for old factory
                Class.forName("org.geotools.data.oracle.OracleDataStoreFactory");
                
                //it is around, let it handle this connection
                return false;
            } 
            catch(ClassNotFoundException e) {
                //old factory not around, pick up the connection
                return true;
            }
        }
        else {
            //check for the old id
            return checkDBType(params, "Oracle");
        }
    }
    
    protected JDBCDataStore createDataStoreInternal(JDBCDataStore dataStore, Map params)
        throws IOException {
        
        // make the schema uppercase if it's not already
        if(dataStore.getDatabaseSchema() != null) {
            dataStore.setDatabaseSchema(dataStore.getDatabaseSchema().toUpperCase());
        }
        
        // setup loose bbox
        OracleDialect dialect = (OracleDialect) dataStore.getSQLDialect();
        Boolean loose = (Boolean) LOOSEBBOX.lookUp(params);
        dialect.setLooseBBOXEnabled(loose == null || Boolean.TRUE.equals(loose));
        
        // check the estimated extents
        Boolean estimated = (Boolean) ESTIMATED_EXTENTS.lookUp(params);
        dialect.setEstimatedExtentsEnabled(estimated == null || Boolean.TRUE.equals(estimated));
        
        // check the geometry metadata table
        String metadataTable = (String) GEOMETRY_METADATA_TABLE.lookUp(params);
        dialect.setGeometryMetadataTable(metadataTable);
        
        // setup proper fetch size
        dataStore.setFetchSize(200);
        
        return dataStore;
    }
    
    @Override
    protected String getJDBCUrl(Map params) throws IOException {
    	String db = (String) DATABASE.lookUp(params);    	
      String host = (String) HOST.lookUp(params);        
      Integer port =(Integer) PORT.lookUp(params);

    	if(db.startsWith("("))
    		return JDBC_PATH + db;  
    	else if(db.startsWith("/") && host != null && port != null)
    		return JDBC_PATH + "//" + host + ":" + port + db;
        else if(host != null && port != null)
    		return JDBC_PATH + host + ":" + port + ":" + db;
        else
        	throw new IOException("Unable to properly compose the JDBC URL string, some parameters as host and port may be null !");
    }
    
    @Override
    protected void setupParameters(Map parameters) {
        // NOTE: when adding parameters here remember to add them to OracleNGOCIDataStoreFactory and
        // OracleNGJNDIDataStoreFactory
        
        super.setupParameters(parameters);
        parameters.put(LOOSEBBOX.key, LOOSEBBOX);
        parameters.put(ESTIMATED_EXTENTS.key, ESTIMATED_EXTENTS);
        parameters.put(MAX_OPEN_PREPARED_STATEMENTS.key, MAX_OPEN_PREPARED_STATEMENTS);
        parameters.put(PORT.key, PORT);
        parameters.put(HOST.key, HOST);
        parameters.put(DATABASE.key, DATABASE);
        parameters.put(DBTYPE.key, DBTYPE);
        parameters.put(GEOMETRY_METADATA_TABLE.key, GEOMETRY_METADATA_TABLE);
    }
    
    @Override
    protected String getValidationQuery() {
        return "select 1 from dual";
    }
}

@@ -4666,24 +4666,16 @@
 Table);%0A
-        
 %0A       
@@ -4679,35 +4679,49 @@
     
-// setup proper f
+if (dataStore.getF
 etch
- size%0A
+Size() %3C= 0) %7B%0A    
     
@@ -4753,16 +4753,26 @@
 e(200);%0A
+        %7D%0A
         

Make default batch size for oracle only a default, it was hard-coded before