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 GestionnaireService extends AbstractServiceImpl implements Service {


	public GestionnaireService(ProviderEndpoint pep) throws BPELException {
		super(pep);
		this.setName(new QName("http://petals.ow2.org/gestionnaire/", "gestionnaire"));

		// Create sendAppelOffre operation
		sendAppelOffre();
	}

	private void sendAppelOffre() throws BPELException {
		try {
			BPELExternalMessage in = new BPELExternalMessageImpl();
			in.setService(this.getName());
			in.setEndpoint(this.getProviderEndpoint().getName());
			in.setQName(new QName("http://petals.ow2.org/gestionnaire/", "sendAppelOffreRequest"));
			in.setMessage("" +
					"<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
					"<ns:sendAppelOffreRequest xmlns:ns=\"http://petals.ow2.org/gestionnaire/\">" +
					"    <tns:sendAppelOffre xmlns:tns=\"http://petals.ow2.org/gestionnaire/\">" +
					"         <tns:offre xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" type=\"xsd:string\">Send Offer for create Security perimeter</tns:offre>" +
					"    </tns:sendAppelOffre>" +
					"</ns:sendAppelOffreRequest>" 
			);
			BPELExternalMessage out = new BPELExternalMessageImpl();
			out.setService(this.getName());
			out.setEndpoint(this.getProviderEndpoint().getName());
			out.setQName(new QName("http://petals.ow2.org/gestionnaire/", "sendAppelOffreResponse"));
			out.setMessage("" +
					"<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
					"<sendAppelOffreResponse xmlns:ns=\"http://petals.ow2.org/gestionnaire/\">" +
					"   <sendAppelOffreResponse xmlns:tns=\"http://petals.ow2.org/gestionnaire/\">" +
					"      <out type=\"xsd:string\">SecurityPerimeterPolicyEndpoint</out>" +
					"   </sendAppelOffreResponse>" +
					"</sendAppelOffreResponse>" 
			);
			Operation sendAppelOffre = new OperationImpl("sendAppelOffre", Operation.IN_OUT, this);
			sendAppelOffre.addMessageExchangeInstances(in, out, null);
			this.addOperation(sendAppelOffre);
		} catch (CoreException e) {
			throw new BPELException(e);
		}
	}

}
