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 EmsService extends AbstractServiceImpl implements Service {


	public EmsService(ProviderEndpoint pep) throws BPELException {
		super(pep);
		this.setName(new QName("http://petals.ow2.org/ems/", "Ems"));

		// Create operation
		carePeople();
		bringPeopleToMedicalPost();
	}

	private void carePeople() throws BPELException {
		try {
			Operation carePeople = new OperationImpl("carePeople", Operation.IN_OUT, this);
			this.addOperation(carePeople);
			
			
			BPELExternalMessage in = new BPELExternalMessageImpl();
			in.setService(this.getName());
			in.setEndpoint(this.getProviderEndpoint().getName());
			in.setQName(new QName("http://petals.ow2.org/ems/", "carePeopleRequest"));
			in.setMessage("" +
					"<ns4:carePeople xmlns:ns4=\"http://petals.ow2.org/ems/\">" +
					"  <carePeopleRequest xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:type=\"xs:string\">?</carePeopleRequest>" +
					"</ns4:carePeople>" 
			);
			BPELExternalMessage out = new BPELExternalMessageImpl();
			out.setService(this.getName());
			out.setEndpoint(this.getProviderEndpoint().getName());
			out.setQName(new QName("http://petals.ow2.org/ems/", "carePeopleResponse"));
			out.setMessage("" +
					"<ems:carePeopleResponse xmlns:ems=\"http://petals.ow2.org/ems/\">" +
					"         <carePeopleResponse>?</carePeopleResponse>" +
					"</ems:carePeopleResponse>" 
			);
			carePeople.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);
		}
	}
}
