/**
 * Copyright (c) 2009-2012 EBM WebSourcing, 2012-2013 Linagora
 * 
 * This program/library is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 2.1 of the License, or (at your
 * option) any later version.
 * 
 * This program/library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
 * for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program/library; If not, see <http://www.gnu.org/licenses/>
 * for the GNU Lesser General Public License version 2.1.
 */
package org.ow2.petals.bc.sftp;

import java.util.Arrays;
import java.util.List;

import javax.jbi.messaging.Fault;
import javax.jbi.messaging.MessagingException;
import javax.xml.namespace.QName;

import org.ow2.petals.component.framework.api.Message.MEPConstants;
import org.ow2.petals.component.framework.api.exception.PEtALSCDKException;
import org.ow2.petals.component.framework.api.message.Exchange;
import org.ow2.petals.component.framework.util.MtomUtil;
import org.ow2.petals.component.framework.util.SourceUtil;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import com.ebmwebsourcing.easycommons.xml.DocumentBuilders;

/**
 * This class intends to provide some useful methods like a message exchange fault setter.
 * 
 * @author Charles Casadei - EBM WebSourcing
 */
public class SFTPUtil {

    /**
     * Name of the XML tag used to represent a filename.
     */
    private static final String FILENAME = "filename";

    /**
     * Name of the XML tag used to represent an IO fault.
     */
    private static final String IO_FAULT = "ioFault";

    /**
     * Name of the XML tag used to represent a configuration fault.
     */
    private static final String CONFIGURATION_FAULT = "configurationFault";

    /**
     * Generates an XML payload from an operation name an a list of filenames.
     * 
     * @param names
     *            filenames which will be contained in the XML payload
     * @param operation
     *            the operation which requested to build a filelist
     * @return an XML payload containing a list of filenames
     */
    public static String generateFileNameList(final List<String> names, final QName operation) {
        final StringBuffer result = new StringBuffer();
        result.append("<tns:" + operation.getLocalPart() + "Response xmlns:tns='");
        result.append(operation.getNamespaceURI());
        result.append("'>");
        for (final String string : names) {
            result.append("<tns:" + FILENAME + ">");
            result.append(string);
            result.append("</tns:" + FILENAME + ">");
        }
        result.append("</tns:" + operation.getLocalPart() + "Response>");

        return result.toString();
    }

    /**
     * Generate a MTOM response for a list of files.
     * 
     * @param names
     * @param operation
     * @return
     */
    public static Document generateMTOMListResponse(final List<String> names, final QName operation) {
        Document doc = DocumentBuilders.newDocument();
        
        Element root = doc.createElementNS(operation.getNamespaceURI(),
                "tns:" + operation.getLocalPart() + "Response");
        root.appendChild(MtomUtil.generateMtomStructure(doc, names,
                new QName(operation.getNamespaceURI(), "attachments", "tns"),
                new QName(operation.getNamespaceURI(), "filename", "tns")));
        doc.appendChild(root);
        return doc;
    }

    /***
     * Generate a MTOM response for a single file.
     * 
     * @param names
     * @param operation
     * @return
     */
    public static Document generateMTOMResponse(final String names, final QName operation) {
        Document doc = DocumentBuilders.newDocument();
        
        Element root = doc.createElementNS(operation.getNamespaceURI(),
                "tns:" + operation.getLocalPart() + "Response");
        root.appendChild(MtomUtil.generateMtomStructure(doc, Arrays.asList(names), new QName(
                operation.getNamespaceURI(), "attachment", "tns"),
                new QName(operation.getNamespaceURI(), "filename", "tns")));
        doc.appendChild(root);
        
        return doc;
    }

