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.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 configured gallery types.<p> 124 * 125 * @return the configured gallery types 126 */ 127 public String getGalleryTypes() { 128 129 return m_galleryTypes; 130 } 131 132 /** 133 * Returns the required information for the initial item list to load.<p> 134 * 135 * If a gallery should be shown, the path to the gallery must be specified, 136 * for a category the category path (e.g wurstsorten/kochwurst/).<p> 137 * 138 * @return the required information for the initial item list to load 139 */ 140 public String getStartup() { 141 142 return m_startup; 143 } 144 145 /** 146 * Returns the type of the initial item list to load, either gallery or category.<p> 147 * 148 * @return the type of the initial image list to load 149 */ 150 public String getType() { 151 152 return m_type; 153 } 154 155 /** 156 * Initializes the widget configuration using the given configuration string.<p> 157 * 158 * @param cms an initialized instance of a CmsObject 159 * @param messages the dialog messages 160 * @param param the widget parameter to generate the widget for 161 * @param configuration the widget configuration string 162 */ 163 protected void init(CmsObject cms, CmsMessages messages, I_CmsWidgetParameter param, String configuration) { 164 165 if (configuration == null) { 166 // no configuration String found, return 167 return; 168 } 169 configuration = CmsMacroResolver.resolveMacros(configuration, cms, messages); 170 JSONObject jsonObj = new JSONObject(); 171 try { 172 jsonObj = new JSONObject(configuration); 173 } catch (JSONException e) { 174 // initialization failed 175 return; 176 } 177 // determine the class name that fills in values dynamically 178 setClassName(jsonObj.optString(CONFIG_KEY_CLASS, null)); 179 I_CmsGalleryWidgetDynamicConfiguration dynConf = null; 180 if (getClassName() != null) { 181 try { 182 dynConf = (I_CmsGalleryWidgetDynamicConfiguration)Class.forName(getClassName()).newInstance(); 183 } catch (Exception e) { 184 // class not found 185 } 186 } 187 // determine the initial item list settings 188 setType(jsonObj.optString(CONFIG_KEY_TYPE)); 189 if ((CONFIG_VALUE_DYNAMIC.equals(getType()) || CmsStringUtil.isEmpty(getType())) && (dynConf != null)) { 190 setType(dynConf.getType(cms, messages, param)); 191 } 192 setGalleryTypes(jsonObj.optString(CONFIG_KEY_GALLERYTYPES, null)); 193 setStartup(jsonObj.optString(CONFIG_KEY_STARTUP)); 194 if ((CONFIG_VALUE_DYNAMIC.equals(getStartup()) || CmsStringUtil.isEmpty(getStartup())) && (dynConf != null)) { 195 setStartup(dynConf.getStartup(cms, messages, param)); 196 } 197 } 198 199 /** 200 * Sets the optional class name for generating dynamic configurations, must implement {@link I_CmsGalleryWidgetDynamicConfiguration}.<p> 201 * 202 * @param className the optional class name for generating dynamic configurations 203 */ 204 protected void setClassName(String className) { 205 206 m_className = className; 207 } 208 209 /** 210 * Sets the configured gallery types.<p> 211 * 212 * @param galleryTypes the configured gallery types 213 */ 214 protected void setGalleryTypes(String galleryTypes) { 215 216 m_galleryTypes = galleryTypes; 217 } 218 219 /** 220 * Sets the required information for the initial item list to load.<p> 221 * 222 * If a gallery should be shown, the path to the gallery must be specified, 223 * for a category the category path.<p> 224 * 225 * @param startup the required information for the initial item list to load 226 */ 227 protected void setStartup(String startup) { 228 229 m_startup = startup; 230 } 231 232 /** 233 * Sets the type of the initial item list to load, either gallery or category.<p> 234 * 235 * @param type the type of the initial item list to load 236 */ 237 protected void setType(String type) { 238 239 m_type = type; 240 } 241 242 /** 243 * Returns the values as a parameter string.<p> 244 * 245 * @return the values as a parameter string 246 * */ 247 public String getConfigString() { 248 249 String result = ""; 250 if (m_startup != null) { 251 result += "&startup=" + m_startup; 252 } 253 if (m_type != null) { 254 result += "&type=" + m_type; 255 } 256 257 return result; 258 } 259 260}