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.setPageId(getRequest().getParameter(I_CmsGalleryProviderConstants.CONFIG_PAGE_ID));
197        conf.setReferencePath(getRequest().getParameter(I_CmsGalleryProviderConstants.CONFIG_REFERENCE_PATH));
198        conf.setGalleryPath(getRequest().getParameter(I_CmsGalleryProviderConstants.CONFIG_GALLERY_PATH));
199        conf.setCurrentElement(getRequest().getParameter(I_CmsGalleryProviderConstants.CONFIG_CURRENT_ELEMENT));
200        String resourceTypes = getRequest().getParameter(I_CmsGalleryProviderConstants.CONFIG_RESOURCE_TYPES);
201        String useLinkDefaultTypes = getRequest().getParameter(
202            I_CmsGalleryProviderConstants.PARAM_USE_LINK_DEFAULT_TYPES);
203        if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(resourceTypes)) {
204            conf.setResourceTypes(Arrays.asList(resourceTypes.split(",")));
205        }
206        if (Boolean.parseBoolean(useLinkDefaultTypes)) {
207            try {
208                CmsObject cms = getCmsObject();
209                CmsResource referenceResource = cms.readResource(
210                    conf.getReferencePath(),
211                    CmsResourceFilter.IGNORE_EXPIRATION);
212                String searchTypes = CmsVfsFileWidget.getDefaultSearchTypes(cms, referenceResource);
213                conf.setSearchTypes(CmsStringUtil.splitAsList(searchTypes, ","));
214            } catch (CmsException e) {
215                LOG.warn(e.getLocalizedMessage(), e);
216            }
217        }
218
219        String galleryTypes = getRequest().getParameter(I_CmsGalleryProviderConstants.CONFIG_GALLERY_TYPES);
220        if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(galleryTypes)) {
221            conf.setGalleryTypes(galleryTypes.split(","));
222        }
223        String tabs = getRequest().getParameter(I_CmsGalleryProviderConstants.CONFIG_TAB_CONFIG);
224        if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(tabs)) {
225            conf.setTabConfiguration(CmsGalleryTabConfiguration.resolve(tabs));
226        } else {
227            conf.setTabConfiguration(CmsGalleryTabConfiguration.getDefault());
228        }
229        String galleryStoragePrefix = getRequest().getParameter(
230            I_CmsGalleryProviderConstants.CONFIG_GALLERY_STORAGE_PREFIX);
231        if (CmsStringUtil.isEmptyOrWhitespaceOnly(galleryStoragePrefix)) {
232            galleryStoragePrefix = "";
233        }
234        conf.setGalleryStoragePrefix(galleryStoragePrefix);
235        return conf;
236    }
237
238    /**
239     * Returns the serialized initial data for gallery dialog depending on the given mode.<p>
240     *
241     * @param galleryMode the gallery mode
242     *
243     * @return the data
244     *
245     * @throws Exception if something goes wrong
246     */
247    private String export(GalleryMode galleryMode) throws Exception {
248
249        CmsGalleryConfiguration conf = createGalleryConfigurationFromRequest(galleryMode);
250        CmsGalleryDataBean data = CmsGalleryService.getInitialSettings(getRequest(), conf);
251        CmsGallerySearchBean search = null;
252        if (GalleryTabId.cms_tab_results.equals(data.getStartTab())) {
253            search = CmsGalleryService.getSearch(getRequest(), data);
254        }
255        Set<String> folderFilter = data.getStartFolderFilter();
256        if ((folderFilter != null) && !folderFilter.isEmpty()) {
257            data.setVfsPreloadData(CmsGalleryService.generateVfsPreloadData(getCmsObject(), null, folderFilter));
258        }
259        if ((search != null) && (search.getScope() != null) && (search.getScope() != data.getScope())) {
260            // default selected scope option should be the one for which the search has been actually performed
261            data.setScope(search.getScope());
262        } else if ((search != null) && (search.getScope() == null)) {
263            data.setScope(OpenCms.getWorkplaceManager().getGalleryDefaultScope());
264        }
265
266        StringBuffer sb = new StringBuffer();
267        sb.append(
268            exportDictionary(
269                CmsGalleryDataBean.DICT_NAME,
270                I_CmsGalleryService.class.getMethod("getInitialSettings", CmsGalleryConfiguration.class),
271                data));
272        sb.append(
273            exportDictionary(
274                CmsGallerySearchBean.DICT_NAME,
275                I_CmsGalleryService.class.getMethod("getSearch", CmsGalleryDataBean.class),
276                search));
277        return sb.toString();
278    }
279
280    /**
281     * Returns a javascript tag that contains a variable deceleration that has the close link as value.<p>
282     *
283     * @return a javascript tag that contains a variable deceleration that has the close link as value
284     */
285    private String exportCloseLink() {
286
287        String closeLink = null;
288        if (getRequest().getAttribute(I_CmsGalleryProviderConstants.ATTR_CLOSE_LINK) != null) {
289            closeLink = (String)getRequest().getAttribute(I_CmsGalleryProviderConstants.ATTR_CLOSE_LINK);
290        }
291        if (CmsStringUtil.isEmptyOrWhitespaceOnly(closeLink)) {
292            closeLink = CmsWorkplace.FILE_EXPLORER_FILELIST;
293        }
294
295        StringBuffer sb = new StringBuffer();
296        // var closeLink = '/system/workplace/views/explorer/explorer_files.jsp';
297        sb.append(wrapScript("var ", I_CmsGalleryProviderConstants.ATTR_CLOSE_LINK, " = \'", link(closeLink), "\';"));
298        return sb.toString();
299    }
300}