package com.eactive.eai.rms.onl.loader.dao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.eactive.eai.rms.common.base.SqlMapClientTemplateDao;
import com.eactive.eai.rms.common.datasource.DataSourceType;

/**
 * The Class LayoutDao.
 * <ul>
 * <li>1. Function :</li>
 * <li>2. Summary :</li>
 * <li>3. Notification :</li>
 * </ul>
 */
@Repository("loaderDao")
@SuppressWarnings("unchecked")
public class LoaderDaoImpl extends SqlMapClientTemplateDao implements LoaderDao {
	
    public int load(DataSourceType dt,final String table, ArrayList list) {
//		DataSourceContextHolder.setDataSourceType(dt);
    	int count = 0;
    	HashMap hm = (HashMap)list.get(0);
		hm.put("schemaId", dt.getSchema());
    	this.template.update("Loader.delete"+table, hm);
    	
    	for (int i=0;i<list.size();i++){
    		hm = (HashMap)list.get(i);
    		hm.put("schemaId", dt.getSchema());
    		count = count + this.template.update("Loader.insert"+table, hm);
    	}
        return count;
    }
    
    // Adapter Loading Sync
    public int adapterLoadSync(DataSourceType dt,final String table, ArrayList list) {
//    	DataSourceContextHolder.setDataSourceType(dt);
    	int count = 0;
    	HashMap hm = (HashMap)list.get(0);
    	hm.put("schemaId", dt.getSchema());
    	
		if(hm.containsKey("ADPTRUSEYN") && !"0".equals(hm.get("ADPTRUSEYN")))
			hm.put("ADPTRUSEYN", "0");
    	
    	this.template.update("Loader.delete"+table, hm);
    	
    	for (int i=0;i<list.size();i++){
    		hm = (HashMap)list.get(i);
    		hm.put("schemaId", dt.getSchema());
    		count = count + this.template.update("Loader.insert"+table, hm);
    	}
    	return count;
    }

    public int delete(DataSourceType dt,final String table, HashMap hm) {
//		DataSourceContextHolder.setDataSourceType(dt);
    	//int count = 0;
		hm.put("schemaId", dt.getSchema());
    	return this.template.update("Loader.delete"+table, hm);
    }
    
    
    public int updateNode(DataSourceType dt,String instName) {
    	//int count = 0;
//		DataSourceContextHolder.setDataSourceType(dt);
		
    	HashMap hm = new HashMap();
		hm.put("schemaId", dt.getSchema());
		hm.put("instName", instName);
    	return this.template.update("Loader.updateNode", hm);
    }
    
    public int updateInst(DataSourceType dt,String type, String value) {
    	//int count = 0;
//		DataSourceContextHolder.setDataSourceType(dt);
		
    	HashMap hm = new HashMap();
		hm.put("schemaId", dt.getSchema());
		hm.put("type", type);
		hm.put("value", value);
    	return this.template.update("Loader.updateInst", hm);
    }
    public List<LinkedHashMap> selectTable(DataSourceType dt,String tableName ,String key, String value) {
    	//int count = 0;
//		DataSourceContextHolder.setDataSourceType(dt);
		
    	HashMap hm = new HashMap();
		hm.put("schemaId", dt.getSchema());
		hm.put("tableName", tableName);
		hm.put("key", key);
		hm.put("value", value);
    	return this.template.queryForList("Loader.selectTable", hm);
    } 
    public int insertTable(DataSourceType dt,final String tableName, ArrayList list) {
//		DataSourceContextHolder.setDataSourceType(dt);
    	int count = 0;
    	HashMap hm = new HashMap();
		hm.put("schemaId", dt.getSchema());
		hm.put("tableName", tableName);
		this.template.update("Loader.deleteTable", hm);

		LinkedHashMap column = (LinkedHashMap)((LinkedHashMap)list.get(0)).clone();
		ArrayList columnList = new ArrayList();
    	for (Object o:column.keySet()){
    		String key = (String)o;
    		columnList.add(key);
    	}
    	
    	LinkedHashMap<String,Object> value = new LinkedHashMap<String,Object>();
    	for (int i=0;i<list.size();i++){
    		value = (LinkedHashMap)list.get(i);
    		
        	ArrayList valueList = new ArrayList();
        	for (Object o:value.keySet()){
        		String key = (String)o;
        		valueList.add(value.get(key));
        	}
    		hm.put("schemaId", dt.getSchema());
    		hm.put("tableName", tableName);
    		hm.put("columnList", columnList);
    		hm.put("valueList", valueList);
    		count = count + this.template.update("Loader.insertTable", hm);
    	}
        return count;
    } 
    
    public List<LinkedHashMap> selectTableData( DataSourceType dataSourceType, String tableName, String key1, String param1 ) {
        final HashMap param = new HashMap();
        param.put("tableName", tableName);
        param.put("key1", key1);
        param.put("param1", param1);    
        
        return template.queryForList("Loader.selectTableData", param);
        
//        return new SpecifiedDataSourceExecutor<List<LinkedHashMap>>(
//                 dataSourceType) {
//            protected List<LinkedHashMap> doExecute() throws Exception {
//                return template.queryForList("Loader.selectTableData", param);
//            }
//        }.execute();
    }
    public List<LinkedHashMap> selectTableDataLike( DataSourceType dataSourceType, String tableName, String key1, String param1 ) {
        final HashMap param = new HashMap();
        
        param.put("tableName", tableName);
        param.put("key1", key1);
        param.put("param1", param1);            
        
        return template.queryForList("Loader.selectTableDataLike", param);
        
//        return new SpecifiedDataSourceExecutor<List<LinkedHashMap>>(
//                 dataSourceType) {
//            protected List<LinkedHashMap> doExecute() throws Exception {
//                return template.queryForList("Loader.selectTableDataLike", param);
//            }
//        }.execute();
    } 
    public List<LinkedHashMap> selectDataForTableName( DataSourceType dataSourceType, String tableName, String key1, String param1 ) {
        final HashMap param = new HashMap();
        param.put("key1", key1);
        param.put("param1", param1);  
        
        return template.queryForList("Loader.selectDataFor"+tableName, param);
        
//        return new SpecifiedDataSourceExecutor<List<LinkedHashMap>>(
//                 dataSourceType) {
//            protected List<LinkedHashMap> doExecute() throws Exception {
//                return template.queryForList("Loader.selectDataFor"+tableName, param);
//            }
//        }.execute();
    }
    public List<LinkedHashMap> selectTableDataForListValue( DataSourceType dataSourceType, String tableName, String key1, List<String> param1 ) {
        final HashMap param = new HashMap();
        param.put("tableName", tableName);
        param.put("key1", key1);
        param.put("param1", param1);            
        
        return template.queryForList("Loader.selectTableDataForListValue", param);
        
//        return new SpecifiedDataSourceExecutor<List<LinkedHashMap>>(
//                 dataSourceType) {
//            protected List<LinkedHashMap> doExecute() throws Exception {
//                return template.queryForList("Loader.selectTableDataForListValue", param);
//            }
//        }.execute();
    }
    public List<LinkedHashMap> selectSnaAdpApList( DataSourceType dataSourceType, String param1 ) {
        final HashMap param = new HashMap();
        param.put("param1", param1);            
        
        return template.queryForList("Loader.selectSnaAdpApList", param);
        
//        return new SpecifiedDataSourceExecutor<List<LinkedHashMap>>(
//                 dataSourceType) {
//            protected List<LinkedHashMap> doExecute() throws Exception {
//                return template.queryForList("Loader.selectSnaAdpApList", param);
//            }
//        }.execute();
    }
}
