001/*
002 * (C) Copyright 2012 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 *     Thierry Delprat
018 */
019package org.nuxeo.ecm.platform.rendition.extension;
020
021import java.util.List;
022
023import org.apache.commons.logging.Log;
024import org.apache.commons.logging.LogFactory;
025import org.nuxeo.ecm.automation.AutomationService;
026import org.nuxeo.ecm.core.api.Blob;
027import org.nuxeo.ecm.core.api.DocumentModel;
028import org.nuxeo.ecm.core.api.NuxeoPrincipal;
029import org.nuxeo.ecm.core.api.blobholder.BlobHolder;
030import org.nuxeo.ecm.platform.rendition.service.RenditionDefinition;
031import org.nuxeo.runtime.api.Framework;
032
033public class DefaultAutomationRenditionProvider implements RenditionProvider {
034
035    private static final Log log = LogFactory.getLog(DefaultAutomationRenditionProvider.class);
036
037    public static final String VARIANT_POLICY_USER = "user";
038
039    @Override
040    public boolean isAvailable(DocumentModel doc, RenditionDefinition def) {
041        String chain = def.getOperationChain();
042        if (chain == null) {
043            log.error("Can not run Automation rendition if chain is not defined");
044            return false;
045        }
046        AutomationService as = Framework.getService(AutomationService.class);
047
048        try {
049            if (as.getOperation(chain) == null) {
050                log.error("Chain " + chain + " is not defined : rendition can not be used");
051                return false;
052            }
053        } catch (Exception e) {
054            log.error("Unable to test Rendition availability", e);
055            return false;
056        }
057
058        if (!def.isEmptyBlobAllowed()) {
059            BlobHolder bh = doc.getAdapter(BlobHolder.class);
060            if (bh == null) {
061                return false;
062            }
063            try {
064                Blob blob = bh.getBlob();
065                if (blob == null) {
066                    return false;
067                }
068            } catch (Exception e) {
069                log.error("Unable to get Blob to test Rendition availability", e);
070                return false;
071            }
072        }
073        return true;
074    }
075
076    @Override
077    public List<Blob> render(DocumentModel doc, RenditionDefinition definition) {
078        return AutomationRenderer.render(doc, definition, null);
079    }
080
081    /**
082     * Gets the optional {@link org.nuxeo.ecm.platform.rendition.Constants#RENDITION_VARIANT_PROPERTY
083     * RENDITION_VARIANT_PROPERTY} value for a given {@link RenditionDefinition}.
084     *
085     * @param doc the target document
086     * @param definition the rendition definition to use
087     * @return the generated {@link org.nuxeo.ecm.platform.rendition.Constants#RENDITION_VARIANT_PROPERTY
088     *         RENDITION_VARIANT_PROPERTY} value, or {@code null}
089     * @since 8.1
090     */
091    @Override
092    public String getVariant(DocumentModel doc, RenditionDefinition definition) {
093        if (VARIANT_POLICY_USER.equals(definition.getVariantPolicy())) {
094            NuxeoPrincipal principal = doc.getCoreSession().getPrincipal();
095            if (principal.isAdministrator()) {
096                return org.nuxeo.ecm.platform.rendition.Constants.RENDITION_VARIANT_PROPERTY_ADMINISTRATOR_USER;
097            } else {
098                return org.nuxeo.ecm.platform.rendition.Constants.RENDITION_VARIANT_PROPERTY_USER_PREFIX
099                        + principal.getName();
100            }
101        }
102        return null;
103    }
104
105}