/*
 * Copyright (c) 2006-2012 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:
 */
package org.nuxeo.ecm.automation.core.operations.blob;

import java.util.List;

import org.nuxeo.ecm.automation.core.Constants;
import org.nuxeo.ecm.automation.core.annotations.Operation;
import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
import org.nuxeo.ecm.automation.core.annotations.Param;
import org.nuxeo.ecm.automation.core.collectors.BlobListCollector;
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.impl.blob.StringBlob;
import org.nuxeo.ecm.core.api.model.Property;
import org.nuxeo.ecm.core.api.model.impl.ListProperty;

/**
 * Get document blobs inside the file:content and files:files properties
 */
@Operation(id = GetAllDocumentBlobs.ID, category = Constants.CAT_BLOB, label = "Get All Document Files", description = "Gets a list of main file and files that are attached on the input document. The files location should be specified using the blob list properties xpath. Returns a list of files.")
public class GetAllDocumentBlobs {

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

    @Param(name = "xpathMainFile", required = false, values = "file:content")
    protected String xpathMainFile = "file:content";

    @Param(name = "xpathFiles", required = false, values = "files:files")
    protected String xpathFiles = "files:files";

    @OperationMethod(collector = BlobListCollector.class)
    public BlobList run(DocumentModel doc) throws Exception {
        BlobList blobs = new BlobList();
        Blob mainFile = null;
        if (doc.hasSchema("file")) {
            mainFile = (Blob) doc.getPropertyValue(xpathMainFile);
        }
        ListProperty listFiles = null;
        if (doc.hasSchema("files")) {
            listFiles = (ListProperty) doc.getProperty(xpathFiles);
        }
        if (mainFile == null) {
            BlobHolder bh = doc.getAdapter(BlobHolder.class);
            if (bh != null) {
                mainFile = bh.getBlob();
            }
            if (mainFile == null) {
                mainFile = new StringBlob("");
                mainFile.setMimeType("text/plain");
                mainFile.setFilename(doc.getName() + ".null");
            }
        }
        // Add main file to the blob list
        blobs.add(mainFile);
        if (listFiles == null) {
            BlobHolder bh = doc.getAdapter(BlobHolder.class);
            if (bh != null) {
                List<Blob> docBlobs = bh.getBlobs();
                if (docBlobs != null) {
                    for (Blob blob : docBlobs) {
                        blobs.add(blob);
                    }
                }
            }
            return blobs;
        }
        // Add attached files to the blob list
        for (Property p : listFiles) {
            blobs.add((Blob) p.getValue("file"));
        }
        return blobs;
    }
}

@@ -333,16 +333,76 @@
 butors:%0A
+ * %3Ca href=mailto:vpasquier@nuxeo.com%3EVladimir Pasquier%3C/a%3E%0A
  */%0Apack
@@ -655,64 +655,8 @@
 od;%0A
-import org.nuxeo.ecm.automation.core.annotations.Param;%0A
 impo
@@ -907,161 +907,8 @@
 der;
-%0Aimport org.nuxeo.ecm.core.api.impl.blob.StringBlob;%0Aimport org.nuxeo.ecm.core.api.model.Property;%0Aimport org.nuxeo.ecm.core.api.model.impl.ListProperty;
 %0A%0A/*
@@ -916,16 +916,20 @@
 %0A * Get 
+all 
 document
@@ -938,59 +938,8 @@
 lobs
- inside the file:content and files:files properties
 %0A */
@@ -1078,26 +1078,16 @@
  of 
-main file and file
+all blob
 s th
@@ -1128,85 +1128,8 @@
 ent.
- The files location should be specified using the blob list properties xpath.
  Ret
@@ -1249,755 +1249,164 @@
    @
-Param(name = %22xpathMainFile%22, required = false, values = %22file:content%22)%0A    protected String xpathMainFile = %22file:content%22;%0A%0A    @Param(name = %22xpathFiles%22, required = false, values = %22files:files%22)%0A    protected String xpathFiles = %22files:files%22;%0A%0A    @OperationMethod(collector = BlobListCollector.class)%0A    public BlobList run(DocumentModel doc) throws Exception %7B%0A        BlobList blobs = new BlobList();%0A        Blob mainFile = null;%0A        if (doc.hasSchema(%22file%22)) %7B%0A            mainFile = (Blob) doc.getPropertyValue(xpathMainFile);%0A        %7D%0A        ListProperty listFiles = null;%0A        if (doc.hasSchema(%22files%22)) %7B%0A            listFiles = (ListProperty) doc.getProperty(xpathFiles);%0A        %7D%0A        if (mainFile == null) %7B%0A    
+OperationMethod(collector = BlobListCollector.class)%0A    public BlobList run(DocumentModel doc) throws Exception %7B%0A        BlobList blobs = new BlobList();%0A
     
@@ -1459,36 +1459,32 @@
 class);%0A        
-    
 if (bh != null) 
@@ -1501,558 +1501,100 @@
     
-    mainFile = bh.getBlob();%0A            %7D%0A            if (mainFile == null) %7B%0A                mainFile = new StringBlob(%22%22);%0A                mainFile.setMimeType(%22text/plain%22);%0A                mainFile.setFilename(doc.getName() + %22.null%22);%0A            %7D%0A        %7D%0A        // Add main file to the blob list%0A        blobs.add(mainFile);%0A        if (listFiles == null) %7B%0A            BlobHolder bh = doc.getAdapter(BlobHolder.class);%0A            if (bh != null) %7B%0A                List%3CBlob%3E docBlobs = bh.getBlobs();%0A                if (docBlobs != null
+List%3CBlob%3E docBlobs = bh.getBlobs();%0A            if (docBlobs != null && !docBlobs.isEmpty()
 ) %7B%0A
@@ -1605,28 +1605,24 @@
             
-   
- 
 for (Blob bl
@@ -1650,36 +1650,32 @@
                 
-    
 blobs.add(blob);
@@ -1683,36 +1683,32 @@
                 
- 
-   
 %7D%0A              
@@ -1701,33 +1701,45 @@
                 
-%7D
+return blobs;
 %0A            %7D%0A 
@@ -1728,33 +1728,44 @@
 s;%0A            %7D
-%0A
+ else %7B%0A    
             retu
@@ -1759,37 +1759,36 @@
          return 
-blobs
+null
 ;%0A        %7D%0A    
@@ -1785,177 +1785,65 @@
     
-%7D%0A
     
-    // Add attached files to the blob list%0A        for (Property p : listFiles) %7B%0A            blobs.add((Blob) p.getValue(%22file%22));%0A        %7D%0A        return blobs;
+%7D%0A        %7D else %7B%0A            return null;%0A        %7D
 %0A   

refactor and fix download all blobs document operation