/*******************************************************************************
 * Copyright 2015 EMBL - European Bioinformatics Institute
 *
 * Licensed under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied. See the License for the specific
 * language governing permissions and limitations under the
 * License.
 *******************************************************************************/

package org.mousephenotype.cda.indexers.utils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import org.mousephenotype.cda.indexers.beans.OrganisationBean;
import org.mousephenotype.cda.solr.service.dto.ImpressBaseDTO;
import org.mousephenotype.cda.solr.service.dto.ParameterDTO;

/**
 *
 * @author mrelac
 */
public class OntologyUtils {
    
    /**
     * Return relevant impress pipeline map
     *
     * @param connection The database connection
     * 
     * @return a map, indexed by primary key, of <code>ImpressBean</code> instances.
     * 
     * @throws java.sql.SQLException when a database exception occurs
     */
    public static Map<Integer, ImpressBaseDTO> populateImpressPipeline(Connection connection) throws SQLException {
        Map<Integer, ImpressBaseDTO> impressMap;

        String query = "SELECT id, stable_key, name, stable_id FROM phenotype_pipeline";
        try (PreparedStatement p = connection.prepareStatement(query)) {

            impressMap = populateImpressMap(p);
        }

        return impressMap;
    }

    /**
     * Return relevant impress procedure map
     *
     * @param connection The database connection
     * 
     * @return a map, indexed by primary key, of <code>ImpressBean</code> instances.
     *
     * @throws java.sql.SQLException when a database exception occurs
     */
    public static Map<Integer, ImpressBaseDTO> populateImpressProcedure(Connection connection) throws SQLException {
        Map<Integer, ImpressBaseDTO> impressMap;

        String query = "SELECT id, stable_key, name, stable_id FROM phenotype_procedure";

        try (PreparedStatement p = connection.prepareStatement(query)) {

            impressMap = populateImpressMap(p);
        }

        return impressMap;
    }

    /**
     * Return relevant impress parameter map
     *
     * @param connection The database connection
     * 
     * @return a map, indexed by primary key, of <code>ImpressBean</code> instances.
     *
     * @throws java.sql.SQLException when a database exception occurs
     */
    public static Map<Integer, ParameterDTO> populateImpressParameter(Connection connection) 
    throws SQLException {
        
    	Map<Integer, ParameterDTO> impressMap;

        String query = "SELECT id, stable_key, name, stable_id, annotate FROM phenotype_parameter";

        try (PreparedStatement p = connection.prepareStatement(query)) {

            impressMap = populateParameterMap(p);
        }

        return impressMap;
    }

    /**
     * Populate the map with organisation beans
     *
     * @param connection The database connection
     * 
     * @return a map, indexed by internal db id, o
     * <code>OrganisationBean</code> instances.
     *
     * @throws SQLException when a database error occurs
     */
    public static Map<Integer, OrganisationBean> populateOrganisationMap(Connection connection) throws SQLException {

        Map<Integer, OrganisationBean> map = new HashMap<>();
        String query = "SELECT id, name, fullname, country FROM organisation";
        try (PreparedStatement p = connection.prepareStatement(query)) {

            ResultSet resultSet = p.executeQuery();
            while (resultSet.next()) {
                OrganisationBean b = new OrganisationBean();

                b.setId(resultSet.getInt("id"));
                b.setName(resultSet.getString("name"));
                b.setFullname(resultSet.getString("fullname"));
                b.setCountry(resultSet.getString("country"));
                map.put(b.getId(), b);
            }
        }

        return map;
    }


    // PRIVATE METHODS

    
    private static Map<Integer, ImpressBaseDTO> populateImpressMap(PreparedStatement p) throws SQLException {

        Map<Integer, ImpressBaseDTO> impressMap = new HashMap<>();
        ResultSet resultSet = p.executeQuery();

        while (resultSet.next()) {
            ImpressBaseDTO b = new ImpressBaseDTO(resultSet.getInt("id"), 
            		resultSet.getInt("stable_key"), 
            		resultSet.getString("stable_id"), 
            		resultSet.getString("name"));
            impressMap.put(resultSet.getInt("id"), b);
        }

        return impressMap;
    }
    
    
    private static Map<Integer, ParameterDTO> populateParameterMap(PreparedStatement p) throws SQLException {

        Map<Integer, ParameterDTO> impressMap = new HashMap<>();
        ResultSet resultSet = p.executeQuery();

        while (resultSet.next()) {
        	
        	ParameterDTO b = new ParameterDTO();
        	b.setId(resultSet.getInt("id"));
        	b.setStableKey(resultSet.getInt("stable_key")); 
        	b.setStableId(resultSet.getString("stable_id")); 
        	b.setName(resultSet.getString("name"));
        	b.setAnnotate(resultSet.getBoolean("annotate"));
            impressMap.put(resultSet.getInt("id"), b);
            
            System.out.println(b.isAnnotate());
        }

        return impressMap;
    }
}
@@ -5760,56 +5760,8 @@
    %0A
-            System.out.println(b.isAnnotate());%0A
     
@@ -5801,8 +5801,9 @@
 %0A    %7D%0A%7D
+%0A

remove sysout for isAnnotate parameter