package edu.northwestern.bioinformatics.studycalendar.test.restfulapi;

import edu.northwestern.bioinformatics.studycalendar.tools.MapBuilder;
import edu.northwestern.bioinformatics.studycalendar.test.integrated.IntegratedTestDatabaseInitializer;
import edu.northwestern.bioinformatics.studycalendar.test.integrated.RowPreservingInitializer;
import edu.northwestern.bioinformatics.studycalendar.test.integrated.SchemaInitializer;
import gov.nih.nci.cabig.ctms.audit.domain.DataAuditInfo;
import org.springframework.beans.factory.annotation.Required;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Date;

/**
 * @author Rhett Sutphin
 */
public class RestfulApiTestInitializer extends IntegratedTestDatabaseInitializer {
    private static final Collection<String> NO_INITIALIZER_TABLES = Arrays.asList(
        "authentication_system_conf", "bering_version", "change_actions", "delta_node_types");
    private static final Map<String, List<String>> ALTERNATE_PK_TABLES
        = new MapBuilder<String, List<String>>().
            put("csm_user_pe", Arrays.asList("user_protection_element_id")).
            put("scheduled_activity_labels", Arrays.asList("scheduled_activity_id", "label")).
            put("subject_populations", Arrays.asList("assignment_id", "population_id")).
            put("user_role_sites", Arrays.asList("user_role_id", "site_id")).
            put("user_role_study_sites", Arrays.asList("user_role_id", "study_site_id")).
            put("osgi_cm_property_values", Arrays.asList("property_id", "list_index")).
            put("managing_sites", Arrays.asList("study_id", "site_id")).
            toMap();

    private SitesInitializer sitesInitializer;
    private ConfigurationInitializer configurationInitializer;
    private UsersInitializer usersInitializer;
    private SampleActivitySourceInitializer sampleSourceInitializer;

    @Override
    public void oneTimeSetup() {
        initAuditInfo();
        super.oneTimeSetup();
    }

    @Override
    public void beforeAll() {
        initAuditInfo();
        super.beforeAll();
    }

    private void initAuditInfo() {
        DataAuditInfo.setLocal(new DataAuditInfo("restful-api-test", "none", new Date(), "[console]"));
    }

    @Override
    public SchemaInitializer getTableInitializer(String tableName) {
        tableName = tableName.toLowerCase();
        if ("configuration".equals(tableName)) {
            return configurationInitializer;
        } else if (tableName.equals(sitesInitializer.getTableName())) {
            return sitesInitializer;
        } else if (tableName.equals(usersInitializer.getTableName())) {
            return usersInitializer;
        } else if (tableName.equals(sampleSourceInitializer.getTableName())) {
            return sampleSourceInitializer;
        } else if (NO_INITIALIZER_TABLES.contains(tableName)) {
            return null;
        } else if (ALTERNATE_PK_TABLES.containsKey(tableName)) {
            return new RowPreservingInitializer(tableName, ALTERNATE_PK_TABLES.get(tableName));
        } else if (tableName.startsWith("csm_")) {
            String csmlessName = tableName.substring(4);
            return new RowPreservingInitializer(tableName, csmlessName + "_id");
        } else {
            return new RowPreservingInitializer(tableName);
        }
    }

    ////// CONFIGURATION

    @Required
    public void setSitesInitializer(SitesInitializer sitesInitializer) {
        this.sitesInitializer = sitesInitializer;
    }

    @Required
    public void setConfigurationInitializer(ConfigurationInitializer configurationInitializer) {
        this.configurationInitializer = configurationInitializer;
    }

    @Required
    public void setUsersInitializer(UsersInitializer usersInitializer) {
        this.usersInitializer = usersInitializer;
    }

    @Required
    public void setSampleSourceInitializer(SampleActivitySourceInitializer sampleSourceInitializer) {
        this.sampleSourceInitializer = sampleSourceInitializer;
    }
}

@@ -69,79 +69,8 @@
 i;%0A%0A
-import edu.northwestern.bioinformatics.studycalendar.tools.MapBuilder;%0A
 impo
@@ -348,24 +348,95 @@
 nitializer;%0A
+import edu.northwestern.bioinformatics.studycalendar.tools.MapBuilder;%0A
 import gov.n
@@ -481,16 +481,73 @@
 itInfo;%0A
+import org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A
 import o
@@ -600,16 +600,234 @@
 quired;%0A
+import org.springframework.dao.DataAccessException;%0Aimport org.springframework.jdbc.core.ConnectionCallback;%0Aimport org.springframework.jdbc.core.JdbcTemplate;%0A%0Aimport java.sql.Connection;%0Aimport java.sql.SQLException;
 %0Aimport 
@@ -865,32 +865,55 @@
 til.Collection;%0A
+import java.util.Date;%0A
 import java.util
@@ -940,39 +940,16 @@
 til.Map;
-%0Aimport java.util.Date;
 %0A%0A/**%0A *
@@ -1058,16 +1058,85 @@
 lizer %7B%0A
+    private final Logger log = LoggerFactory.getLogger(getClass());%0A%0A
     priv
@@ -2366,16 +2366,482 @@
 Setup();
+%0A%0A        getJdbcTemplate().execute(new ConnectionCallback() %7B%0A            public Object doInConnection(Connection con) throws SQLException, DataAccessException %7B%0A                if (con.getMetaData().getDatabaseProductName().toLowerCase().contains(%22oracle%22)) %7B%0A                    log.debug(%22Purging Oracle recycle bin%22);%0A                    con.createStatement().execute(%22PURGE RECYCLEBIN%22);%0A                %7D%0A                return null;%0A            %7D%0A        %7D);
 %0A    %7D%0A%0A
@@ -4181,24 +4181,126 @@
    %7D%0A    %7D%0A%0A
+    private JdbcTemplate getJdbcTemplate() %7B%0A        return new JdbcTemplate(getDataSource());%0A    %7D%0A%0A
     ////// C

Purge recyclebin after setup on Oracle. #1214.