package com.cim.idm.mwmsextend.generic.esb;


import java.lang.reflect.InvocationTargetException;
import java.text.MessageFormat;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.cim.idm.wmsextend.generic.GenericServiceProxy;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import com.cim.idm.wmsextend.generic.errorHandler.CustomException;
import com.cim.idm.wmsextend.generic.object.ErrorDef;
import com.cim.idm.mwmsextend.generic.util.CommonUtil;
import com.cim.idm.wmsextend.generic.util.SMessageUtil;
import com.cim.idm.mwmsextend.materialpackingstockin.service.MaterialStockInClass;
import com.cim.idm.flowpackage.exception.BpelException;
import com.cim.idm.framework.IDMFrameServiceProxy;
import com.cim.idm.framework.esb.GenericSender;
import com.cim.idm.framework.esb.IRequester;
import com.cim.idm.framework.exception.IDMFrameErrorSignal;
import com.cim.idm.framework.exception.ext.DuplicateNameSignal;
import com.cim.idm.framework.exception.ext.FrameworkErrorSignal;
import com.cim.idm.framework.exception.ext.InvalidStateTransitionSignal;
import com.cim.idm.framework.exception.ext.NotFoundSignal;
import com.cim.idm.framework.util.time.TimeStampUtil;
import com.cim.idm.framework.util.xml.JdomUtils;


public class ESBService implements ApplicationContextAware {
	
	private static Log log = LogFactory.getLog(ESBService.class);
	private ApplicationContext	applicationContext;
	public static final String 		SUCCESS = "0";
	private Map<String, String> sendSubjectMap = new Hashtable<String, String>();
	
	/*
	* Name : init
	* Desc : This function is get SendSubjectMap
	* Author : ZXY IDM Team
	* Date : 2011.01.16
	*/
	public void init()
	{
		sendSubjectMap.put("CNXsvr", makeCustomServerLocalSubject("CNXsvr"));
		sendSubjectMap.put("CNMsvr", makeCustomServerLocalSubject("CNMsvr"));
		sendSubjectMap.put("PDAsvr", makeCustomServerLocalSubject("PDAsvr"));
//		sendSubjectMap.put("AWMsvr", makeCustomServerLocalSubject("AWMsvr"));
		sendSubjectMap.put("QRYsvr", makeCustomServerLocalSubject("QRYsvr"));
//		sendSubjectMap.put("WMSsvr", makeCustomServerLocalSubject("WMSsvr"));
		sendSubjectMap.put("ERPsvr", makeCustomServerLocalSubject("ERPsvr"));
	}
	
	/*
	* Name : getSendSubject
	* Desc : This function is get sendSubjectMap
	* Author : ZXY IDM Team
	* Date : 2011.01.16
	*/
	public String getSendSubject(String serverName)
	{
		return sendSubjectMap.get(serverName);
	}
	
	/*
	* Name : makeCustomServerLocalSubject
	* Desc : This function is makeCustomServerLocalSubject
	* Author : ZXY IDM Team
	* Date : 2011.01.16
	*/
	public String makeCustomServerLocalSubject(String serverName)
	{
		//_LOCAL.KRK0100.DEV.MES.MES.QRYsvr
		//A1.SO.G1.PROD.MES.QRYsvr
		//BOEHF.B3.EIS.DEV.MOD
		//BOEHF.B3.MES.DEVJMNA.MOD.CNMsvr
		
		StringBuilder serverSubject = new StringBuilder();
		if(StringUtils.equals(serverName, "CNMSvr")||
				StringUtils.equals(serverName, "AWMsvr")||
				StringUtils.equals(serverName, "QRYsvr")||
				StringUtils.equals(serverName, "WMSsvr") || 
				StringUtils.equals(serverName, "PDAsvr")){
			
			serverSubject.append(System.getProperty("company")).
			              append("." + System.getProperty("factory")).
			              append("." + System.getProperty("cim")).
			              append("." + System.getProperty("mode")).
			              append("." + serverName);
			
		} else {
			
			
				
				serverSubject.append("_LOCAL." + System.getProperty("company")).
                              append("." + System.getProperty("factory")).
                              append("." + System.getProperty("cim")).
                              append("." + System.getProperty("mode")).
                              append("." + serverName);
		}
				
		log.info("Maked SubjectName=" + serverSubject.toString());
		
		return serverSubject.toString(); 
	}
	
	/*
	* Name : setApplicationContext
	* Desc : This function is setApplicationContext
	* Author : ZXY IDM Team
	* Date : 2011.01.16
	*/
	public void setApplicationContext(ApplicationContext arg0)
			throws BeansException {
		// TODO Auto-generated method stub
		this.applicationContext = arg0;
	}
	
	/*
	* Name : send
	* Desc : This function is send Subject Message
	* Author : ZXY IDM Team
	* Date : 2011.01.16
	*/
	public void send(String subject, Document doc )
	{
		String sendMsg = JdomUtils.toString(doc);
		GenericSender<String, String> genericSender = IDMFrameServiceProxy.getGenericSender();
		IDMFrameServiceProxy.getGenericSender().setDataField("Message");
	    IDMFrameServiceProxy.getGenericSender().send(subject, sendMsg);
		log.info(" SEND : Subject=" + subject);
		loggingSend(sendMsg);
	}
	
	public void send(String subject, String sendMsg )
	{
		GenericServiceProxy.getGenericSender("OICSender").send(subject, sendMsg);
		log.info(" SEND : Subject=" + subject);
		loggingSend(sendMsg);
	}
	
	public void send(String subject, String sendMsg ,String senderName)
	{
		GenericServiceProxy.getGenericSender(senderName).send(subject, sendMsg);
		log.info(" SEND : Subject=" + subject);
		loggingSend(sendMsg);
	}

	/*
	* Name : sendReplyBySenderWithoutLog
	* Desc : This function is sendReplyBySenderWithoutLog Message
	* Author : ZXY IDM Team
	* Date : 2011.01.16
	*/
	public void sendReplyBySenderWithoutLog(String subject, Document doc, String senderName)
	{
		String sendMsg = JdomUtils.toString(doc);
		GenericServiceProxy.getGenericSender(senderName).send(subject, sendMsg);
		log.info(" SEND : Subject=" + subject);
		loggingSend(sendMsg);
	}
	
	/*
	* Name : sendBySender
	* Desc : This function is sendBySender Message
	* Author : ZXY IDM Team
	* Date : 2011.01.16
	*/
	public void sendBySender(Document doc, String senderName) {
		String sendMsg = JdomUtils.toString(doc);
		log.info(sendMsg);
		GenericServiceProxy.getGenericSender(senderName).send(sendMsg);
		log.info(" SEND : Sender=" + senderName);
		loggingSend(sendMsg);
	}
	
	/*
	* Name : sendBySender
	* Desc : This function is sendBySender Message
	* Author : ZXY IDM Team
	* Date : 2011.01.16
	*/
	public void sendBySender(String subject, Document doc, String senderName)
	{
		String sendMsg = JdomUtils.toString(doc);
		GenericServiceProxy.getGenericSender(senderName).send(subject, sendMsg);
		log.info(" SEND : Subject=" + subject);
		loggingSend(sendMsg);
	}


	/*
	* Name : send
	* Desc : This function is send Message
	* Author : ZXY IDM Team
	* Date : 2011.01.16
	*/
	public void send(String replySubject, Document doc, Element element) throws Exception
	{
		String sReplyMsg = null;
		String XPathBody = "//"+SMessageUtil.Message_Tag+"/"+SMessageUtil.Body_Tag;
		String XPathHeader = "//"+SMessageUtil.Message_Tag+"/"+SMessageUtil.Header_Tag;
		
		Element message = new Element( SMessageUtil.Message_Tag );  
		
		Element oriheader = doc.getDocument().getRootElement().getChild("header");
		
		Element header = new Element("header");
		Element body = new Element("body");
		
		Element messagename = new Element("messagename");
		messagename.setText(oriheader.getChildText("messagename"));
		header.addContent(messagename);
		
		Element transactionid = new Element("transactionid");
		transactionid.setText(oriheader.getChildText("transactionid"));
		header.addContent(transactionid);
				
		message.addContent(header);
		
		if ( element != null )
		{
			body = element;
			message.addContent(element);
		}
		Document replyDoc = new Document( message ); 
		   
		sReplyMsg = JdomUtils.toString(replyDoc);
		
		IDMFrameServiceProxy.getGenericSender().reply(replySubject, sReplyMsg);

		log.info(" SEND : Subject=" + replySubject);
		loggingSend(sReplyMsg);

	}

	/*
	* Name : loggingSend
	* Desc : This function is loggingSend Message
	* Author : ZXY IDM Team
	* Date : 2011.01.16
	*/
	private void loggingSend(String sReplyMsg) {
		if(sReplyMsg.length() > 3000) {
			StringBuilder sb = new StringBuilder();
			sb.append(sReplyMsg.substring(0, 2000));
			sb.append("\n..................................\n");
			sb.append(sReplyMsg.substring(sReplyMsg.length()-1000));
			log.info(" SEND : Message=" + sb);
		}else {
			log.info(" SEND : Message=" + sReplyMsg);
		}
	}
	
	/*
	* Name : sendBySender
	* Desc : This function is sendBySender Message
	* Author : ZXY IDM Team
	* Date : 2011.01.16
	*/
	public void sendBySender(String replySubject, Document doc, Element element, String senderName) throws Exception
	{
		String sReplyMsg = null;
		String XPathBody = "//"+SMessageUtil.Message_Tag+"/"+SMessageUtil.Body_Tag;
		String XPathHeader = "//"+SMessageUtil.Message_Tag+"/"+SMessageUtil.Header_Tag;
		
		Element message = new Element( SMessageUtil.Message_Tag );  
		
		Element oriheader = doc.getDocument().getRootElement().getChild("Header");
		
		Element header = new Element("Header");
		Element body = new Element("Body");
		
		Element messagename = new Element("MESSAGENAME");
		messagename.setText(oriheader.getChildText("MESSAGENAME"));
		header.addContent(messagename);
		
		Element shopName = new Element("SHOPNAME");
		shopName.setText(oriheader.getChildText("SHOPNAME"));
		header.addContent(shopName);
		
		Element machineName = new Element("MACHINENAME");
		machineName.setText(oriheader.getChildText("MACHINENAME"));
		header.addContent(machineName);
		
		Element transactionid = new Element("TRANSACTIONID");
		transactionid.setText(oriheader.getChildText("TRANSACTIONID"));
		header.addContent(transactionid);
		
		Element originalSourceSubjectName = new Element("ORIGINALSOURCESUBJECTNAME");
		originalSourceSubjectName.setText(oriheader.getChildText("ORIGINALSOURCESUBJECTNAME"));
		header.addContent(originalSourceSubjectName);
		
		Element sourceSubjectName = new Element("SOURCESUBJECTNAME");
		sourceSubjectName.setText(oriheader.getChildText("SOURCESUBJECTNAME"));
		header.addContent(sourceSubjectName);
		
		Element targetSubjectName = new Element("TARGETSUBJECTNAME");
		targetSubjectName.setText(oriheader.getChildText("TARGETSUBJECTNAME"));
		header.addContent(targetSubjectName);
		
		Element eventUser = new Element("EVENTUSER");
		eventUser.setText(oriheader.getChildText("EVENTUSER"));
		header.addContent(eventUser);
		
		Element eventComment = new Element("EVENTCOMMENT");
		eventComment.setText(oriheader.getChildText("EVENTCOMMENT"));
		header.addContent(eventComment);
				
		message.addContent(header);
		
		if ( element != null )
		{
			body = element;
			message.addContent(element);
		}
		
		//
		Element returnElement = new Element("Return");
		
		Element returnCode = new Element("RETURNCODE");
		returnCode.setText(SUCCESS);
		returnElement.addContent(returnCode);
		
		Element returnMessage = new Element("RETURNMESSAGE");
		returnMessage.setText("");
		returnElement.addContent(returnMessage);
		
		message.addContent(returnElement);
		
		//
		
		Document replyDoc = new Document( message ); 
		   
		sReplyMsg = JdomUtils.toString(replyDoc);
		
		GenericServiceProxy.getGenericSender(senderName).reply(replySubject, sReplyMsg);

		log.info(" SEND : Subject=" + replySubject);
		loggingSend(sReplyMsg);

	}
	
