package com.ebmwebsourcing.easybpel.usecase.sales;

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 StockService extends AbstractServiceImpl implements Service {


	public StockService(ProviderEndpoint pep) throws BPELException {
		super(pep);
		this.setName(new QName("http://petals.ow2.org/sales", "stockAsync"));

		// Create operation
		initiate();
	}

	private void initiate() throws BPELException {
		try {
			Operation initiate = new OperationImpl("initiate", Operation.IN_OUT, this);
			this.addOperation(initiate);


			BPELExternalMessage in = new BPELExternalMessageImpl();
			in.setService(this.getName());
			in.setEndpoint(this.getProviderEndpoint().getName());
			in.setQName(new QName("http://petals.ow2.org/sales", "stockRequestMessage"));
			in.setMessage("" +
					"<tns:stockRequest xmlns:tns=\"http://petals.ow2.org/sales\">" +
					"  <tns:article xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:type=\"tns:StockItem\">" +
					"    <tns:refId xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xsi:type=\"xsd:int\">4</tns:refId>" +
					"    <tns:quantity xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xsi:type=\"xsd:int\">3</tns:quantity>" +
					"  </tns:article>" +
					"</tns:stockRequest>" 
			);
			
			initiate.addMessageExchangeInstances(in, null, null);

		} catch (CoreException e) {
			throw new BPELException(e);
		}
	}


}
