package com.ebmwebsourcing.easybpel.usecase.faultHandler;

import javax.xml.namespace.QName;

import com.ebmwebsourcing.easybpel.model.bpel.api.BPELException;
import com.ebmwebsourcing.easybpel.model.bpel.api.message.BPELExternalMessage;
import com.ebmwebsourcing.easybpel.model.bpel.impl.message.BPELExternalMessageImpl;
import com.ebmwebsourcing.easyviper.core.api.CoreException;
import com.ebmwebsourcing.easyviper.environment.test.env.api.Operation;
import com.ebmwebsourcing.easyviper.environment.test.env.api.ProviderEndpoint;
import com.ebmwebsourcing.easyviper.environment.test.env.api.Service;
import com.ebmwebsourcing.easyviper.environment.test.env.impl.AbstractServiceImpl;
import com.ebmwebsourcing.easyviper.environment.test.env.impl.OperationImpl;

public class FaultWSService extends AbstractServiceImpl implements Service {


	public FaultWSService(ProviderEndpoint pep) throws BPELException {
		super(pep);
		this.setName(new QName("http://petals.ow2.org/FaultWS/", "FaultWS"));

		// Create operation
		getNormalMessage();
		getBusinessMessage();
		getTechnicalMessage();
	}

	private void getNormalMessage() throws BPELException {
		try {
			Operation getNormalMessage = new OperationImpl("getNormalMessage", Operation.IN_OUT, this);
			this.addOperation(getNormalMessage);


			BPELExternalMessage in1 = new BPELExternalMessageImpl();
			in1.setService(this.getName());
			in1.setEndpoint(this.getProviderEndpoint().getName());
			in1.setQName(new QName("http://petals.ow2.org/FaultWS/", "getNormalMessageRequest"));
			in1.setMessage("" +
					"<tns:getNormalMessage xmlns:tns=\"http://petals.ow2.org/FaultWS/\">" +
					" <in xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:type=\"xsd:string\">0</in>" +
					"</tns:getNormalMessage>" 
			);
			BPELExternalMessage out1 = new BPELExternalMessageImpl();
			out1.setService(this.getName());
			out1.setEndpoint(this.getProviderEndpoint().getName());
			out1.setQName(new QName("http://petals.ow2.org/FaultWS/", "getNormalMessageResponse"));
			out1.setMessage("" +
					"<faul:getNormalMessageResponse xmlns:faul=\"http://petals.ow2.org/FaultWS/\">" +
					"         <out>0</out>" +
					"</faul:getNormalMessageResponse>"
			);
			getNormalMessage.addMessageExchangeInstances(in1, out1, null);


			BPELExternalMessage in2 = new BPELExternalMessageImpl();
			in2.setService(this.getName());
			in2.setEndpoint(this.getProviderEndpoint().getName());
			in2.setQName(new QName("http://petals.ow2.org/FaultWS/", "getNormalMessageRequest"));
			in2.setMessage("" +
					"<getNormalMessage>" +
					"  <in xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:type=\"xsd:string\">1</in>" +
					"</getNormalMessage>" 
			);
			BPELExternalMessage out2 = new BPELExternalMessageImpl();
			out2.setService(this.getName());
			out2.setEndpoint(this.getProviderEndpoint().getName());
			out2.setQName(new QName("http://petals.ow2.org/FaultWS/", "getNormalMessageResponse"));
			out2.setMessage("" +
					"<faul:getNormalMessageResponse xmlns:faul=\"http://petals.ow2.org/FaultWS/\">" +
					"         <out>1</out>" +
					"</faul:getNormalMessageResponse>"
			);
			getNormalMessage.addMessageExchangeInstances(in2, out2, null);

		} catch (CoreException e) {
			throw new BPELException(e);
		}
	}