	/*
	* Name : send
	* Desc : This function is send Message
	* Author : ZXY IDM Team
	* Date : 2011.01.16
	*/
	public void send(String beanName, String subject, Document doc )
	{
		String sendMsg = doc.toString();
		IRequester requester = (IRequester)IDMFrameServiceProxy.getApplicationContext().getBean(beanName);
		requester.send(subject, doc.toString());
		
		log.info(" SEND : Subject=" + subject);
		loggingSend(sendMsg);
		
	}
	
	/*
	* Name : sendReply
	* Desc : This function is sendReply Message
	* Author : ZXY IDM Team
	* Date : 2011.01.16
	*/
	public void sendReply( String replySubject )
	{
	   String sReplyMsg = SMessageUtil.replyXMLMessage(SUCCESS );

	   IDMFrameServiceProxy.getGenericSender().reply(replySubject, sReplyMsg);
	   
	   log.info(" SERP : Subject=" + replySubject);
	   loggingReply(sReplyMsg);
	}
	
	/*
	* Name : sendReplyBySender
	* Desc : This function is sendReplyBySender Message
	* Author : ZXY IDM Team
	* Date : 2011.01.16
	*/
	public void sendReplyBySender( String replySubject , String senderName)
	{
	   String sReplyMsg = SMessageUtil.replyXMLMessage(SUCCESS );

	   
	   GenericServiceProxy.getGenericSender(senderName).reply(replySubject, sReplyMsg);
	   
	   log.info(" SERP : Subject=" + replySubject);
	   loggingReply(sReplyMsg);
	}
	
	/*
	* Name : sendReply
	* Desc : This function is sendReply Message
	* Author : ZXY IDM Team
	* Date : 2011.01.16
	*/
	public void sendReply( String replySubject, String sReplyMsg )
	{
		
	   IDMFrameServiceProxy.getGenericSender().setDataField(SMessageUtil.dataField_TAG);
	   IDMFrameServiceProxy.getGenericSender().reply(replySubject, sReplyMsg);

	   log.info(" SERP : Subject=" + replySubject);
	   loggingReply(sReplyMsg);
	}
	
	/*
	* Name : sendReplyBySenderB18
	* Desc : This function is sendReplyBySender Message
	* Author : 10819222
	* Date : 2024.08.12
	*/
	public void sendErpReplyBySender( String replySubject, Document doc, String senderName) throws Exception
	{
		sendErpReplyBySender(replySubject, doc, senderName,SUCCESS,"");
	}
	
	
	/*
	* Name : sendReplyBySenderB18
	* Desc : This function is sendReplyBySender Message
	* Author : 10819222
	* Date : 2024.08.12
	*/
	public void sendErpReplyBySender( String replySubject, Document doc , String senderName, String resultCode, String resultMessage) throws Exception 
	{
	   String sReplyMsg = null;
	   SMessageUtil.setResultItemValue(doc, SMessageUtil.Result_ReturnCode, resultCode);
	   SMessageUtil.setResultItemValue(doc, SMessageUtil.Result_ErrorMessage, resultMessage);
	  
	   sReplyMsg = JdomUtils.toString(doc);
	   String resultType="";
	   if(resultCode.equalsIgnoreCase("0")) 
	   {
		   resultType="S";
		   resultMessage="成功";
	   }else 
	   {
		   resultType="E";
		   resultMessage="失败";
	   }
	   Element eRoot=doc.getDocument().getRootElement();
	   String messageID=eRoot.getChild("Header").getChild("MESSAGENAME").getText().toString();
	   Element root = new Element("RESPONSEDATA");
	   Element interfaceID=new Element("INTERFACEID");
	   interfaceID.addContent(messageID);
	   root.addContent(interfaceID);
	   Element transID=new Element("TRANSID");
	   transID.addContent("");
	   root.addContent(transID);
	   //Element mID=new Element("MESSAGEID");
	   //mID.addContent("null");
	   //root.addContent(mID);
	   Element resType=new Element("RESULTTYPE");
	   resType.addContent(resultType);
	   root.addContent(resType);
	   Element resMessage=new Element("RESULTMESSAGE");
	   resMessage.addContent(resultMessage);
	   root.addContent(resMessage);
	   Element resultString=new Element("RESULTSTRING");
	   
	   
	   Element messageTag=new Element(eRoot.getName());

	   //root.addContent(messageTag);
	   
	   resultString.addContent(messageTag);
	   root.addContent(resultString);
	   
	   Document document = new Document(root);
	   java.util.List<Element> children = eRoot.getChildren(); // 获取所有子元素
	   generateXmlData(messageTag,children,resMessage);
   
       sReplyMsg=JdomUtils.toString(document);
       //sReplyMsg=JdomUtils.toString(doc);
       GenericSender sender=((GenericSender) applicationContext.getBean(senderName));
       //sender.setDataField("Message");
       sender.reply(replySubject, sReplyMsg);
       /*
       com.tibco.tibrv.TibrvMsg tibrvMsg=new  com.tibco.tibrv.TibrvMsg();
       tibrvMsg.setSendSubject(replySubject);
       tibrvMsg.add("INTERFACEID", messageID);
       tibrvMsg.add("TRANSID", "");
       tibrvMsg.add("MESSAGEID", "null");
       tibrvMsg.add("RESULTTYPE", resultType);
       tibrvMsg.add("RESULTMESSAGE", resultMessage);
       tibrvMsg.add("REQUESTSTRING","<![CDATA["+sReplyMsg+"]]>");
       com.tibco.tibrv.TibrvRvdTransport tibrv=new com.tibco.tibrv.TibrvRvdTransport("21320",null,null);
       tibrv.send(tibrvMsg);
       */
	   //com.tibco.tibrv.TibrvRvdTransport tibrv=(com.tibco.tibrv.TibrvRvdTransport)applicationContext.getBean(senderName);
	   
	   log.info(" SERP : Subject=" + replySubject);
	   log.info(" SERP : Subject=" + sReplyMsg);
	   //loggingReply(tibrvMsg.toString());
	   
	   loggingReply(sReplyMsg);

	   //GenericServiceProxy.getGenericSender(senderName).reply(replySubject, sReplyMsg);


	  
	}
	/*
	* Name : sendErrorBySenderB18
	* Desc : This function is sendErrorBySender Message
	* Author : 10819222
	* Date : 2024.08.12
	*/
	public void sendErpErrorBySender( String replySubject, Document doc, String lanuage, CustomException e, String senderName) {
		sendErpErrorBySender(replySubject, doc, lanuage, (Exception)e, senderName);
	}
	/*
	* Name : sendErrorBySenderB18
	* Desc : This function is sendErrorBySender Message
	* Author : 10819222
	* Date : 2024.08.12
	*/
	public void sendErpErrorBySender( String replySubject, Document doc, String lanuage, Exception e, String senderName){
		sendErpErrorBySender(replySubject, doc, lanuage, e, senderName,null);
	}
	/*
	* Name : sendErrorBySenderB18
	* Desc : This function is sendErrorBySender Message
	* Author : 10819222
	* Date : 2024.08.12
	*/
	private void sendErpErrorBySender( String replySubject, Document doc, String lanuage, Exception e, String senderName, String returnCode)
	{
		if(e instanceof BpelException){
			Exception nativeException = ((BpelException)e).getNativeException();
			if(nativeException != null) {
				e = nativeException;
			}
		}
	   String sReplyMsg = this.makeSendError(replySubject, doc, lanuage, e, returnCode);
	   
	   Element eRoot=doc.getDocument().getRootElement();
	   String messageID=eRoot.getChild("Header").getChild("MESSAGENAME").getText().toString();
	   Element root = new Element("RESPONSEDATA");
	   Element interfaceID=new Element("INTERFACEID");
	   interfaceID.addContent(messageID);
	   root.addContent(interfaceID);
	   Element transID=new Element("TRANSID");
	   transID.addContent("");
	   root.addContent(transID);
	   Element mID=new Element("MESSAGEID");
	   mID.addContent("null");
	   root.addContent(mID);
	   Element resType=new Element("RESULTTYPE");
	   resType.addContent("E");
	   root.addContent(resType);
	   Element resMessage=new Element("RESULTMESSAGE");
	   resMessage.addContent(e.getMessage());
	   root.addContent(resMessage);
	   Element resultString=new Element("RESULTSTRING");

	   
	   Element messageTag=new Element(eRoot.getName());

	   //root.addContent(messageTag);
	   
	   resultString.addContent(messageTag);
	   root.addContent(resultString);
	   
	   Document document = new Document(root);
	   java.util.List<Element> children = eRoot.getChildren(); // 获取所有子元素
	   generateXmlData(messageTag,children,resMessage);
	       // 将Document转换为String
	   
	   sReplyMsg=JdomUtils.toString(document);

	   
	   if(StringUtils.isNotEmpty(replySubject) == true)
		   GenericServiceProxy.getGenericSender(senderName).reply(replySubject, sReplyMsg);

	   log.error(" SERP : Subject=" + replySubject);
	   loggingReply(sReplyMsg);
	}
	
	private void generateXmlData(Element parentElement,java.util.List<Element> children,Element resMsg) {
		 for (Element child : children) {
	           // 检查每个子元素，‌根据业务逻辑进行修改
	           // 例如，‌我们可以根据某些条件将某个子元素移动到新的父元素下
	           // 这里是一个假设的例子，‌你需要根据实际情况调整代码
	    	   /*
	           if (child.getName().equals("someCondition")) {
	               // 假设我们要将这个子元素移动到新的父元素下
	               Element newParent = doc.getRootElement().getChild("newParentElement");
	               newParent.addContent(child); // 将子元素添加到新的父元素下
	               // 从原父元素中移除该子元素（‌如果需要的话）‌
	               // root.removeChild(child); // 这行代码可能需要根据实际情况调整，‌因为直接从原父元素中移除可能不是必要的或正确的操作。‌通常我们只需要调整父子关系即可。‌
	           }*/
	    	   if(child.getName().equals("RETURNMESSAGE")) {
	    		   resMsg.addContent(child.getText());
	    	   }
	    	   Element newChild=new Element(child.getName());
	    	   newChild.addContent(child.getText());
	    	   parentElement.addContent(newChild);
	    	   if(!child.getChildren().isEmpty())
	    	   {
	    		   java.util.List<Element> secChildren=child.getChildren();
	    		   generateXmlData(newChild,secChildren,resMsg);
	    	   }
	       }
		
	}
	/*
	* Name : sendReplyBySender
	* Desc : This function is sendReplyBySender Message
	* Author : ZXY IDM Team
	* Date : 2011.01.16
	*/
	public void sendReplyBySender( String replySubject, String sReplyMsg , String senderName)
	{
		
		
		GenericServiceProxy.getGenericSender(senderName).reply(replySubject, sReplyMsg);

	   log.info(" SERP : Subject=" + replySubject);
	   loggingReply(sReplyMsg);
	}
	
