package bpeladdws.popup.actions;


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;

import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;
import java.io.File;


public class NewAction implements IObjectActionDelegate {

	private IWorkbenchPart part;
	private ISelection gselection;
	
	/**
	 * Constructor for Action1.
	 */
	public NewAction() {
		super();
	}

	/**
	 * @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart)
	 */
	public void setActivePart(IAction action, IWorkbenchPart targetPart) {
	this.part=targetPart;
	}

	private  boolean AddReferences(String szPath,String szOut)
	{
		boolean badd1=false;
		boolean badd2=false;
		
		try{
			BufferedWriter writer2 = null;
			FileWriter output2;
			output2 = new FileWriter(szOut);
			writer2 = new BufferedWriter(output2);
			// Open the file that is the first 
		    // command line parameter
		    FileInputStream fstream = new FileInputStream(szPath);
		    // Get the object of DataInputStream
		    DataInputStream in = new DataInputStream(fstream);
		        BufferedReader br = new BufferedReader(new InputStreamReader(in));
		    String strLine;
		    //Read File Line By Line
		    while ((strLine = br.readLine()) != null)   {
		    	writer2.write(strLine);
		    	writer2.write("\n");
		    	if (strLine.contains("<import namespace=\""))
		    	{
		    		if (badd1==false)
		    		{
		    			System.out.println("adding -- 1");
		    			writer2.write("   <import namespace=\"http://components.framework.ellectraweb.eu/\" location=\"Partners/eMailWSService/eMailWS.wsdl\" importType=\"http://schemas.xmlsoap.org/wsdl/\"/>");
		    			writer2.write("\n");
		    			badd1=true;
		    		}
		    	}
		    	if (strLine.contains("<partnerLink name=\""))
		    	{
		    		if (badd2==false)
		    		{
		    			System.out.println("adding -- " +"2");
		    			writer2.write("    <partnerLink name=\"WS_SendEmail\" xmlns:tns=\"http://enterprise.netbeans.org/bpel/eMailWSWrapper\" partnerLinkType=\"tns:eMailWSLinkType\" partnerRole=\"eMailWSRole\"/>");
		    			writer2.write("\n");
		    			badd2=true;
		    		}
		    	}
		      // Print the content on the console
		      //System.out.println (strLine);
		    }
		    //Close the input stream
		    in.close();
		    writer2.flush();
		    output2.close();
		    }catch (Exception e){//Catch exception if any
		      System.err.println("Error: " + e.getMessage());
		      return false;
		    }
		    boolean success = (new File(szPath)).delete();
		    if (!success) {
		        // Deletion failed
		    	return false;
		    }

		   
			try {
				 FileInputStream fis;
				fis = new FileInputStream(szOut);
				FileOutputStream fos = new FileOutputStream(szPath);
		        byte[] buf = new byte[1024];
		        int i = 0;
		        try {
					while((i=fis.read(buf))!=-1) {
					    fos.write(buf, 0, i);
					}
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					return false;
				}
		        try {
					fis.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					return false;
				}
		        try {
					fos.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					return false;
				}
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return false;
			}
			
			boolean success1 = (new File(szOut)).delete();
		    if (!success1) {
		        // Deletion failed
		    	return false;
		    }
	        
return true;
	}
	/**
	 * @see IActionDelegate#run(IAction)
	 */
	public void run(IAction action) {
		BufferedWriter writer2 = null;
		try {
		Shell shell = new Shell();
		FileWriter output2;
		output2 = new FileWriter("c:\\bpelplugin.log");
		writer2 = new BufferedWriter(output2);
		String dd2=gselection.toString();
		dd2=dd2.substring(3, dd2.length()-1);
		String szFullPathbb=dd2;
		szFullPathbb=szFullPathbb.replace("/", "\\");
		dd2=dd2.substring(0, dd2.lastIndexOf("/"));
		String df=dd2;
		df=df.replace("/", "\\");
		AddReferences("C:\\Eclipse_ePP_AF\\"+szFullPathbb,"C:\\Eclipse_ePP_AF\\"+df+"\\temp.bpel");
		
		//dd2=dd2.substring(0, dd2.lastIndexOf("/"));
		
		dd2.replace("/", "\\");
		writer2.write("Project Name " + dd2);

		writer2.write("dri1 " + "C:\\Eclipse_ePP_AF\\"+szFullPathbb);
		writer2.write("dri2 " + "C:\\Eclipse_ePP_AF\\"+df+"\\temp.bpel");
		
		
			FileWriter output;
			
			boolean status;
		    status = new File("C:\\Eclipse_ePP_AF\\"+dd2+"\\Partners\\eMailWSService").mkdir();
		    writer2.write("Create partner Link :  " + status);
			output = new FileWriter("C:\\Eclipse_ePP_AF\\"+dd2+"\\Partners\\eMailWSService\\"+"eMailWS_xsd_1.xsd");
			BufferedWriter writer = new BufferedWriter(output);
			writer2.write("Create XSD");
writer.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?><!-- Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.2-hudson-182-RC1. --><xs:schema xmlns:tns=\"http://components.framework.ellectraweb.eu/\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" version=\"1.0\" targetNamespace=\"http://components.framework.ellectraweb.eu/\">");
writer.write("\n");
writer.write("<xs:element name=\"postMail\" type=\"tns:postMail\"></xs:element>");
writer.write("\n");
writer.write("<xs:element name=\"postMailResponse\" type=\"tns:postMailResponse\"></xs:element>");
writer.write("\n");
writer.write("<xs:complexType name=\"postMail\">");
writer.write("\n");
writer.write("<xs:sequence>");
writer.write("\n");
writer.write("<xs:element name=\"from\" type=\"xs:string\" minOccurs=\"0\"></xs:element>");
writer.write("\n");
writer.write("<xs:element name=\"subject\" type=\"xs:string\" minOccurs=\"0\"></xs:element>");
writer.write("\n");
writer.write("<xs:element name=\"to\" type=\"xs:string\" minOccurs=\"0\" maxOccurs=\"unbounded\"></xs:element>");
writer.write("\n");
writer.write("<xs:element name=\"cc\" type=\"xs:string\" minOccurs=\"0\" maxOccurs=\"unbounded\"></xs:element>");
writer.write("\n");
writer.write("<xs:element name=\"bcc\" type=\"xs:string\" minOccurs=\"0\" maxOccurs=\"unbounded\"></xs:element>");
writer.write("\n");
writer.write("<xs:element name=\"content\" type=\"xs:string\" minOccurs=\"0\"></xs:element>");
writer.write("\n");
writer.write("<xs:element name=\"fileNamesList\" type=\"xs:string\" minOccurs=\"0\" maxOccurs=\"unbounded\"></xs:element>");
writer.write("\n");
writer.write("<xs:element name=\"filePathsList\" type=\"xs:string\" minOccurs=\"0\" maxOccurs=\"unbounded\"></xs:element>");
writer.write("\n");
writer.write("</xs:sequence>");
writer.write("\n");
writer.write("</xs:complexType>");
writer.write("\n");
writer.write("<xs:complexType name=\"postMailResponse\">");
writer.write("\n");
writer.write("<xs:sequence>");
writer.write("\n");
writer.write("<xs:element name=\"return\" type=\"xs:int\"></xs:element>");
writer.write("\n");
writer.write("</xs:sequence>");
writer.write("\n");
writer.write("</xs:complexType>");
																																										writer.write("</xs:schema>");
			writer.flush();
			output.close();
			
			output = new FileWriter("C:\\Eclipse_ePP_AF\\"+dd2+"\\Partners\\eMailWSService\\"+"eMailWS.wsdl");
			writer = new BufferedWriter(output);
			writer2.write("Create WSDL");
writer.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?><!-- Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.2-hudson-182-RC1. --><!-- Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.2-hudson-182-RC1. --><definitions xmlns:wsu=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd\" xmlns:wsp=\"http://schemas.xmlsoap.org/ws/2004/09/policy\" xmlns:soap=\"http://schemas.xmlsoap.org/wsdl/soap/\" xmlns:tns=\"http://components.framework.ellectraweb.eu/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns=\"http://schemas.xmlsoap.org/wsdl/\" targetNamespace=\"http://components.framework.ellectraweb.eu/\" name=\"eMailWSService\">");
writer.write("\n");
writer.write("<wsp:UsingPolicy></wsp:UsingPolicy>");
writer.write("\n");
writer.write("<wsp:Policy wsu:Id=\"eMailWSPortBinding_postMail_WSAT_Policy\">");
writer.write("\n");
writer.write("<wsp:ExactlyOne>");
writer.write("\n");
writer.write("<wsp:All>");
writer.write("\n");
writer.write("<ns1:ATAlwaysCapability xmlns:ns1=\"http://schemas.xmlsoap.org/ws/2004/10/wsat\" wsp:Optional=\"false\"></ns1:ATAlwaysCapability>");
writer.write("\n");
writer.write("<ns2:ATAssertion xmlns:ns3=\"http://schemas.xmlsoap.org/ws/2002/12/policy\" xmlns:ns2=\"http://schemas.xmlsoap.org/ws/2004/10/wsat\" ns3:Optional=\"true\" wsp:Optional=\"true\"></ns2:ATAssertion>");
writer.write("\n");
writer.write("</wsp:All>");
writer.write("\n");
writer.write("</wsp:ExactlyOne>");
writer.write("\n");
writer.write("</wsp:Policy>");
writer.write("\n");
writer.write("<types>");
writer.write("\n");
writer.write("<xsd:schema>");
writer.write("\n");
writer.write("<xsd:import namespace=\"http://components.framework.ellectraweb.eu/\" schemaLocation=\"eMailWS_xsd_1.xsd\"></xsd:import>");
writer.write("\n");
writer.write("</xsd:schema>");
writer.write("\n");
writer.write("</types>");
writer.write("\n");
writer.write("<message name=\"postMail\">");
writer.write("\n");
writer.write("<part name=\"parameters\" element=\"tns:postMail\"></part>");
writer.write("\n");
writer.write("</message>");
writer.write("\n");
writer.write("<message name=\"postMailResponse\">");
writer.write("\n");
writer.write("<part name=\"parameters\" element=\"tns:postMailResponse\"></part>");
writer.write("\n");
writer.write("</message>");
writer.write("\n");
writer.write("<portType name=\"eMailWS\">");
writer.write("\n");
writer.write("<operation name=\"postMail\">");
writer.write("\n");
writer.write("<input message=\"tns:postMail\"></input>");
writer.write("\n");
writer.write("<output message=\"tns:postMailResponse\"></output>");
writer.write("\n");
writer.write("</operation>");
writer.write("\n");
writer.write("</portType>");
writer.write("\n");
writer.write("<binding name=\"eMailWSPortBinding\" type=\"tns:eMailWS\">");
writer.write("\n");
writer.write("<soap:binding transport=\"http://schemas.xmlsoap.org/soap/http\" style=\"document\"></soap:binding>");
writer.write("\n");
writer.write("<operation name=\"postMail\">");
writer.write("\n");
writer.write("<wsp:PolicyReference URI=\"#eMailWSPortBinding_postMail_WSAT_Policy\"></wsp:PolicyReference>");
writer.write("\n");
writer.write("<soap:operation soapAction=\"\"></soap:operation>");
writer.write("\n");
writer.write("<input>");
writer.write("\n");
writer.write("<soap:body use=\"literal\"></soap:body>");
writer.write("\n");
writer.write("</input>");
writer.write("\n");
writer.write("<output>");
writer.write("\n");
writer.write("<soap:body use=\"literal\"></soap:body>");
writer.write("\n");
writer.write("</output>");
writer.write("\n");
writer.write("</operation>");
writer.write("\n");
writer.write("</binding>");
writer.write("\n");
writer.write("<service name=\"eMailWSService\">");
writer.write("\n");
writer.write("<port name=\"eMailWSPort\" binding=\"tns:eMailWSPortBinding\">");
writer.write("\n");
writer.write("<soap:address location=\"http://localhost:8080/eMailWSService/eMailWS\"></soap:address>");
writer.write("\n");
writer.write("</port>");
writer.write("\n");
writer.write("</service>");
writer.write("\n");
writer.write("</definitions>");

