package gov.nih.nci.cabig.caaers.tools;

import gov.nih.nci.cabig.ctms.tools.DataSourceSelfDiscoveringPropertiesFactoryBean;

/**
 * This class searches through the following directories, looking for a file named
 * ${databaseConfigurationName}.properties:
 * <ul>
 *   <li><kbd>${catalina.home}/conf/caaers</kbd></li>
 *   <li><kbd>${user.home}/.caaers</kbd></li>
 *   <li><kbd>/etc/caaers</kbd></li>
 * </ul>
 *
 * It loads this file and returns it when {@link #getObject} is called.
 *
 * @author Rhett Sutphin
 */
public class CaaersDataSourcePropertiesFactoryBean extends DataSourceSelfDiscoveringPropertiesFactoryBean {
    public static final String QUARTZ_DELEGATE_PROPERTY_NAME= "jdbc.quartz.delegateClassName";
    public static final String SCHEMA_PROPERTY_NAME = "datasource.schema";
    public static final String AUTH_MODE_PROPERTY_NAME = "authenticationMode";

    public CaaersDataSourcePropertiesFactoryBean() {
        setApplicationDirectoryName("caaers");
    }

    /**
     * Sets reasonable defaults for caAERS-specific properties
     */
    @Override
    protected void computeProperties() {
        String quartzDelegateClass = selectQuartzDelegateClass();
        if(quartzDelegateClass != null) properties.setProperty(QUARTZ_DELEGATE_PROPERTY_NAME, quartzDelegateClass);
        String schema = selectSchema();
        if(schema != null) properties.setProperty(SCHEMA_PROPERTY_NAME, schema);
        if(properties.getProperty(AUTH_MODE_PROPERTY_NAME) == null) properties.setProperty(AUTH_MODE_PROPERTY_NAME, "local");
    }

    /**
     * To determin the quartz delegate class to use
     * @return
     */
    private String selectQuartzDelegateClass(){
    	////hibernate for time being will use default (http://jira.opensymphony.com/browse/QUARTZ-560)
    	String defaultClass = "org.quartz.impl.jdbcjobstore.StdJDBCDelegate"; 
    	String postgresClass = "org.quartz.impl.jdbcjobstore.PostgreSQLDelegate";
    	String oracleClass = "org.quartz.impl.jdbcjobstore.oracle.OracleDelegate";
    	String rdbms = properties.getProperty(RDBMS_PROPERTY_NAME);
    	String driver = properties.getProperty(DRIVER_PROPERTY_NAME);
    	
    	String db = (rdbms != null) ? rdbms : driver;
    	if(db != null){ 
    		if(db.toLowerCase().contains("postgres")) return postgresClass;
    		if(db.toLowerCase().contains("oracle")) return oracleClass;
    	}
    	return defaultClass;
    	
    }
    /**
     * Jackrabbit calls database vendor as schema , jackrabbit needs this variable
     * @return
     */
    private String selectSchema() {
    	String rdbms = properties.getProperty(RDBMS_PROPERTY_NAME);
    	String driver = properties.getProperty(DRIVER_PROPERTY_NAME);
    	
    	String db = (rdbms != null) ? rdbms : driver;
    	if(db != null){ 
    		if(db.toLowerCase().contains("postgres")) return "postgresql";
    		if(db.toLowerCase().contains("oracle")) return "oracle";
    	}
    	return db;   	
    	
    }
}

@@ -867,16 +867,415 @@
 onMode%22;
+%0A    public static final String WEBSSO_BASE_URL = %22webSSO.server.baseUrl%22;%0A    public static final String WEBSSO_SERVER_TRUST_CERTIFICATE = %22webSSO.server.trustCert%22;%0A    public static final String WEBSSO_HOST_CERTIFICATE = %22hostCertificate%22;%0A    public static final String WEBSSO_HOST_KEY = %22hostKey%22;%0A    public static final String WEBSSO_CAS_ACEGI_SECURITY_URL = %22webSSO.cas.acegi.security.url%22 ;
 %0A%0A    pu
@@ -1934,24 +1934,708 @@
 , %22local%22);%0A
+        if(properties.getProperty(WEBSSO_BASE_URL) == null) properties.setProperty(WEBSSO_BASE_URL, %22http://dummyurl.com/url%22);%0A        if(properties.getProperty(WEBSSO_SERVER_TRUST_CERTIFICATE) == null) properties.setProperty(WEBSSO_SERVER_TRUST_CERTIFICATE, %22dummyTrustCerts%22);%0A        if(properties.getProperty(WEBSSO_HOST_CERTIFICATE) == null) properties.setProperty(WEBSSO_HOST_CERTIFICATE, %22dummyHostCert%22);%0A        if(properties.getProperty(WEBSSO_HOST_KEY) == null) properties.setProperty(WEBSSO_HOST_KEY, %22dummykey%22);%0A        if(properties.getProperty(WEBSSO_CAS_ACEGI_SECURITY_URL) == null) properties.setProperty(WEBSSO_CAS_ACEGI_SECURITY_URL, %22http://dummy.com/casurl/%22);%0A
     %7D%0A%0A    /

added dummy values for the following WEB-SSO related properties (which are in datasource.properties). #authenticationMode=local #authenticationMode=webSSO webSSO.server.baseUrl=https://cbvapp-d1017.nci.nih.gov:48443/cas webSSO.server.trustCert=C:\\a\\cacerts hostCertificate=c:\\a\\manav.local-cert.pem hostKey=c:\\a\\manav.local-cert.pem webSSO.cas.acegi.security.url=http://localhost:8080/caaers/j_acegi_cas_security_check