	/*
	* Name : sendReply
	* Desc : This function is sendReply Message
	* Author : ZXY IDM Team
	* Date : 2011.01.16
	*/
	public void sendReply( String replySubject, Document doc, Element element ) throws Exception
	{
	   String sReplyMsg = null;
	   SMessageUtil.setResultItemValue(doc, SMessageUtil.Result_ReturnCode, SUCCESS);
	   SMessageUtil.setResultItemValue(doc, SMessageUtil.Result_ErrorMessage, "");
	   String XPathResult = "//"+SMessageUtil.Message_Tag+"/"+SMessageUtil.Result_Name_Tag;
	  
	   if ( element != null )
	   {
	      JdomUtils.getNode(doc, XPathResult).addContent(element);
	   }
	   
	   sReplyMsg = JdomUtils.toString(doc);
	
	   IDMFrameServiceProxy.getGenericSender().setDataField(SMessageUtil.dataField_TAG);
	   IDMFrameServiceProxy.getGenericSender().reply(replySubject, sReplyMsg);

	   log.info(" SERP : Subject=" + replySubject);
	   loggingReply(sReplyMsg);
	}
	
	
	/*
	* Name : sendReplyBySender
	* Desc : This function is sendReplyBySender Message
	* Author : ZXY IDM Team
	* Date : 2011.01.16
	*/
	public void sendReplyBySender( String replySubject, Document doc, String senderName) throws Exception
	{
		sendReplyBySender(replySubject, doc, senderName,SUCCESS,"");
	}
	
	/*
	* Name : sendReplyBySendere
	* Desc : This function is sendReplyBySender Message
	* Author : ZXY IDM Team
	* Date : 2011.01.16
	*/
	public void sendReplyBySender( String replySubject, Document doc, Element element , String senderName) throws Exception
	{
	   String sReplyMsg = null;
	   SMessageUtil.setResultItemValue(doc, SMessageUtil.Result_ReturnCode, SUCCESS);
	   SMessageUtil.setResultItemValue(doc, SMessageUtil.Result_ErrorMessage, "");
	   String XPathResult = "//"+SMessageUtil.Message_Tag+"/"+SMessageUtil.Body_Tag;
	  
	   if ( element != null )
	   {
	      JdomUtils.getNode(doc, XPathResult).addContent(element);
	   }
	   
	   sReplyMsg = JdomUtils.toString(doc);
	
//	   GenericSender genericSender = GenericServiceProxy.getGenericSender(senderName);
//	   genericSender.setDataField(SMessageUtil.dataField_TAG);
//	   genericSender.reply(replySubject, sReplyMsg);
	   
	   ((GenericSender) applicationContext.getBean(senderName)).reply(replySubject, sReplyMsg);

	   log.info(" SERP : Subject = " + replySubject);
	   log.info(" SERP : Message = " + sReplyMsg);
	   loggingReply(sReplyMsg);
	}
	
	//SMT回复消息
		public void SMTsendReplyBySender( String replySubject, Document doc, String senderName,String checkresult ) throws Exception
		{
		   String sReplyMsg = null;
		   String[] List=checkresult.split("\\|");
		   String MSGreturncode=List[1];
		   String MSGdetail=List[0];
		   SMessageUtil.setResultItemValue(doc, SMessageUtil.Result_ReturnCode, MSGreturncode);
		   SMessageUtil.setResultItemValue(doc, SMessageUtil.Result_ErrorMessage, MSGdetail);
		   //String XPathResult = "//"+SMessageUtil.Message_Tag+"/"+SMessageUtil.Body_Tag;
		   
		   sReplyMsg = JdomUtils.toString(doc);
		
		   GenericSender genericSender = GenericServiceProxy.getGenericSender(senderName);
		   genericSender.setDataField(SMessageUtil.dataField_TAG);
		   genericSender.reply(replySubject, sReplyMsg);

		   log.info(" SERP : Subject = " + replySubject);
		   log.info(" SERP : Message = " + sReplyMsg);
		   loggingReply(sReplyMsg);
		}
		
		
	//SMT拆分
		public void SMTSplitsendReplyBySender( String replySubject, Document doc, Element element , String senderName,String checkresult ) throws Exception
		{
		   String sReplyMsg = null;
		   String[] List=checkresult.split("\\|");
		   String MSGreturncode=List[1];
		   String MSGdetail=List[0];
		   SMessageUtil.setResultItemValue(doc, SMessageUtil.Result_ReturnCode, MSGreturncode);
		   SMessageUtil.setResultItemValue(doc, SMessageUtil.Result_ErrorMessage, MSGdetail);
		   String XPathResult = "//"+SMessageUtil.Message_Tag+"/"+SMessageUtil.Body_Tag;
		  
		   if ( element != null )
		   {
		      JdomUtils.getNode(doc, XPathResult).addContent(element);
		   }
		   
		   sReplyMsg = JdomUtils.toString(doc);
		
		   GenericSender genericSender = GenericServiceProxy.getGenericSender(senderName);
		   genericSender.setDataField(SMessageUtil.dataField_TAG);
		   genericSender.reply(replySubject, sReplyMsg);

		   log.info(" SERP : Subject = " + replySubject);
		   log.info(" SERP : Message = " + sReplyMsg);
		   loggingReply(sReplyMsg);
		}

	/*
	* Name : loggingReply
	* Desc : This function is loggingReply Message
	* Author : ZXY IDM Team
	* Date : 2011.01.16
	*/
	private void loggingReply(String sReplyMsg) {
		if(sReplyMsg.length() > 3000) {
				StringBuilder sb = new StringBuilder();
				sb.append(sReplyMsg);
//				sb.append(sReplyMsg.substring(0, 2000));
//				sb.append("\n..................................\n");
//				sb.append(sReplyMsg.substring(sReplyMsg.length()-1000));
				log.info(" SERP : Message=" + sb);
			}else {
				log.info(" SERP : Message=" + sReplyMsg);
			}
	}
	
	/*
	* Name : sendReplyBodyChange
	* Desc : This function is sendReplyBodyChange Message
	* Author : ZXY IDM Team
	* Date : 2011.01.16
	*/
	public void sendReplyBodyChange(String replySubject, Document doc, Element element) throws Exception
	{
		String replyMsg = null;
		String sReplyMsg = null;
		   SMessageUtil.setResultItemValue(doc, SMessageUtil.Result_ReturnCode, SUCCESS);
		   SMessageUtil.setResultItemValue(doc, SMessageUtil.Result_ErrorMessage, "");
		   String XPathResult = "//"+SMessageUtil.Message_Tag+"/"+SMessageUtil.Result_Name_Tag;
		  
		   if ( element != null )
		   {
			   Element srcBody = JdomUtils.getNode(doc, "//" + SMessageUtil.Message_Tag + "/" + SMessageUtil.Body_Tag);
			   srcBody.removeContent();
			   srcBody.addContent(element);
		   }
		   
		   sReplyMsg = JdomUtils.toString(doc);
		
		   IDMFrameServiceProxy.getGenericSender().setDataField(SMessageUtil.dataField_TAG);
		   IDMFrameServiceProxy.getGenericSender().reply(replySubject, sReplyMsg);

		   log.info(" SERP : Subject=" + replySubject);
		   loggingReply(sReplyMsg);
	}
	
	/*
	* Name : sendReplyBodyChangeBySender
	* Desc : This function is sendReplyBodyChangeBySender Message
	* Author : ZXY IDM Team
	* Date : 2011.01.16
	*/
	public void sendReplyBodyChangeBySender(String replySubject, Document doc, Element element, String senderName) throws Exception
	{
		String replyMsg = null;
		String sReplyMsg = null;
		   SMessageUtil.setResultItemValue(doc, SMessageUtil.Result_ReturnCode, SUCCESS);
		   SMessageUtil.setResultItemValue(doc, SMessageUtil.Result_ErrorMessage, "");
		   String XPathResult = "//"+SMessageUtil.Message_Tag+"/"+SMessageUtil.Result_Name_Tag;
		  
		   if ( element != null )
		   {
			   Element srcBody = JdomUtils.getNode(doc, "//" + SMessageUtil.Message_Tag + "/" + SMessageUtil.Body_Tag);
			   srcBody.removeContent();
			   srcBody.addContent(element);
		   }
		   
		   sReplyMsg = JdomUtils.toString(doc);
		
		   
		   GenericServiceProxy.getGenericSender(senderName).reply(replySubject, sReplyMsg);

		   log.info(" SERP : Subject=" + replySubject);
		   loggingReply(sReplyMsg);
	}
	
	/*
	* Name : sendReply
	* Desc : This function is sendReply Message
	* Author : ZXY IDM Team
	* Date : 2011.01.16
	*/
	public void sendReply( String replySubject, Document doc ) 
	{
	   String sReplyMsg = null;
	   SMessageUtil.setResultItemValue(doc, SMessageUtil.Result_ReturnCode, SUCCESS);
	   SMessageUtil.setResultItemValue(doc, SMessageUtil.Result_ErrorMessage, "");
	  
	   sReplyMsg = JdomUtils.toString(doc);

	   IDMFrameServiceProxy.getGenericSender().setDataField(SMessageUtil.dataField_TAG);
	   IDMFrameServiceProxy.getGenericSender().reply(replySubject, sReplyMsg);

	   log.info(" SERP : Subject=" + replySubject);
	   loggingReply(sReplyMsg);
	}
	
