package com.eactive.eai.transformer.function.util;

import org.apache.commons.lang3.StringUtils;

import com.eactive.eai.common.property.PropManager;
import com.softforum.xdbe.XcryptoException;
import com.softforum.xdbe.xCrypto;
public class XdbCrypto {
	private static XdbCrypto instance ;
	private static String pattern ="";
	
	
	private XdbCrypto(){
		
	}
    public static XdbCrypto getInstance() throws Exception {
        if(instance == null) {
            synchronized(XdbCrypto.class) {
                if(instance == null) {
                	instance = new XdbCrypto();
                	try{
                		instance.init();
                	}catch(XcryptoException e){
                		throw new Exception("암호 설정 오류" + e.getMessage(),e);
                	}
                }   
            }
        }
        return instance;   
    }
    private void init() throws XcryptoException{
    	PropManager prop = PropManager.getInstance();
    	String config = prop.getProperty("xDB", "config.path","/sw/xDB/conf/xdsp_pool.properties");
    	pattern = prop.getProperty("xDB", "pattern","normal");
    	xCrypto.RegisterEx(pattern, 2, config, "pool1", "db", "ow", "tb", pattern);
    	//sample 예제
    	//xCrypto.RegisterEx("normal", 2, "C:\\xDB\\conf\\xdsp_pool.properties", "pool1", "db", "ow", "tb", "normal");
    }
    /**
     * null 일경우  "" 로 return
     * "" 일경우 "" 로 return
     * @param text
     * @return
     * @throws XcryptoException
     */
    public static String encrypt(String text) throws XcryptoException{
//    	if (text == null){
//    		return "";
//    	}
    	if (StringUtils.isEmpty(text)){
    		return text;
    	}
    	return xCrypto.Encrypt(pattern, text);
    }
    /**
     * null 일경우  "" 로 return
     * "" 일경우 "" 로 return
     * @param text
     * @return
     * @throws XcryptoException
     */
    public static String decrypt(String text) throws XcryptoException{
//    	if (text == null){
//    		return "";
//    	}
    	if (StringUtils.isEmpty(text)){
    		return text;
    	}
    	return xCrypto.Decrypt(pattern, text);
    }
    
}