package com.ebmwebsourcing.easybpel.usecase.securemail;

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 MailService extends AbstractServiceImpl implements Service {


	public MailService(ProviderEndpoint pep) throws BPELException {
		super(pep);
		this.setName(new QName("http://mail.test/", "ServiceMail"));

		// Create operation
		getEmailByUid();
	}

	private void getEmailByUid() throws BPELException {
		try {
			Operation getEmailByUid = new OperationImpl("getEmailByUid", Operation.IN_OUT, this);
			this.addOperation(getEmailByUid);
			
			
			BPELExternalMessage in = new BPELExternalMessageImpl();
			in.setService(this.getName());
			in.setEndpoint(this.getProviderEndpoint().getName());
			in.setQName(new QName("http://mail.test/", "getEmailByUid"));
			in.setMessage("" +
					"<tns:getEmailByUid xmlns:tns=\"http://mail.test/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:type=\"tns:getEmailByUid\">" +
					"  <tns:arg0 xsi:type=\"xsd:string\">adrien_login</tns:arg0>" +
					"</tns:getEmailByUid>" 
			);
			BPELExternalMessage out = new BPELExternalMessageImpl();
			out.setService(this.getName());
			out.setEndpoint(this.getProviderEndpoint().getName());
			out.setQName(new QName("http://petals.ow2.org/policemen/", "setSecurityPerimeterResponse"));
			out.setMessage("" +
					"<mail:getEmailByUidResponse xmlns:mail=\"http://mail.test/\">" +
					"         <mail:return>adrien@ebm</mail:return>" +
					"</mail:getEmailByUidResponse>"
			);
			getEmailByUid.addMessageExchangeInstances(in, out, null);
			
		} catch (CoreException e) {
			throw new BPELException(e);
		}
	}

}
