001/*
002 * (C) Copyright 2015 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl-2.1.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Thierry Delprat <tdelprat@nuxeo.com>
016 *     Antoine Taillefer <ataillefer@nuxeo.com>
017 */
018
019package org.nuxeo.ecm.automation.server.jaxrs.batch;
020
021import java.io.IOException;
022import java.io.InputStream;
023import java.util.List;
024import java.util.Map;
025
026import org.nuxeo.ecm.core.api.Blob;
027import org.nuxeo.ecm.core.api.CoreSession;
028import org.nuxeo.ecm.core.transientstore.api.TransientStore;
029
030/**
031 * Service interface to collect inputs (Blobs) for an operation or operation chain.
032 *
033 * @since 5.4.2
034 */
035public interface BatchManager {
036
037    /**
038     * Returns the {@link TransientStore} backing the batches.
039     *
040     * @since 7.4
041     */
042    TransientStore getTransientStore();
043
044    /**
045     * Adds an inputStream as a blob to a batch. Will create a new {@link Batch} if needed.
046     * <p>
047     * Streams are persisted as temporary files.
048     */
049    void addStream(String batchId, String idx, InputStream is, String name, String mime) throws IOException;
050
051    /**
052     * Adds an inputStream as a chunk to a batch. Will create a new {@link Batch} if needed.
053     * <p>
054     * Streams are persisted as temporary files.
055     *
056     * @since 7.4
057     */
058    void addStream(String batchId, String idx, InputStream is, int chunkCount, int chunkIdx, String name, String mime,
059            long fileSize) throws IOException;
060
061    /**
062     * Returns true if there is a batch for the given {@code batchId}, false otherwise.
063     *
064     * @since 5.7.2
065     */
066    boolean hasBatch(String batchId);
067
068    /**
069     * Get Blobs associated to a given batch. Returns null if batch does not exist
070     *
071     * @param batchId
072     * @return
073     */
074    List<Blob> getBlobs(String batchId);
075
076    /**
077     * Get Blobs associated to a given batch. Returns null if batch does not exist Wait for upload in progress if needed
078     *
079     * @since 5.7
080     * @param batchId
081     * @return
082     */
083    List<Blob> getBlobs(String batchId, int timeoutS);
084
085    Blob getBlob(String batchId, String fileId);
086
087    Blob getBlob(String batchId, String fileId, int timeoutS);
088
089    /**
090     * @since 7.4
091     */
092    List<BatchFileEntry> getFileEntries(String batchId);
093
094    /**
095     * @since 7.4
096     */
097    BatchFileEntry getFileEntry(String batchId, String fileId);
098
099    /**
100     * Cleanup the temporary storage associated to the batch
101     *
102     * @param batchId
103     */
104    void clean(String batchId);
105
106    /**
107     * Initialize a batch by with an automatically generated id.
108     *
109     * @return the batch id
110     * @since 7.4
111     */
112    String initBatch();
113
114    /**
115     * Initialize a batch with a given batchId and Context Name If batchId is not provided, it will be automatically
116     * generated
117     *
118     * @param batchId
119     * @param contextName
120     * @return the batchId
121     */
122    String initBatch(String batchId, String contextName);
123
124    /**
125     * Executes the chain or operation on the {@code Blobs} from the given {@code batchId}.
126     * <p>
127     * This method does not clean the temporary storage associated to the {@code batchId}.
128     *
129     * @since 5.7
130     */
131    Object execute(String batchId, String chainOrOperationId, CoreSession session, Map<String, Object> contextParams,
132            Map<String, Object> operationParams);
133
134    /**
135     * Executes the chain or operation on the {@code Blob} from the given {@code batchId} and {@code fileIdx}.
136     * <p>
137     * This method does not clean the temporary storage associated to the {@code batchId}.
138     *
139     * @since 5.7.2
140     */
141    Object execute(String batchId, String fileIdx, String chainOrOperationId, CoreSession session,
142            Map<String, Object> contextParams, Map<String, Object> operationParams);
143
144    /**
145     * Executes the chain or operation on the {@code Blobs} from the given {@code batchId}.
146     * <p>
147     * This method clean the temporary storage associated to the {@code batchId} after the execution.
148     *
149     * @since 5.7
150     */
151    Object executeAndClean(String batchId, String chainOrOperationId, CoreSession session,
152            Map<String, Object> contextParams, Map<String, Object> operationParams);
153
154}