001/*
002 * (C) Copyright 2017 Nuxeo (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 *     Guillaume Renard <grenard@nuxeo.com>
018 */
019package org.nuxeo.ecm.automation.core.operations.blob;
020
021import java.util.Collections;
022import java.util.List;
023import java.util.UUID;
024import java.util.stream.Collectors;
025
026import org.nuxeo.ecm.automation.core.Constants;
027import org.nuxeo.ecm.automation.core.annotations.Context;
028import org.nuxeo.ecm.automation.core.annotations.Operation;
029import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
030import org.nuxeo.ecm.automation.core.annotations.Param;
031import org.nuxeo.ecm.automation.core.work.BlobListZipWork;
032import org.nuxeo.ecm.core.api.Blob;
033import org.nuxeo.ecm.core.api.CoreSession;
034import org.nuxeo.ecm.core.api.DocumentModel;
035import org.nuxeo.ecm.core.api.DocumentModelList;
036import org.nuxeo.ecm.core.api.NuxeoException;
037import org.nuxeo.ecm.core.api.impl.blob.AsyncBlob;
038import org.nuxeo.ecm.core.io.download.DownloadService;
039import org.nuxeo.ecm.core.transientstore.api.TransientStore;
040import org.nuxeo.ecm.core.transientstore.api.TransientStoreService;
041import org.nuxeo.ecm.core.work.api.Work;
042import org.nuxeo.ecm.core.work.api.WorkManager;
043import org.nuxeo.ecm.core.work.api.WorkManager.Scheduling;
044import org.nuxeo.runtime.api.Framework;
045
046/**
047 * Asynchronous Bulk Download Operation.
048 *
049 * @since 9.3
050 */
051@Operation(id = BulkDownload.ID, category = Constants.CAT_BLOB, label = "Bulk Download", description = "Prepare a Zip of a list of documents which is build asynchrously. Produced Zip will be available in the TransientStore with the key returned by the JSON.")
052public class BulkDownload {
053
054    public static final String ID = "Blob.BulkDownload";
055
056    @Context
057    protected CoreSession session;
058
059    @Param(name = "filename", required = false)
060    protected String fileName;
061
062    @OperationMethod
063    public Blob run(DocumentModelList docs) {
064        TransientStoreService tss = Framework.getService(TransientStoreService.class);
065        TransientStore ts = tss.getStore(DownloadService.TRANSIENT_STORE_STORE_NAME);
066        if (ts == null) {
067            throw new NuxeoException("Unable to find download Transient Store");
068        }
069        String key = UUID.randomUUID().toString();
070        List<String> docIds = docs.stream().map(DocumentModel::getId).collect(Collectors.toList());
071        Work work = new BlobListZipWork(key, session.getPrincipal().getName(), fileName, docIds,
072                DownloadService.TRANSIENT_STORE_STORE_NAME);
073        ts.setCompleted(key, false);
074        Blob blob = new AsyncBlob(key);
075        ts.putBlobs(key, Collections.singletonList(blob));
076        Framework.getService(WorkManager.class).schedule(work, Scheduling.IF_NOT_SCHEDULED);
077        return blob;
078    }
079
080}