001/* 002 * (C) Copyright 2015 Nuxeo SA (http://nuxeo.com/) and contributors. 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 * <a href="mailto:tdelprat@nuxeo.com">Tiry</a> 016 */ 017package org.nuxeo.ecm.platform.rendition.lazy; 018 019import java.io.IOException; 020import java.io.Serializable; 021import java.util.List; 022 023import org.apache.commons.logging.Log; 024import org.apache.commons.logging.LogFactory; 025import org.nuxeo.ecm.core.api.Blob; 026import org.nuxeo.ecm.core.api.CoreSession; 027import org.nuxeo.ecm.core.api.DocumentModel; 028import org.nuxeo.ecm.core.api.DocumentRef; 029import org.nuxeo.ecm.core.api.NuxeoException; 030import org.nuxeo.ecm.core.transientstore.api.StorageEntry; 031import org.nuxeo.ecm.core.transientstore.api.TransientStore; 032import org.nuxeo.ecm.core.transientstore.api.TransientStoreService; 033import org.nuxeo.ecm.core.work.AbstractWork; 034import org.nuxeo.ecm.core.work.api.Work; 035import org.nuxeo.ecm.platform.rendition.service.RenditionDefinition; 036import org.nuxeo.ecm.platform.rendition.service.RenditionService; 037import org.nuxeo.ecm.platform.rendition.service.RenditionServiceImpl; 038import org.nuxeo.runtime.api.Framework; 039 040/** 041 * @author <a href="mailto:tdelprat@nuxeo.com">Tiry</a> 042 * @since 7.2 043 */ 044public abstract class AbstractRenditionBuilderWork extends AbstractWork implements Work, Serializable { 045 046 private static final long serialVersionUID = 1L; 047 048 protected final String key; 049 050 protected final DocumentRef docRef; 051 052 protected final String repositoryName; 053 054 protected final String renditionName; 055 056 protected static Log log = LogFactory.getLog(AbstractRenditionBuilderWork.class); 057 058 public AbstractRenditionBuilderWork(String key, DocumentModel doc, RenditionDefinition def) { 059 this.key = key; 060 docRef = doc.getRef(); 061 repositoryName = doc.getRepositoryName(); 062 renditionName = def.getName(); 063 } 064 065 @Override 066 public String getId() { 067 return "rendition:" + key; 068 } 069 070 @Override 071 public String getTitle() { 072 return "Lazy Rendition for " + renditionName + " on " + docRef.toString(); 073 } 074 075 protected String getTransientStoreName() { 076 return AbstractLazyCachableRenditionProvider.CACHE_NAME; 077 } 078 079 @Override 080 public void work() { 081 initSession(); 082 DocumentModel doc = session.getDocument(docRef); 083 084 RenditionService rs = Framework.getService(RenditionService.class); 085 RenditionDefinition def = ((RenditionServiceImpl)rs).getRenditionDefinition(renditionName); 086 087 List<Blob> blobs = doComputeRendition(session, doc, def); 088 089 TransientStoreService tss = Framework.getService(TransientStoreService.class); 090 TransientStore ts = tss.getStore(getTransientStoreName()); 091 092 StorageEntry entry = ts.get(key); 093 if (entry == null) { 094 throw new NuxeoException("Rendition TransientStore entry can not be null"); 095 } 096 entry.setBlobs(blobs); 097 entry.put(AbstractLazyCachableRenditionProvider.COMPLETED_KEY, true); 098 ts.put(entry); 099 } 100 101 /** 102 * Does the actual Rendition Computation : this code will be called from inside an Asynchronous Work 103 * 104 * @param session 105 * @param doc 106 * @param def 107 * @return 108 */ 109 protected abstract List<Blob> doComputeRendition(CoreSession session, DocumentModel doc, RenditionDefinition def); 110 111}