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 actionContext.setDocumentManager(doc.getCoreSession()); 085 return actionContext; 086 } 087 088 @Override 089 public String getContributionId(RenditionDefinition renditionDefinition) { 090 return renditionDefinition.getName(); 091 } 092 093 @Override 094 public void contributionUpdated(String id, RenditionDefinition contrib, RenditionDefinition newOrigContrib) { 095 if (contrib.isEnabled()) { 096 descriptors.put(id, contrib); 097 setupProvider(contrib); 098 } else { 099 descriptors.remove(id); 100 } 101 } 102 103 protected void setupProvider(RenditionDefinition definition) { 104 if (definition.getProviderClass() == null) { 105 definition.setProvider(new DefaultAutomationRenditionProvider()); 106 } else { 107 try { 108 RenditionProvider provider = definition.getProviderClass().newInstance(); 109 definition.setProvider(provider); 110 } catch (Exception e) { 111 log.error("Unable to create RenditionProvider", e); 112 } 113 } 114 } 115 116 @Override 117 public void contributionRemoved(String id, RenditionDefinition contrib) { 118 descriptors.remove(id); 119 } 120 121 @Override 122 public RenditionDefinition clone(RenditionDefinition contrib) { 123 return new RenditionDefinition(contrib); 124 } 125 126 @Override 127 public void merge(RenditionDefinition source, RenditionDefinition dest) { 128 dest.merge(source); 129 } 130 131}