/**
 * <!-- 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.ui.mapping.hl7.V2MessageIndex;

import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;


/**
 * Description of class definition
 *
 * @author   OWNER: wangeug  $Date: Jan 22, 2009
 * @author   LAST UPDATE: $Author: wangeug 
 * @version  REVISION: $Revision: 1.1 $
 * @date 	 DATE: $Date: 2009-02-24 16:00:40 $
 * @since caAdapter v4.2
 */

public class V2MessageSchemaIndexLoader {
	private static V2MessageIndex v2Index;
	
	public  static V2MessageIndex loadMessageInfos() throws Exception {
		if (v2Index!=null)
			return v2Index;
		String zipFilePath="lib/HL7v2xsd.zip";
		System.out.println("MIFIndexParser.loadMIFInfosZip()..zipFilePath"+zipFilePath);
		ZipFile zip = new ZipFile(zipFilePath);
		Enumeration entryEnum=zip.entries();

		v2Index = new V2MessageIndex();
		while (entryEnum.hasMoreElements())
		{
			ZipEntry zipEntry=(ZipEntry)entryEnum.nextElement();			
			String fileName=zipEntry.getName();
 			v2Index.addMessageType(fileName);
		}
		return v2Index;
	}
	public static void saveV2MessageIndexObject() throws Exception {
		OutputStream os = new FileOutputStream("v2MessageIndex.obj");
		ObjectOutputStream oos = new ObjectOutputStream(os); 
		V2MessageIndex v2Index= loadMessageInfos() ;
		oos.writeObject(v2Index);
		oos.close();
		os.close();
	}
	
	public static V2MessageIndex loadV2MessageIndexObject() {
		try {
			System.out
					.println("V2MessageSchemaIndexLoader.loadV2MessageIndexObject()...:v2MessageIndex.obj");
			
			InputStream is =V2MessageSchemaIndexLoader.class.getClassLoader().getResource("v2MessageIndex.obj").openStream();
//				ClassLoader.getSystemClassLoader().getResource("v2MessageIndex.obj").openStream();
//			.getSystemResourceAsStream("/v2MessageIndex.obj");
			ObjectInputStream ois = new ObjectInputStream(is);
			V2MessageIndex v2Index= (V2MessageIndex)ois.readObject();
			ois.close();
			is.close();
			return v2Index;
		}catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static void main(String[] args)
	{
		try {
			V2MessageSchemaIndexLoader.saveV2MessageIndexObject();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}


/**
* HISTORY: $Log: not supported by cvs2svn $
* HISTORY: Revision 1.1  2009/01/23 18:22:00  wangeug
* HISTORY: Load V2 meta with version number and message schema name; do not use the absolute path of schema file
* HISTORY:
**/
move from other package