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.jsp.util; 029 030import org.opencms.ade.configuration.CmsADEConfigData; 031import org.opencms.file.CmsObject; 032import org.opencms.file.types.I_CmsResourceType; 033import org.opencms.main.OpenCms; 034import org.opencms.util.CmsStringUtil; 035import org.opencms.workplace.CmsWorkplaceMessages; 036import org.opencms.workplace.explorer.CmsExplorerTypeSettings; 037import org.opencms.xml.containerpage.I_CmsFormatterBean; 038 039import java.util.ArrayList; 040import java.util.Arrays; 041import java.util.Collections; 042import java.util.List; 043import java.util.Locale; 044import java.util.Set; 045 046import com.google.common.collect.ArrayListMultimap; 047import com.google.common.collect.Multimap; 048 049/** 050 * Wrapper for resource type information for use in JSPs. 051 */ 052public class CmsResourceTypeInfoWrapper implements I_CmsFormatterInfo { 053 054 /** Suffix for the description key - by appending it to the key, we get another key that can be used for adding text to the description. */ 055 public static final String DESCRIPTION_KEY_EXTENSION_SUFFIX = ".addon"; 056 057 /** Whether the type is active in the sitemap configuration. */ 058 private boolean m_active; 059 060 /** The active formatters. */ 061 private List<I_CmsFormatterBean> m_activeFormatters = new ArrayList<>(); 062 063 /** The active formatters grouped by container type. */ 064 private Multimap<String, I_CmsFormatterBean> m_activeFormattersByContainerType = ArrayListMultimap.create(); 065 066 /** The current CMS context. */ 067 private CmsObject m_cms; 068 069 /** The current sitemap configuration. */ 070 private CmsADEConfigData m_config; 071 072 private CmsJspStandardContextBean m_context; 073 074 /** The wrapped resource type. */ 075 private I_CmsResourceType m_type; 076 077 /** 078 * Creates a new instance. 079 * 080 * @param context the standard context bean 081 * @param cms the current CMS context 082 * @param config the current sitemap configuration 083 * @param type the type to wrap 084 */ 085 public CmsResourceTypeInfoWrapper( 086 CmsJspStandardContextBean context, 087 CmsObject cms, 088 CmsADEConfigData config, 089 I_CmsResourceType type) { 090 091 m_cms = cms; 092 m_config = config; 093 m_type = type; 094 m_context = context; 095 for (I_CmsFormatterBean formatter : config.getActiveFormatters().values()) { 096 if (!formatter.getResourceTypeNames().contains(type.getTypeName())) { 097 continue; 098 } 099 m_activeFormatters.add(formatter); 100 for (String containerType : formatter.getContainerTypes()) { 101 m_activeFormattersByContainerType.put(containerType, formatter); 102 } 103 } 104 m_active = m_config.getResourceTypes().stream().anyMatch( 105 sitemapConfigType -> m_type.getTypeName().equals(sitemapConfigType.getTypeName())); 106 } 107 108 /** 109 * Gets the formatter information beans for a specific container type. 110 * 111 * @param containerType the container type 112 * @return the formatter information 113 */ 114 public List<CmsFormatterInfoWrapper> formatterInfoForContainer(String containerType) { 115 116 return m_context.wrapFormatters(m_activeFormattersByContainerType.get(containerType)); 117 118 } 119 120 /** 121 * Gets the type description in the current locale. 122 * 123 * @return the type description 124 */ 125 public String getDescription() { 126 127 return getDescription(m_cms.getRequestContext().getLocale()); 128 } 129 130 /** 131 * Gets the description for the type in the given locale. 132 * 133 * @param locale the locale to use 134 * @return the type description 135 */ 136 public String getDescription(Locale locale) { 137 138 try { 139 String name = m_type.getTypeName(); 140 CmsExplorerTypeSettings settings = OpenCms.getWorkplaceManager().getExplorerTypeSetting(name); 141 if (settings != null) { 142 // try to find the localized key 143 String key = settings.getInfo(); 144 if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(key)) { 145 CmsWorkplaceMessages messages = OpenCms.getWorkplaceManager().getMessages(locale); 146 String result = messages.keyDefault(key, name); 147 String extension = messages.keyDefault(key + DESCRIPTION_KEY_EXTENSION_SUFFIX, null); 148 if (extension != null) { 149 result += extension; 150 } 151 return result; 152 } 153 } 154 return ""; 155 } catch (Throwable e) { 156 return m_type.getTypeName(); 157 } 158 } 159 160 /** 161 * @see org.opencms.jsp.util.I_CmsFormatterInfo#getDescriptionKey() 162 */ 163 public String getDescriptionKey() { 164 165 CmsExplorerTypeSettings explorerType = OpenCms.getWorkplaceManager().getExplorerTypeSetting( 166 m_type.getTypeName()); 167 return explorerType.getInfo(); 168 } 169 170 /** 171 * Gets the keys used for the description text. 172 * 173 * @return the list of description keys 174 */ 175 public List<String> getDescriptionKeys() { 176 177 String normalKey = getDescriptionKey(); 178 if (normalKey == null) { 179 return Collections.emptyList(); 180 } else { 181 return Collections.unmodifiableList(Arrays.asList(normalKey, normalKey + DESCRIPTION_KEY_EXTENSION_SUFFIX)); 182 } 183 } 184 185 /** 186 * @see org.opencms.jsp.util.I_CmsFormatterInfo#getDescriptionRaw() 187 */ 188 public String getDescriptionRaw() { 189 190 return getDescriptionKey(); 191 } 192 193 /** 194 * Gets the set of container types configured for any active formatters for this resource type. 195 * 196 * @return the set of container types for formatters 197 */ 198 public Set<String> getFormatterContainerTypes() { 199 200 return Collections.unmodifiableSet(m_activeFormattersByContainerType.keySet()); 201 } 202 203 /** 204 * Gets the formatter information beans for all active formatters for this type. 205 * 206 * @return the formatter information beans 207 */ 208 public List<CmsFormatterInfoWrapper> getFormatterInfo() { 209 210 return m_context.wrapFormatters(m_activeFormatters); 211 212 } 213 214 /** 215 * Returns true if the type is active in the current sitemap configuration. 216 * 217 * @return true if the type is active 218 */ 219 public boolean getIsActive() { 220 221 return m_active; 222 223 } 224 225 /** 226 * @see org.opencms.jsp.util.I_CmsFormatterInfo#getIsFormatter() 227 */ 228 public boolean getIsFormatter() { 229 230 return false; 231 } 232 233 /** 234 * @see org.opencms.jsp.util.I_CmsFormatterInfo#getIsFunction() 235 */ 236 public boolean getIsFunction() { 237 238 return false; 239 } 240 241 /** 242 * @see org.opencms.jsp.util.I_CmsFormatterInfo#getIsResourceType() 243 */ 244 public boolean getIsResourceType() { 245 246 return true; 247 } 248 249 /** 250 * Gets the type name. 251 * 252 * @return the type name 253 */ 254 public String getName() { 255 256 return m_type.getTypeName(); 257 } 258 259 /** 260 * Gets the user-readable nice name of the type in the current locale. 261 * 262 * @return the nice name 263 */ 264 public String getNiceName() { 265 266 return niceName(m_cms.getRequestContext().getLocale()); 267 } 268 269 /** 270 * @see org.opencms.jsp.util.I_CmsFormatterInfo#getNiceNameKey() 271 */ 272 public String getNiceNameKey() { 273 274 CmsExplorerTypeSettings explorerType = OpenCms.getWorkplaceManager().getExplorerTypeSetting( 275 m_type.getTypeName()); 276 return explorerType.getKey(); 277 278 } 279 280 /** 281 * @see org.opencms.jsp.util.I_CmsFormatterInfo#getNiceNameRaw() 282 */ 283 public String getNiceNameRaw() { 284 285 return getNiceNameKey(); 286 } 287 288 /** 289 * Gets the nice name of the type in the given locale. 290 * 291 * @param locale the locale to use 292 * @return the nice name for the type 293 */ 294 public String niceName(Locale locale) { 295 296 try { 297 return CmsWorkplaceMessages.getResourceTypeName(locale, m_type.getTypeName()); 298 } catch (Throwable e) { 299 return m_type.getTypeName(); 300 } 301 } 302 303}