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 GmbH & Co. KG, 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.ade.galleries; 029 030import org.opencms.ade.galleries.shared.CmsGalleryConfiguration; 031import org.opencms.ade.galleries.shared.CmsGalleryDataBean; 032import org.opencms.ade.galleries.shared.CmsGallerySearchBean; 033import org.opencms.ade.galleries.shared.CmsGalleryTabConfiguration; 034import org.opencms.ade.galleries.shared.I_CmsGalleryProviderConstants; 035import org.opencms.ade.galleries.shared.I_CmsGalleryProviderConstants.GalleryMode; 036import org.opencms.ade.galleries.shared.I_CmsGalleryProviderConstants.GalleryTabId; 037import org.opencms.ade.galleries.shared.rpc.I_CmsGalleryService; 038import org.opencms.file.CmsObject; 039import org.opencms.file.CmsResource; 040import org.opencms.file.CmsResourceFilter; 041import org.opencms.gwt.CmsGwtActionElement; 042import org.opencms.gwt.shared.CmsCoreData; 043import org.opencms.main.CmsException; 044import org.opencms.main.CmsLog; 045import org.opencms.main.OpenCms; 046import org.opencms.util.CmsStringUtil; 047import org.opencms.widgets.CmsVfsFileWidget; 048import org.opencms.workplace.CmsWorkplace; 049 050import java.util.Arrays; 051import java.util.Set; 052 053import javax.servlet.http.HttpServletRequest; 054import javax.servlet.http.HttpServletResponse; 055import javax.servlet.jsp.PageContext; 056 057import org.apache.commons.logging.Log; 058 059/** 060 * Gallery action used to generate the gallery dialog.<p> 061 * 062 * see jsp file <tt>/system/modules/org.opencms.ade.galleries/testVfs.jsp</tt>.<p> 063 * 064 * @since 8.0.0 065 */ 066public class CmsGalleryActionElement extends CmsGwtActionElement { 067 068 /** The OpenCms module name. */ 069 public static final String CMS_MODULE_NAME = "org.opencms.ade.galleries"; 070 071 /** The GWT module name. */ 072 public static final String GWT_MODULE_NAME = CmsCoreData.ModuleKey.galleries.name(); 073 074 /** The log instance for this class. */ 075 private static final Log LOG = CmsLog.getLog(CmsGalleryActionElement.class); 076 077 /** The gallery mode. */ 078 private GalleryMode m_galleryMode; 079 080 /** 081 * Constructor.<p> 082 * 083 * @param context the JSP page context object 084 * @param req the JSP request 085 * @param res the JSP response 086 */ 087 public CmsGalleryActionElement(PageContext context, HttpServletRequest req, HttpServletResponse res) { 088 089 super(context, req, res); 090 091 try { 092 m_galleryMode = GalleryMode.valueOf( 093 getRequest().getParameter(I_CmsGalleryProviderConstants.CONFIG_GALLERY_MODE).trim()); 094 } catch (Exception e) { 095 m_galleryMode = GalleryMode.view; 096 LOG.debug("Could not parse gallery mode parameter.", e); 097 } 098 } 099 100 /** 101 * @see org.opencms.gwt.CmsGwtActionElement#export() 102 */ 103 @Override 104 public String export() throws Exception { 105 106 return ""; 107 } 108 109 /** 110 * @see org.opencms.gwt.CmsGwtActionElement#exportAll() 111 */ 112 @Override 113 public String exportAll() throws Exception { 114 115 StringBuffer sb = new StringBuffer(); 116 sb.append(super.export()); 117 sb.append(export(m_galleryMode)); 118 sb.append(exportCloseLink()); 119 sb.append(exportModuleScriptTag(GWT_MODULE_NAME)); 120 return sb.toString(); 121 } 122 123 /** 124 * Returns the serialized initial data for gallery dialog within the container-page editor.<p> 125 * 126 * @return the data 127 * 128 * @throws Exception if something goes wrong 129 */ 130 public String exportForContainerpage() throws Exception { 131 132 return ClientMessages.get().export(getRequest()); 133 } 134 135 /** 136 * Exports the gallery messages for widget use.<p> 137 * 138 * @return the gallery messages 139 */ 140 public String exportWidget() { 141 142 return ClientMessages.get().export(getRequest()); 143 } 144 145 /** 146 * Returns the editor title.<p> 147 * 148 * @return the editor title 149 */ 150 public String getTitle() { 151 152 return Messages.get().getBundle(getWorkplaceLocale()).key(Messages.GUI_GALLERIES_TITLE_0); 153 } 154 155 /** 156 * Returns if the current gallery mode is the editor mode (used inside a rich text editor).<p> 157 * 158 * @return <code>true</code> if the gallery was opened from the editor 159 */ 160 public boolean isEditorMode() { 161 162 return m_galleryMode == GalleryMode.editor; 163 } 164 165 /** 166 * Returns true if the gallery mode is set to 'view'. 167 * 168 * @return true if the gallery mode is 'view' 169 */ 170 public boolean isViewMode() { 171 172 return m_galleryMode == GalleryMode.view; 173 } 174 175 /** 176 * Returns if the current gallery mode is the widget mode (used within xml-content editor etc.).<p> 177 * 178 * @return <code>true</code> if the gallery was opened as a widget 179 */ 180 public boolean isWidgetMode() { 181 182 return m_galleryMode == GalleryMode.widget; 183 } 184 185 /** 186 * Uses the request parameters of the current request to create a gallery configuration object.<p> 187 * 188 * @param galleryMode the gallery mode 189 * 190 * @return the gallery configuration 191 */ 192 private CmsGalleryConfiguration createGalleryConfigurationFromRequest(GalleryMode galleryMode) { 193 194 CmsGalleryConfiguration conf = new CmsGalleryConfiguration(); 195 conf.setGalleryMode(galleryMode); 196 conf.setReferencePath(getRequest().getParameter(I_CmsGalleryProviderConstants.CONFIG_REFERENCE_PATH)); 197 conf.setGalleryPath(getRequest().getParameter(I_CmsGalleryProviderConstants.CONFIG_GALLERY_PATH)); 198 conf.setCurrentElement(getRequest().getParameter(I_CmsGalleryProviderConstants.CONFIG_CURRENT_ELEMENT)); 199 String resourceTypes = getRequest().getParameter(I_CmsGalleryProviderConstants.CONFIG_RESOURCE_TYPES); 200 String useLinkDefaultTypes = getRequest().getParameter( 201 I_CmsGalleryProviderConstants.PARAM_USE_LINK_DEFAULT_TYPES); 202 if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(resourceTypes)) { 203 conf.setResourceTypes(Arrays.asList(resourceTypes.split(","))); 204 } 205 if (Boolean.parseBoolean(useLinkDefaultTypes)) { 206 try { 207 CmsObject cms = getCmsObject(); 208 CmsResource referenceResource = cms.readResource( 209 conf.getReferencePath(), 210 CmsResourceFilter.IGNORE_EXPIRATION); 211 String searchTypes = CmsVfsFileWidget.getDefaultSearchTypes(cms, referenceResource); 212 conf.setSearchTypes(CmsStringUtil.splitAsList(searchTypes, ",")); 213 } catch (CmsException e) { 214 LOG.warn(e.getLocalizedMessage(), e); 215 } 216 } 217 218 String galleryTypes = getRequest().getParameter(I_CmsGalleryProviderConstants.CONFIG_GALLERY_TYPES); 219 if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(galleryTypes)) { 220 conf.setGalleryTypes(galleryTypes.split(",")); 221 } 222 String tabs = getRequest().getParameter(I_CmsGalleryProviderConstants.CONFIG_TAB_CONFIG); 223 if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(tabs)) { 224 conf.setTabConfiguration(CmsGalleryTabConfiguration.resolve(tabs)); 225 } else { 226 conf.setTabConfiguration(CmsGalleryTabConfiguration.getDefault()); 227 } 228 String galleryStoragePrefix = getRequest().getParameter( 229 I_CmsGalleryProviderConstants.CONFIG_GALLERY_STORAGE_PREFIX); 230 if (CmsStringUtil.isEmptyOrWhitespaceOnly(galleryStoragePrefix)) { 231 galleryStoragePrefix = ""; 232 } 233 conf.setGalleryStoragePrefix(galleryStoragePrefix); 234 return conf; 235 } 236 237 /** 238 * Returns the serialized initial data for gallery dialog depending on the given mode.<p> 239 * 240 * @param galleryMode the gallery mode 241 * 242 * @return the data 243 * 244 * @throws Exception if something goes wrong 245 */ 246 private String export(GalleryMode galleryMode) throws Exception { 247 248 CmsGalleryConfiguration conf = createGalleryConfigurationFromRequest(galleryMode); 249 CmsGalleryDataBean data = CmsGalleryService.getInitialSettings(getRequest(), conf); 250 CmsGallerySearchBean search = null; 251 if (GalleryTabId.cms_tab_results.equals(data.getStartTab())) { 252 search = CmsGalleryService.getSearch(getRequest(), data); 253 } 254 Set<String> folderFilter = data.getStartFolderFilter(); 255 if ((folderFilter != null) && !folderFilter.isEmpty()) { 256 data.setVfsPreloadData(CmsGalleryService.generateVfsPreloadData(getCmsObject(), null, folderFilter)); 257 } 258 if ((search != null) && (search.getScope() != null) && (search.getScope() != data.getScope())) { 259 // default selected scope option should be the one for which the search has been actually performed 260 data.setScope(search.getScope()); 261 } else if ((search != null) && (search.getScope() == null)) { 262 data.setScope(OpenCms.getWorkplaceManager().getGalleryDefaultScope()); 263 } 264 265 StringBuffer sb = new StringBuffer(); 266 sb.append( 267 exportDictionary( 268 CmsGalleryDataBean.DICT_NAME, 269 I_CmsGalleryService.class.getMethod("getInitialSettings", CmsGalleryConfiguration.class), 270 data)); 271 sb.append( 272 exportDictionary( 273 CmsGallerySearchBean.DICT_NAME, 274 I_CmsGalleryService.class.getMethod("getSearch", CmsGalleryDataBean.class), 275 search)); 276 return sb.toString(); 277 } 278 279 /** 280 * Returns a javascript tag that contains a variable deceleration that has the close link as value.<p> 281 * 282 * @return a javascript tag that contains a variable deceleration that has the close link as value 283 */ 284 private String exportCloseLink() { 285 286 String closeLink = null; 287 if (getRequest().getAttribute(I_CmsGalleryProviderConstants.ATTR_CLOSE_LINK) != null) { 288 closeLink = (String)getRequest().getAttribute(I_CmsGalleryProviderConstants.ATTR_CLOSE_LINK); 289 } 290 if (CmsStringUtil.isEmptyOrWhitespaceOnly(closeLink)) { 291 closeLink = CmsWorkplace.FILE_EXPLORER_FILELIST; 292 } 293 294 StringBuffer sb = new StringBuffer(); 295 // var closeLink = '/system/workplace/views/explorer/explorer_files.jsp'; 296 sb.append(wrapScript("var ", I_CmsGalleryProviderConstants.ATTR_CLOSE_LINK, " = \'", link(closeLink), "\';")); 297 return sb.toString(); 298 } 299}