package com.ebmwebsourcing.easybpel.usecase.crisis;

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 FiremenService extends AbstractServiceImpl implements Service {


	public FiremenService(ProviderEndpoint pep) throws BPELException {
		super(pep);
		this.setName(new QName("http://petals.ow2.org/firemen/", "Firemen"));

		// Create operation
		fightExplosion();
		howManyToRescue();
		rescuePeople();
	}

	private void fightExplosion() throws BPELException {
		try {
			Operation fightExplosion = new OperationImpl("fightExplosion", Operation.IN_OUT, this);
			this.addOperation(fightExplosion);
			
			
			BPELExternalMessage in = new BPELExternalMessageImpl();
			in.setService(this.getName());
			in.setEndpoint(this.getProviderEndpoint().getName());
			in.setQName(new QName("http://petals.ow2.org/firemen/", "fightExplosionRequest"));
			in.setMessage("" +
					"<ns1:fightExplosion xmlns:ns1=\"http://petals.ow2.org/firemen/\">" +
					"  <fightExplosionRequest xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:type=\"xs:string\">start isycri process</fightExplosionRequest>" +
					"</ns1:fightExplosion>" 
			);
			BPELExternalMessage out = new BPELExternalMessageImpl();
			out.setService(this.getName());
			out.setEndpoint(this.getProviderEndpoint().getName());
			out.setQName(new QName("http://petals.ow2.org/firemen/", "fightExplosionRequestResponse"));
			out.setMessage("" +
					"<fir:fightExplosionResponse xmlns:fir=\"http://petals.ow2.org/firemen/\">" +
					"         <fightExplosionResponse>OK</fightExplosionResponse>" +
					"</fir:fightExplosionResponse>"
			);
			fightExplosion.addMessageExchangeInstances(in, out, null);
			
		} catch (CoreException e) {
			throw new BPELException(e);
		}
	}

	private void howManyToRescue() throws BPELException {
		try {
			Operation howManyToRescue = new OperationImpl("howManyToRescue", Operation.IN_OUT, this);
			this.addOperation(howManyToRescue);
			
			
			BPELExternalMessage in = new BPELExternalMessageImpl();
			in.setService(this.getName());
			in.setEndpoint(this.getProviderEndpoint().getName());
			in.setQName(new QName("http://petals.ow2.org/firemen/", "howManyToRescueRequest"));
			in.setMessage("" +
					"<ns1:howManyToRescue xmlns:ns1=\"http://petals.ow2.org/firemen/\">" +
					"  <howManyToRescueRequest xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:type=\"xs:string\">OK</howManyToRescueRequest>" +
					"</ns1:howManyToRescue>"
			);
			BPELExternalMessage out = new BPELExternalMessageImpl();
			out.setService(this.getName());
			out.setEndpoint(this.getProviderEndpoint().getName());
			out.setQName(new QName("http://petals.ow2.org/firemen/", "howManyToRescueResponse"));
			out.setMessage("" +
					"<fir:howManyToRescueResponse xmlns:fir=\"http://petals.ow2.org/firemen/\">" +
					"  <howManyToRescueResponse xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" type=\"xsd:int\" >3</howManyToRescueResponse>" +
					"</fir:howManyToRescueResponse>"
			);
			howManyToRescue.addMessageExchangeInstances(in, out, null);
			
		} catch (CoreException e) {
			throw new BPELException(e);
		}
	}
	
	
	private void rescuePeople() throws BPELException {
		try {
			Operation rescuePeople = new OperationImpl("rescuePeople", Operation.IN_OUT, this);
			this.addOperation(rescuePeople);
			
			
			BPELExternalMessage in = new BPELExternalMessageImpl();
			in.setService(this.getName());
			in.setEndpoint(this.getProviderEndpoint().getName());
			in.setQName(new QName("http://petals.ow2.org/firemen/", "rescuePeopleRequest"));
			in.setMessage("" +
					"<ns1:rescuePeople xmlns:ns1=\"http://petals.ow2.org/firemen/\">" +
					"  <rescuePeopleRequest xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:type=\"xs:string\">3</rescuePeopleRequest>" +
					"</ns1:rescuePeople>"
			);
			BPELExternalMessage out = new BPELExternalMessageImpl();
			out.setService(this.getName());
			out.setEndpoint(this.getProviderEndpoint().getName());
			out.setQName(new QName("http://petals.ow2.org/firemen/", "rescuePeopleResponse"));
			out.setMessage("" +
					"<fir:rescuePeopleResponse xmlns:fir=\"http://petals.ow2.org/firemen/\">" +
					"    <rescuePeopleResponse>?</rescuePeopleResponse>" +
					"</fir:rescuePeopleResponse>"
			);
			rescuePeople.addMessageExchangeInstances(in, out, null);
			
		} catch (CoreException e) {
			throw new BPELException(e);
		}
	}
}