	/*
	* Name : sendReplyBySender
	* Desc : This function is sendReplyBySender Message
	* Author : ZXY IDM Team
	* Date : 2011.01.16
	*/
	public void sendReplyBySender( String replySubject, Document doc , String senderName, String resultCode, String resultMessage) 
	{
	   String sReplyMsg = null;
	   SMessageUtil.setResultItemValue(doc, SMessageUtil.Result_ReturnCode, resultCode);
	   SMessageUtil.setResultItemValue(doc, SMessageUtil.Result_ErrorMessage, resultMessage);
	  
	   sReplyMsg = JdomUtils.toString(doc);

	   
	   //GenericServiceProxy.getGenericSender(senderName).setDataField(SMessageUtil.dataField_TAG);
	   //GenericServiceProxy.getGenericSender(senderName).reply(replySubject, sReplyMsg);
	   ((GenericSender) applicationContext.getBean(senderName)).reply(replySubject, sReplyMsg);

	   log.info(" SERP : Subject=" + replySubject);
	   log.info(" SERP : Subject=" + sReplyMsg);
	   loggingReply(sReplyMsg);
	}

	
	public void sendQueryReplyBySender( String replySubject, Document doc , String senderName, String resultCode, String resultMessage) 
	{
	   Element root = doc.getDocument().getRootElement();
	   Element headerElement = root.getChild("Header");
	   /*Element originalElement = bodyElement.getChild("ORIGINALSOURCESUBJECTNAME");
	   Element listenerElement = bodyElement.getChild("listener");*/
	   headerElement.removeChild("ORIGINALSOURCESUBJECTNAME");
	   headerElement.removeChild("listener");
	   String sReplyMsg = null;	   
	   SMessageUtil.setResultItemValue(doc, SMessageUtil.Result_ReturnCode, "0");
	   SMessageUtil.setResultItemValue(doc, SMessageUtil.Result_ErrorMessage, resultMessage);
	   
	   sReplyMsg = JdomUtils.toString(doc);
	   
	   	  
	   GenericServiceProxy.getGenericSender(senderName).reply(replySubject, sReplyMsg);
	   log.info(" SERP : Subject=" + replySubject);
	   loggingReply(sReplyMsg);
	}
	public void sendReplyBodyBySender( String replySubject, Document doc , String senderName, String resultCode, String resultMessage) 
	{
	   String sReplyMsg = null;
	   SMessageUtil.setBodyItemValue(doc, SMessageUtil.Result_ReturnCode, resultCode);
	   SMessageUtil.setBodyItemValue(doc, SMessageUtil.Result_ErrorMessage, resultMessage);
	  
	   sReplyMsg = JdomUtils.toString(doc);

	   
	   GenericServiceProxy.getGenericSender(senderName).reply(replySubject, sReplyMsg);

	   log.info(" SERP : Subject=" + replySubject);
	   loggingReply(sReplyMsg);
	}
	
	public void sendReplyBodyBySender( String test,String replySubject, Document doc , String senderName, String resultCode, String resultMessage) 
	{
	   String sReplyMsg = null;
	   SMessageUtil.setResultItemValue(doc, SMessageUtil.Result_ReturnCode, resultCode);
	   SMessageUtil.setResultItemValue(doc, SMessageUtil.Result_ErrorMessage, resultMessage);
	  
	   sReplyMsg = JdomUtils.toString(doc);

	   GenericServiceProxy.getGenericSender(senderName).setDataField(SMessageUtil.Body_Tag);
	   GenericServiceProxy.getGenericSender(senderName).reply(replySubject, sReplyMsg);


	   log.info(" SERP : Subject=" + replySubject);
	   loggingReply(sReplyMsg);
	}

	/*
	* Name : sendError
	* Desc : This function is sendError Message
	* Author : ZXY IDM Team
	* Date : 2011.01.16
	*/
	public void sendError( String replySubject, Document doc, String lanuage, CustomException e)
	{
		Exception e2 = (Exception)e;
		sendError(  replySubject,  doc,  lanuage, e2);
	}
	
	/*
	* Name : sendError
	* Desc : This function is sendError Message
	* Author : ZXY IDM Team
	* Date : 2011.01.16
	*/
	public void sendError( String replySubject, Document doc, String lanuage, Exception e)
	{
	
	   String sReplyMsg = this.makeSendError(replySubject, doc, lanuage, e);

	   IDMFrameServiceProxy.getGenericSender().setDataField(SMessageUtil.dataField_TAG);
	   if(StringUtils.isNotEmpty(replySubject) == true)
		   IDMFrameServiceProxy.getGenericSender().reply(replySubject, sReplyMsg);

	   log.error(" SERP : Subject=" + replySubject);
	   loggingReply(sReplyMsg);
	}
	
	/*
	* Name : makeSendError
	* Desc : This function is makeSendError Message
	* Author : ZXY IDM Team
	* Date : 2011.01.16
	*/
	private String makeSendError( String replySubject, Document doc, String lanuage, Exception e){
		return makeSendError(replySubject, doc, lanuage, e, null);
	}
	
	/*
	* Name : makeSendError
	* Desc : This function is makeSendError Message
	* Author : ZXY IDM Team
	* Date : 2011.01.16
	*/
	private String makeSendError( String replySubject, Document doc, String lanuage, Exception e, String returnCode){
		
		String errorCode = "";
		String korErrorMsg = "";
		String engErrorMsg = "";
		String chaErrorMsg = "";
//		String locErrorMsg = "";
		
		Throwable orgEx = e;
		
		Throwable targetEx;
		Throwable cause = orgEx;
		if(e instanceof BpelException) {
			cause = e.getCause();
		}
		if ( cause instanceof InvocationTargetException )
			targetEx = ((InvocationTargetException) cause).getTargetException();
		else if ( cause instanceof CustomException )
			targetEx = ((CustomException) cause);
		else if ( cause instanceof NotFoundSignal )
			targetEx = ((NotFoundSignal) cause);
		else if ( cause instanceof DuplicateNameSignal )
			targetEx = ((DuplicateNameSignal) cause);
		else if ( cause instanceof FrameworkErrorSignal )
			targetEx = ((FrameworkErrorSignal) cause);
		else if ( cause instanceof InvalidStateTransitionSignal )
			targetEx = ((InvalidStateTransitionSignal) cause);
		else if ( cause instanceof IDMFrameErrorSignal )
			targetEx = ((IDMFrameErrorSignal) cause);
		else
			targetEx = orgEx;
		
		if ( targetEx instanceof NotFoundSignal )
		{
			errorCode = "NotFoundSignal";
			ErrorDef errorDef = GenericServiceProxy.getErrorDefMap().getErrorDef(errorCode);
		
			korErrorMsg = 
					MessageFormat.format(errorDef.getKor_errorMessage(), ((NotFoundSignal)targetEx).getErrorCode(), ((NotFoundSignal)targetEx).getMessage() );
			engErrorMsg = 
					MessageFormat.format(errorDef.getEng_errorMessage(), ((NotFoundSignal)targetEx).getErrorCode(), ((NotFoundSignal)targetEx).getMessage() );
			chaErrorMsg = 
					MessageFormat.format(errorDef.getCha_errorMessage(), ((NotFoundSignal)targetEx).getErrorCode(), ((NotFoundSignal)targetEx).getMessage() );
//			locErrorMsg = 
//					MessageFormat.format(errorDef.getLoc_errorMessage(), ((NotFoundSignal)targetEx).getErrorCode(), ((NotFoundSignal)targetEx).getMessage() );
			
		}
		else if ( targetEx instanceof DuplicateNameSignal )
		{
			errorCode = "DuplicateNameSignal";
			ErrorDef errorDef = GenericServiceProxy.getErrorDefMap().getErrorDef(errorCode);

			korErrorMsg =
					MessageFormat.format(errorDef.getKor_errorMessage(), ((DuplicateNameSignal)targetEx).getErrorCode(), ((DuplicateNameSignal)targetEx).getMessage() );
			engErrorMsg = 
					MessageFormat.format(errorDef.getEng_errorMessage(), ((DuplicateNameSignal)targetEx).getErrorCode(), ((DuplicateNameSignal)targetEx).getMessage() );
			chaErrorMsg = 
					MessageFormat.format(errorDef.getCha_errorMessage(), ((DuplicateNameSignal)targetEx).getErrorCode(), ((DuplicateNameSignal)targetEx).getMessage() );
//			locErrorMsg = 
//					MessageFormat.format(errorDef.getLoc_errorMessage(), ((DuplicateNameSignal)targetEx).getErrorCode(), ((DuplicateNameSignal)targetEx).getMessage() );
		}
		else if ( targetEx instanceof FrameworkErrorSignal)
		{
			errorCode = "FrameworkErrorSignal";
			ErrorDef errorDef = GenericServiceProxy.getErrorDefMap().getErrorDef(errorCode);
		
			korErrorMsg =
					MessageFormat.format(errorDef.getKor_errorMessage(), ((FrameworkErrorSignal)targetEx).getErrorCode(), ((FrameworkErrorSignal)targetEx).getMessage() );
			engErrorMsg = 
					MessageFormat.format(errorDef.getEng_errorMessage(), ((FrameworkErrorSignal)targetEx).getErrorCode(), ((FrameworkErrorSignal)targetEx).getMessage() );
			chaErrorMsg = 
					MessageFormat.format(errorDef.getCha_errorMessage(), ((FrameworkErrorSignal)targetEx).getErrorCode(), ((FrameworkErrorSignal)targetEx).getMessage() );
//			locErrorMsg = 
//					MessageFormat.format(errorDef.getLoc_errorMessage(), ((FrameworkErrorSignal)targetEx).getErrorCode(), ((FrameworkErrorSignal)targetEx).getMessage() );
		}
		else if ( targetEx instanceof InvalidStateTransitionSignal)
		{
			errorCode = "InvalidStateTransitionSignal";
			ErrorDef errorDef = GenericServiceProxy.getErrorDefMap().getErrorDef(errorCode);
			
			korErrorMsg =
					MessageFormat.format(errorDef.getKor_errorMessage(), ((InvalidStateTransitionSignal)targetEx).getErrorCode(), ((InvalidStateTransitionSignal)targetEx).getMessage() );
			engErrorMsg = 
					MessageFormat.format(errorDef.getEng_errorMessage(), ((InvalidStateTransitionSignal)targetEx).getErrorCode(), ((InvalidStateTransitionSignal)targetEx).getMessage() );
			chaErrorMsg = 
					MessageFormat.format(errorDef.getCha_errorMessage(), ((InvalidStateTransitionSignal)targetEx).getErrorCode(), ((InvalidStateTransitionSignal)targetEx).getMessage() );
//			locErrorMsg = 
//					MessageFormat.format(errorDef.getLoc_errorMessage(), ((InvalidStateTransitionSignal)targetEx).getErrorCode(), ((InvalidStateTransitionSignal)targetEx).getMessage() );
		}
		else if ( targetEx instanceof CustomException )
		{
			errorCode = ((CustomException)targetEx).errorDef.getErrorCode();
			ErrorDef errorDef = ((CustomException)targetEx).errorDef;
			
			korErrorMsg = errorDef.getKor_errorMessage();
			engErrorMsg = errorDef.getEng_errorMessage();
			chaErrorMsg = errorDef.getCha_errorMessage();
//			locErrorMsg = errorDef.getLoc_errorMessage();			
		}
		else if ( targetEx instanceof IDMFrameErrorSignal )
		{
			errorCode = "IDMFrameErrorSignal";
			ErrorDef errorDef = GenericServiceProxy.getErrorDefMap().getErrorDef(errorCode);
			
			korErrorMsg =
				MessageFormat.format(errorDef.getKor_errorMessage(), ((IDMFrameErrorSignal)targetEx).getErrorCode(), ((IDMFrameErrorSignal)targetEx).getMessage() );
			engErrorMsg = 
				MessageFormat.format(errorDef.getEng_errorMessage(), ((IDMFrameErrorSignal)targetEx).getErrorCode(), ((IDMFrameErrorSignal)targetEx).getMessage() );
			chaErrorMsg = 
				MessageFormat.format(errorDef.getCha_errorMessage(), ((IDMFrameErrorSignal)targetEx).getErrorCode(), ((IDMFrameErrorSignal)targetEx).getMessage() );
//			locErrorMsg = 
//				MessageFormat.format(errorDef.getLoc_errorMessage(), ((IDMFrameErrorSignal)targetEx).getErrorCode(), ((nanoFrameErrorSignal)targetEx).getMessage() );
			
		}
		else 
		{
			errorCode = "UndefinedCode";
			ErrorDef errorDef = GenericServiceProxy.getErrorDefMap().getErrorDef("UndefinedCode");
			if(errorDef != null) {
				korErrorMsg =
					MessageFormat.format(errorDef.getKor_errorMessage(), targetEx.getMessage());
				engErrorMsg =
					MessageFormat.format(errorDef.getEng_errorMessage(), targetEx.getMessage());
				chaErrorMsg =
					MessageFormat.format(errorDef.getCha_errorMessage(), targetEx.getMessage());
//				locErrorMsg = 
//					MessageFormat.format(errorDef.getLoc_errorMessage(), targetEx.getMessage());
			}
			else {
				korErrorMsg = e.getMessage();
				engErrorMsg = e.getMessage();
				chaErrorMsg = e.getMessage();
//				locErrorMsg = e.getMessage();
			}
		}
		
	   String sReplyMsg = null;
	   errorCode = returnCode == null ? errorCode:returnCode;
	   SMessageUtil.setResultItemValue(doc, SMessageUtil.Result_ReturnCode, errorCode);
	   
	   if ( lanuage.toUpperCase().startsWith("KOR"))
		   SMessageUtil.setResultItemValue(doc, SMessageUtil.Result_ErrorMessage, korErrorMsg);
	   else if ( lanuage.toUpperCase().startsWith("ENG") )
		   SMessageUtil.setResultItemValue(doc, SMessageUtil.Result_ErrorMessage, engErrorMsg);
	   else if ( lanuage.toUpperCase().startsWith("CHA") )
		   SMessageUtil.setResultItemValue(doc, SMessageUtil.Result_ErrorMessage, chaErrorMsg);
	   else
		   SMessageUtil.setResultItemValue(doc, SMessageUtil.Result_ErrorMessage, engErrorMsg);
	  
	  
	   sReplyMsg = toString(doc);
	   
	   return sReplyMsg;
	}
	
