001/* 002 * (C) Copyright 2014 Nuxeo SA (http://nuxeo.com/) and contributors. 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 * Vincent Vergnolle 016 */ 017package org.nuxeo.ecm.platform.picture; 018 019import java.util.ArrayList; 020import java.util.Collections; 021import java.util.HashMap; 022import java.util.List; 023import java.util.Map; 024 025import org.apache.commons.lang.StringUtils; 026import org.apache.commons.logging.Log; 027import org.apache.commons.logging.LogFactory; 028import org.nuxeo.ecm.platform.picture.api.PictureConversion; 029import org.nuxeo.runtime.model.ContributionFragmentRegistry; 030 031/** 032 * Registry for the {@link org.nuxeo.ecm.platform.picture.api.PictureConversion} class (merge supported) 033 * 034 * @since 7.1 035 */ 036public class PictureConversionRegistry extends ContributionFragmentRegistry<PictureConversion> { 037 038 private static final Log log = LogFactory.getLog(PictureConversionRegistry.class); 039 040 protected final Map<String, PictureConversion> pictureConversions = new HashMap<>(); 041 042 public PictureConversion getPictureConversion(String id) { 043 return pictureConversions.get(id); 044 } 045 046 /** 047 * Returns picture conversion collection sorted by order. 048 */ 049 public List<PictureConversion> getPictureConversions() { 050 List<PictureConversion> entries = new ArrayList<>(pictureConversions.values()); 051 Collections.sort(entries); 052 return entries; 053 } 054 055 @Override 056 public String getContributionId(PictureConversion pictureConversion) { 057 return pictureConversion.getId(); 058 } 059 060 @Override 061 public void contributionUpdated(String id, PictureConversion pictureConversion, 062 PictureConversion oldPictureConversion) { 063 if (pictureConversions.containsKey(id)) { 064 contributionRemoved(id, pictureConversion); 065 } 066 067 if (pictureConversion.isEnabled()) { 068 if (!StringUtils.isBlank(id)) { 069 pictureConversions.put(id, pictureConversion); 070 } else { 071 log.warn(String.format("Missing 'id' for picture conversion %s, not registering it.", pictureConversion)); 072 } 073 074 } 075 } 076 077 @Override 078 public void contributionRemoved(String id, PictureConversion pictureConversion) { 079 pictureConversions.remove(id); 080 } 081 082 @Override 083 public PictureConversion clone(PictureConversion pictureConversion) { 084 return pictureConversion.clone(); 085 } 086 087 @Override 088 public void merge(PictureConversion source, PictureConversion dest) { 089 if (source.isEnabledSet() && source.isEnabled() != dest.isEnabled()) { 090 dest.setEnabled(source.isEnabled()); 091 } 092 093 if (source.isDefaultSet() && source.isDefault()) { 094 dest.setDefault(source.isDefault()); 095 } 096 097 // cannot disable default picture conversion 098 if (!dest.isEnabled() && dest.isDefault()) { 099 dest.setEnabled(true); 100 if (log.isWarnEnabled()) { 101 String message = String.format("The picture conversion '%s' is marked as default, enabling it.", 102 dest.getId()); 103 log.warn(message); 104 } 105 } 106 107 String chainId = source.getChainId(); 108 if (!StringUtils.isBlank(chainId)) { 109 dest.setChainId(chainId); 110 } 111 112 String tag = source.getTag(); 113 if (!StringUtils.isBlank(tag)) { 114 dest.setTag(tag); 115 } 116 117 String description = source.getDescription(); 118 if (!StringUtils.isBlank(description)) { 119 dest.setDescription(description); 120 } 121 122 Integer order = source.getOrder(); 123 if (order != null) { 124 dest.setOrder(order); 125 } 126 127 Integer maxSize = source.getMaxSize(); 128 if (maxSize != null) { 129 dest.setMaxSize(maxSize); 130 } 131 132 List<String> newFilterIds = new ArrayList<>(); 133 newFilterIds.addAll(dest.getFilterIds()); 134 newFilterIds.addAll(source.getFilterIds()); 135 dest.setFilterIds(newFilterIds); 136 137 if (source.isRenditionSet()) { 138 dest.setRendition(source.isRendition()); 139 } 140 141 if (source.isRenditionVisibleSet()) { 142 dest.setRenditionVisible(source.isRenditionVisible()); 143 } 144 } 145}