package com.eactive.eai.adapter.db.sql;

import com.eactive.eai.adapter.AdapterPropManager;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Properties;

public class AdapterUtils {

	public static void main(String[] args) {
		String updateSql = "update EAIINF01 set RFLAG = 'C' where UNID = '%UNID%'";
		LinkedHashMap<String, Object> map = new LinkedHashMap<String, Object>();
		map.put("Unid", "ID00000001");
		
		String prefix = "%", surfix = "%";
		// System.out.println(updateSql);
		// System.out.println(getUpdateQuery(updateSql, map, prefix, surfix));
	}
	
	public static Properties getAdapterProerties(String propGroupName) {
		AdapterPropManager pmanager = AdapterPropManager.getInstance();
		Properties prop = null;
		if(pmanager == null) {
			prop = new Properties();			
		}
		else {
			prop = pmanager.getProperties(propGroupName);
		}
		return prop;
	}
	
	public static String getUpdateQuery(String updateSql, HashMap<String, Object> map) {
		return getUpdateQuery(updateSql, map, "%", "%");
	}
	
	public static String getUpdateQuery(String updateSql, HashMap<String, Object> map, String prefix, String surfix) {
		String convertedSql = updateSql;
		
		String paramName = null;
		for(String colName : map.keySet()) {
			paramName = prefix+colName.toUpperCase()+surfix;
			if(updateSql.indexOf(paramName) > -1) {
				convertedSql = convertedSql.replaceAll(paramName, (String)map.get(colName));
			}
		}
		return convertedSql;
	}
	
}
