001/* 002 * This library is part of OpenCms - 003 * the Open Source Content Management System 004 * 005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com) 006 * 007 * This library is free software; you can redistribute it and/or 008 * modify it under the terms of the GNU Lesser General Public 009 * License as published by the Free Software Foundation; either 010 * version 2.1 of the License, or (at your option) any later version. 011 * 012 * This library is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 015 * Lesser General Public License for more details. 016 * 017 * For further information about Alkacon Software, please see the 018 * company website: http://www.alkacon.com 019 * 020 * For further information about OpenCms, please see the 021 * project website: http://www.opencms.org 022 * 023 * You should have received a copy of the GNU Lesser General Public 024 * License along with this library; if not, write to the Free Software 025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 026 */ 027 028package org.opencms.ade.configuration.formatters; 029 030import org.opencms.main.CmsLog; 031import org.opencms.util.CmsUUID; 032import org.opencms.xml.containerpage.I_CmsFormatterBean; 033 034import java.util.Collection; 035import java.util.Collections; 036import java.util.HashMap; 037import java.util.Map; 038import java.util.stream.Collectors; 039 040import org.apache.commons.logging.Log; 041 042import com.google.common.collect.ArrayListMultimap; 043import com.google.common.collect.Maps; 044import com.google.common.collect.Multimap; 045import com.google.common.collect.Multimaps; 046 047/** 048 * Represents the currently cached collection of all formatter beans extracted from formatter configuration files.<p> 049 * 050 * Objects of this class are immutable, but have a method to create an updated copy.<p> 051 */ 052public class CmsFormatterConfigurationCacheState { 053 054 /** The log instance for this class. */ 055 private static final Log LOG = CmsLog.getLog(CmsFormatterConfigurationCacheState.class); 056 057 /** The automatically enabled formatters. */ 058 private Map<CmsUUID, I_CmsFormatterBean> m_autoEnabledFormatters; 059 060 /** Map of formatter beans by structure id. */ 061 private Map<CmsUUID, I_CmsFormatterBean> m_formatters = new HashMap<CmsUUID, I_CmsFormatterBean>(); 062 063 /** The map of formatters by resource type. */ 064 private Multimap<String, I_CmsFormatterBean> m_formattersByType; 065 066 /** 067 * Creates a new instance.<p> 068 * 069 * @param formatters the initial map of formatters 070 */ 071 public CmsFormatterConfigurationCacheState(Map<CmsUUID, I_CmsFormatterBean> formatters) { 072 073 m_formatters = new HashMap<CmsUUID, I_CmsFormatterBean>(formatters); 074 if (LOG.isDebugEnabled()) { 075 LOG.debug("Created new formatter configuration: "); 076 LOG.debug(m_formatters.toString()); 077 } 078 } 079 080 /** 081 * Creates a new copy of this state in which some entries are removed or replaced.<p> 082 * 083 * This does not change the state object on which the method is called. 084 * 085 * @param updateFormatters a map of formatters to change, where the key is the structure id and the value is either the replacement or null if the map entry should be removed 086 * 087 * @return the updated copy 088 */ 089 public CmsFormatterConfigurationCacheState createUpdatedCopy(Map<CmsUUID, I_CmsFormatterBean> updateFormatters) { 090 091 Map<CmsUUID, I_CmsFormatterBean> newFormatters = Maps.newHashMap(getFormatters()); 092 for (Map.Entry<CmsUUID, I_CmsFormatterBean> entry : updateFormatters.entrySet()) { 093 CmsUUID key = entry.getKey(); 094 I_CmsFormatterBean value = entry.getValue(); 095 if (value != null) { 096 newFormatters.put(key, value); 097 } else { 098 newFormatters.remove(key); 099 } 100 } 101 return new CmsFormatterConfigurationCacheState(newFormatters); 102 } 103 104 /** 105 * Gets the map of formatters which are automatically enabled.<p> 106 * 107 * @return the map of automatically enabled formatters with structure ids as keys 108 */ 109 public Map<CmsUUID, I_CmsFormatterBean> getAutoEnabledFormatters() { 110 111 if (m_autoEnabledFormatters == null) { 112 Map<CmsUUID, I_CmsFormatterBean> result = Maps.newHashMap(); 113 for (Map.Entry<CmsUUID, I_CmsFormatterBean> entry : m_formatters.entrySet()) { 114 if (entry.getValue().isAutoEnabled()) { 115 result.put(entry.getKey(), entry.getValue()); 116 } 117 } 118 m_autoEnabledFormatters = result; 119 } 120 return Collections.unmodifiableMap(m_autoEnabledFormatters); 121 } 122 123 /** 124 * Gets the map of all formatters.<p> 125 * 126 * @return the map of all formatters 127 */ 128 public Map<CmsUUID, I_CmsFormatterBean> getFormatters() { 129 130 return Collections.unmodifiableMap(m_formatters); 131 } 132 133 /** 134 * Gets the formatters for a specific resource types, and optionally only returns those which are automatically enabled.<p> 135 * 136 * @param resourceType the resource type name 137 * @param filterAutoEnabled true if only the automatically enabled formatters should be returned 138 * 139 * @return the formatters for the type 140 */ 141 public Collection<I_CmsFormatterBean> getFormattersForType(String resourceType, boolean filterAutoEnabled) { 142 143 Collection<I_CmsFormatterBean> result = getFormattersByType().get(resourceType); 144 145 if (filterAutoEnabled) { 146 result = result.stream().filter(formatter -> formatter.isAutoEnabled()).collect(Collectors.toList()); 147 } 148 return result; 149 } 150 151 /** 152 * Gets the formatters as a multimap with the resource types as keys and caches this multimap if necessary.<p> 153 * 154 * @return the multimap of formatters by resource type 155 */ 156 private Multimap<String, I_CmsFormatterBean> getFormattersByType() { 157 158 if (m_formattersByType == null) { 159 ArrayListMultimap<String, I_CmsFormatterBean> formattersByType = ArrayListMultimap.create(); 160 for (I_CmsFormatterBean formatter : m_formatters.values()) { 161 for (String typeName : formatter.getResourceTypeNames()) { 162 formattersByType.put(typeName, formatter); 163 } 164 } 165 m_formattersByType = formattersByType; 166 } 167 Multimap<String, I_CmsFormatterBean> result = Multimaps.unmodifiableMultimap(m_formattersByType); 168 return result; 169 } 170 171}