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.io.Serializable;
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.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        setOriginatingUsername(doc.getCoreSession().getPrincipal().getName());
064    }
065
066    @Override
067    public String getId() {
068        return "rendition:" + key;
069    }
070
071    @Override
072    public String getTitle() {
073        return "Lazy Rendition for " + renditionName + " on " + docRef.toString();
074    }
075
076    protected String getTransientStoreName() {
077        return AbstractLazyCachableRenditionProvider.CACHE_NAME;
078    }
079
080    @Override
081    public void work() {
082        openUserSession();
083        DocumentModel doc = session.getDocument(docRef);
084
085        RenditionService rs = Framework.getService(RenditionService.class);
086        RenditionDefinition def = ((RenditionServiceImpl) rs).getRenditionDefinition(renditionName);
087
088        List<Blob> blobs = doComputeRendition(session, doc, def);
089
090        TransientStoreService tss = Framework.getService(TransientStoreService.class);
091        TransientStore ts = tss.getStore(getTransientStoreName());
092
093        if (!ts.exists(key)) {
094            throw new NuxeoException("Rendition TransientStore entry can not be null");
095        }
096        ts.putBlobs(key, blobs);
097        ts.setCompleted(key, true);
098    }
099
100    /**
101     * Does the actual Rendition Computation : this code will be called from inside an Asynchronous Work
102     *
103     * @param session
104     * @param doc
105     * @param def
106     * @return
107     */
108    protected abstract List<Blob> doComputeRendition(CoreSession session, DocumentModel doc, RenditionDefinition def);
109
110}