001/*
002 * (C) Copyright 2015 Nuxeo SA (http://nuxeo.com/) and others.
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 *     Thomas Roger
016 */
017
018package org.nuxeo.ecm.platform.rendition.service;
019
020import static org.apache.commons.logging.LogFactory.getLog;
021
022import java.util.ArrayList;
023import java.util.HashMap;
024import java.util.List;
025import java.util.Map;
026
027import org.apache.commons.logging.Log;
028import org.jboss.el.ExpressionFactoryImpl;
029import org.nuxeo.ecm.core.api.DocumentModel;
030import org.nuxeo.ecm.platform.actions.ActionContext;
031import org.nuxeo.ecm.platform.actions.ELActionContext;
032import org.nuxeo.ecm.platform.actions.ejb.ActionManager;
033import org.nuxeo.ecm.platform.el.ExpressionContext;
034import org.nuxeo.ecm.platform.rendition.extension.DefaultAutomationRenditionProvider;
035import org.nuxeo.ecm.platform.rendition.extension.RenditionProvider;
036import org.nuxeo.runtime.api.Framework;
037import org.nuxeo.runtime.model.ContributionFragmentRegistry;
038
039/**
040 * Registry for {@link RenditionDefinition} objects.
041 *
042 * @since 7.3
043 */
044public class RenditionDefinitionRegistry extends ContributionFragmentRegistry<RenditionDefinition> {
045
046    private static final Log log = getLog(RenditionDefinitionRegistry.class);
047
048    protected Map<String, RenditionDefinition> descriptors = new HashMap<>();
049
050    public RenditionDefinition getRenditionDefinition(String name) {
051        RenditionDefinition renditionDefinition = descriptors.get(name);
052        if (renditionDefinition == null) {
053            // could be the CMIS name
054            for (RenditionDefinition rd : descriptors.values()) {
055                if (name.equals(rd.getCmisName())) {
056                    renditionDefinition = rd;
057                    break;
058                }
059            }
060        }
061        return renditionDefinition;
062    }
063
064    public List<RenditionDefinition> getRenditionDefinitions(DocumentModel doc) {
065        List<RenditionDefinition> renditionDefinitions = new ArrayList<>();
066
067        for (RenditionDefinition descriptor : descriptors.values()) {
068            if (canUseRenditionDefinition(descriptor, doc) && descriptor.getProvider().isAvailable(doc, descriptor)) {
069                renditionDefinitions.add(descriptor);
070            }
071        }
072
073        return renditionDefinitions;
074    }
075
076    protected boolean canUseRenditionDefinition(RenditionDefinition renditionDefinition, DocumentModel doc) {
077        ActionManager actionService = Framework.getService(ActionManager.class);
078        return actionService.checkFilters(renditionDefinition.getFilterIds(), createActionContext(doc));
079    }
080
081    protected ActionContext createActionContext(DocumentModel doc) {
082        ActionContext actionContext = new ELActionContext(new ExpressionContext(), new ExpressionFactoryImpl());
083        actionContext.setCurrentDocument(doc);
084        return actionContext;
085    }
086
087    @Override
088    public String getContributionId(RenditionDefinition renditionDefinition) {
089        return renditionDefinition.getName();
090    }
091
092    @Override
093    public void contributionUpdated(String id, RenditionDefinition contrib, RenditionDefinition newOrigContrib) {
094        if (contrib.isEnabled()) {
095            descriptors.put(id, contrib);
096            setupProvider(contrib);
097        } else {
098            descriptors.remove(id);
099        }
100    }
101
102    protected void setupProvider(RenditionDefinition definition) {
103        if (definition.getProviderClass() == null) {
104            definition.setProvider(new DefaultAutomationRenditionProvider());
105        } else {
106            try {
107                RenditionProvider provider = definition.getProviderClass().newInstance();
108                definition.setProvider(provider);
109            } catch (Exception e) {
110                log.error("Unable to create RenditionProvider", e);
111            }
112        }
113    }
114
115    @Override
116    public void contributionRemoved(String id, RenditionDefinition contrib) {
117        descriptors.remove(id);
118    }
119
120    @Override
121    public RenditionDefinition clone(RenditionDefinition contrib) {
122        return contrib.clone();
123    }
124
125    @Override
126    public void merge(RenditionDefinition source, RenditionDefinition dest) {
127        if (source.isEnabledSet() && source.isEnabled() != dest.isEnabled()) {
128            dest.setEnabled(source.isEnabled());
129        }
130
131        String cmisName = source.getCmisName();
132        if (cmisName != null) {
133            dest.setCmisName(cmisName);
134        }
135
136        String label = source.getLabel();
137        if (label != null) {
138            dest.setLabel(label);
139        }
140
141        String icon = source.getIcon();
142        if (icon != null) {
143            dest.setIcon(icon);
144        }
145
146        String kind = source.getKind();
147        if (kind != null) {
148            dest.setKind(kind);
149        }
150
151        String operationChain = source.getOperationChain();
152        if (operationChain != null) {
153            dest.setOperationChain(operationChain);
154        }
155
156        if (source.isEmptyBlobAllowedSet() && source.isEmptyBlobAllowed() != dest.isEmptyBlobAllowed()) {
157            dest.setAllowEmptyBlob(source.isEmptyBlobAllowed());
158        }
159
160        if (source.isVisibleSet() && source.isVisible() != dest.isVisible()) {
161            dest.setVisible(source.isVisible());
162        }
163
164        Class<? extends RenditionProvider> providerClass = source.getProviderClass();
165        if (providerClass != null) {
166            dest.setProviderClass(providerClass);
167        }
168
169        String contentType = source.getContentType();
170        if (contentType != null) {
171            dest.setContentType(contentType);
172        }
173
174        List<String> newFilterIds = new ArrayList<>();
175        newFilterIds.addAll(dest.getFilterIds());
176        newFilterIds.addAll(source.getFilterIds());
177        dest.setFilterIds(newFilterIds);
178    }
179}