package com.ebmwebsourcing.easybpel.usecase.collecteRegimeCNAV;

import java.io.File;
import java.io.IOException;

import javax.xml.namespace.QName;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

import com.ebmwebsourcing.easybpel.model.bpel.api.message.BPELExternalMessage;
import com.ebmwebsourcing.easybpel.model.bpel.impl.message.BPELExternalMessageImpl;
import com.ebmwebsourcing.easyviper.environment.test.env.api.Operation;
import com.ebmwebsourcing.easyviper.environment.test.env.api.Service;
import com.ebmwebsourcing.easyviper.environment.test.env.impl.OperationImpl;

/**
 * 
 * @author alouis
 *
 */
public class MockServiceBuilder {

	/**
	 * Build a full <code>Operation</code> object with all information set. If
	 * there is no response set, the operation is "InOnly"; InOut otherwise.
	 * 
	 * @param namespace
	 *            the namespace of the interface. used as ns for all operation
	 *            parameters
	 * @param operationName
	 *            the operation name
	 * @param serviceName
	 *            the service name
	 * @param endpointName
	 *            the endpoint name
	 * @param requestXmlFile
	 *            file containing the attempted request
	 * @param requestMessageName
	 *            request message name
	 * @param responseXmlFile
	 *            file containing the response to return may be null if no
	 *            response is returned
	 * @param responseMessageName
	 *            response message name may be null if no response is returned
	 * @param faultXmlFile
	 *            file containing the fault to return may be null if no response
	 *            is returned
	 * @param faultMessageName
	 *            fault message name may be null if no response is returned
	 * @return the corresponding operation
	 * @throws IOException
	 * @throws JDOMException
	 */

	public static Operation buildOperation(Service s, String namespace,
			String operationName, String serviceName, String endpointName,
			File requestXmlFile, String requestMessageName,
			File responseXmlFile, String responseMessageName,
			File faultXmlFile, String faultMessageName) throws JDOMException,
			IOException {

		Operation operation = new OperationImpl(
				operationName,
				(responseXmlFile == null ? Operation.IN_ONLY : Operation.IN_OUT), s);
		BPELExternalMessage in = buildEmptyMessage(namespace, serviceName,
				endpointName, requestXmlFile, requestMessageName);

		BPELExternalMessage out = null;
		if (responseXmlFile != null) {
			out = buildEmptyMessage(namespace, serviceName, endpointName,
					responseXmlFile, responseMessageName);

		}

		BPELExternalMessage fault = null;
		if (faultXmlFile != null) {
			fault = buildEmptyMessage(namespace, serviceName, endpointName,
					faultXmlFile, faultMessageName);

		}
		operation.addMessageExchangeInstances(in, out, fault);
		return operation;
	}

	/**
	 * @param namespace
	 *            the namespace of the interface
	 * @param serviceName
	 * @param endpointName
	 * @param xmlFile
	 * @param messageName
	 * @return
	 * @throws JDOMException
	 * @throws IOException
	 */
	public static BPELExternalMessage buildEmptyMessage(String namespace,
			String serviceName, String endpointName, File xmlFile,
			String messageName) throws JDOMException, IOException {
		BPELExternalMessage msg = new BPELExternalMessageImpl();
		// FIXME setService = itf or srv name ?
		msg.setService(new QName(namespace, serviceName));
		msg.setEndpoint(endpointName);
		msg.setQName(new QName(namespace, messageName));

		msg.setContent(buildContent(xmlFile));
		return msg;
	}

	/**
	 * Return a jdom Element corresponding to the root element of the Xml given
	 * file
	 * 
	 * @param xmlFile
	 * @return
	 * @throws IOException
	 * @throws JDOMException
	 */
	public static Element buildContent(File xmlFile) throws JDOMException,
			IOException {
		SAXBuilder parser = new SAXBuilder();
		Document doc = parser.build(xmlFile);
		return doc.getRootElement();

	}
}
