package com.ebmwebsourcing.easybpel.usecase.mail;

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 AnnuService extends AbstractServiceImpl implements Service {


	public AnnuService(ProviderEndpoint pep) throws BPELException {
		super(pep);
		this.setName(new QName("http://webservices.acme.fr/", "ServicesCSIA"));

		// Create  operation
		getEmailByNID();
		getChefUniteParNID();
	}

	private void getEmailByNID() throws BPELException {
		try {
			Operation getEmailByNID = new OperationImpl("getEmailByNID", Operation.IN_OUT, this);
			this.addOperation(getEmailByNID);

			BPELExternalMessage in = new BPELExternalMessageImpl();
			in.setService(this.getName());
			in.setEndpoint(this.getProviderEndpoint().getName());
			in.setQName(new QName("http://webservices.acme.fr/", "getEmailByNIDRequest"));
			in.setMessage("" +
					"<ns0:getEmailByNIDRequest xmlns:ns0=\"http://webservices.acme.fr/\">" +
					"  <nid xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:type=\"xs:string\">04 560 30663</nid>" +
					"</ns0:getEmailByNIDRequest>" 
			);
			BPELExternalMessage out = new BPELExternalMessageImpl();
			out.setService(this.getName());
			out.setEndpoint(this.getProviderEndpoint().getName());
			out.setQName(new QName("http://webservices.acme.fr/", "getEmailByNIDResponse"));
			out.setMessage("" +
					"      <web:getEmailByNIDResponse soapenv:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:web=\"http://webservices.acme.fr/\">" +
					"         <return xsi:type=\"xsd:string\">nicolas.salatge@ebmwebsourcing.com</return>" +
					"      </web:getEmailByNIDResponse>"
			);

			getEmailByNID.addMessageExchangeInstances(in, out, null);

		} catch (CoreException e) {
			throw new BPELException(e);
		}
	}
	
	private void getChefUniteParNID() throws BPELException {
		try {
			Operation getChefUniteParNID = new OperationImpl("getChefUniteParNID", Operation.IN_OUT, this);
			this.addOperation(getChefUniteParNID);

			BPELExternalMessage in = new BPELExternalMessageImpl();
			in.setService(this.getName());
			in.setEndpoint(this.getProviderEndpoint().getName());
			in.setQName(new QName("http://webservices.acme.fr/", "getChefUniteParNIDRequest"));
			in.setMessage("" +
					"<ns0:getChefUniteParNIDRequest xmlns:ns0=\"http://webservices.acme.fr/\">" +
					"  <nid xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:type=\"xs:string\">04 560 30663</nid>" +
					"</ns0:getChefUniteParNIDRequest>" 
			);
			BPELExternalMessage out = new BPELExternalMessageImpl();
			out.setService(this.getName());
			out.setEndpoint(this.getProviderEndpoint().getName());
			out.setQName(new QName("http://webservices.acme.fr/", "getChefUniteParNIDResponse"));
			out.setMessage("" +
					"      <web:getChefUniteParNIDResponse soapenv:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:web=\"http://webservices.acme.fr/\">" +
					"         <return xsi:type=\"xsd:string\">nicolas.salatge@ebmwebsourcing.com</return>" +
					"      </web:getChefUniteParNIDResponse>"
			);

			getChefUniteParNID.addMessageExchangeInstances(in, out, null);

		} catch (CoreException e) {
			throw new BPELException(e);
		}
	}

}
