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.file.CmsObject; 031import org.opencms.main.CmsLog; 032import org.opencms.util.CmsMacroResolver; 033import org.opencms.util.CmsStringUtil; 034import org.opencms.widgets.CmsSelectWidgetOption; 035import org.opencms.xml.content.CmsXmlContentProperty; 036import org.opencms.xml.content.CmsXmlContentProperty.Visibility; 037import org.opencms.xml.content.CmsXmlContentPropertyHelper; 038 039import java.util.ArrayList; 040import java.util.List; 041import java.util.Locale; 042import java.util.Set; 043import java.util.stream.Collectors; 044 045import org.apache.commons.beanutils.BeanUtils; 046import org.apache.commons.logging.Log; 047 048/** 049 * Wrapper used to access element setting definition information in JSP code. 050 */ 051public class CmsSettingDefinitionWrapper { 052 053 /** Logger instance for this class. */ 054 private static final Log LOG = CmsLog.getLog(CmsSettingDefinitionWrapper.class); 055 056 /** The definition. containing the original message keys.*/ 057 private CmsXmlContentProperty m_definitionWithKeys; 058 059 /** The raw setting definition (unresolved macros). */ 060 private CmsXmlContentProperty m_rawDefinition; 061 062 /** The resolved definition. */ 063 private CmsXmlContentProperty m_resolvedDefinition; 064 065 /** The CmsObject used. */ 066 private CmsObject m_cms; 067 068 /** 069 * Creates a new instance. 070 * 071 * @param cms the current CMS context 072 * @param settingDef the raw setting definition 073 * @param resolver the macro resolver to use 074 */ 075 public CmsSettingDefinitionWrapper(CmsObject cms, CmsXmlContentProperty settingDef, CmsMacroResolver resolver) { 076 077 m_rawDefinition = settingDef; 078 m_cms = cms; 079 m_resolvedDefinition = CmsXmlContentPropertyHelper.resolveMacrosInProperty(settingDef, resolver); 080 CmsKeyDummyMacroResolver keyResolver = new CmsKeyDummyMacroResolver(resolver); 081 m_definitionWithKeys = CmsXmlContentPropertyHelper.resolveMacrosInProperty(settingDef, keyResolver); 082 } 083 084 /** 085 * Gets the default value. 086 * 087 * @return the default value 088 */ 089 public String getDefaultValue() { 090 091 return m_resolvedDefinition.getDefault(); 092 } 093 094 /** 095 * Gets the description. 096 * 097 * @return the description 098 */ 099 public String getDescription() { 100 101 return m_resolvedDefinition.getDescription(); 102 } 103 104 /** 105 * Gets the description key. 106 * 107 * @return the description key 108 */ 109 public String getDescriptionKey() { 110 111 return CmsKeyDummyMacroResolver.getKey(m_definitionWithKeys.getDescription()); 112 } 113 114 /** 115 * Gets the display name. 116 * 117 * @return the display name 118 */ 119 public String getDisplayName() { 120 121 return m_resolvedDefinition.getNiceName(); 122 } 123 124 /** 125 * Gets the display name key. 126 * 127 * @return the display name key 128 */ 129 public String getDisplayNameKey() { 130 131 return CmsKeyDummyMacroResolver.getKey(m_definitionWithKeys.getNiceName()); 132 } 133 134 /** 135 * Tries to interpret the widget configuration as a select option configuration and returns the list of select options if this succeeds, and null otherwise. 136 * 137 * @return the list of parsed select options, or null if the widget configuration couldn't be interpreted that way 138 */ 139 public List<CmsSelectWidgetOption> getParsedSelectOptions() { 140 141 String widgetConfig = getWidgetConfig(); 142 if (CmsStringUtil.isEmptyOrWhitespaceOnly(widgetConfig)) { 143 // passing an empty/null configuration to parseOptions would result in an empty list, not null, and we want null here 144 return null; 145 } 146 try { 147 List<CmsSelectWidgetOption> options = org.opencms.widgets.CmsSelectWidgetOption.parseOptions(widgetConfig); 148 List<CmsSelectWidgetOption> result = new ArrayList<>(); 149 Set<String> values = options.stream().map(option -> option.getValue()).collect(Collectors.toSet()); 150 String defaultValue = getDefaultValue(); 151 Locale locale = m_cms.getRequestContext().getLocale(); 152 if (CmsStringUtil.isEmptyOrWhitespaceOnly(defaultValue) || !values.contains(defaultValue)) { 153 CmsSelectWidgetOption noValue = new CmsSelectWidgetOption( 154 "", 155 true, 156 org.opencms.gwt.Messages.get().getBundle(locale).key( 157 org.opencms.gwt.Messages.GUI_SELECTBOX_EMPTY_SELECTION_0)); 158 result.add(noValue); 159 } 160 161 result.addAll(options); 162 return result; 163 } catch (Exception e) { 164 LOG.info(e.getLocalizedMessage(), e); 165 return null; 166 } 167 } 168 169 /** 170 * Gets the property name. 171 * 172 * @return the property name 173 */ 174 public String getPropertyName() { 175 176 return m_resolvedDefinition.getName(); 177 } 178 179 /** 180 * Gets the visibility. 181 * 182 * @return the visibility 183 */ 184 public String getVisibility() { 185 186 return "" + m_resolvedDefinition.getVisibility(Visibility.elementAndParentIndividual); 187 } 188 189 /** 190 * Gets the widget. 191 * 192 * @return the widget 193 */ 194 public String getWidget() { 195 196 return m_resolvedDefinition.getWidget(); 197 } 198 199 /** 200 * Gets the widget config. 201 * 202 * @return the widget config 203 */ 204 public String getWidgetConfig() { 205 206 return m_resolvedDefinition.getWidgetConfiguration(); 207 } 208 209 /** 210 * @see java.lang.Object#toString() 211 */ 212 @Override 213 public String toString() { 214 215 // for debugging 216 try { 217 return BeanUtils.describe(this).toString(); 218 } catch (Exception e) { 219 e.printStackTrace(); 220 return "???"; 221 } 222 223 } 224 225}