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 *     Thomas Roger
018 */
019
020package org.nuxeo.ecm.platform.rendition.service;
021
022import java.util.ArrayList;
023import java.util.HashMap;
024import java.util.List;
025import java.util.Map;
026
027import org.nuxeo.ecm.core.api.DocumentModel;
028import org.nuxeo.ecm.platform.actions.ActionContext;
029import org.nuxeo.ecm.platform.actions.ELActionContext;
030import org.nuxeo.ecm.platform.actions.ejb.ActionManager;
031import org.nuxeo.runtime.api.Framework;
032import org.nuxeo.runtime.model.ContributionFragmentRegistry;
033
034/**
035 * Registry for {@link RenditionDefinitionProviderDescriptor} objects.
036 *
037 * @since 7.2
038 */
039public class RenditionDefinitionProviderRegistry extends
040        ContributionFragmentRegistry<RenditionDefinitionProviderDescriptor> {
041
042    protected Map<String, RenditionDefinitionProviderDescriptor> descriptors = new HashMap<>();
043
044    public List<RenditionDefinition> getRenditionDefinitions(DocumentModel doc) {
045        List<RenditionDefinition> renditionDefinitions = new ArrayList<>();
046
047        for (RenditionDefinitionProviderDescriptor descriptor : descriptors.values()) {
048            if (canUseRenditionDefinitionProvider(descriptor, doc)) {
049                RenditionDefinitionProvider provider = descriptor.getProvider();
050                renditionDefinitions.addAll(provider.getRenditionDefinitions(doc));
051            }
052        }
053
054        return renditionDefinitions;
055    }
056
057    public RenditionDefinition getRenditionDefinition(String name, DocumentModel doc) {
058        List<RenditionDefinition> renditionDefinitions = getRenditionDefinitions(doc);
059        for (RenditionDefinition renditionDefinition : renditionDefinitions) {
060            if (renditionDefinition.getName().equals(name)) {
061                return renditionDefinition;
062            }
063        }
064        return null;
065    }
066
067    protected boolean canUseRenditionDefinitionProvider(
068            RenditionDefinitionProviderDescriptor renditionDefinitionProviderDescriptor, DocumentModel doc) {
069        ActionManager actionService = Framework.getService(ActionManager.class);
070        return actionService.checkFilters(renditionDefinitionProviderDescriptor.getFilterIds(),
071                createActionContext(doc));
072    }
073
074    protected ActionContext createActionContext(DocumentModel doc) {
075        ActionContext actionContext = new ELActionContext();
076        actionContext.setCurrentDocument(doc);
077        actionContext.setDocumentManager(doc.getCoreSession());
078        return actionContext;
079    }
080
081    @Override
082    public String getContributionId(RenditionDefinitionProviderDescriptor contrib) {
083        return contrib.getName();
084    }
085
086    @Override
087    public void contributionUpdated(String id, RenditionDefinitionProviderDescriptor contrib,
088            RenditionDefinitionProviderDescriptor newOrigContrib) {
089        if (contrib.isEnabled()) {
090            descriptors.put(id, contrib);
091        } else {
092            descriptors.remove(id);
093        }
094    }
095
096    @Override
097    public void contributionRemoved(String id, RenditionDefinitionProviderDescriptor contrib) {
098        descriptors.remove(id);
099    }
100
101    @Override
102    public RenditionDefinitionProviderDescriptor clone(RenditionDefinitionProviderDescriptor contrib) {
103        return contrib.clone();
104    }
105
106    @Override
107    public void merge(RenditionDefinitionProviderDescriptor source, RenditionDefinitionProviderDescriptor dest) {
108        if (source.isEnabledSet() && source.isEnabled() != dest.isEnabled()) {
109            dest.setEnabled(source.isEnabled());
110        }
111
112        Class<? extends RenditionDefinitionProvider> providerClass = source.getProviderClass();
113        if (providerClass != null) {
114            dest.setProviderClass(providerClass);
115        }
116
117        List<String> newFilterIds = new ArrayList<>();
118        newFilterIds.addAll(dest.getFilterIds());
119        newFilterIds.addAll(source.getFilterIds());
120        dest.setFilterIds(newFilterIds);
121    }
122}