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;
029
030import org.opencms.ade.configuration.formatters.CmsFormatterConfigurationCacheState;
031import org.opencms.file.CmsObject;
032import org.opencms.file.CmsResource;
033import org.opencms.file.CmsResourceFilter;
034import org.opencms.file.collectors.I_CmsCollectorPostCreateHandler;
035import org.opencms.flex.CmsFlexController;
036import org.opencms.jsp.util.CmsJspStandardContextBean;
037import org.opencms.main.CmsException;
038import org.opencms.main.CmsLog;
039import org.opencms.main.OpenCms;
040import org.opencms.util.CmsUUID;
041import org.opencms.xml.containerpage.CmsContainerElementBean;
042import org.opencms.xml.containerpage.I_CmsFormatterBean;
043
044import java.util.HashMap;
045import java.util.LinkedHashMap;
046import java.util.Map;
047import java.util.Map.Entry;
048
049import javax.servlet.ServletRequest;
050import javax.servlet.ServletResponse;
051import javax.servlet.jsp.tagext.BodyTagSupport;
052
053import org.apache.commons.logging.Log;
054
055/**
056 * The 'simpledisplay' tag can be used to display a single resource using a formatter. It also allows to activate direct editing.<p>
057 * It is less flexible but simpler to use than the 'display' tag in that it only allows you to specify a single, fixed formatter configuration as an attribute,
058 * rather than a set of type-dependent formatters with the displayformatter tag.
059 *
060 */
061public class CmsJspTagSimpleDisplay extends BodyTagSupport implements I_CmsJspTagParamParent {
062
063    /** The log object for this class. */
064    private static final Log LOG = CmsLog.getLog(CmsJspTagSimpleDisplay.class);
065
066    /** The serial version id. */
067    private static final long serialVersionUID = 2285680951218629093L;
068
069    /** Flag, indicating if the create option should be displayed. */
070    private boolean m_canCreate;
071
072    /** Flag, indicating if the delete option should be displayed. */
073    private boolean m_canDelete;
074
075    /** The tag attribute's value, specifying the path to the (sub)sitemap where new content should be created. */
076    private String m_creationSiteMap;
077
078    /** The editable flag. */
079    private boolean m_editable;
080
081    /** The settings parameter map. */
082    private Map<String, String> m_parameterMap;
083
084    /** The pass settings flag. */
085    private boolean m_passSettings;
086
087    /** The fully qualified class name of the post create handler to use. */
088    private String m_postCreateHandler;
089
090    /** The element settings to be used. */
091    private Map<String, String> m_settings;
092
093    /** The site path to the resource to display. */
094    private String m_value;
095
096    /** Stores the formatter path. */
097    private String m_formatterPath;
098
099    /**
100     * Constructor.<p>
101     */
102    public CmsJspTagSimpleDisplay() {
103
104        m_parameterMap = new LinkedHashMap<String, String>();
105    }
106
107    /**
108     * @see org.opencms.jsp.I_CmsJspTagParamParent#addParameter(java.lang.String, java.lang.String)
109     */
110    public void addParameter(String name, String value) {
111
112        // No null values allowed in parameters
113        if ((name == null) || (value == null)) {
114            return;
115        }
116
117        m_parameterMap.put(name, value);
118    }
119
120    /**
121     * @see javax.servlet.jsp.tagext.BodyTagSupport#doEndTag()
122     */
123    @Override
124    public int doEndTag() {
125
126        ServletRequest request = pageContext.getRequest();
127        ServletResponse response = pageContext.getResponse();
128        if (CmsFlexController.isCmsRequest(request)) {
129            // this will always be true if the page is called through OpenCms
130            CmsObject cms = CmsFlexController.getCmsObject(request);
131            try {
132                boolean isOnline = cms.getRequestContext().getCurrentProject().isOnlineProject();
133                CmsResource res = null;
134                if (CmsUUID.isValidUUID(m_value)) {
135                    CmsUUID structureId = new CmsUUID(m_value);
136                    res = isOnline
137                    ? cms.readResource(structureId)
138                    : cms.readResource(structureId, CmsResourceFilter.IGNORE_EXPIRATION);
139                } else {
140                    res = isOnline
141                    ? cms.readResource(m_value)
142                    : cms.readResource(m_value, CmsResourceFilter.IGNORE_EXPIRATION);
143                }
144                CmsResource formatterResource = cms.readResource(m_formatterPath);
145                CmsFormatterConfigurationCacheState formatterCache = OpenCms.getADEManager().getCachedFormatters(
146                    cms.getRequestContext().getCurrentProject().isOnlineProject());
147                I_CmsFormatterBean formatter = formatterCache.getFormatters().get(formatterResource.getStructureId());
148                Map<String, String> settings = new HashMap<String, String>();
149                String formatterId = formatter.getId();
150                int prefixLength = formatterId.length() + 1;
151                for (Entry<String, String> entry : m_parameterMap.entrySet()) {
152                    if (entry.getKey().startsWith(formatterId)) {
153                        settings.put(entry.getKey().substring(prefixLength), entry.getValue());
154                    } else if (!settings.containsKey(entry.getKey())) {
155                        settings.put(entry.getKey(), entry.getValue());
156                    }
157                }
158
159                CmsJspTagDisplay.displayAction(
160                    res,
161                    formatter,
162                    settings,
163                    m_editable,
164                    m_canCreate,
165                    m_canDelete,
166                    m_creationSiteMap,
167                    m_postCreateHandler,
168                    pageContext,
169                    request,
170                    response);
171            } catch (CmsException e) {
172                LOG.error(e.getLocalizedMessage(), e);
173            }
174        }
175        release();
176        return EVAL_PAGE;
177    }
178
179    /**
180     * @see javax.servlet.jsp.tagext.BodyTagSupport#doStartTag()
181     */
182    @Override
183    public int doStartTag() {
184
185        if (Boolean.valueOf(m_passSettings).booleanValue()) {
186            CmsContainerElementBean element = CmsJspStandardContextBean.getInstance(
187                pageContext.getRequest()).getElement();
188            if (element != null) {
189                m_parameterMap.putAll(element.getSettings());
190            }
191        }
192        if (m_settings != null) {
193            m_parameterMap.putAll(m_settings);
194        }
195
196        return EVAL_BODY_BUFFERED;
197    }
198
199    /**
200     * Returns the editable.<p>
201     *
202     * @return the editable
203     */
204    public boolean getEditable() {
205
206        return m_editable;
207    }
208
209    /**
210     * Returns the passSettings.<p>
211     *
212     * @return the passSettings
213     */
214    public boolean getPassSettings() {
215
216        return m_passSettings;
217    }
218
219    /**
220     * Returns the element settings to be used.<p>
221     *
222     * @return the element settings to be used
223     */
224    public Map<String, String> getSettings() {
225
226        return m_settings;
227    }
228
229    /**
230     * Returns the value.<p>
231     *
232     * @return the value
233     */
234    public String getValue() {
235
236        return m_value;
237    }
238
239    /**
240     * @see javax.servlet.jsp.tagext.BodyTagSupport#release()
241     */
242    @Override
243    public void release() {
244
245        super.release();
246        m_parameterMap.clear();
247        m_settings = null;
248        m_passSettings = false;
249        m_editable = false;
250        m_value = null;
251    }
252
253    /** Setter for the "create" attribute of the tag.
254     * @param canCreate value of the tag's attribute "create".
255     */
256    public void setCreate(boolean canCreate) {
257
258        m_canCreate = canCreate;
259    }
260
261    /** Setter for the "create" attribute of the tag.
262     * @param canCreate value of the tag's attribute "create".
263     */
264    public void setCreate(String canCreate) {
265
266        m_canCreate = Boolean.valueOf(canCreate).booleanValue();
267    }
268
269    /** Setter for the "creationSiteMap" attribute of the tag.
270     * @param sitePath value of the "creationSiteMap" attribute of the tag.
271     */
272    public void setCreationSiteMap(String sitePath) {
273
274        m_creationSiteMap = sitePath;
275    }
276
277    /**Setter for the "delete" attribute of the tag.
278     * @param canDelete value of the "delete" attribute of the tag.
279     */
280    public void setDelete(boolean canDelete) {
281
282        m_canDelete = canDelete;
283    }
284
285    /**Setter for the "delete" attribute of the tag.
286     * @param canDelete value of the "delete" attribute of the tag.
287     */
288    public void setDelete(String canDelete) {
289
290        m_canDelete = Boolean.valueOf(canDelete).booleanValue();
291    }
292
293    /**
294     * Sets the editable.<p>
295     *
296     * @param editable the editable to set
297     */
298    public void setEditable(boolean editable) {
299
300        m_editable = editable;
301    }
302
303    /**
304     * Sets the editable.<p>
305     *
306     * @param editable the editable to set
307     */
308    public void setEditable(String editable) {
309
310        m_editable = Boolean.valueOf(editable).booleanValue();
311    }
312
313    /**
314     * Sets the formatter path.<p>
315     *
316     * @param formatter the formatter path
317     */
318    public void setFormatter(String formatter) {
319
320        m_formatterPath = formatter;
321    }
322
323    /**
324     * Sets the passSettings.<p>
325     *
326     * @param passSettings the passSettings to set
327     */
328    public void setPassSettings(Boolean passSettings) {
329
330        m_passSettings = passSettings.booleanValue();
331    }
332
333    /** Setter for the "postCreateHandler" attribute of the tag.
334     * @param postCreateHandler fully qualified class name of the {@link I_CmsCollectorPostCreateHandler} to use.
335     */
336    public void setPostCreateHandler(final String postCreateHandler) {
337
338        m_postCreateHandler = postCreateHandler;
339    }
340
341    /**
342     * Sets the element settings to be used.<p>
343     *
344     * @param settings the element settings to be used
345     */
346    public void setSettings(Map<String, String> settings) {
347
348        m_settings = settings;
349    }
350
351    /**
352     * Sets the value.<p>
353     *
354     * @param value the value to set
355     */
356    public void setValue(String value) {
357
358        m_value = value;
359    }
360
361}