    /**
     * Sets an IOFault on the given message exchange.
     * 
     * @param exchange
     * @param ioMessage
     * @throws javax.jbi.messaging.MessagingException
     */
    public static void setIOFaultOnExchange(final Exchange exchange, final String ioMessage)
            throws MessagingException {
        final StringBuffer faultMessage = new StringBuffer();
        faultMessage.append("<tns:" + IO_FAULT + " xmlns:tns='");
        faultMessage.append(exchange.getOperation().getNamespaceURI());
        faultMessage.append("'><tns:message>");
        faultMessage.append(ioMessage);
        faultMessage.append("</tns:message></tns:" + IO_FAULT + ">");
        final Fault fault = exchange.createFault();
        try {
            fault.setContent(SourceUtil.createSource(faultMessage.toString()));
            exchange.setFault(fault);
        } catch (final PEtALSCDKException e) {
            throw new MessagingException(e.getMessage());
        }
    }

    /**
     * Sets a ConfigurationFault on the given message exchange.
     * 
     * @param exchange
     * @param configurationError
     * @throws javax.jbi.messaging.MessagingException
     */
    public static void setConfigurationFaultOnExchange(final Exchange exchange,
            final String configurationError) throws MessagingException {
        final StringBuffer faultMessage = new StringBuffer();
        faultMessage.append("<tns:" + CONFIGURATION_FAULT + " xmlns:tns='");
        faultMessage.append(exchange.getOperation().getNamespaceURI());
        faultMessage.append("'><tns:element>");
        faultMessage.append(configurationError);
        faultMessage.append("</tns:element></tns:" + CONFIGURATION_FAULT + ">");
        final Fault fault = exchange.createFault();
        try {
            fault.setContent(SourceUtil.createSource(faultMessage.toString()));
            exchange.setFault(fault);
        } catch (final PEtALSCDKException e) {
            throw new MessagingException(e.getMessage());
        }
    }

    /**
     * Checks if the MEP of the given message exchange is valid against the
     * given configuration.
     * 
     * @param exchange
     *            a message exchange
     * @param isInOnly
     *            if the InOnly MEP is supported
     * @param isRobustInOnly
     *            if the RobustInOnly MEP is supported
     * @param isInOptionalOut
     *            if the InOptionalOut MEP is supported
     * @param isInOut
     *            if the InOut MEP is supported
     * @return true if the MEP is valid against the given configuration
     */
    public static final boolean checkMEP(final Exchange exchange, final boolean isInOnly,
            final boolean isRobustInOnly, final boolean isInOptionalOut, final boolean isInOut) {

        // If the MEP InOnly is accepted
        if (isInOnly && exchange.isInOnlyPattern()) {
            return true;
        }

        // If tge MEP RobustInOnly is accepted
        else if (isRobustInOnly && exchange.isRobustInOnlyPattern()) {
            return true;
        }

        // If the MEP InOptionalOut is accepted
        else if (isInOptionalOut && exchange.isInOptionalOutPattern()) {
            return true;
        }

        // If the MEP InOut is accepted
        else if (isInOnly && exchange.isInOutPattern()) {
            return true;
        }

        return false;
    }

    /**
     * Add a root element to the given document.
     * 
     * @param document
     * @param rootElement
     */
    public final static void addRootElement(Document document, QName rootElement) {
        final Element rootElt = document.createElementNS(rootElement.getNamespaceURI(),
                rootElement.getLocalPart());
        rootElt.appendChild(document.getDocumentElement());
        document.appendChild(rootElt);
    }

    /**
     * Build a message to indicate the mep available for this operation. TODO :
     * Move it in the cdk api.
     * 
     * @param operation
     * @param mep
     * @return
     */
    public static String getValidMepMessageForOperation(final String operation, MEPConstants... mep) {
        StringBuilder builder = new StringBuilder();
        builder.append("Exchange pattern for the " + operation.toUpperCase()
                + " operation must be ");
        for (int i = 0; i < mep.length; i++) {
            if (i > 0)
                builder.append(" or ");
            builder.append(mep[i]);
        }
        return builder.toString();
    }
}
