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