/*
 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     bstefanescu
 */
package org.nuxeo.ecm.automation.core.operations.blob;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;

import org.nuxeo.ecm.automation.core.Constants;
import org.nuxeo.ecm.automation.core.annotations.Context;
import org.nuxeo.ecm.automation.core.annotations.Operation;
import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
import org.nuxeo.ecm.automation.core.util.BlobList;
import org.nuxeo.ecm.core.api.Blob;
import org.nuxeo.ecm.core.api.DocumentModel;
import org.nuxeo.ecm.core.api.blobholder.BlobHolder;
import org.nuxeo.ecm.core.api.blobholder.SimpleBlobHolder;
import org.nuxeo.ecm.core.convert.api.ConversionService;

/**
 * Save the input document
 * 
 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
 * @author tiry
 */
@Operation(id = BlobToPDF.ID, category = Constants.CAT_CONVERSION, label = "Convert To PDF", description = "Convert the input file to a PDF and return the new file.")
public class BlobToPDF {

    public static final String ID = "Blob.ToPDF";

    @Context
    protected ConversionService service;

    @OperationMethod
    public Blob run(DocumentModel doc) throws Exception {
        BlobHolder bh = doc.getAdapter(BlobHolder.class);
        if (bh == null) {
            return null;
        }
        BlobHolder pdfBh = service.convertToMimeType("application/pdf", bh,
                new HashMap<String, Serializable>());
        Blob result = pdfBh.getBlob();

        String fname = result.getFilename();
        if (fname == null || fname.isEmpty()) {
            fname = bh.getBlob().getFilename();
            fname = fname + ".pdf";
            result.setFilename(fname);
        }
        result.setMimeType("application/pdf");
        return result;
    }

    @OperationMethod
    public Blob run(Blob blob) throws Exception {
        BlobHolder bh = new SimpleBlobHolder(blob);
        bh = service.convertToMimeType("application/pdf", bh,
                new HashMap<String, Serializable>());
        Blob result = bh.getBlob();
        adjustBlobName(blob, result);
        return result;
    }

    @OperationMethod
    public BlobList run(BlobList blobs) throws Exception {
        BlobHolder bh = new SimpleBlobHolder(blobs);
        bh = service.convertToMimeType("application/pdf", bh,
                new HashMap<String, Serializable>());
        // TODO optimize this
        List<Blob> result = bh.getBlobs();
        for (int i = 0, size = result.size(); i < size; i++) {
            adjustBlobName(blobs.get(i), result.get(i));
        }
        return new BlobList(result);
    }

    protected void adjustBlobName(Blob in, Blob out) {
        String fname = in.getFilename();
        if (fname == null) {
            fname = "Unknown_" + System.identityHashCode(in);
        }
        out.setFilename(fname + ".pdf");
        out.setMimeType("application/pdf");
    }

}

@@ -1868,30 +1868,91 @@
     
-if (f
+String filename = bh.getBlob().getFilename();%0A        if (file
 name 
-=
+!
 = null 
-%7C%7C f
+&& !file
 name
@@ -1981,30 +1981,226 @@
     
-fname = bh.getBlob().g
+// add pdf extension%0A            int pos = filename.lastIndexOf('.');%0A            if (pos %3E 0) %7B%0A                filename = filename.substring(0, pos);%0A            %7D%0A            filename += %22.pdf%22;%0A            result.s
 etFi
@@ -2198,32 +2198,40 @@
 ult.setFilename(
+filename
 );%0A            f
@@ -2229,35 +2229,55 @@
     
-    
+%7D else if (
 fname 
+!
 = 
-fname + %22.pdf%22;
+null && !fname.isEmpty()) %7B
 %0A   
@@ -2313,32 +2313,90 @@
 name);%0A        %7D
+ else %7B%0A            result.setFilename(%22file%22);%0A        %7D%0A
 %0A        result.

fix filename for PDF generated by Blob.ToPDF