001/*
002 * (C) Copyright 2015 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 *     Thomas Roger
018 */
019
020package org.nuxeo.ecm.core.transientstore.work;
021
022import java.io.Serializable;
023import java.util.List;
024import java.util.Map;
025
026import org.nuxeo.ecm.core.api.Blob;
027import org.nuxeo.ecm.core.api.blobholder.BlobHolder;
028import org.nuxeo.ecm.core.api.blobholder.SimpleBlobHolderWithProperties;
029import org.nuxeo.ecm.core.transientstore.api.TransientStore;
030import org.nuxeo.ecm.core.transientstore.api.TransientStoreService;
031import org.nuxeo.ecm.core.work.AbstractWork;
032import org.nuxeo.runtime.api.Framework;
033
034/**
035 * A work allowing to store a result in the {@link TransientStore}.
036 *
037 * @since 7.4
038 */
039public abstract class TransientStoreWork extends AbstractWork {
040
041    private static final long serialVersionUID = 1L;
042
043    public static final String STORE_NAME = "transientStoreWorkCache";
044
045    public static final String KEY_SUFFIX = "_result";
046
047    protected String entryKey;
048
049    /**
050     * Stores the given {@link BlobHolder} as an entry with the given {@code key} in the transient store used by the
051     * {@code TransientStoreWork}.
052     */
053    public static void putBlobHolder(String key, BlobHolder bh) {
054        getStore().putBlobs(key, bh.getBlobs());
055        Map<String, Serializable> properties = bh.getProperties();
056        if (properties != null) {
057            getStore().putParameters(key, properties);
058        }
059    }
060
061    /**
062     * Returns a {@link BlobHolder} representing the entry with the given {@code key} in the transient store used by the
063     * {@code TransientStoreWork} or null if the entry doesn't exist.
064     */
065    public static BlobHolder getBlobHolder(String key) {
066        List<Blob> blobs = getStore().getBlobs(key);
067        Map<String, Serializable> params = getStore().getParameters(key);
068        if (blobs == null && params == null) {
069            return null;
070        }
071        return new SimpleBlobHolderWithProperties(blobs, params);
072    }
073
074    public static void removeBlobHolder(String key) {
075        getStore().remove(key);
076    }
077
078    protected static TransientStore getStore() {
079        TransientStoreService transientStoreService = Framework.getService(TransientStoreService.class);
080        return transientStoreService.getStore(STORE_NAME);
081    }
082
083    public TransientStoreWork() {
084        super();
085        computeEntryKey();
086    }
087
088    public TransientStoreWork(String id) {
089        super(id);
090        computeEntryKey();
091    }
092
093    protected void computeEntryKey() {
094        entryKey = getId() + KEY_SUFFIX;
095    }
096
097    protected void putBlobHolder(BlobHolder bh) {
098        putBlobHolder(entryKey, bh);
099    }
100
101    @Override
102    public String getWorkInstanceResult() {
103        return entryKey;
104    }
105}