	/*
	* Name : toString
	* Desc : This function is Message to String
	* Author : ZXY IDM Team
	* Date : 2011.01.16
	*/
	private static String toString(Document doc) {
		XMLOutputter out = new XMLOutputter();
		Format format = Format.getPrettyFormat();
		format.setIndent("\t");
		out.setFormat(format);
		
		String s = out.outputString(doc);
		return s;
	}
	
	/*
	* Name : sendErrorBySender
	* Desc : This function is sendErrorBySender Message
	* Author : ZXY IDM Team
	* Date : 2011.01.16
	*/
	public void sendErrorBySender( String replySubject, Document doc, String lanuage, CustomException e, String senderName, String returnCode) {
		sendErrorBySender(replySubject, doc, lanuage, (Exception)e, senderName,returnCode);
	}
	
	/*
	* Name : sendErrorBySender
	* Desc : This function is sendErrorBySender Message
	* Author : ZXY IDM Team
	* Date : 2011.01.16
	*/
	public void sendErrorBySender( String replySubject, Document doc, String lanuage, CustomException e, String senderName) {
		sendErrorBySender(replySubject, doc, lanuage, (Exception)e, senderName);
	}
	
	/*
	* Name : sendErrorBySender
	* Desc : This function is sendErrorBySender Message
	* Author : ZXY IDM Team
	* Date : 2011.01.16
	*/
	public void sendErrorBySender( String replySubject, Document doc, String lanuage, BpelException e, String senderName, String returnCode) {
		Exception nativeException = e.getNativeException();
		sendErrorBySender(replySubject, doc, lanuage, nativeException, senderName,returnCode);
	}
	
	/*
	* Name : sendErrorBySender
	* Desc : This function is sendErrorBySender Message
	* Author : ZXY IDM Team
	* Date : 2011.01.16
	*/
	public void sendErrorBySender( String replySubject, Document doc, String lanuage, BpelException e, String senderName) {
		Exception nativeException = e.getNativeException();
		sendErrorBySender(replySubject, doc, lanuage, nativeException, senderName);
	}
	
	/*
	* Name : sendErrorBySender
	* Desc : This function is sendErrorBySender Message
	* Author : ZXY IDM Team
	* Date : 2011.01.16
	*/
	public void sendErrorBySender( String replySubject, Document doc, String lanuage, Exception e, String senderName){
		sendErrorBySender(replySubject, doc, lanuage, e, senderName,null);
	}
	
	public String getReturnErrorMessage(String lanuage, Exception e){
		String errorCode = "";
		String korErrorMsg = "";
		String engErrorMsg = "";
		String chaErrorMsg = "";
//		String locErrorMsg = "";
		
		Throwable orgEx = e;
		
		Throwable targetEx;
		Throwable cause = orgEx;
		if(e instanceof BpelException) {
			cause = e.getCause();
		}
		if ( cause instanceof InvocationTargetException )
			targetEx = ((InvocationTargetException) cause).getTargetException();
		else if ( cause instanceof CustomException )
			targetEx = ((CustomException) cause);
		else if ( cause instanceof NotFoundSignal )
			targetEx = ((NotFoundSignal) cause);
		else if ( cause instanceof DuplicateNameSignal )
			targetEx = ((DuplicateNameSignal) cause);
		else if ( cause instanceof FrameworkErrorSignal )
			targetEx = ((FrameworkErrorSignal) cause);
		else if ( cause instanceof InvalidStateTransitionSignal )
			targetEx = ((InvalidStateTransitionSignal) cause);
		else if ( cause instanceof IDMFrameErrorSignal )
			targetEx = ((IDMFrameErrorSignal) cause);
		else
			targetEx = orgEx;
		
		if ( targetEx instanceof NotFoundSignal )
		{
			errorCode = "NotFoundSignal";
			ErrorDef errorDef = GenericServiceProxy.getErrorDefMap().getErrorDef(errorCode);
		
			korErrorMsg = 
					MessageFormat.format(errorDef.getKor_errorMessage(), ((NotFoundSignal)targetEx).getErrorCode(), ((NotFoundSignal)targetEx).getMessage() );
			engErrorMsg = 
					MessageFormat.format(errorDef.getEng_errorMessage(), ((NotFoundSignal)targetEx).getErrorCode(), ((NotFoundSignal)targetEx).getMessage() );
			chaErrorMsg = 
					MessageFormat.format(errorDef.getCha_errorMessage(), ((NotFoundSignal)targetEx).getErrorCode(), ((NotFoundSignal)targetEx).getMessage() );
//			locErrorMsg = 
//					MessageFormat.format(errorDef.getLoc_errorMessage(), ((NotFoundSignal)targetEx).getErrorCode(), ((NotFoundSignal)targetEx).getMessage() );
			
		}
		else if ( targetEx instanceof DuplicateNameSignal )
		{
			errorCode = "DuplicateNameSignal";
			ErrorDef errorDef = GenericServiceProxy.getErrorDefMap().getErrorDef(errorCode);

			korErrorMsg =
					MessageFormat.format(errorDef.getKor_errorMessage(), ((DuplicateNameSignal)targetEx).getErrorCode(), ((DuplicateNameSignal)targetEx).getMessage() );
			engErrorMsg = 
					MessageFormat.format(errorDef.getEng_errorMessage(), ((DuplicateNameSignal)targetEx).getErrorCode(), ((DuplicateNameSignal)targetEx).getMessage() );
			chaErrorMsg = 
					MessageFormat.format(errorDef.getCha_errorMessage(), ((DuplicateNameSignal)targetEx).getErrorCode(), ((DuplicateNameSignal)targetEx).getMessage() );
//			locErrorMsg = 
//					MessageFormat.format(errorDef.getLoc_errorMessage(), ((DuplicateNameSignal)targetEx).getErrorCode(), ((DuplicateNameSignal)targetEx).getMessage() );
		}
		else if ( targetEx instanceof FrameworkErrorSignal)
		{
			errorCode = "FrameworkErrorSignal";
			ErrorDef errorDef = GenericServiceProxy.getErrorDefMap().getErrorDef(errorCode);
		
			korErrorMsg =
					MessageFormat.format(errorDef.getKor_errorMessage(), ((FrameworkErrorSignal)targetEx).getErrorCode(), ((FrameworkErrorSignal)targetEx).getMessage() );
			engErrorMsg = 
					MessageFormat.format(errorDef.getEng_errorMessage(), ((FrameworkErrorSignal)targetEx).getErrorCode(), ((FrameworkErrorSignal)targetEx).getMessage() );
			chaErrorMsg = 
					MessageFormat.format(errorDef.getCha_errorMessage(), ((FrameworkErrorSignal)targetEx).getErrorCode(), ((FrameworkErrorSignal)targetEx).getMessage() );
//			locErrorMsg = 
//					MessageFormat.format(errorDef.getLoc_errorMessage(), ((FrameworkErrorSignal)targetEx).getErrorCode(), ((FrameworkErrorSignal)targetEx).getMessage() );
		}
		else if ( targetEx instanceof InvalidStateTransitionSignal)
		{
			errorCode = "InvalidStateTransitionSignal";
			ErrorDef errorDef = GenericServiceProxy.getErrorDefMap().getErrorDef(errorCode);
			
			korErrorMsg =
					MessageFormat.format(errorDef.getKor_errorMessage(), ((InvalidStateTransitionSignal)targetEx).getErrorCode(), ((InvalidStateTransitionSignal)targetEx).getMessage() );
			engErrorMsg = 
					MessageFormat.format(errorDef.getEng_errorMessage(), ((InvalidStateTransitionSignal)targetEx).getErrorCode(), ((InvalidStateTransitionSignal)targetEx).getMessage() );
			chaErrorMsg = 
					MessageFormat.format(errorDef.getCha_errorMessage(), ((InvalidStateTransitionSignal)targetEx).getErrorCode(), ((InvalidStateTransitionSignal)targetEx).getMessage() );
//			locErrorMsg = 
//					MessageFormat.format(errorDef.getLoc_errorMessage(), ((InvalidStateTransitionSignal)targetEx).getErrorCode(), ((InvalidStateTransitionSignal)targetEx).getMessage() );
		}
		else if ( targetEx instanceof CustomException )
		{
			errorCode = ((CustomException)targetEx).errorDef.getErrorCode();
			ErrorDef errorDef = ((CustomException)targetEx).errorDef;
			
			korErrorMsg = errorDef.getKor_errorMessage();
			engErrorMsg = errorDef.getEng_errorMessage();
			chaErrorMsg = errorDef.getCha_errorMessage();
//			locErrorMsg = errorDef.getLoc_errorMessage();			
		}
		else if ( targetEx instanceof IDMFrameErrorSignal )
		{
			errorCode = "IDMFrameErrorSignal";
			ErrorDef errorDef = GenericServiceProxy.getErrorDefMap().getErrorDef(errorCode);
			
			korErrorMsg =
				MessageFormat.format(errorDef.getKor_errorMessage(), ((IDMFrameErrorSignal)targetEx).getErrorCode(), ((IDMFrameErrorSignal)targetEx).getMessage() );
			engErrorMsg = 
				MessageFormat.format(errorDef.getEng_errorMessage(), ((IDMFrameErrorSignal)targetEx).getErrorCode(), ((IDMFrameErrorSignal)targetEx).getMessage() );
			chaErrorMsg = 
				MessageFormat.format(errorDef.getCha_errorMessage(), ((IDMFrameErrorSignal)targetEx).getErrorCode(), ((IDMFrameErrorSignal)targetEx).getMessage() );
//			locErrorMsg = 
//				MessageFormat.format(errorDef.getLoc_errorMessage(), ((IDMFrameErrorSignal)targetEx).getErrorCode(), ((nanoFrameErrorSignal)targetEx).getMessage() );
		}
		else 
		{
			errorCode = "UndefinedCode";
			ErrorDef errorDef = GenericServiceProxy.getErrorDefMap().getErrorDef("UndefinedCode");
			if(errorDef != null) {
				korErrorMsg =
					MessageFormat.format(errorDef.getKor_errorMessage(), targetEx.getMessage());
				engErrorMsg =
					MessageFormat.format(errorDef.getEng_errorMessage(), targetEx.getMessage());
				chaErrorMsg =
					MessageFormat.format(errorDef.getCha_errorMessage(), targetEx.getMessage());
//				locErrorMsg = 
//					MessageFormat.format(errorDef.getLoc_errorMessage(), targetEx.getMessage());
			}
			else {
				korErrorMsg = e.getMessage();
				engErrorMsg = e.getMessage();
				chaErrorMsg = e.getMessage();
//				locErrorMsg = e.getMessage();
			}
		}
		
		if ( lanuage.toUpperCase().startsWith("KOR")){
			return korErrorMsg;
		}
		else if ( lanuage.toUpperCase().startsWith("ENG") ){
			return engErrorMsg;
		}
		else if ( lanuage.toUpperCase().startsWith("CHA") )
			return chaErrorMsg;
		else
			return engErrorMsg;
	}
	
