package com.eactive.eai.transformer;

import java.util.Iterator;

import org.json.simple.JSONObject;
import org.json.simple.JSONValue;

import com.eactive.eai.common.util.Logger;
import com.eactive.eai.transformer.engine.TransformEngine;
import com.eactive.eai.transformer.engine.Transformer;
import com.eactive.eai.transformer.layout.Item;
import com.eactive.eai.transformer.message.Message;
import com.eactive.eai.transformer.message.MessageFactory;
import com.eactive.eai.transformer.transform.Transform;
import com.eactive.eai.transformer.transform.TransformManager;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;


public class TransformJson4Test extends TestCase {
	protected static Logger logger = Logger.getLogger("transformer");

	TransformEngine engine = null;
    
    
    public TransformJson4Test(String name) {
        super(name);
    }

    public static Test suite() {
        /*
        * the dynamic way
        */
        return new TestSuite(TransformJson4Test.class);
    }

    public void setUp() throws Exception {
        // 변환 초기화
        engine = TransformEngine.getInstance();

        if (!engine.isStarted()) {
            engine.start();
        }
    }

    public void tearDown() throws Exception {
        //
    }

//    public void testJsonToFlatTransform() throws Exception {
//        //logger.debug("Transform08900001Test | testTransform()");
//
//        //----------------------------------------------------------------------
//        // source message 생성
//        //----------------------------------------------------------------------
//    	String data ="{\"EAI\":{\"INDIVIDUAL\":{\"addr\":\"\",\"deptNm\":\"D\",\"deptCd\":\"12000\",\"email\":\"\",\"telNo\":\"\"},\"COMMON\":{\"empNo\":\"0200\"}}}";
//    	
//        Message source = MessageFactory.getFactory().getMessage("TST_JSN0400KEDS2_REQ1");
//        source.setData(data.getBytes());
//        System.out.println(source);
//
//        //----------------------------------------------------------------------
//        // 변환 방식 1
//        //----------------------------------------------------------------------
//        Transformer transformer = engine.getTransformer();
//        Message target = transformer.transform("TRANSFORM_TST_JSN0400KEDS2_REQ", new Message[] { source });
//
////        System.out.println(target);
//        System.out.println(target.toString());
//        
//    }
 

    
	public void testUJsonToXmlTransform() throws Exception {
		System.out.println("Json TO Xml ==============================================================");
	
		// ----------------------------------------------------------------------
		// source message 생성
		// ----------------------------------------------------------------------
		String data = "";
		data = "{\"ACCT_ROWCNT\":2\r\n,\"ACCT_DATAGRID\":[\r\n" + 
				"{\"ACCT_RAMT\":1000000,\"ACNO\":\"00000000000000\",\"ACCT_DESC\":\"테스트계좌입니다\"}\r\n" + 
				",{\"ACCT_RAMT\":1000000,\"ACNO\":\"00000000000001\",\"ACCT_DESC\":\"테스트계좌입니다\"}\r\n" + 
				",{\"ACCT_RAMT\":1000000,\"ACNO\":\"00000000000002\",\"ACCT_DESC\":\"테스트계좌입니다\"}\r\n" + 
				",{\"ACCT_RAMT\":1000000,\"ACNO\":\"00000000000003\",\"ACCT_DESC\":\"테스트계좌입니다\"}\r\n" + 
				",{\"ACCT_RAMT\":1000000,\"ACNO\":\"00000000000004\",\"ACCT_DESC\":\"테스트계좌입니다\"}\r\n" + 
				",{\"ACCT_RAMT\":1000000,\"ACNO\":\"00000000000005\",\"ACCT_DESC\":\"테스트계좌입니다\"}\r\n" + 
				",{\"ACCT_RAMT\":1000000,\"ACNO\":\"00000000000006\",\"ACCT_DESC\":\"테스트계좌입니다\"}\r\n" + 
				",{\"ACCT_RAMT\":1000000,\"ACNO\":\"00000000000007\",\"ACCT_DESC\":\"테스트계좌입니다\"}\r\n" + 
				",{\"ACCT_RAMT\":1000000,\"ACNO\":\"00000000000008\",\"ACCT_DESC\":\"테스트계좌입니다\"}\r\n" + 
				",{\"ACCT_RAMT\":1000000,\"ACNO\":\"00000000000009\",\"ACCT_DESC\":\"테스트계좌입니다\"}\r\n" + 
				",{\"ACCT_RAMT\":1000000,\"ACNO\":\"00000000000010\",\"ACCT_DESC\":\"테스트계좌입니다\"}\r\n" + 
				"]" + 
				"}";


		
		Message source = MessageFactory.getFactory().getMessage("XYZ_SITT00000107_REQ");
		System.out.println("source===="+source);
		
		String rootItemName = source.getLayout().getRootItem().getName();
		JSONObject value = (JSONObject) JSONValue.parse(data);
		JSONObject root = new JSONObject();
		root.put(rootItemName, value);
		data = root.toJSONString();
		
		//참조정보 제거
		for (Iterator iter = source.fullIterator(); iter.hasNext(); ) {
			Item x = (Item) iter.next();
			if (x.getOccRef() !=null && x.getOccRef().trim().length() > 0 ){
				x.setOccRef("");
				System.out.println("remove occRef=>"+x.getItemPath());
			}
		}
		
//		Item item =source.getItem("XYZ_SITT00000107_REQ[-1].ACCT_DATAGRID[-1]",false);
//		System.out.println(item.getItemPath()+"=>"+item);
//		System.out.println("--------"+item.getOccRef());;
//		item.setOccRef("");
//		System.out.println("--------"+item.getOccRef());;
//		System.out.println(item.getItemPath()+"=>"+item);
		
		
		
		source.setData(data.getBytes("UTF-8"));
		System.out.println(source.getData());
		System.out.println(source);
			
		Transformer transformer = engine.getTransformer();
		
		
        Transform transform = TransformManager.getManager().getTransform("TRANSFORM_XYZ_MITTCBK00000012S1_REQ");

		Message targetMessage =MessageFactory.getFactory().getMessage(transform.getTargetLayout().getName());
		//참조정보제거
		for (Iterator iter = targetMessage.fullIterator(); iter.hasNext(); ) {
			Item x = (Item) iter.next();
			if (x.getOccRef() !=null && x.getOccRef().trim().length() > 0 ){
				x.setOccRef("");
				System.out.println("remove occRef=>"+x.getItemPath());
			}
		}
		
//		Message target = transformer.transform("TRANSFORM_XYZ_MITTCBK00000012S1_REQ", new Message[] { source });
		Message target = transformer.transform("TRANSFORM_XYZ_MITTCBK00000012S1_REQ",targetMessage, new Message[] { source },null).getResultMessage();
		
		System.out.println("["+target.getData()+"]");
		System.out.println("dddddddddddddd"+target);
		
//		Item item = target;
//		ArrayList alist = getItemInfo(item, 0 ,false );
		
		
	}
	public void atestUJsonToXmlTransform1() throws Exception {
		System.out.println("Json TO Xml ==============================================================");
		
		// ----------------------------------------------------------------------
		// source message 생성
		// ----------------------------------------------------------------------
		String data = "";
		data = "{\"ACCT_ROWCNT\":2\r\n,\"ACCT_DATAGRID\":[\r\n" + 
				"{\"ACCT_RAMT\":1000000,\"ACNO\":\"00000000000000\",\"ACCT_DESC\":\"테스트계좌입니다\"}\r\n" + 
				",{\"ACCT_RAMT\":1000000,\"ACNO\":\"00000000000001\",\"ACCT_DESC\":\"테스트계좌입니다\"}\r\n" + 
				",{\"ACCT_RAMT\":1000000,\"ACNO\":\"00000000000002\",\"ACCT_DESC\":\"테스트계좌입니다\"}\r\n" + 
				",{\"ACCT_RAMT\":1000000,\"ACNO\":\"00000000000003\",\"ACCT_DESC\":\"테스트계좌입니다\"}\r\n" + 
				",{\"ACCT_RAMT\":1000000,\"ACNO\":\"00000000000004\",\"ACCT_DESC\":\"테스트계좌입니다\"}\r\n" + 
				",{\"ACCT_RAMT\":1000000,\"ACNO\":\"00000000000005\",\"ACCT_DESC\":\"테스트계좌입니다\"}\r\n" + 
				",{\"ACCT_RAMT\":1000000,\"ACNO\":\"00000000000006\",\"ACCT_DESC\":\"테스트계좌입니다\"}\r\n" + 
				",{\"ACCT_RAMT\":1000000,\"ACNO\":\"00000000000007\",\"ACCT_DESC\":\"테스트계좌입니다\"}\r\n" + 
				",{\"ACCT_RAMT\":1000000,\"ACNO\":\"00000000000008\",\"ACCT_DESC\":\"테스트계좌입니다\"}\r\n" + 
				",{\"ACCT_RAMT\":1000000,\"ACNO\":\"00000000000009\",\"ACCT_DESC\":\"테스트계좌입니다\"}\r\n" + 
				",{\"ACCT_RAMT\":1000000,\"ACNO\":\"00000000000010\",\"ACCT_DESC\":\"테스트계좌입니다\"}\r\n" + 
				"]" + 
				"}";
		data = "{\"ACCT_DATAGRID\":[\r\n" + 
				"{\"ACCT_RAMT\":1000000,\"ACNO\":\"00000000000000\",\"ACCT_DESC\":\"테스트계좌입니다\"}\r\n" + 
				",{\"ACCT_RAMT\":1000000,\"ACNO\":\"00000000000001\",\"ACCT_DESC\":\"테스트계좌입니다\"}\r\n" + 
				",{\"ACCT_RAMT\":1000000,\"ACNO\":\"00000000000002\",\"ACCT_DESC\":\"테스트계좌입니다\"}\r\n" + 
				",{\"ACCT_RAMT\":1000000,\"ACNO\":\"00000000000003\",\"ACCT_DESC\":\"테스트계좌입니다\"}\r\n" + 
				",{\"ACCT_RAMT\":1000000,\"ACNO\":\"00000000000004\",\"ACCT_DESC\":\"테스트계좌입니다\"}\r\n" + 
				",{\"ACCT_RAMT\":1000000,\"ACNO\":\"00000000000005\",\"ACCT_DESC\":\"테스트계좌입니다\"}\r\n" + 
				",{\"ACCT_RAMT\":1000000,\"ACNO\":\"00000000000006\",\"ACCT_DESC\":\"테스트계좌입니다\"}\r\n" + 
				",{\"ACCT_RAMT\":1000000,\"ACNO\":\"00000000000007\",\"ACCT_DESC\":\"테스트계좌입니다\"}\r\n" + 
				",{\"ACCT_RAMT\":1000000,\"ACNO\":\"00000000000008\",\"ACCT_DESC\":\"테스트계좌입니다\"}\r\n" + 
				",{\"ACCT_RAMT\":1000000,\"ACNO\":\"00000000000009\",\"ACCT_DESC\":\"테스트계좌입니다\"}\r\n" + 
				",{\"ACCT_RAMT\":1000000,\"ACNO\":\"00000000000010\",\"ACCT_DESC\":\"테스트계좌입니다\"}\r\n" + 
				"]" + 
				"}";
		
		
		Object result = TransformService.transform("", true, "TRANSFORM_XYZ_MITTCBK00000012S1_REQ", new Object[]{data}, true);
		
		System.out.println(new String((byte[])result,"UTF-8"));
		

		
	}
	

}
