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 AuthService extends AbstractServiceImpl implements Service {


	public AuthService(ProviderEndpoint pep) throws BPELException {
		super(pep);
		this.setName(new QName("http://auth.air.defense.gouv.fr", "AuthImplService"));

		// Create operation
		auth();
	}

	private void auth() throws BPELException {
		try {
			Operation auth = new OperationImpl("auth", Operation.IN_OUT, this);
			this.addOperation(auth);
			
			
			BPELExternalMessage in = new BPELExternalMessageImpl();
			in.setService(this.getName());
			in.setEndpoint(this.getProviderEndpoint().getName());
			in.setQName(new QName("http://auth.air.defense.gouv.fr", "auth"));
			in.setMessage("" +
					"<impl:auth xmlns:impl=\"http://auth.air.defense.gouv.fr\">" +
					"  <impl:uid>adrien_login</impl:uid>" +
					"  <impl:pwd>adrien_pwd</impl:pwd>" +
					"</impl:auth>"
			);
			BPELExternalMessage out = new BPELExternalMessageImpl();
			out.setService(this.getName());
			out.setEndpoint(this.getProviderEndpoint().getName());
			out.setQName(new QName("http://auth.air.defense.gouv.fr", "authResponse"));
			out.setMessage("" +
					"<auth:authResponse xmlns:auth=\"http://auth.air.defense.gouv.fr\">" + 
					"         <auth:authReturn>true</auth:authReturn>" +
					"</auth:authResponse>" 
			);
			auth.addMessageExchangeInstances(in, out, null);
			
		} catch (CoreException e) {
			throw new BPELException(e);
		}
	}

}
