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.nuxeo.ecm.core.api.DocumentModel; 031import org.nuxeo.ecm.platform.actions.ActionContext; 032import org.nuxeo.ecm.platform.actions.ELActionContext; 033import org.nuxeo.ecm.platform.actions.ejb.ActionManager; 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(); 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}