	/*
	* Name : sendErrorBySender
	* Desc : This function is sendErrorBySender Message
	* Author : ZXY IDM Team
	* Date : 2011.01.16
	*/
	private void sendErrorBySender( String replySubject, Document doc, String lanuage, Exception e, String senderName, String returnCode)
	{
		if(e instanceof BpelException){
			Exception nativeException = ((BpelException)e).getNativeException();
			if(nativeException != null) {
				e = nativeException;
			}
		}
	   String sReplyMsg = this.makeSendError(replySubject, doc, lanuage, e, returnCode);
	   
	   
	   if(StringUtils.isNotEmpty(replySubject) == true)
		   GenericServiceProxy.getGenericSender(senderName).reply(replySubject, sReplyMsg);

	   log.error(" SERP : Subject=" + replySubject);
	   loggingReply(sReplyMsg);
	}
	
	/*
	* Name : insertCNMMessageLog
	* Desc : This function is insertCNMMessageLog 
	* Author : ZXY IDM Team
	* Date : 2011.01.16
	*/
	public static void insertCNMMessageLog(Document doc)
	{
		try
		{
			Element headerElement = doc.getRootElement().getChild("Header");
			String serverName = "CNMsvr";
			String eventName = headerElement.getChildText("MESSAGENAME");
			String eventTimekey  = TimeStampUtil.getCurrentEventTimeKey();
			String eventUser = headerElement.getChildText("EVENTUSER");
			String messageLog = JdomUtils.toString(doc);
					
			String sql =  " INSERT INTO CT_MESSAGELOG(SERVERNAME, EVENTTIMEKEY, EVENTNAME, EVENTUSER,MESSAGELOG) "
					    + " VALUES(:serverName, :eventTimeKey, :eventName, :eventUser, :messageLog) ";
			
			Map<String, String> bindMap = new HashMap<String, String>();
			bindMap.put("serverName", serverName );
			bindMap.put("eventTimeKey", eventTimekey );
			bindMap.put("eventName", eventName );
			bindMap.put("eventUser", eventUser );
			bindMap.put("messageLog", messageLog );
			
			IDMFrameServiceProxy.getSqlTemplate().update(sql, bindMap);
			log.info("Insert message is success");
		}
		catch(Exception e)
		{
			log.info("Insert message is fail");
		}
		
	}
	
	/*
	* Name : makeErrorMessageLog
	* Desc : This function is makeErrorMessageLog
	* Author : ZXY IDM Team
	* Date : 2011.01.16
	*/
	public String makeErrorMessageLog( Document doc, Exception e, String language)
	{
		String errorCode = "";
		String korErrorMsg = "";
		String engErrorMsg = "";
		String chaErrorMsg = "";
//		String locErrorMsg = "";
		
		//Throwable orgEx = (BpelException)e;
		Throwable orgEx = e;
		
		Throwable targetEx;
		if ( orgEx.getCause() instanceof InvocationTargetException )
			targetEx = ((InvocationTargetException) orgEx.getCause()).getTargetException();
		else if ( orgEx.getCause() instanceof CustomException )
			targetEx = ((CustomException) orgEx.getCause());
		else if ( orgEx.getCause() instanceof NotFoundSignal )
			targetEx = ((NotFoundSignal) orgEx.getCause());
		else if ( orgEx.getCause() instanceof DuplicateNameSignal )
			targetEx = ((DuplicateNameSignal) orgEx.getCause());
		else if ( orgEx.getCause() instanceof FrameworkErrorSignal )
			targetEx = ((FrameworkErrorSignal) orgEx.getCause());
		else if ( orgEx.getCause() instanceof InvalidStateTransitionSignal )
			targetEx = ((InvalidStateTransitionSignal) orgEx.getCause());
		else if ( orgEx.getCause() instanceof IDMFrameErrorSignal )
			targetEx = ((IDMFrameErrorSignal) orgEx.getCause());
		else
			targetEx = orgEx;
		
		if ( targetEx instanceof NotFoundSignal )
		{
			errorCode = "NotFoundSignal";
			ErrorDef errorDef = GenericServiceProxy.getErrorDefMap().getErrorDef(errorCode);
		
			korErrorMsg = 
					MessageFormat.format(errorDef.getKor_errorMessage(), ((NotFoundSignal)targetEx).getErrorCode(), ((NotFoundSignal)targetEx).getMessage() );
			engErrorMsg = 
					MessageFormat.format(errorDef.getEng_errorMessage(), ((NotFoundSignal)targetEx).getErrorCode(), ((NotFoundSignal)targetEx).getMessage() );
			chaErrorMsg = 
					MessageFormat.format(errorDef.getCha_errorMessage(), ((NotFoundSignal)targetEx).getErrorCode(), ((NotFoundSignal)targetEx).getMessage() );
//			locErrorMsg = 
//					MessageFormat.format(errorDef.getLoc_errorMessage(), ((NotFoundSignal)targetEx).getErrorCode(), ((NotFoundSignal)targetEx).getMessage() );
			
		}
		else if ( targetEx instanceof DuplicateNameSignal )
		{
			errorCode = "DuplicateNameSignal";
			ErrorDef errorDef = GenericServiceProxy.getErrorDefMap().getErrorDef(errorCode);

			korErrorMsg =
					MessageFormat.format(errorDef.getKor_errorMessage(), ((DuplicateNameSignal)targetEx).getErrorCode(), ((DuplicateNameSignal)targetEx).getMessage() );
			engErrorMsg = 
					MessageFormat.format(errorDef.getEng_errorMessage(), ((DuplicateNameSignal)targetEx).getErrorCode(), ((DuplicateNameSignal)targetEx).getMessage() );
			chaErrorMsg = 
					MessageFormat.format(errorDef.getCha_errorMessage(), ((DuplicateNameSignal)targetEx).getErrorCode(), ((DuplicateNameSignal)targetEx).getMessage() );
//			locErrorMsg = 
//					MessageFormat.format(errorDef.getLoc_errorMessage(), ((DuplicateNameSignal)targetEx).getErrorCode(), ((DuplicateNameSignal)targetEx).getMessage() );
		}
		else if ( targetEx instanceof FrameworkErrorSignal)
		{
			errorCode = "FrameworkErrorSignal";
			ErrorDef errorDef = GenericServiceProxy.getErrorDefMap().getErrorDef(errorCode);
		
			korErrorMsg =
					MessageFormat.format(errorDef.getKor_errorMessage(), ((FrameworkErrorSignal)targetEx).getErrorCode(), ((FrameworkErrorSignal)targetEx).getMessage() );
			engErrorMsg = 
					MessageFormat.format(errorDef.getEng_errorMessage(), ((FrameworkErrorSignal)targetEx).getErrorCode(), ((FrameworkErrorSignal)targetEx).getMessage() );
			chaErrorMsg = 
					MessageFormat.format(errorDef.getCha_errorMessage(), ((FrameworkErrorSignal)targetEx).getErrorCode(), ((FrameworkErrorSignal)targetEx).getMessage() );
//			locErrorMsg = 
//					MessageFormat.format(errorDef.getLoc_errorMessage(), ((FrameworkErrorSignal)targetEx).getErrorCode(), ((FrameworkErrorSignal)targetEx).getMessage() );
		}
		else if ( targetEx instanceof InvalidStateTransitionSignal)
		{
			errorCode = "InvalidStateTransitionSignal";
			ErrorDef errorDef = GenericServiceProxy.getErrorDefMap().getErrorDef(errorCode);
			
			korErrorMsg =
					MessageFormat.format(errorDef.getKor_errorMessage(), ((InvalidStateTransitionSignal)targetEx).getErrorCode(), ((InvalidStateTransitionSignal)targetEx).getMessage() );
			engErrorMsg = 
					MessageFormat.format(errorDef.getEng_errorMessage(), ((InvalidStateTransitionSignal)targetEx).getErrorCode(), ((InvalidStateTransitionSignal)targetEx).getMessage() );
			chaErrorMsg = 
					MessageFormat.format(errorDef.getCha_errorMessage(), ((InvalidStateTransitionSignal)targetEx).getErrorCode(), ((InvalidStateTransitionSignal)targetEx).getMessage() );
//			locErrorMsg = 
//					MessageFormat.format(errorDef.getLoc_errorMessage(), ((InvalidStateTransitionSignal)targetEx).getErrorCode(), ((InvalidStateTransitionSignal)targetEx).getMessage() );
		}
		else if ( targetEx instanceof CustomException )
		{
			errorCode = ((CustomException)targetEx).errorDef.getErrorCode();
			ErrorDef errorDef = ((CustomException)targetEx).errorDef;
			
			korErrorMsg = errorDef.getKor_errorMessage();
			engErrorMsg = errorDef.getEng_errorMessage();
			chaErrorMsg = errorDef.getCha_errorMessage();
//			locErrorMsg = errorDef.getLoc_errorMessage();			
		}
		else if ( targetEx instanceof IDMFrameErrorSignal )
		{
			errorCode = "IDMFrameErrorSignal";
			ErrorDef errorDef = GenericServiceProxy.getErrorDefMap().getErrorDef(errorCode);
			
			korErrorMsg =
				MessageFormat.format(errorDef.getKor_errorMessage(), ((IDMFrameErrorSignal)targetEx).getErrorCode(), ((IDMFrameErrorSignal)targetEx).getMessage() );
			engErrorMsg = 
				MessageFormat.format(errorDef.getEng_errorMessage(), ((IDMFrameErrorSignal)targetEx).getErrorCode(), ((IDMFrameErrorSignal)targetEx).getMessage() );
			chaErrorMsg = 
				MessageFormat.format(errorDef.getCha_errorMessage(), ((IDMFrameErrorSignal)targetEx).getErrorCode(), ((IDMFrameErrorSignal)targetEx).getMessage() );
//			locErrorMsg = 
//				MessageFormat.format(errorDef.getLoc_errorMessage(), ((IDMFrameErrorSignal)targetEx).getErrorCode(), ((IDMFrameErrorSignal)targetEx).getMessage() );
			
		}
		else 
		{
			errorCode = "UndefinedCode";
			ErrorDef errorDef = GenericServiceProxy.getErrorDefMap().getErrorDef("UndefinedCode");
			
			korErrorMsg =
					MessageFormat.format(errorDef.getKor_errorMessage(), targetEx.getMessage());
			engErrorMsg =
					MessageFormat.format(errorDef.getEng_errorMessage(), targetEx.getMessage());
			chaErrorMsg =
					MessageFormat.format(errorDef.getCha_errorMessage(), targetEx.getMessage());
//			locErrorMsg = 
//					MessageFormat.format(errorDef.getLoc_errorMessage(), targetEx.getMessage());
		}
		
	   String sReplyMsg = null;
	   SMessageUtil.setResultItemValue(doc, SMessageUtil.Result_ReturnCode, errorCode);
	   
	   if ( language.equals("KOR") == true )
		   SMessageUtil.setResultItemValue(doc, SMessageUtil.Result_ErrorMessage, korErrorMsg);
	   else if ( language.equals("ENG") == true )
		   SMessageUtil.setResultItemValue(doc, SMessageUtil.Result_ErrorMessage, engErrorMsg);
	   else if ( language.equals("CHA") == true )
		   SMessageUtil.setResultItemValue(doc, SMessageUtil.Result_ErrorMessage, chaErrorMsg);
	   else
		   SMessageUtil.setResultItemValue(doc, SMessageUtil.Result_ErrorMessage, engErrorMsg);
	  
	  
	   sReplyMsg = JdomUtils.toString(doc);
	   
	   CommonUtil.insertErrorMessageLog(doc);
	   
	   return sReplyMsg;
	}	
	
