package cz.hobrasoft.pdfmu.operation;

import cz.hobrasoft.pdfmu.PdfmuError;
import cz.hobrasoft.pdfmu.jackson.RpcError;
import cz.hobrasoft.pdfmu.jackson.RpcError.Data;
import java.util.SortedMap;
import org.apache.commons.lang3.text.StrSubstitutor;

/**
 * This exception is thrown by {@link Operation#execute(Namespace)} to notify
 * {@link cz.hobrasoft.pdfmu.Main#main(String[])} that the operation has
 * encountered a condition it cannot recover from.
 *
 * @author <a href="mailto:filip.bartek@hobrasoft.cz">Filip Bartek</a>
 */
public class OperationException extends Exception {

    // Configuration
    private static final int defaultErrorCode = -1;

    // TODO: Remove
    /**
     * Constructs an instance of <code>OperationException</code> with the
     * specified detail message.
     *
     * @param msg the detail message.
     */
    public OperationException(String msg) {
        super(msg);
    }

    // TODO: Remove
    /**
     * Constructs a chained exception with the specified detail message.
     *
     * @param msg the detail message.
     * @param cause the original cause.
     */
    public OperationException(String msg, Throwable cause) {
        super(msg, cause);
    }

    private PdfmuError e = null;
    private SortedMap<String, Object> messageArguments = null;

    public OperationException(PdfmuError e, Throwable cause) {
        this(e, cause, null);
    }

    /**
     * Creates a chained operation exception with error identifier and message
     * arguments.
     *
     * @param e the error identifier.
     * @param cause the original cause.
     * @param messageArguments the arguments of the message.
     */
    public OperationException(PdfmuError e, Throwable cause, SortedMap<String, Object> messageArguments) {
        super(e.toString(), cause);
        assert e != null;
        this.e = e;
        this.messageArguments = messageArguments;
    }

    /**
     * Returns the error code associated with this exception
     *
     * <p>
     * The code should uniquely identify the error that caused the exception.
     *
     * @return the error code associated with this exception
     */
    public int getCode() {
        if (e != null) {
            return e.getCode();
        } else {
            return defaultErrorCode;
        }
    }

    @Override
    public String getLocalizedMessage() {
        if (e != null) {
            String pattern = e.getMessagePattern();
            if (pattern != null) {
                StrSubstitutor sub = new StrSubstitutor(messageArguments);
                return sub.replace(pattern);
            } else {
                return super.getLocalizedMessage();
            }
        } else {
            return super.getLocalizedMessage();
        }
    }

    private RpcError getRpcError() {
        RpcError re = new RpcError(getCode(), getLocalizedMessage());
        Throwable cause = getCause();
        if (cause != null || messageArguments != null) {
            re.data = new Data();
            if (cause != null) {
                re.data.causeMessage = cause.getLocalizedMessage();
            }
            re.data.arguments = messageArguments;
        }
        return re;
    }
}

@@ -65,32 +65,74 @@
 mu.PdfmuError;%0D%0A
+import cz.hobrasoft.pdfmu.WritingMapper;%0D%0A
 import cz.hobras
@@ -221,27 +221,138 @@
 ort 
-java.util.SortedMap
+cz.hobrasoft.pdfmu.jackson.RpcResponse;%0D%0Aimport java.io.IOException;%0D%0Aimport java.util.SortedMap;%0D%0Aimport java.util.logging.Logger
 ;%0D%0Ai
@@ -748,24 +748,122 @@
 eption %7B%0D%0A%0D%0A
+    private static final Logger logger = Logger.getLogger(OperationException.class.getName());%0D%0A%0D%0A
     // Confi
@@ -3552,12 +3552,305 @@
 %0D%0A    %7D%0D
+%0A%0D%0A    public void writeInWritingMapper(WritingMapper wm) %7B%0D%0A        RpcResponse response = new RpcResponse(getRpcError());%0D%0A%0D%0A        try %7B%0D%0A            wm.writeValue(response);%0D%0A        %7D catch (IOException ex) %7B%0D%0A            logger.severe(%22Could not write JSON output.%22);%0D%0A        %7D%0D%0A    %7D%0D
 %0A%7D%0D%0A

Add method writeInWritingMapper