001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (https://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: https://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: https://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.widgets;
029
030import org.opencms.file.CmsObject;
031import org.opencms.i18n.CmsMessages;
032import org.opencms.json.JSONException;
033import org.opencms.json.JSONObject;
034import org.opencms.util.CmsMacroResolver;
035import org.opencms.util.CmsStringUtil;
036
037/**
038 * Configuration options for the gallery widget (e.g. DownloadGalleryWidget).<p>
039 *
040 * The configuration options are read from the configuration String of the widget. For nested XML schemas the configuration String must be defined inside the nested content.<p>
041 *
042 * The configuration String has to be formatted as JSON object, with the following possible keys:<p>
043 * <ul>
044 * <li><code>class</code>: optional class implementing {@link I_CmsGalleryWidgetDynamicConfiguration} to dynamically
045 *            configure startup parameters and format values.</li>
046 * <li><code>startup</code>: the startup folder, can be dynamically generated by the provided class,
047 *            in that case, use 'dynamic' as value.</li>
048 * <li><code>type</code>: the startup folder type, can be 'gallery' or 'category'. Can be dynamically generated
049 *            by the provided class, in that case, use 'dynamic' as value.</li>
050 * </ul>
051 *
052 * Example configurations can look like this:<p>
053 * <code>{type: 'gallery', startup: '/demo_en/images/'}</code><p>
054 * <code>{type: 'category', startup: 'wurstsorten/kochwurst/'}</code><p>
055 *
056 * @since 7.5.0
057 */
058public class CmsGalleryWidgetConfiguration {
059
060    /** Configuration key name for the class configuration. */
061    public static final String CONFIG_KEY_CLASS = "class";
062
063    /** Configuration key name for the gallery types configuration. */
064    public static final String CONFIG_KEY_GALLERYTYPES = "gallerytypes";
065
066    /** Configuration key name for the startup configuration. */
067    public static final String CONFIG_KEY_STARTUP = "startup";
068
069    /** Configuration key name for the type configuration. */
070    public static final String CONFIG_KEY_TYPE = "type";
071
072    /** Configuration value name for a dynamic configuration. */
073    public static final String CONFIG_VALUE_DYNAMIC = "dynamic";
074
075    /** The optional class name for generating dynamic configurations, must implement {@link I_CmsGalleryWidgetDynamicConfiguration}. */
076    protected String m_className;
077
078    /** The required information for the initial item list to load. */
079    protected String m_startup;
080
081    /** The type of the initial item list to load, either gallery or category. */
082    protected String m_type;
083
084    /** The configured gallery types. */
085    private String m_galleryTypes;
086
087    /**
088     * Generates an initialized configuration for the gallery item widget using the given configuration string.<p>
089     *
090     * @param cms an initialized instance of a CmsObject
091     * @param widgetDialog the dialog where the widget is used on
092     * @param param the widget parameter to generate the widget for
093     * @param configuration the widget configuration string
094     */
095    public CmsGalleryWidgetConfiguration(
096        CmsObject cms,
097        CmsMessages widgetDialog,
098        I_CmsWidgetParameter param,
099        String configuration) {
100
101        init(cms, widgetDialog, param, configuration);
102    }
103
104    /**
105     * Default constructor.<p>
106     */
107    protected CmsGalleryWidgetConfiguration() {
108
109        // empty constructor is required for class registration
110    }
111
112    /**
113     * Returns the optional class name for generating dynamic configurations, must implement {@link I_CmsGalleryWidgetDynamicConfiguration}.<p>
114     *
115     * @return the optional class name for generating dynamic configurations
116     */
117    public String getClassName() {
118
119        return m_className;
120    }
121
122    /**
123     * Returns the values as a parameter string.<p>
124     *
125     * @return the values as a parameter string
126     */
127    public String getConfigString() {
128
129        String result = "";
130        if (m_startup != null) {
131            result += "&startup=" + m_startup;
132        }
133        if (m_type != null) {
134            result += "&type=" + m_type;
135        }
136
137        return result;
138    }
139
140    /**
141     * Returns the configured gallery types.<p>
142     *
143     * @return the configured gallery types
144     */
145    public String getGalleryTypes() {
146
147        return m_galleryTypes;
148    }
149
150    /**
151     * Returns the required information for the initial item list to load.<p>
152     *
153     * If a gallery should be shown, the path to the gallery must be specified,
154     * for a category the category path (e.g wurstsorten/kochwurst/).<p>
155     *
156     * @return the required information for the initial item list to load
157     */
158    public String getStartup() {
159
160        return m_startup;
161    }
162
163    /**
164     * Returns the type of the initial item list to load, either gallery or category.<p>
165     *
166     * @return the type of the initial image list to load
167     */
168    public String getType() {
169
170        return m_type;
171    }
172
173    /**
174     * Initializes the widget configuration using the given configuration string.<p>
175     *
176     * @param cms an initialized instance of a CmsObject
177     * @param messages the dialog messages
178     * @param param the widget parameter to generate the widget for
179     * @param configuration the widget configuration string
180     */
181    protected void init(CmsObject cms, CmsMessages messages, I_CmsWidgetParameter param, String configuration) {
182
183        if (configuration == null) {
184            // no configuration String found, return
185            return;
186        }
187        configuration = CmsMacroResolver.resolveMacros(configuration, cms, messages);
188        JSONObject jsonObj = new JSONObject();
189        try {
190            jsonObj = new JSONObject(configuration);
191        } catch (JSONException e) {
192            // initialization failed
193            return;
194        }
195        // determine the class name that fills in values dynamically
196        setClassName(jsonObj.optString(CONFIG_KEY_CLASS, null));
197        I_CmsGalleryWidgetDynamicConfiguration dynConf = null;
198        if (getClassName() != null) {
199            try {
200                dynConf = (I_CmsGalleryWidgetDynamicConfiguration)Class.forName(getClassName()).newInstance();
201            } catch (Exception e) {
202                // class not found
203            }
204        }
205        // determine the initial item list settings
206        setType(jsonObj.optString(CONFIG_KEY_TYPE));
207        if ((CONFIG_VALUE_DYNAMIC.equals(getType()) || CmsStringUtil.isEmpty(getType())) && (dynConf != null)) {
208            setType(dynConf.getType(cms, messages, param));
209        }
210        setGalleryTypes(jsonObj.optString(CONFIG_KEY_GALLERYTYPES, null));
211        setStartup(jsonObj.optString(CONFIG_KEY_STARTUP));
212        if ((CONFIG_VALUE_DYNAMIC.equals(getStartup()) || CmsStringUtil.isEmpty(getStartup())) && (dynConf != null)) {
213            setStartup(dynConf.getStartup(cms, messages, param));
214        }
215    }
216
217    /**
218     * Sets the optional class name for generating dynamic configurations, must implement {@link I_CmsGalleryWidgetDynamicConfiguration}.<p>
219     *
220     * @param className the optional class name for generating dynamic configurations
221     */
222    protected void setClassName(String className) {
223
224        m_className = className;
225    }
226
227    /**
228     * Sets the configured gallery types.<p>
229     *
230     * @param galleryTypes the configured gallery types
231     */
232    protected void setGalleryTypes(String galleryTypes) {
233
234        m_galleryTypes = galleryTypes;
235    }
236
237    /**
238     * Sets the required information for the initial item list to load.<p>
239     *
240     * If a gallery should be shown, the path to the gallery must be specified,
241     * for a category the category path.<p>
242     *
243     * @param startup the required information for the initial item list to load
244     */
245    protected void setStartup(String startup) {
246
247        m_startup = startup;
248    }
249
250    /**
251     * Sets the type of the initial item list to load, either gallery or category.<p>
252     *
253     * @param type the type of the initial item list to load
254     */
255    protected void setType(String type) {
256
257        m_type = type;
258    }
259
260}