001/*
002 * (C) Copyright 2018 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 *     pierre
018 */
019package org.nuxeo.ecm.core.bulk.action.computation;
020
021import java.io.IOException;
022import java.nio.file.Files;
023import java.nio.file.Path;
024import java.util.Collections;
025import java.util.List;
026
027import org.apache.logging.log4j.LogManager;
028import org.apache.logging.log4j.Logger;
029import org.nuxeo.ecm.core.api.Blob;
030import org.nuxeo.ecm.core.transientstore.api.TransientStore;
031import org.nuxeo.ecm.core.transientstore.api.TransientStoreService;
032import org.nuxeo.lib.stream.computation.AbstractComputation;
033import org.nuxeo.lib.stream.computation.ComputationContext;
034import org.nuxeo.lib.stream.log.Name;
035import org.nuxeo.runtime.api.Framework;
036
037/**
038 * @since 10.3
039 */
040public abstract class AbstractTransientBlobComputation extends AbstractComputation {
041
042    private static final Logger log = LogManager.getLogger(AbstractTransientBlobComputation.class);
043
044    protected Path temp;
045
046    protected String id;
047
048    @Override
049    public void init(ComputationContext context) {
050        super.init(context);
051        id = Name.idOfUrn(metadata.name());
052        try {
053            temp = Files.createTempDirectory(id);
054            Framework.trackFile(temp.toFile(), temp);
055        } catch (IOException e) {
056            throw new IllegalStateException("Cannot create temp directory for " + this);
057        }
058    }
059
060    public AbstractTransientBlobComputation(String name) {
061        super(name, 1, 1);
062    }
063
064    public AbstractTransientBlobComputation(String name, int nbOutputStreams) {
065        super(name, 1, nbOutputStreams);
066    }
067
068
069    protected String getTransientStoreKey(String commandId) {
070        return id + commandId;
071    }
072
073    public Blob getBlob(String key, String storeName) {
074        TransientStore store = Framework.getService(TransientStoreService.class).getStore(storeName);
075        List<Blob> blobs = store.getBlobs(key);
076        Blob blob = blobs == null || blobs.isEmpty() ? null : blobs.get(0);
077        if (blob == null) {
078            log.error("[{}] Could not retrieve blob for key {}", id, key);
079        }
080        return blob;
081    }
082
083    protected void storeBlob(Blob blob, String commandId, String storeName) {
084        TransientStore store = Framework.getService(TransientStoreService.class).getStore(storeName);
085        store.putBlobs(getTransientStoreKey(commandId), Collections.singletonList(blob));
086        store.setCompleted(getTransientStoreKey(commandId), true);
087    }
088
089    protected Path createTemp(String commandId) {
090        return temp.resolve(commandId + ".csv");
091    }
092}