	private void getBusinessMessage() throws BPELException {
		try {
			Operation getBusinessMessage = new OperationImpl("getBusinessMessage", Operation.IN_OUT, this);
			this.addOperation(getBusinessMessage);


			BPELExternalMessage in1 = new BPELExternalMessageImpl();
			in1.setService(this.getName());
			in1.setEndpoint(this.getProviderEndpoint().getName());
			in1.setQName(new QName("http://petals.ow2.org/FaultWS/", "getBusinessMessage"));
			in1.setMessage("" +
					"<tns:getBusinessMessage xmlns:tns=\"http://petals.ow2.org/FaultWS/\">" +
					"  <in xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:type=\"xsd:string\">0</in>" +
					"</tns:getBusinessMessage>" 
			);
			BPELExternalMessage fault1 = new BPELExternalMessageImpl();
			fault1.setService(this.getName());
			fault1.setEndpoint(this.getProviderEndpoint().getName());
			fault1.setQName(new QName("http://petals.ow2.org/FaultWS/", "getNormalMessageResponse"));
			fault1.setMessage("" +
					"<soap:Fault xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">" + 
					"         <faultcode>soap:Server</faultcode>" +
					"         <faultstring>GetBusinessMessageFault0Msg...</faultstring>" +
					"         <detail>" +
					"            <ns2:myfault xmlns:ns2=\"http://petals.ow2.org/FaultWS/\">" +
					"                  <msg>Fault 0</msg>" +
					"                  <errorCode>0</errorCode>" +
					"            </ns2:myfault>" +
					"		  </detail>" +
					"</soap:Fault>" 
			);
			getBusinessMessage.addMessageExchangeInstances(in1, null, fault1);


			BPELExternalMessage in2 = new BPELExternalMessageImpl();
			in2.setService(this.getName());
			in2.setEndpoint(this.getProviderEndpoint().getName());
			in2.setQName(new QName("http://petals.ow2.org/FaultWS/", "getBusinessMessage"));
			in2.setMessage("" +
					"<tns:getBusinessMessage xmlns:tns=\"http://petals.ow2.org/FaultWS/\">" +
					"  <in xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:type=\"xsd:string\">1</in>" +
					"</tns:getBusinessMessage>" 
			);
			BPELExternalMessage fault2 = new BPELExternalMessageImpl();
			fault2.setService(this.getName());
			fault2.setEndpoint(this.getProviderEndpoint().getName());
			fault2.setQName(new QName("http://petals.ow2.org/FaultWS/", "getNormalMessageResponse"));
			fault2.setMessage("" +
					"<soap:Fault xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">" + 
					"         <faultcode>soap:Server</faultcode>" +
					"         <faultstring>GetBusinessMessageFault1Msg...</faultstring>" +
					"         <detail>" +
					"            <ns2:myOtherFault xmlns:ns2=\"http://petals.ow2.org/FaultWS/\">" +
					"                <kinfFault>Fault 1</kinfFault>" +
					"                <type>1</type>" +
					"            </ns2:myOtherFault>" +
					"		  </detail>" +
					"</soap:Fault>" 
			);
			getBusinessMessage.addMessageExchangeInstances(in2, null, fault2);
		} catch (CoreException e) {
			throw new BPELException(e);
		}
	}


	private void getTechnicalMessage() throws BPELException {
		try {
			Operation getTechnicalMessage = new OperationImpl("getTechnicalMessage", Operation.IN_OUT, this);
			this.addOperation(getTechnicalMessage);


			BPELExternalMessage in = new BPELExternalMessageImpl();
			in.setService(this.getName());
			in.setEndpoint(this.getProviderEndpoint().getName());
			in.setQName(new QName("http://petals.ow2.org/FaultWS/", "getTechnicalMessage"));
			in.setMessage("" +
					"<tns:getTechnicalMessage xmlns:tns=\"http://petals.ow2.org/FaultWS/\">" +
					"  <in xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:type=\"xsd:string\">Fault 0=&gt; Recatched in FH</in>" +
					"</tns:getTechnicalMessage>"
			);
			BPELExternalMessage fault = new BPELExternalMessageImpl();
			fault.setService(this.getName());
			fault.setEndpoint(this.getProviderEndpoint().getName());
			fault.setQName(new QName("http://petals.ow2.org/FaultWS/", "getTechnicalMessageResponse"));
			fault.setMessage("" +
					"<soap:Fault xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">" + 
					"		<faultcode>soap:Server</faultcode>" +
					"	    <faultstring>Technical BPELError</faultstring>" +
					"</soap:Fault>" 
			);
			getTechnicalMessage.addMessageExchangeInstances(in, null, fault);

		} catch (CoreException e) {
			throw new BPELException(e);
		}
	}

}
