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