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 static org.apache.commons.logging.LogFactory.getLog; 023 024import java.util.ArrayList; 025import java.util.HashMap; 026import java.util.List; 027import java.util.Map; 028 029import org.apache.commons.logging.Log; 030import org.jboss.el.ExpressionFactoryImpl; 031import org.nuxeo.ecm.core.api.DocumentModel; 032import org.nuxeo.ecm.platform.actions.ActionContext; 033import org.nuxeo.ecm.platform.actions.ELActionContext; 034import org.nuxeo.ecm.platform.actions.ejb.ActionManager; 035import org.nuxeo.ecm.platform.el.ExpressionContext; 036import org.nuxeo.ecm.platform.rendition.extension.DefaultAutomationRenditionProvider; 037import org.nuxeo.ecm.platform.rendition.extension.RenditionProvider; 038import org.nuxeo.runtime.api.Framework; 039import org.nuxeo.runtime.model.ContributionFragmentRegistry; 040 041/** 042 * Registry for {@link RenditionDefinition} objects. 043 * 044 * @since 7.3 045 */ 046public class RenditionDefinitionRegistry extends ContributionFragmentRegistry<RenditionDefinition> { 047 048 private static final Log log = getLog(RenditionDefinitionRegistry.class); 049 050 protected Map<String, RenditionDefinition> descriptors = new HashMap<>(); 051 052 public RenditionDefinition getRenditionDefinition(String name) { 053 RenditionDefinition renditionDefinition = descriptors.get(name); 054 if (renditionDefinition == null) { 055 // could be the CMIS name 056 for (RenditionDefinition rd : descriptors.values()) { 057 if (name.equals(rd.getCmisName())) { 058 renditionDefinition = rd; 059 break; 060 } 061 } 062 } 063 return renditionDefinition; 064 } 065 066 public List<RenditionDefinition> getRenditionDefinitions(DocumentModel doc) { 067 List<RenditionDefinition> renditionDefinitions = new ArrayList<>(); 068 069 for (RenditionDefinition descriptor : descriptors.values()) { 070 if (canUseRenditionDefinition(descriptor, doc) && descriptor.getProvider().isAvailable(doc, descriptor)) { 071 renditionDefinitions.add(descriptor); 072 } 073 } 074 075 return renditionDefinitions; 076 } 077 078 protected boolean canUseRenditionDefinition(RenditionDefinition renditionDefinition, DocumentModel doc) { 079 ActionManager actionService = Framework.getService(ActionManager.class); 080 return actionService.checkFilters(renditionDefinition.getFilterIds(), createActionContext(doc)); 081 } 082 083 protected ActionContext createActionContext(DocumentModel doc) { 084 ActionContext actionContext = new ELActionContext(new ExpressionContext(), new ExpressionFactoryImpl()); 085 actionContext.setCurrentDocument(doc); 086 actionContext.setDocumentManager(doc.getCoreSession()); 087 return actionContext; 088 } 089 090 @Override 091 public String getContributionId(RenditionDefinition renditionDefinition) { 092 return renditionDefinition.getName(); 093 } 094 095 @Override 096 public void contributionUpdated(String id, RenditionDefinition contrib, RenditionDefinition newOrigContrib) { 097 if (contrib.isEnabled()) { 098 descriptors.put(id, contrib); 099 setupProvider(contrib); 100 } else { 101 descriptors.remove(id); 102 } 103 } 104 105 protected void setupProvider(RenditionDefinition definition) { 106 if (definition.getProviderClass() == null) { 107 definition.setProvider(new DefaultAutomationRenditionProvider()); 108 } else { 109 try { 110 RenditionProvider provider = definition.getProviderClass().newInstance(); 111 definition.setProvider(provider); 112 } catch (Exception e) { 113 log.error("Unable to create RenditionProvider", e); 114 } 115 } 116 } 117 118 @Override 119 public void contributionRemoved(String id, RenditionDefinition contrib) { 120 descriptors.remove(id); 121 } 122 123 @Override 124 public RenditionDefinition clone(RenditionDefinition contrib) { 125 return new RenditionDefinition(contrib); 126 } 127 128 @Override 129 public void merge(RenditionDefinition source, RenditionDefinition dest) { 130 dest.merge(source); 131 } 132 133}