																																										writer.write("</xs:schema>");
			writer.flush();
			output.close();

			output = new FileWriter("C:\\Eclipse_ePP_AF\\"+dd2+"\\Partners\\eMailWSService\\"+"eMailWSWrapper.wsdl");
			writer = new BufferedWriter(output);
			writer2.write("Create WSDL Wrapper");
writer.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
writer.write("\n");
writer.write("<definitions");
writer.write("\n");
writer.write("xmlns=\"http://schemas.xmlsoap.org/wsdl/\"");
writer.write("\n");
writer.write("xmlns:soap=\"http://schemas.xmlsoap.org/wsdl/soap/\"");
writer.write("\n");
writer.write("xmlns:wsdl=\"http://schemas.xmlsoap.org/wsdl/\"");
writer.write("\n");
writer.write("xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" name=\"eMailWSWrapper\" targetNamespace=\"http://enterprise.netbeans.org/bpel/eMailWSWrapper\" xmlns:tns=\"http://enterprise.netbeans.org/bpel/eMailWSWrapper\" xmlns:plnk=\"http://docs.oasis-open.org/wsbpel/2.0/plnktype\" xmlns:ns=\"http://components.framework.ellectraweb.eu/\">");
writer.write("\n");
writer.write("<import location=\"eMailWS.wsdl\" namespace=\"http://components.framework.ellectraweb.eu/\"/>");
writer.write("\n");
writer.write("<plnk:partnerLinkType name=\"eMailWSLinkType\">");
writer.write("\n");
writer.write("<plnk:role name=\"eMailWSRole\" portType=\"ns:eMailWS\"/>");
writer.write("\n");
writer.write("</plnk:partnerLinkType>");
writer.write("\n");
writer.write("</definitions>");
			writer.flush();
			output.close();

			
		} catch (IOException e) {
		
			e.printStackTrace();
			try {
				writer2.write("Error on File"+e.getMessage());
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
		try {
			writer2.flush();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		

	}

	/**
	 * @see IActionDelegate#selectionChanged(IAction, ISelection)
	 */
	public void selectionChanged(IAction action, ISelection selection) {
	this.gselection=selection;
	}

}
