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 implements I_CmsInfoWrapper {
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 raw description configured for the setting.
116     *
117     * @return the raw description
118     */
119    public String getDescriptionRaw() {
120
121        return m_rawDefinition.getDescription();
122    }
123
124    /**
125     * Gets the display name.
126     *
127     * @return the display name
128     */
129    public String getDisplayName() {
130
131        return m_resolvedDefinition.getNiceName();
132    }
133
134    /**
135     * Gets the display name key.
136     *
137     * @return the display name key
138     */
139    public String getDisplayNameKey() {
140
141        return CmsKeyDummyMacroResolver.getKey(m_definitionWithKeys.getNiceName());
142    }
143
144    /**
145     * Gets the raw configured display name.
146     *
147     * @return the raw display name
148     */
149    public String getDisplayNameRaw() {
150        return m_rawDefinition.getNiceName();
151    }
152
153    /**
154     * Tries to interpret the widget configuration as a select option configuration and returns the list of select options if this succeeds, and null otherwise.
155     *
156     * @return the list of parsed select options, or null if the widget configuration couldn't be interpreted that way
157     */
158    public List<CmsSelectWidgetOption> getParsedSelectOptions() {
159
160        String widgetConfig = getWidgetConfig();
161        if (CmsStringUtil.isEmptyOrWhitespaceOnly(widgetConfig)) {
162            // passing an empty/null configuration to parseOptions would result in an empty list, not null, and we want null here
163            return null;
164        }
165        try {
166            List<CmsSelectWidgetOption> options = org.opencms.widgets.CmsSelectWidgetOption.parseOptions(widgetConfig);
167            List<CmsSelectWidgetOption> result = new ArrayList<>();
168            Set<String> values = options.stream().map(option -> option.getValue()).collect(Collectors.toSet());
169            String defaultValue = getDefaultValue();
170            Locale locale = m_cms.getRequestContext().getLocale();
171            if (CmsStringUtil.isEmptyOrWhitespaceOnly(defaultValue) || !values.contains(defaultValue)) {
172                CmsSelectWidgetOption noValue = new CmsSelectWidgetOption(
173                    "",
174                    true,
175                    org.opencms.gwt.Messages.get().getBundle(locale).key(
176                        org.opencms.gwt.Messages.GUI_SELECTBOX_EMPTY_SELECTION_0));
177                result.add(noValue);
178            }
179
180            result.addAll(options);
181            return result;
182        } catch (Exception e) {
183            LOG.info(e.getLocalizedMessage(), e);
184            return null;
185        }
186    }
187
188    /**
189     * Gets the property name.
190     *
191     * @return the property name
192     */
193    public String getPropertyName() {
194
195        return m_resolvedDefinition.getName();
196    }
197
198    /**
199     * Gets the visibility.
200     *
201     * @return the visibility
202     */
203    public String getVisibility() {
204
205        return "" + m_resolvedDefinition.getVisibility(Visibility.elementAndParentIndividual);
206    }
207
208    /**
209     * Gets the widget.
210     *
211     * @return the widget
212     */
213    public String getWidget() {
214
215        return m_resolvedDefinition.getWidget();
216    }
217
218    /**
219     * Gets the widget config.
220     *
221     * @return the widget config
222     */
223    public String getWidgetConfig() {
224
225        return m_resolvedDefinition.getWidgetConfiguration();
226    }
227
228    /**
229     * @see java.lang.Object#toString()
230     */
231    @Override
232    public String toString() {
233
234        // for debugging
235        try {
236            return BeanUtils.describe(this).toString();
237        } catch (Exception e) {
238            e.printStackTrace();
239            return "???";
240        }
241
242    }
243
244}