package com.kbstar.eai.common.header; 

import java.util.Arrays;

import com.eactive.eai.common.header.HeaderActionSupport;


/** 
* 1. 기능 : ARS공동망 의 Header를 EAI에서 생성하거나 삭제해야하는 경우를 위한 Class
* 2. 처리 개요 : 
*     - 메시지종류에 따라 헤더를 생성하거나 삭제하는 API를 제공
* 3. 주의사항 
*     - AD 의 경우는 S는 ADD, R 는 DELETE
*     - DA 의 경우는 S는 DELETE, R 는 ADD
* @author  : 
* @version : v 1.0.0
* @see :
* @since : 
*/
public class ARSHeaderAction extends HeaderActionSupport 
{ 
    private static final int HEADER_LENGTH = 7; // 공동망 헤더Length

    public ARSHeaderAction(){
    	super.setHeaderLength(HEADER_LENGTH);
    	
    }

	public byte[] addHeaderDA(byte[] orgData , byte[] message) {
		return addHeader(orgData,message);   	

    }
	
	public byte[] addHeaderAD(byte[] orgData , byte[] message) {
		return addHeader(orgData,message);
        

    }   	
	public byte[] addHeader(byte[] orgData , byte[] message ) {
		byte[] full        = null;
    	full = new byte[HEADER_LENGTH+message.length];
    	byte [] header = new byte[HEADER_LENGTH];

    	
		byte[] hdr	= {'H', 'D', 'R'};
		System.arraycopy(hdr, 0, header, 4, 3);
    	
		//
		System.arraycopy(header, 0, full, 0, HEADER_LENGTH );
		System.arraycopy(message, 0, full, HEADER_LENGTH, message.length);
		
		
		// 전체 전문길이 계산
		byte len[] = Integer.toString( message.length +3 ).getBytes();
		
		
		//길이값 '0'으로 셋팅
		Arrays.fill(full, 0, 4, (byte)'0');
		//길이값  value 셋팅
		System.arraycopy(len, 0, full, 4 - len.length, len.length);     
 
        return full;
        

    }   	
	
//	   public static void main(String args[]) throws Exception {
//	   test();
//}

// private static void test() throws Exception {
//		   String actionName = "com.kbstar.eai.common.header.ARSHeaderAction";
		   

		    /////전송 AD 
//	        HeaderAction action = HeaderActionFactory.createAction(actionName);
//
//	        Object[] bizObjs =new Object[1];
//	        bizObjs[0] = "1234567890".getBytes();
//	        Object bizObj = null;
//	        if(bizObjs != null && bizObjs.length > 0) {
//	            bizObj = bizObjs[0];
//	        }
//	        byte[] orgData = null; 
//			System.out.println("orgData=>"+new String(orgData==null?"".getBytes():orgData));
//			System.out.println("message=>"+new String(bizObj==null?"".getBytes():(byte[])bizObj));
//			System.out.println("sendRecv=>"+HeaderActionKeys.MESSAGE_SEND);
//			System.out.println("commandType=>"+HeaderActionKeys.ADD_DELETE_HEADER);	    
//			byte[] header = action.excute(orgData ,(byte[])bizObj, HeaderActionKeys.MESSAGE_SEND, HeaderActionKeys.ADD_DELETE_HEADER);
//			System.out.println("header=>"+new String(header));
		   	/////////////
		    
		    /////수신 AD 
//	        HeaderAction action = HeaderActionFactory.createAction(actionName);
//
//	        Object[] bizObjs =new Object[1];
//	        bizObjs[0] = "0013HDR1234567890".getBytes();
//	        Object bizObj = null;
//	        if(bizObjs != null && bizObjs.length > 0) {
//	            bizObj = bizObjs[0];
//	        }
//	        byte[] orgData = null; 
//       	    //orgData = new byte[((byte[])bizObj).length ];
//   	 	    //System.arraycopy(((byte[])bizObj), 0, orgData, 0,orgData.length);
//			System.out.println("orgData=>"+new String(orgData==null?"".getBytes():orgData));
//			System.out.println("message=>"+new String(bizObj==null?"".getBytes():(byte[])bizObj));
//			System.out.println("sendRecv=>"+HeaderActionKeys.MESSAGE_RECV);
//			System.out.println("commandType=>"+HeaderActionKeys.ADD_DELETE_HEADER);	        
//	        
//            byte[] header = action.excute(orgData ,(byte[])bizObj, HeaderActionKeys.MESSAGE_RECV, HeaderActionKeys.ADD_DELETE_HEADER);
//            System.out.println("header=>"+new String(header));
            ///////////////////////////////////////////

		   
		    /////전송 DA 
//	        HeaderAction action = HeaderActionFactory.createAction(actionName);
//
//	        Object[] bizObjs =new Object[1];
//	        bizObjs[0] = "0013HDR1234567890".getBytes();
//	        Object bizObj = null;
//	        if(bizObjs != null && bizObjs.length > 0) {
//	            bizObj = bizObjs[0];
//	        }
//	        byte[] orgData = null; 
//       	orgData = new byte[((byte[])bizObj).length ];
//   		System.arraycopy(((byte[])bizObj), 0, orgData, 0,orgData.length);
//			System.out.println("orgData=>"+new String(orgData==null?"".getBytes():orgData));
//			System.out.println("message=>"+new String(bizObj==null?"".getBytes():(byte[])bizObj));
//			System.out.println("sendRecv=>"+HeaderActionKeys.MESSAGE_SEND);
//			System.out.println("commandType=>"+HeaderActionKeys.DELETE_ADD_HEADER);	               
//         byte[] header = action.excute(orgData ,(byte[])bizObj, HeaderActionKeys.MESSAGE_SEND, HeaderActionKeys.DELETE_ADD_HEADER);
//         System.out.println("header=>"+new String(header));
		   	/////////////
		    
		    /////수신 DA 
//	        HeaderAction action = HeaderActionFactory.createAction(actionName);
//
//	        Object[] bizObjs =new Object[1];
//	        bizObjs[0] = "1234567890".getBytes();
//	        Object bizObj = null;
//	        if(bizObjs != null && bizObjs.length > 0) {
//	            bizObj = bizObjs[0];
//	        }
//	        byte[] orgData = null; 
//      	orgData = new byte["0110200805021112280000000000010200X203    1370100000002SFX          1                               1234567890".getBytes().length ];
//  	 	System.arraycopy("0110200805021112280000000000010200X203    1370100000002SFX          1                               1234567890".getBytes(), 0, orgData, 0,orgData.length);
//			System.out.println("orgData=>"+new String(orgData==null?"".getBytes():orgData));
//			System.out.println("message=>"+new String(bizObj==null?"".getBytes():(byte[])bizObj));
//			System.out.println("sendRecv=>"+HeaderActionKeys.MESSAGE_RECV);
//			System.out.println("commandType=>"+HeaderActionKeys.DELETE_ADD_HEADER);	                   
//         byte[] header = action.excute(orgData ,(byte[])bizObj, HeaderActionKeys.MESSAGE_RECV, HeaderActionKeys.DELETE_ADD_HEADER);
//         System.out.println("header=>"+new String(header));
            /////////////////////////////////////////		   
//            
//            
//	    }	
} 

