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 org.nuxeo.ecm.core.transientstore.StorageEntryImpl; 021import org.nuxeo.ecm.core.transientstore.api.StorageEntry; 022import org.nuxeo.ecm.core.transientstore.api.TransientStore; 023import org.nuxeo.ecm.core.transientstore.api.TransientStoreService; 024import org.nuxeo.ecm.core.work.AbstractWork; 025import org.nuxeo.runtime.api.Framework; 026 027/** 028 * A work allowing to store a result in the {@link TransientStore}. 029 * 030 * @since 7.4 031 */ 032public abstract class TransientStoreWork extends AbstractWork { 033 034 public static final String STORE_NAME = "transientStoreWorkCache"; 035 036 public static final String KEY_SUFFIX = "_result"; 037 038 protected String entryKey; 039 040 protected transient StorageEntry entry; 041 042 /** 043 * Returns a storage entry given its {@code key} from the transient store used by the {@code TransientStoreWork}. 044 */ 045 public static StorageEntry getStorageEntry(String key) { 046 TransientStore store = getStore(); 047 return store.get(key); 048 } 049 050 /** 051 * Remove a storage entry given its {@code key} from the transient store used by the {@code TransientStoreWork}. 052 */ 053 public static void removeStorageEntry(String key) { 054 getStore().remove(key); 055 } 056 057 protected static void saveStorageEntry(StorageEntry storageEntry) { 058 getStore().put(storageEntry); 059 } 060 061 protected static TransientStore getStore() { 062 TransientStoreService transientStoreService = Framework.getService(TransientStoreService.class); 063 return transientStoreService.getStore(STORE_NAME); 064 } 065 066 public TransientStoreWork() { 067 super(); 068 computeEntryKey(); 069 } 070 071 public TransientStoreWork(String id) { 072 super(id); 073 computeEntryKey(); 074 } 075 076 protected void computeEntryKey() { 077 entryKey = getId() + KEY_SUFFIX; 078 } 079 080 protected StorageEntry getStorageEntry() { 081 if (entry == null) { 082 entry = getStorageEntry(entryKey); 083 if (entry == null) { 084 entry = new StorageEntryImpl(entryKey); 085 } 086 } 087 return entry; 088 } 089 090 protected void saveStorageEntry() { 091 if (entry != null) { 092 saveStorageEntry(entry); 093 } 094 } 095 096 @Override 097 public String getWorkInstanceResult() { 098 return entryKey; 099 } 100}