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 RedCrossService extends AbstractServiceImpl implements Service {


	public RedCrossService(ProviderEndpoint pep) throws BPELException {
		super(pep);
		this.setName(new QName("http://petals.ow2.org/redcross/", "Redcross"));

		// Create operation
		setMedicalPost();
		bringPeopleToMedicalPost();
	}

	private void setMedicalPost() throws BPELException {
		try {
			Operation setMedicalPost = new OperationImpl("setMedicalPost", Operation.IN_OUT, this);
			this.addOperation(setMedicalPost);
			
			
			BPELExternalMessage in = new BPELExternalMessageImpl();
			in.setService(this.getName());
			in.setEndpoint(this.getProviderEndpoint().getName());
			in.setQName(new QName("http://petals.ow2.org/redcross/", "setMedicalPostRequest"));
			in.setMessage("" +
					"<ns2:setMedicalPost xmlns:ns2=\"http://petals.ow2.org/redcross/\">" +
					"  <setMedicalPostRequest xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:type=\"xs:string\">3</setMedicalPostRequest>" +
					"</ns2:setMedicalPost>" 
			);
			BPELExternalMessage out = new BPELExternalMessageImpl();
			out.setService(this.getName());
			out.setEndpoint(this.getProviderEndpoint().getName());
			out.setQName(new QName("http://petals.ow2.org/redcross/", "setMedicalPostResponse"));
			out.setMessage("" +
					"<red:setMedicalPostResponse xmlns:red=\"http://petals.ow2.org/redcross/\">" +
					"         <setMedicalPostResponse>OK</setMedicalPostResponse>" +
					"</red:setMedicalPostResponse>"
			);
			setMedicalPost.addMessageExchangeInstances(in, out, null);
			
		} catch (CoreException e) {
			throw new BPELException(e);
		}
	}


	private void bringPeopleToMedicalPost() throws BPELException {
		try {
			Operation bringPeopleToMedicalPost = new OperationImpl("bringPeopleToMedicalPost", Operation.IN_OUT, this);
			this.addOperation(bringPeopleToMedicalPost);
			
			
			BPELExternalMessage in = new BPELExternalMessageImpl();
			in.setService(this.getName());
			in.setEndpoint(this.getProviderEndpoint().getName());
			in.setQName(new QName("http://petals.ow2.org/redcross/", "bringPeopleToMedicalPostRequest"));
			in.setMessage("" +
					"<ns2:bringPeopleToMedicalPost xmlns:ns2=\"http://petals.ow2.org/redcross/\">" +
					"  <bringPeopleToMedicalPostRequest xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:type=\"xs:string\">3</bringPeopleToMedicalPostRequest>" +
					"</ns2:bringPeopleToMedicalPost>" 
			);
			BPELExternalMessage out = new BPELExternalMessageImpl();
			out.setService(this.getName());
			out.setEndpoint(this.getProviderEndpoint().getName());
			out.setQName(new QName("http://petals.ow2.org/redcross/", "bringPeopleToMedicalPostResponse"));
			out.setMessage("" +
					"<red:bringPeopleToMedicalPostResponse xmlns:red=\"http://petals.ow2.org/redcross/\">" +
					"  <bringPeopleToMedicalPostResponse>?</bringPeopleToMedicalPostResponse>" +
					"</red:bringPeopleToMedicalPostResponse>"
			);
			bringPeopleToMedicalPost.addMessageExchangeInstances(in, out, null);
			
		} catch (CoreException e) {
			throw new BPELException(e);
		}
	}
}
