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