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 /** 075 * Returns true if a {@link BlobHolder} is stored for the given {@code key}. 076 * @since 8.3 077 */ 078 public static boolean containsBlobHolder(String key) { 079 return getStore().exists(key); 080 } 081 082 public static void removeBlobHolder(String key) { 083 getStore().remove(key); 084 } 085 086 protected static TransientStore getStore() { 087 TransientStoreService transientStoreService = Framework.getService(TransientStoreService.class); 088 return transientStoreService.getStore(STORE_NAME); 089 } 090 091 public TransientStoreWork() { 092 super(); 093 computeEntryKey(); 094 } 095 096 public TransientStoreWork(String id) { 097 super(id); 098 computeEntryKey(); 099 } 100 101 protected void computeEntryKey() { 102 entryKey = getId() + KEY_SUFFIX; 103 } 104 105 protected void putBlobHolder(BlobHolder bh) { 106 putBlobHolder(entryKey, bh); 107 } 108 109 public String getEntryKey() { 110 return entryKey; 111 } 112}