001/*
002 * (C) Copyright 2006-2012 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 * <a href=mailto:vpasquier@nuxeo.com>Vladimir Pasquier</a>
018 */
019package org.nuxeo.ecm.automation.core.operations.blob;
020
021import java.util.List;
022
023import org.nuxeo.ecm.automation.core.Constants;
024import org.nuxeo.ecm.automation.core.annotations.Operation;
025import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
026import org.nuxeo.ecm.automation.core.collectors.BlobListCollector;
027import org.nuxeo.ecm.automation.core.util.BlobList;
028import org.nuxeo.ecm.core.api.Blob;
029import org.nuxeo.ecm.core.api.DocumentModel;
030import org.nuxeo.ecm.core.api.blobholder.BlobHolder;
031
032/**
033 * Get all document blobs
034 */
035@Operation(id = GetAllDocumentBlobs.ID, category = Constants.CAT_BLOB, label = "Get All Document Files", description = "Gets a list of all blobs that are attached on the input document. Returns a list of files.", aliases = { "Blob.GetAll" })
036public class GetAllDocumentBlobs {
037
038    public static final String ID = "Document.GetBlobs";
039
040    @OperationMethod(collector = BlobListCollector.class)
041    public BlobList run(DocumentModel doc) {
042        BlobList blobs = new BlobList();
043        BlobHolder bh = doc.getAdapter(BlobHolder.class);
044        if (bh != null) {
045            List<Blob> docBlobs = bh.getBlobs();
046            if (docBlobs != null && !docBlobs.isEmpty()) {
047                for (Blob blob : docBlobs) {
048                    blobs.add(blob);
049                }
050                return blobs;
051            } else {
052                return null;
053            }
054        } else {
055            return null;
056        }
057    }
058}