001/*
002 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     bstefanescu
011 */
012package org.nuxeo.ecm.automation.core.operations.blob;
013
014import java.util.List;
015
016import org.nuxeo.ecm.automation.core.Constants;
017import org.nuxeo.ecm.automation.core.annotations.Operation;
018import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
019import org.nuxeo.ecm.automation.core.annotations.Param;
020import org.nuxeo.ecm.automation.core.collectors.BlobListCollector;
021import org.nuxeo.ecm.automation.core.util.BlobList;
022import org.nuxeo.ecm.core.api.Blob;
023import org.nuxeo.ecm.core.api.DocumentModel;
024import org.nuxeo.ecm.core.api.blobholder.BlobHolder;
025import org.nuxeo.ecm.core.api.model.Property;
026import org.nuxeo.ecm.core.api.model.impl.ListProperty;
027
028/**
029 * Get document blobs inside the files:files property
030 *
031 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
032 * @author tiry
033 */
034@Operation(id = GetDocumentBlobs.ID, category = Constants.CAT_BLOB, label = "Get Document Files", description = "Gets a list of files that are attached on the input document. The files location should be specified using the blob list property xpath. Returns a list of files.", aliases = { "Blob.GetList" })
035public class GetDocumentBlobs {
036
037    public static final String ID = "Document.GetBlobsByProperty";
038
039    @Param(name = "xpath", required = false, values = "files:files")
040    protected String xpath = "files:files";
041
042    @OperationMethod(collector = BlobListCollector.class)
043    public BlobList run(DocumentModel doc) {
044        BlobList blobs = new BlobList();
045        ListProperty list = (ListProperty) doc.getProperty(xpath);
046        if (list == null) {
047            BlobHolder bh = doc.getAdapter(BlobHolder.class);
048            if (bh != null) {
049                List<Blob> docBlobs = bh.getBlobs();
050                if (docBlobs != null) {
051                    for (Blob blob : docBlobs) {
052                        blobs.add(blob);
053                    }
054                }
055            }
056            return blobs;
057        }
058        for (Property p : list) {
059            blobs.add((Blob) p.getValue("file"));
060        }
061        return blobs;
062    }
063
064}