	public void makeWMSErrorMessageLog(Document doc){
		CommonUtil.insertWMSErrorMessageLog(doc);
	}
	
	/*
	* Name : getShipToMDLMaterialReportReply
	* Desc : This function is get ShipToMDLMaterialReportReply Message
	* Author : ZXY IDM Team
	* Date : 2017.08.22
	*/
	public void getShipToMDLMaterialReportReply(String replySubject, Document doc) throws Exception
	{
		String sReceiveMsg = null;
		
		Element message = new Element( SMessageUtil.Message_Tag );  
		
		Element oriheader = doc.getDocument().getRootElement().getChild("Header");
		Element oriBody = doc.getDocument().getRootElement().getChild("Body");
		Element oriReturn = doc.getDocument().getRootElement().getChild("Return");
		
		String shipRequestName = oriBody.getChildText("INVOICENO");
		Map<String, String> bindMap = new HashMap<String, String>();			
		bindMap.put("shipRequestName", shipRequestName);
		if(oriReturn.getChildText("RETURNCODE").equals("0")){
			String sql1 = "update MATERIALSHIPREQUEST set mesReceiveFlag = 'Y' "
					   + " WHERE shipRequestName = :shipRequestName";
			IDMFrameServiceProxy.getSqlTemplate().update(sql1, bindMap);
		}else{
			String sql2 = "update MATERIALSHIPREQUEST set mesReceiveFlag = 'N' "
					   + " WHERE shipRequestName = :shipRequestName";
			IDMFrameServiceProxy.getSqlTemplate().update(sql2, bindMap);
		}
		
		Element header = new Element("Header");
		
		Element messagename = new Element("MESSAGENAME");
		messagename.setText(oriheader.getChildText("MESSAGENAME"));
		header.addContent(messagename);
		
		Element transactionid = new Element("TRANSACTIONID");
		transactionid.setText(oriheader.getChildText("TRANSACTIONID"));
		header.addContent(transactionid);
		
		Element originalSourceSubjectName = new Element("ORIGINALSOURCESUBJECTNAME");
		originalSourceSubjectName.setText(oriheader.getChildText("ORIGINALSOURCESUBJECTNAME"));
		header.addContent(originalSourceSubjectName);
		
		Element sourceSubjectName = new Element("SOURCESUBJECTNAME");
		sourceSubjectName.setText(oriheader.getChildText("SOURCESUBJECTNAME"));
		header.addContent(sourceSubjectName);
		
		Element targetSubjectName = new Element("TARGETSUBJECTNAME");
		targetSubjectName.setText(oriheader.getChildText("TARGETSUBJECTNAME"));
		header.addContent(targetSubjectName);
		
		Element eventUser = new Element("EVENTUSER");
		eventUser.setText(oriheader.getChildText("EVENTUSER"));
		header.addContent(eventUser);
		
		Element eventComment = new Element("EVENTCOMMENT");
		eventComment.setText(oriheader.getChildText("EVENTCOMMENT"));
		header.addContent(eventComment);
				
		message.addContent(header);
		
		Element returnElement = new Element("Return");
		
		Element returnCode = new Element("RETURNCODE");
		returnCode.setText(oriBody.getChildText("RETURNCODE"));
		returnElement.addContent(returnCode);
		
		Element returnMessage = new Element("RETURNMESSAGE");
		returnMessage.setText("");
		returnElement.addContent(returnMessage);
		
		message.addContent(returnElement);
		
		Document receiveDoc = new Document( message ); 
		   
		sReceiveMsg = JdomUtils.toString(receiveDoc);

		log.info(" SEND : Subject=" + replySubject);
		loggingSend(sReceiveMsg);
	}
	public static void sendByFGMSSender( Document doc, String senderName )
	{
		if ( log.isInfoEnabled() )
		{
			log.debug("senderName = " + senderName);
		}

		// add return
		SMessageUtil.setResultItemValue(doc, SMessageUtil.Result_ReturnCode, SUCCESS);
		SMessageUtil.setResultItemValue(doc, SMessageUtil.Result_ErrorMessage, "");

		String sendMsg = JdomUtils.toString(doc);

		log.debug(" Send Message =  " + sendMsg);

		GenericServiceProxy.getGenericSender(senderName).setDataField("xmlData");
		//GenericServiceProxy.getGenericSender(senderName).setDataField(SMessageUtil.DataFieldNameXmlData);
		GenericServiceProxy.getGenericSender(senderName).send(sendMsg);

		log.debug(" SEND : Message=" + sendMsg);
	}
	public static void sendByMesSender( Document doc, String senderName )
	{
		if ( log.isInfoEnabled() )
		{
			log.debug("senderName = " + senderName);
		}

		// add return
		SMessageUtil.setResultItemValue(doc, SMessageUtil.Result_ReturnCode, SUCCESS);
		SMessageUtil.setResultItemValue(doc, SMessageUtil.Result_ErrorMessage, "");

		String sendMsg = JdomUtils.toString(doc);

		log.debug(" Send Message =  " + sendMsg);
		if(senderName.equals("PMSSender"))
		{
			GenericServiceProxy.getGenericSender(senderName).setDataField("Message");//部分现地消息为xmldata，需修改
		}
		else
		{
			//SMessageUtil.dataField_TAG
		}
		////SMessageUtil.dataField_TAG
		GenericServiceProxy.getGenericSender(senderName).send(sendMsg);

		log.debug(" SEND : Message=" + sendMsg);
	}
	//FOR GERP
	 public String sendRequestBySenderWithSReply(Document doc, String senderName,String subjectName) {
		    Document msg = new Document();
		    String mm="";
		    String sendMsg = JdomUtils.toString(doc);
		    log.info("send started!");  
		    try {
		    mm=GenericServiceProxy.getGenericSender(senderName).sendRequest(sendMsg,2000).toString();
//		    msg = (Document)GenericServiceProxy.getGenericSender(senderName).sendRequest(sendMsg,2000);
//		    		.sendRequest(sendMsg);
//		    log.info(" SEND : Sender=" + senderName);
//		    loggingSend(sendMsg);
//		    log.info("receive:---------"+msg+"--------");
//		    log.info(" Receive : Reply=" + msg.toString());
//		    loggingSend(msg.toString());
	        int start = mm.indexOf("<Body>") + "<Body>".length();
	        int end = mm.indexOf("</Body>");
	        String bodyContent = mm.substring(start, end).trim();
	        String[] items = bodyContent.split("<\\/ITEM>\\s*<ITEM>");
          log.info("Found " + items + " ITEM elements.");
         if(null != items && items.length > 0) {
         	// 遍历每个 <ITEM> 元素
             for (String item : items) {
            	 if (!item.trim().isEmpty()) {
            		// 在这里处理每个 ITEM 元素的内容
                     String siteName = extractElementValue(item, "SITENAME");
                     String partsPackingName = extractElementValue(item, "PARTSPACKINGNAME");
                     String partsSpecName = extractElementValue(item, "PARTSSPECNAME");
                     String quantity = extractElementValue(item, "QUANTITY");
                     String stockState = extractElementValue(item, "STOCKSTATE");
                     String shipRequestName = "";
                     String packingType = extractElementValue(item, "PACKINGTYPE");
                     String avaliableState = extractElementValue(item, "AVAILABLESTATE");
                     String serialNumber = extractElementValue(item, "SERIALNUMBER");
                     String repairState = extractElementValue(item, "REPAIRSTATE");
                     String useCount = extractElementValue(item, "USECOUNT");
                     String locationName = extractElementValue(item, "LOCATIONNAME");
                     String unit = extractElementValue(item, "UNIT");
                     String returnType = extractElementValue(item, "RETURNTYPE");
                     String charge = extractElementValue(item, "CHARGE");
                     String judgeFlag = extractElementValue(item, "JUDGEFLAG");
                     String ponumber = extractElementValue(item, "PONUMBER");
                     String costCenter = extractElementValue(item, "COSTCENTER");
                     String erpFactory = extractElementValue(item, "ERPFACTORY");
                     String shipRequestDetailName = "";
                     String repairFlag = extractElementValue(item, "REPAIRFLAG");
                     //不同的业务，发送报文的字段不同
                     if(extractElementValue(item, "STOCKOUTSHIPREQUESTNAME").isEmpty()) {
                     	shipRequestName = extractElementValue(item, "SHIPREQUESTNAME");
                     	shipRequestDetailName = extractElementValue(item, "SHIPREQUESTDETAILNAME");
                     }else {
                     	shipRequestName = extractElementValue(item, "STOCKOUTSHIPREQUESTNAME");
                         shipRequestDetailName = extractElementValue(item, "STOCKOUTSHIPREQUESTDETAILNAME");
                     }
                     String queryPMSPreMaterialPackingByKeySql = "SELECT SITENAME,PARTSPACKINGNAME,PARTSSPECNAME,STOCKSTATE,AVALIABLESTATE,SHIPREQUESTNAME,SHIPREQUESTDETAILNAME,SERIALNUMBER,REPAIRSTATE,USECOUNT,LOCATION,QUANTITY,UNIT,RETURNCODE,CHARGE,BWTAR,RECEIVEREQUESTNAME,RECEIVEFLAG FROM PMSPREMATERIALPACKING " + "WHERE SITENAME = :siteName and PARTSPACKINGNAME = :partsPackingName and SHIPREQUESTNAME = :shipRequestName and SHIPREQUESTDETAILNAME = :shipRequestDetailName ";
             		Map<String, String> queryPMSPreMaterialPackingByKeyMap = new HashMap<String, String>();
             		queryPMSPreMaterialPackingByKeyMap.put("siteName", siteName);
             		queryPMSPreMaterialPackingByKeyMap.put("partsPackingName", partsPackingName);
             		queryPMSPreMaterialPackingByKeyMap.put("shipRequestName", shipRequestName);
             		queryPMSPreMaterialPackingByKeyMap.put("shipRequestDetailName", shipRequestDetailName);
             		String insetPMSPreMaterialPackingSql = 
             				" insert into PMSPREMATERIALPACKING (SITENAME,PARTSPACKINGNAME,PARTSSPECNAME,STOCKSTATE,AVALIABLESTATE,SHIPREQUESTNAME,SHIPREQUESTDETAILNAME,SERIALNUMBER,REPAIRSTATE,USECOUNT,LOCATION,QUANTITY,UNIT,RETURNCODE,CHARGE,BWTAR,RECEIVEREQUESTNAME,REPAIRFLAG) "
             				+ " VALUES (:SITENAME,:PARTSPACKINGNAME,:PARTSSPECNAME,:STOCKSTATE,:AVALIABLESTATE,:SHIPREQUESTNAME,:SHIPREQUESTDETAILNAME,:SERIALNUMBER,:REPAIRSTATE,:USECOUNT,:LOCATION,:QUANTITY,:UNIT,:RETURNCODE,:CHARGE,:BWTAR,:RECEIVEREQUESTNAME,:REPAIRFLAG) ";
             		//Float zfMengSumFloat =  Float.parseFloat(zfMengSum);
             		Map<String, Object> insetPMSPreMaterialPackingMap = new HashMap<String, Object>();
             		List<Map<String, Object>> queryResult = IDMFrameServiceProxy.getSqlTemplate().queryForList(queryPMSPreMaterialPackingByKeySql, queryPMSPreMaterialPackingByKeyMap);
             		if (queryResult == null || queryResult.isEmpty()) {
             			// Create
             			insetPMSPreMaterialPackingMap.put("SITENAME", siteName);
             			insetPMSPreMaterialPackingMap.put("PARTSPACKINGNAME", partsPackingName);
             			insetPMSPreMaterialPackingMap.put("PARTSSPECNAME", partsSpecName);
             			insetPMSPreMaterialPackingMap.put("STOCKSTATE", stockState);
             			insetPMSPreMaterialPackingMap.put("AVALIABLESTATE", avaliableState);
             			insetPMSPreMaterialPackingMap.put("SHIPREQUESTNAME", shipRequestName);
             			insetPMSPreMaterialPackingMap.put("SHIPREQUESTDETAILNAME", shipRequestDetailName);
             			insetPMSPreMaterialPackingMap.put("SERIALNUMBER", serialNumber);
             			insetPMSPreMaterialPackingMap.put("REPAIRSTATE", repairState);
             			insetPMSPreMaterialPackingMap.put("USECOUNT", useCount);
             			insetPMSPreMaterialPackingMap.put("LOCATION", locationName);
             			insetPMSPreMaterialPackingMap.put("QUANTITY", quantity);
             			insetPMSPreMaterialPackingMap.put("UNIT", unit);
             			insetPMSPreMaterialPackingMap.put("RETURNCODE", returnType);
             			insetPMSPreMaterialPackingMap.put("CHARGE", charge);
             			insetPMSPreMaterialPackingMap.put("BWTAR", judgeFlag);
             			insetPMSPreMaterialPackingMap.put("RECEIVEREQUESTNAME", ponumber);                			                			
             			//insetPMSPreMaterialPackingMap.put("RECEIVEFLAG", "");
             			insetPMSPreMaterialPackingMap.put("REPAIRFLAG", repairFlag);
             			IDMFrameServiceProxy.getSqlTemplate().update(insetPMSPreMaterialPackingSql, insetPMSPreMaterialPackingMap);
             		}
            	 }
             }
         } 
		    } catch (Exception e) {
	            log.error("Failed to parse XML response.", e);
	        } 
		    log.info("receive:---------"+mm+"--------");
		    return mm;
//		    return msg;
	}
		/**
		 * 解析XML字符串获取子元素
		 * @param item XML的item元素
		 * @param elementName 所获取item中的子元素标签
		 * @return 返回
		 */
		private String extractElementValue(String item, String elementName) {
	        String pattern = "<" + elementName + ">[^<]*</" + elementName + ">";
	        Pattern r = Pattern.compile(pattern);
	        Matcher m = r.matcher(item);
	        if (m.find()) {
	            String match = m.group();
	            int start = match.indexOf('>') + 1;
	            int end = match.lastIndexOf('<');
	            return match.substring(start, end).trim();
	        }
	        return "";
	    }
		
