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 * <a href="mailto:tdelprat@nuxeo.com">Tiry</a> 018 */ 019package org.nuxeo.ecm.platform.rendition.lazy; 020 021import java.util.ArrayList; 022import java.util.List; 023 024import org.apache.commons.logging.Log; 025import org.apache.commons.logging.LogFactory; 026import org.nuxeo.ecm.core.api.Blob; 027import org.nuxeo.ecm.core.api.CoreSession; 028import org.nuxeo.ecm.core.api.DocumentModel; 029import org.nuxeo.ecm.core.api.DocumentRef; 030import org.nuxeo.ecm.core.api.NuxeoException; 031import org.nuxeo.ecm.core.api.impl.blob.StringBlob; 032import org.nuxeo.ecm.core.transientstore.api.TransientStore; 033import org.nuxeo.ecm.core.transientstore.api.TransientStoreService; 034import org.nuxeo.ecm.core.transientstore.work.TransientStoreWork; 035import org.nuxeo.ecm.platform.rendition.impl.LazyRendition; 036import org.nuxeo.ecm.platform.rendition.service.RenditionDefinition; 037import org.nuxeo.ecm.platform.rendition.service.RenditionService; 038import org.nuxeo.ecm.platform.rendition.service.RenditionServiceImpl; 039import org.nuxeo.runtime.api.Framework; 040 041/** 042 * @author <a href="mailto:tdelprat@nuxeo.com">Tiry</a> 043 * @since 7.2 044 */ 045public abstract class AbstractRenditionBuilderWork extends TransientStoreWork { 046 047 private static final long serialVersionUID = 1L; 048 049 protected final String key; 050 051 protected final DocumentRef docRef; 052 053 protected final String repositoryName; 054 055 protected final String renditionName; 056 057 protected static Log log = LogFactory.getLog(AbstractRenditionBuilderWork.class); 058 059 public AbstractRenditionBuilderWork(String key, DocumentModel doc, RenditionDefinition def) { 060 this.key = key; 061 docRef = doc.getRef(); 062 repositoryName = doc.getRepositoryName(); 063 renditionName = def.getName(); 064 setOriginatingUsername(doc.getCoreSession().getPrincipal().getName()); 065 } 066 067 @Override 068 public String getId() { 069 return "rendition:" + key; 070 } 071 072 @Override 073 public String getTitle() { 074 return "Lazy Rendition for " + renditionName + " on " + docRef.toString(); 075 } 076 077 protected String getTransientStoreName() { 078 return AbstractLazyCachableRenditionProvider.CACHE_NAME; 079 } 080 081 @Override 082 public void work() { 083 openUserSession(); 084 DocumentModel doc = session.getDocument(docRef); 085 086 RenditionService rs = Framework.getService(RenditionService.class); 087 RenditionDefinition def = ((RenditionServiceImpl) rs).getRenditionDefinition(renditionName); 088 089 List<Blob> blobs = doComputeRendition(session, doc, def); 090 doStore(blobs); 091 } 092 093 @Override 094 public void cleanUp(boolean ok, Exception e) { 095 super.cleanUp(ok, e); 096 if (ok) { 097 return; 098 } 099 List<Blob> blobs = new ArrayList<Blob>(); 100 StringBlob emptyBlob = new StringBlob(""); 101 emptyBlob.setFilename("error"); 102 emptyBlob.setMimeType("text/plain;" + LazyRendition.ERROR_MARKER); 103 blobs.add(emptyBlob); 104 doStore(blobs); 105 } 106 107 void doStore(List<Blob> blobs) { 108 TransientStoreService tss = Framework.getService(TransientStoreService.class); 109 TransientStore ts = tss.getStore(getTransientStoreName()); 110 111 if (!ts.exists(key)) { 112 throw new NuxeoException("Rendition TransientStore entry can not be null"); 113 } 114 ts.putBlobs(key, blobs); 115 ts.setCompleted(key, true); 116 } 117 118 /** 119 * Does the actual Rendition Computation : this code will be called from inside an Asynchronous Work 120 * 121 * @param session 122 * @param doc 123 * @param def 124 * @return 125 */ 126 protected abstract List<Blob> doComputeRendition(CoreSession session, DocumentModel doc, RenditionDefinition def); 127 128}