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     * @since 8.4
051     */
052    public static String computeEntryKey(String id) {
053        return id + KEY_SUFFIX;
054    }
055
056    /**
057     * Stores the given {@link BlobHolder} as an entry with the given {@code key} in the transient store used by the
058     * {@code TransientStoreWork}.
059     */
060    public static void putBlobHolder(String key, BlobHolder bh) {
061        getStore().putBlobs(key, bh.getBlobs());
062        Map<String, Serializable> properties = bh.getProperties();
063        if (properties != null) {
064            getStore().putParameters(key, properties);
065        }
066    }
067
068    /**
069     * Returns a {@link BlobHolder} representing the entry with the given {@code key} in the transient store used by the
070     * {@code TransientStoreWork} or null if the entry doesn't exist.
071     */
072    public static BlobHolder getBlobHolder(String key) {
073        List<Blob> blobs = getStore().getBlobs(key);
074        Map<String, Serializable> params = getStore().getParameters(key);
075        if (blobs == null && params == null) {
076            return null;
077        }
078        return new SimpleBlobHolderWithProperties(blobs, params);
079    }
080
081    /**
082     * Returns true if a {@link BlobHolder} is stored for the given {@code key}.
083     * @since 8.3
084     */
085    public static boolean containsBlobHolder(String key) {
086        return getStore().exists(key);
087    }
088
089    public static void removeBlobHolder(String key) {
090        getStore().remove(key);
091    }
092
093    protected static TransientStore getStore() {
094        TransientStoreService transientStoreService = Framework.getService(TransientStoreService.class);
095        return transientStoreService.getStore(STORE_NAME);
096    }
097
098    public TransientStoreWork() {
099        computeEntryKey();
100    }
101
102    public TransientStoreWork(String id) {
103        super(id);
104        computeEntryKey();
105    }
106
107    protected void computeEntryKey() {
108        entryKey = computeEntryKey(getId());
109    }
110
111    protected void putBlobHolder(BlobHolder bh) {
112        putBlobHolder(entryKey, bh);
113    }
114
115    public String getEntryKey() {
116        return entryKey;
117    }
118
119    @Override
120    public boolean isIdempotent() {
121        return false;
122    }
123
124}