		public static void sendByMesSenderPMS( Document doc, String senderName ,List<MaterialStockInClass> msilist)
		{
			if ( log.isInfoEnabled() )
			{
				log.debug("senderName = " + senderName);
			}
			String mm="";
			
			// add return
			SMessageUtil.setResultItemValue(doc, SMessageUtil.Result_ReturnCode, SUCCESS);
			SMessageUtil.setResultItemValue(doc, SMessageUtil.Result_ErrorMessage, "");

			String sendMsg = JdomUtils.toString(doc);

			log.debug(" Send Message =  " + sendMsg);
			if(senderName.equals("PMSSender"))
			{
				GenericServiceProxy.getGenericSender(senderName).setDataField("Message");//部分现地消息为xmldata，需修改
			}
			else
			{
				GenericServiceProxy.getGenericSender(senderName).setDataField(SMessageUtil.dataField_TAG);
				
			}
			GenericServiceProxy.getGenericSender(senderName).send(sendMsg);
//			Map<String, Object> updatePMSPREMATERIALPACKINGMap = new HashMap<String, Object>();
//			String updatePMSPREMATERIALPACKINGSql = " UPDATE PMSPREMATERIALPACKING SET SENDCODE = :SENDCODE, RETURNMESSAGE = :RETURNMESSAGE WHERE SITENAME = :SITENAME AND PARTSPACKINGNAME = :PARTSPACKINGNAME ";
//			  mm=GenericServiceProxy.getGenericSender(senderName).sendRequest(sendMsg,2000).toString();
//			  int startCode = mm.indexOf("<RETURNCODE>") + "<RETURNCODE>".length();
//		      int endCode = mm.indexOf("</RETURNCODE>");
//		      String code = "N";
//		      if (startCode != -1 && endCode != -1) {
//		    	 code = mm.substring(startCode, endCode).trim();
//		      }
//		      int startMsg = mm.indexOf("<RETURNMESSAGE>") + "<RETURNMESSAGE>".length();
//		      int endMsg = mm.indexOf("</RETURNMESSAGE>");
//		      String msg = "发送失败！";
//		      if (startCode != -1 && endCode != -1) {
//		    	  msg = mm.substring(startMsg, endMsg).trim();
//			  }
//		
//		      for (int i = 0; i < msilist.size(); i++) {
//					MaterialStockInClass msa = msilist.get(i);
//					List<MaterialPacking> materialpackinglist = new MaterialPackingStockInUtil().GetBackStockInList(msa);
//					if (null != materialpackinglist && materialpackinglist.size() > 0) {
//						for (MaterialPacking mp : materialpackinglist) {
//							String PARTSPACKINGNAME = mp.getKey().getMaterialPackingName();
//							 updatePMSPREMATERIALPACKINGMap.put("SITENAME", mp.getKey().getSiteName());
//					    	  updatePMSPREMATERIALPACKINGMap.put("PARTSPACKINGNAME", PARTSPACKINGNAME);
//					    	  updatePMSPREMATERIALPACKINGMap.put("SENDCODE", code);
//					    	  updatePMSPREMATERIALPACKINGMap.put("RETURNMESSAGE", msg);
//							IDMFrameServiceProxy.getSqlTemplate().update(updatePMSPREMATERIALPACKINGSql, updatePMSPREMATERIALPACKINGMap);
//						}
//					}
//				}
			log.debug(" SEND : Message=" + sendMsg);
		}
		
		public static void sendByMesSenderPMSCompleteShipRequest( Document doc, String senderName ,String receiveActno,String siteName)
		{
			if ( log.isInfoEnabled() )
			{
				log.debug("senderName = " + senderName);
			}
			String mm="";
			
			// add return
			SMessageUtil.setResultItemValue(doc, SMessageUtil.Result_ReturnCode, SUCCESS);
			SMessageUtil.setResultItemValue(doc, SMessageUtil.Result_ErrorMessage, "");

			String sendMsg = JdomUtils.toString(doc);

			log.debug(" Send Message =  " + sendMsg);
			if(senderName.equals("PMSSender"))
			{
				GenericServiceProxy.getGenericSender(senderName).setDataField("Message");//部分现地消息为xmldata，需修改
			}
			else
			{
				//SMessageUtil.dataField_TAG
			}
			////SMessageUtil.dataField_TAG
			//GenericServiceProxy.getGenericSender(senderName).send(sendMsg);
			Map<String, Object> updatePMSMATERIALINTERFACEMap = new HashMap<String, Object>();
			String updatePMSMATERIALINTERFACESql = " UPDATE PMSMATERIALINTERFACE SET RETURNCODE = :RETURNCODE, RETURNMESSAGE = :RETURNMESSAGE WHERE SITENAME = :SITENAME AND RECEIVEACTNO = :RECEIVEACTNO ";
			  mm=GenericServiceProxy.getGenericSender(senderName).sendRequest(sendMsg,2000).toString();
			  int startCode = mm.indexOf("<RETURNCODE>") + "<RETURNCODE>".length();
		      int endCode = mm.indexOf("</RETURNCODE>");
		      String code = "N";
		      if (startCode != -1 && endCode != -1) {
		    	 code = mm.substring(startCode, endCode).trim();
		      }
		      int startMsg = mm.indexOf("<RETURNMESSAGE>") + "<RETURNMESSAGE>".length();
		      int endMsg = mm.indexOf("</RETURNMESSAGE>");
		      String msg = "发送失败！";
		      if (startCode != -1 && endCode != -1) {
		    	  msg = mm.substring(startMsg, endMsg).trim();
			  }
		      updatePMSMATERIALINTERFACEMap.put("SITENAME", siteName);
		      updatePMSMATERIALINTERFACEMap.put("RECEIVEACTNO", receiveActno);
		      updatePMSMATERIALINTERFACEMap.put("RETURNCODE", code);
		      updatePMSMATERIALINTERFACEMap.put("RETURNMESSAGE", msg);
			IDMFrameServiceProxy.getSqlTemplate().update(updatePMSMATERIALINTERFACESql, updatePMSMATERIALINTERFACEMap);
			log.debug(" SEND : Message=" + sendMsg);
		}
}