package org.openhealthtools.mdht.uml.cda.core.internal.generate;

import java.io.PrintWriter;
import java.util.Map;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EPackage.Registry;
import org.eclipse.emf.ecore.EcorePackage;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.emf.mapping.ecore2xml.Ecore2XMLPackage;
import org.eclipse.uml2.uml.Class;
import org.eclipse.uml2.uml.Package;
import org.eclipse.uml2.uml.UMLPackage;
import org.eclipse.uml2.uml.UMLPlugin;
import org.eclipse.uml2.uml.resource.UMLResource;
import org.openhealthtools.mdht.uml.cda.core.profile.CDAPackage;
import org.openhealthtools.mdht.uml.cda.core.util.InstanceGenerator;
import org.openhealthtools.mdht.uml.cda.util.CDAUtil;
import org.openhealthtools.mdht.uml.common.util.UMLUtil;

public class Generate {

	public static void main(String[] args) {

		
		if (args.length != 4) 
		{
			System.out.println("Error running XML Generate, Invalid arguments");			
			return ;
		}

		
		System.out.println("XML Sample Snippet generated for "+ args[1]);
		System.out.println();
		System.out.println();
		System.out.println();
		
		ResourceSet resourceSet = new ResourceSetImpl();

		// Initialize registry
		Registry packageRegistry = resourceSet.getPackageRegistry();
		packageRegistry.put(EcorePackage.eNS_URI, EcorePackage.eINSTANCE);
		packageRegistry.put(Ecore2XMLPackage.eNS_URI, Ecore2XMLPackage.eINSTANCE);
		packageRegistry.put(UMLPackage.eNS_URI, UMLPackage.eINSTANCE);

		// CDA Profile to registry
		packageRegistry.put(CDAPackage.eNS_URI, CDAPackage.eINSTANCE);
		
		String umlPluginLocation = args[2]; 
		String cdaResourcePluginLocation = args[3]; 		
		String cdaProfileLocation = org.openhealthtools.mdht.uml.cda.resources.util.CDAResource.CDA_PROFILE_URI+"#_cxOJEIEVEd6H8o-hO3-B4Q";
		
		String umlModelLocation=args[0];
		String umlClassName = args[1];
	
		// Initialize pathmaps
		resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put(UMLResource.FILE_EXTENSION, UMLResource.Factory.INSTANCE);
		Map<URI, URI> uriMap = resourceSet.getURIConverter().getURIMap();
		
		// Create and Add UML PathMaps
		URI uri = URI.createURI(umlPluginLocation);
		uriMap.put(URI.createURI(UMLResource.LIBRARIES_PATHMAP), uri.appendSegment("libraries").appendSegment(""));
		uriMap.put(URI.createURI(UMLResource.METAMODELS_PATHMAP), uri.appendSegment("metamodels").appendSegment(""));
		uriMap.put(URI.createURI(UMLResource.PROFILES_PATHMAP), uri.appendSegment("profiles").appendSegment(""));

		// Create and Add CDA Profile PathMaps
		URI cdaresourceURI = URI.createURI(cdaResourcePluginLocation);
		URI cdaPathmap = URI.createURI(org.openhealthtools.mdht.uml.cda.resources.util.CDAResource.PROFILES_PATHMAP);		
		uriMap.put(cdaPathmap, cdaresourceURI.appendSegment("profiles").appendSegment(""));
		UMLPlugin.getEPackageNsURIToProfileLocationMap().put(org.openhealthtools.mdht.uml.cda.core.profile.CDAPackage.eNS_URI,URI.createURI(cdaProfileLocation));
		

		// Load the CDA Packages
		CDAUtil.loadPackages();
		
		// Open the model
		URI modelFile = URI.createFileURI(umlModelLocation);

		Resource umlResource = resourceSet.getResource(modelFile, true);

		Package umlPackage = (Package) EcoreUtil.getObjectByType(umlResource.getContents(), UMLPackage.eINSTANCE.getPackage());

		if (umlPackage != null) {
			
			Class generationTarget = UMLUtil.getClassByName(umlPackage, umlClassName);

			if (generationTarget != null )
			{

				InstanceGenerator generator = new InstanceGenerator();

	

				EObject instance = generator.createInstance(generationTarget, 5);
				
				PrintWriter writer = new PrintWriter(System.out);
	
				
				generator.save(instance, writer);
				
		
				
				writer.close();
				
			}

		}
	
	}

}

@@ -108,16 +108,50 @@
 til.Map;
+%0Aimport java.util.StringTokenizer;
 %0A%0Aimport
@@ -1091,18 +1091,16 @@
 rgs) %7B%0A%0A
-%09%09
 %0A%09%09if (a
@@ -1213,19 +1213,16 @@
  ;%0A%09%09%7D%0A%0A
-%09%09%0A
 %09%09System
@@ -3527,16 +3527,23 @@
 age());%0A
+%09%09%0A%09%0A%09%09
 %0A%09%09if (u
@@ -3563,16 +3563,55 @@
 null) %7B%0A
+%09%0A%09%09%09EcoreUtil.resolveAll(umlPackage);%0A
 %09%09%09%0A%09%09%09C
@@ -3781,16 +3781,20 @@
 nerator(
+true
 );%0A%0A%09%0A%0A%09

update output