package com.ebmwebsourcing.easybpel.usecase.notification;

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 ProducerNotificationService extends AbstractServiceImpl implements Service {


	public ProducerNotificationService(ProviderEndpoint pEpt) throws BPELException {
		super(pEpt);
		this.setName(new QName("http://example.org/services/WeatherManagementService.wsdl", "WeatherManagementService"));

		// Create operation
		subscribe();
		//unSuscribe();
	}

	private void subscribe() throws BPELException {
		try {
			Operation suscribe = new OperationImpl("Subscribe", Operation.IN_OUT, this);
			this.addOperation(suscribe);


			BPELExternalMessage in = new BPELExternalMessageImpl();
			in.setService(this.getName());
			in.setEndpoint(this.getProviderEndpoint().getName());
			in.setQName(new QName("http://docs.oasis-open.org/wsn/bw-2", "SubscribeRequest"));
			in.setMessage("" +
					"<ns:SubscribeRequest xmlns:ns=\"http://docs.oasis-open.org/wsn/bw-2\">&lt;foo:bar /&gt;"+
					"</ns:SubscribeRequest>"
			);
			
			BPELExternalMessage out = new BPELExternalMessageImpl();
			out.setService(this.getName());
			out.setEndpoint(this.getProviderEndpoint().getName());
			out.setQName(new QName("http://docs.oasis-open.org/wsn/bw-2", "SubscribeReponse"));
			out.setMessage(""+
					"<b:SubscribeResponse xmlns:b=\"http://docs.oasis-open.org/wsn/b-2\" xmlns:add=\"http://www.w3.org/2005/08/addressing\">" +
						"<b:SubscriptionReference>" +
							"<add:Address>?</add:Address>" +
							"<add:ReferenceParameters></add:ReferenceParameters>" +
							"<add:Metadata></add:Metadata>" +
						"</b:SubscriptionReference>" +
						"<b:CurrentTime>?</b:CurrentTime>" +
						"<b:TerminationTime>?</b:TerminationTime>" +
					"</b:SubscribeResponse>");
			suscribe.addMessageExchangeInstances(in, out, null);
			
		} catch (CoreException e) {
			throw new BPELException(e);
		}
	}
	
	
	
	private void unSuscribe() throws BPELException {
		try {
			Operation suscribe = new OperationImpl("unSuscribe", Operation.IN_ONLY, this);
			this.addOperation(suscribe);


			BPELExternalMessage in = new BPELExternalMessageImpl();
			in.setService(this.getName());
			in.setEndpoint(this.getProviderEndpoint().getName());
			in.setQName(new QName("http://org.ow2.petals/demo/travelagency/airline/", "bookFlightRequest"));
			in.setMessage("" +
					"<b:Unsubscribe xmlns:b=\"http://docs.oasis-open.org/wsn/b-2\">" +
					"</b:Unsubscribe>" 
			);
			BPELExternalMessage out = new BPELExternalMessageImpl();
			out.setService(this.getName());
			out.setEndpoint(this.getProviderEndpoint().getName());
			out.setQName(new QName("http://org.ow2.petals/demo/travelagency/airline/", "bookFlightResponse"));
			out.setMessage("" +
				      "<b:UnsubscribeResponse>"+
				      "</b:UnsubscribeResponse>"
			);
			suscribe.addMessageExchangeInstances(in, out, null);

		} catch (CoreException e) {
			throw new BPELException(e);
		}
	}

}
