/**
 * <!-- LICENSE_TEXT_START -->
The contents of this file are subject to the caAdapter Software License (the "License"). You may obtain a copy of the License at the following location: 
[caAdapter Home Directory]\docs\caAdapter_license.txt, or at:
http://ncicb.nci.nih.gov/infrastructure/cacore_overview/caadapter/indexContent/docs/caAdapter_License
 * <!-- LICENSE_TEXT_END -->
 */
package gov.nih.nci.caadapter.hl7.v2meta;

import gov.nih.nci.caadapter.common.util.FileUtil;

import java.net.URL;
import java.util.Hashtable;

import javax.xml.namespace.QName;

import com.sun.encoder.Encoder;
import com.sun.encoder.EncoderConfigurationException;
import com.sun.encoder.EncoderFactory;
import com.sun.encoder.EncoderType;
import com.sun.encoder.MetaRef;

/**
 * Description of class definition
 *
 * @author   OWNER: wangeug  $Date: Oct 23, 2008
 * @author   LAST UPDATE: $Author: wangeug 
 * @version  REVISION: $Revision: 1.2 $
 * @date 	 DATE: $Date: 2009-02-25 15:58:03 $
 * @since caAdapter v4.2
 */

public class V2MessageEncoderFactory {
	
	private static Hashtable<String, Encoder> encoderHash=new Hashtable<String, Encoder>();
	
	public static Encoder getV2MessageEncoder(String v2Version, String msgXsd)
	{
		String encoderKey=v2Version+"_"+msgXsd;
		Encoder rtnEncoder=encoderHash.get(encoderKey);
		if (rtnEncoder==null)
		{
			//first time loading this kind of v2 message
			System.out.println("V2MessageEncoderFactory.getV3MessageEncoder()..intializer v2 message encoder:"+v2Version+"/"+msgXsd);
    		long csvbegintime = System.currentTimeMillis();
			//Get the encoder factory instance
			EncoderFactory factory;
			try {
				factory = EncoderFactory.newInstance();
				//Get the encoder type instance using an encoding style
				EncoderType type = factory.makeType("hl7encoder-1.0");
				//Specify a top element
				String tolName=msgXsd.substring(0, msgXsd.indexOf(".xsd"));
				QName topElem = new QName("urn:hl7-org:v2xml", tolName);
				String v2SchemaHome="hl7v2xsd";
				String v2XsdFile=v2SchemaHome+"/"+v2Version+"/"+msgXsd;
				//Construct the metadata instance
				URL metaURL=null;
				metaURL=FileUtil.retrieveResourceURL(v2XsdFile);
				MetaRef meta = factory.makeMeta(metaURL,topElem);
//				MetaRef meta = factory.makeMeta(ClassLoader.getSystemResource(v2XsdFile),topElem);			
				System.out.println("V2MessageEncoderFactory.getV3MessageEncoder()...load V2Meta:"+(System.currentTimeMillis()-csvbegintime));
								
				//Create the encoder instance, HL7Encoder
				rtnEncoder = factory.newEncoder(type, meta);
				System.out.println("V2MessageEncoderFactory.getV3MessageEncoder()...initialize encoder:"+(System.currentTimeMillis()-csvbegintime));
				encoderHash.put(encoderKey, rtnEncoder);
			} catch (EncoderConfigurationException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} 
		}
		
		return rtnEncoder;
	}

}


/**
* HISTORY: $Log: not supported by cvs2svn $
* HISTORY: Revision 1.1  2008/10/24 19:37:15  wangeug
* HISTORY: transfer a v2 message into v3 message using SUN v2 schema
* HISTORY:
**/
@@ -924,17 +924,17 @@
 sion: 1.
-2
+3
  $%0A * @d
@@ -962,21 +962,21 @@
 09-0
-2-25 15:58:03
+4-17 20:07:39
  $%0A 
@@ -1426,33 +1426,33 @@
 oderFactory.getV
-3
+2
 MessageEncoder()
@@ -2157,24 +2157,124 @@
 v2XsdFile);%0A
+%09%09%09%09System.out%0A%09%09%09%09%09%09.println(%22V2MessageEncoderFactory.getV2MessageEncoder()...metatURL:%22+metaURL);%0A
 %09%09%09%09MetaRef 
@@ -2451,33 +2451,33 @@
 oderFactory.getV
-3
+2
 MessageEncoder()
@@ -2493,16 +2493,31 @@
 2Meta:%22+
+meta+%22..time:%22+
 (System.
@@ -2708,17 +2708,17 @@
 ory.getV
-3
+2
 MessageE
@@ -2958,18 +2958,16 @@
 %09%09%7D %0A%09%09%7D
-%0A%09
 %09%0A%09%09retu
@@ -3037,16 +3037,108 @@
 s2svn $%0A
+* HISTORY: Revision 1.2  2009/02/25 15:58:03  wangeug%0A* HISTORY: enable webstart%0A* HISTORY:%0A
 * HISTOR

clean code