package com.ebmwebsourcing.easybpel.usecase.seinedemo;

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 AP214Service extends AbstractServiceImpl implements Service {


	public AP214Service(ProviderEndpoint pep) throws BPELException {
		super(pep);
		this.setName(new QName("http://seinedemo.ebmwebsourcing.com/AP214/", "AP214Service"));

		// Create operation
		this.createUploadAP214DatasetOperation();
		this.createDownloadAP214DatasetOperation();
		//this.createPutEnvelopeOperation();
	}

	private void createUploadAP214DatasetOperation() throws BPELException {
		try {
			Operation uploadAP214Dataset = new OperationImpl("UploadAP214Dataset", Operation.IN_OUT, this);
			BPELExternalMessage in = new BPELExternalMessageImpl();
			in.setService(this.getName());
			in.setEndpoint(this.getProviderEndpoint().getName());
			in.setQName(new QName("http://seinedemo.ebmwebsourcing.com/AP214/", "UploadAP214Dataset"));
			in.setMessage("" +
					"<tns:UploadAP214DatasetRequest_AP214DatasetId_AP214Dataset xmlns:tns=\"http://seinedemo.ebmwebsourcing.com/AP214/\" >" +
			  "<AP214DatasetId xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:type=\"xsd:string\">45</AP214DatasetId>" +
			  "<AP214Dataset xmlns:ap214=\"http://AP214/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:type=\"ap214:AP214_Dataset_Type\">" +
			    "<ap214:document xmlns:ap214=\"http://seinedemo.ebmwebsourcing.com/AP214/\">" +
			     " <jaws9:description xmlns:jaws9=\"http://Document_and_file_management.PLM_services.valueObjects/jaws\">75</jaws9:description>" +
			      "<jaws9:uid xmlns:jaws9=\"http://Document_and_file_management.PLM_services.valueObjects/jaws\">888</jaws9:uid>" +
			      "<jaws9:document_id xmlns:jaws9=\"http://Document_and_file_management.PLM_services.valueObjects/jaws\">888</jaws9:document_id>" +
			      "<jaws9:name xmlns:jaws9=\"http://Document_and_file_management.PLM_services.valueObjects/jaws\">58</jaws9:name>" +
			    "</ap214:document>" +
			  "</AP214Dataset>" +
			"</tns:UploadAP214DatasetRequest_AP214DatasetId_AP214Dataset>"
			);

			BPELExternalMessage out = new BPELExternalMessageImpl();
			out.setService(this.getName());
			out.setEndpoint(this.getProviderEndpoint().getName());
			out.setQName(new QName("http://seinedemo.ebmwebsourcing.com/AP214/", "UploadAP214Dataset"));
			out.setMessage("" +
					"<tns:UploadAP214DatasetResponse_AP214Boolean xmlns:tns=\"http://seinedemo.ebmwebsourcing.com/AP214/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:type=\"xsd:string\">" +
					"true" +
					"</tns:UploadAP214DatasetResponse_AP214Boolean>" 
			);
			uploadAP214Dataset.addMessageExchangeInstances(in, out, null);

			this.addOperation(uploadAP214Dataset);
		} catch (CoreException e) {
			throw new BPELException(e);
		}
	}

	private void createDownloadAP214DatasetOperation() throws BPELException {
		try {
			Operation downloadAP214Dataset = new OperationImpl("DownloadAP214Dataset", Operation.IN_OUT, this);
			BPELExternalMessage in = new BPELExternalMessageImpl();
			in.setService(this.getName());
			in.setEndpoint(this.getProviderEndpoint().getName());
			in.setQName(new QName("http://seinedemo.ebmwebsourcing.com/AP214/", "DownloadAP214Dataset"));
			in.setMessage("" +
					"<tns:DownloadAP214DatasetRequest_AP214DatasetId xmlns:tns=\"http://seinedemo.ebmwebsourcing.com/AP214/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:type=\"xsd:string\">" +
						"45" +
					"</tns:DownloadAP214DatasetRequest_AP214DatasetId>"
			);

			BPELExternalMessage out = new BPELExternalMessageImpl();
			out.setService(this.getName());
			out.setEndpoint(this.getProviderEndpoint().getName());
			out.setQName(new QName("http://seinedemo.ebmwebsourcing.com/AP214/", "DownloadAP214Dataset"));
			out.setMessage("" +
					"<tns:DownloadAP214DatasetResponse_AP214Dataset xmlns:tns=\"http://seinedemo.ebmwebsourcing.com/AP214/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:type=\"xsd:string\">" +
						"<ap214:document xmlns:ap214=\"http://seinedemo.ebmwebsourcing.com/AP214/\">" +
						" <jaws9:description xmlns:jaws9=\"http://Document_and_file_management.PLM_services.valueObjects/jaws\">75</jaws9:description>" +
						"<jaws9:uid xmlns:jaws9=\"http://Document_and_file_management.PLM_services.valueObjects/jaws\">888</jaws9:uid>" +
						"<jaws9:document_id xmlns:jaws9=\"http://Document_and_file_management.PLM_services.valueObjects/jaws\">888</jaws9:document_id>" +
						"<jaws9:name xmlns:jaws9=\"http://Document_and_file_management.PLM_services.valueObjects/jaws\">58</jaws9:name>" +
						"</ap214:document>" +
					"</tns:DownloadAP214DatasetResponse_AP214Dataset>" 
			);
			downloadAP214Dataset.addMessageExchangeInstances(in, out, null);

			this.addOperation(downloadAP214Dataset);
		} catch (CoreException e) {
			throw new BPELException(e);
		}
	}
	
	private void createPutEnvelopeOperation() throws BPELException {
		try {
			Operation putEnvelope = new OperationImpl("PutEnvelope", Operation.IN_OUT, this);

			BPELExternalMessage in = new BPELExternalMessageImpl();
			in.setService(this.getName());
			in.setEndpoint(this.getProviderEndpoint().getName());
			in.setQName(new QName("http://seinedemo.ebmwebsourcing.com/AP214/", "PutEnvelope"));
			in.setMessage("" +
				      "<tns:PutEnvelope xmlns:tns=\"http://seinedemo.ebmwebsourcing.com/AP239/\" xmlns:ap2=\"http://AP239/\" xmlns:jaws=\"http://Entities/AP239/message/jaws\" xmlns:jaws1=\"http://Entities/AP239/envelope/jaws\" xmlns:jaws2=\"http://ValueObjects/AP239/jaws\">" +
				      "   <ap2:datasetId>45</ap2:datasetId>" +
				      "   <ap2:envelopeId>99</ap2:envelopeId>" +
				      "   <ap2:receivers>dassault</ap2:receivers>" +
				      "   <ap2:sender>thales</ap2:sender>" +
				      "</tns:PutEnvelope>"
			);
			
			BPELExternalMessage out = new BPELExternalMessageImpl();
			out.setService(this.getName());
			out.setEndpoint(this.getProviderEndpoint().getName());
			out.setQName(new QName("http://seinedemo.ebmwebsourcing.com/AP214/", "PutEnvelope"));
			out.setMessage("" +
					"<tns:AP239Boolean xmlns:tns=\"http://seinedemo.ebmwebsourcing.com/AP239/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:type=\"xsd:string\">" +
					"true" +
					"</tns:AP239Boolean>" 
			);
			putEnvelope.addMessageExchangeInstances(in, out, null);

			this.addOperation(putEnvelope);
		} catch (CoreException e) {
			throw new BPELException(e);
		}
	}
}
