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.util;
029
030import org.opencms.file.CmsObject;
031import org.opencms.gwt.shared.CmsGwtConstants;
032import org.opencms.jsp.CmsJspResourceWrapper;
033import org.opencms.main.CmsException;
034import org.opencms.main.CmsLog;
035import org.opencms.util.CmsStringUtil;
036import org.opencms.xml.types.I_CmsXmlContentValue;
037
038import java.util.Collection;
039import java.util.Date;
040
041import org.apache.commons.logging.Log;
042
043/**
044 * Provides access to common object types through wrappers.<p>
045 *
046 * @since 11.0
047 */
048public final class CmsJspObjectValueWrapper extends A_CmsJspValueWrapper {
049
050    /** The log object for this class. */
051    private static final Log LOG = CmsLog.getLog(CmsJspObjectValueWrapper.class);
052
053    /** Constant for the null (non existing) value. */
054    protected static final CmsJspObjectValueWrapper NULL_VALUE_WRAPPER = new CmsJspObjectValueWrapper();
055
056    /** Calculated hash code. */
057    private int m_hashCode;
058
059    /** The wrapped XML content value. */
060    private Object m_object;
061
062    /**
063     * Private constructor, used for creation of NULL constant value, use factory method to create instances.<p>
064     *
065     * @see #createWrapper(CmsObject, Object)
066     */
067    private CmsJspObjectValueWrapper() {
068
069        // cast needed to avoid compiler confusion with constructors
070        this((CmsObject)null, (I_CmsXmlContentValue)null);
071    }
072
073    /**
074     * Private constructor, use factory method to create instances.<p>
075     *
076     * @param cms the current users OpenCms context
077     * @param value the object to warp
078     */
079    private CmsJspObjectValueWrapper(CmsObject cms, Object value) {
080
081        // a null value is used for constant generation
082        m_cms = cms;
083        m_object = value;
084    }
085
086    /**
087     * Factory method to create a new XML content value wrapper.<p>
088     *
089     * In case either parameter is <code>null</code>, the {@link #NULL_VALUE_WRAPPER} is returned.<p>
090     *
091     * @param cms the current users OpenCms context
092     * @param value the object to warp
093     *
094     * @return a new content value wrapper instance, or <code>null</code> if any parameter is <code>null</code>
095     */
096    public static CmsJspObjectValueWrapper createWrapper(CmsObject cms, Object value) {
097
098        if ((value != null) && (cms != null)) {
099            return new CmsJspObjectValueWrapper(cms, value);
100        }
101        // if no value is available,
102        return NULL_VALUE_WRAPPER;
103    }
104
105    /**
106     * Returns if direct edit is enabled.<p>
107     *
108     * @param cms the current cms context
109     *
110     * @return <code>true</code> if direct edit is enabled
111     */
112    static boolean isDirectEditEnabled(CmsObject cms) {
113
114        return !cms.getRequestContext().getCurrentProject().isOnlineProject()
115            && (cms.getRequestContext().getAttribute(CmsGwtConstants.PARAM_DISABLE_DIRECT_EDIT) == null);
116    }
117
118    /**
119     * Returns <code>true</code> in case there was an object wrapped.<p>
120     *
121     * @return <code>true</code> in case there was an object wrapped
122     */
123    @Override
124    public boolean getExists() {
125
126        return m_object != null;
127    }
128
129    /**
130     * Returns <code>true</code> in case the object is empty, that is either <code>null</code> or an empty String.<p>
131     *
132     * In case the object does not exist, <code>true</code> is returned.<p>
133     *
134     * @return <code>true</code> in case the object is empty
135     */
136    @SuppressWarnings("rawtypes")
137    @Override
138    public boolean getIsEmpty() {
139
140        if (m_object == null) {
141            // this is the case for non existing values
142            return true;
143        } else if (m_object instanceof String) {
144            // return values for String
145            return CmsStringUtil.isEmpty((String)m_object);
146        } else if (m_object instanceof Collection) {
147            // if a collection has any entries it is not emtpy
148            return !((Collection)m_object).isEmpty();
149        } else {
150            // assume all other non-null objects are not empty
151            return false;
152        }
153    }
154
155    /**
156     * Returns <code>true</code> in case the object is empty or whitespace only, that is either <code>null</code> or an empty String.<p>
157     *
158     * In case the object does not exist, <code>true</code> is returned.<p>
159     *
160     * @return <code>true</code> in case the object is empty
161     */
162    @Override
163    public boolean getIsEmptyOrWhitespaceOnly() {
164
165        if (m_object instanceof String) {
166            // return values for simple type
167            return CmsStringUtil.isEmptyOrWhitespaceOnly((String)m_object);
168        } else {
169            // use isEmpty() for all other object types
170            return getIsEmpty();
171        }
172    }
173
174    /**
175     * Returns the wrapped object value.<p>
176     *
177     * @return the wrapped object value
178     */
179    @Override
180    public Object getObjectValue() {
181
182        return m_object;
183    }
184
185    /**
186     * Converts the wrapped value to a date. Keeps it as date, if it already is one.<p>
187     *
188     * @return the date
189     *
190     * @see A_CmsJspValueWrapper#getToDate()
191     */
192    @Override
193    public Date getToDate() {
194
195        if (m_object instanceof Date) {
196            return (Date)m_object;
197        }
198        return super.getToDate();
199    }
200
201    /**
202     * @see org.opencms.jsp.util.A_CmsJspValueWrapper#getToResource()
203     */
204    @Override
205    public CmsJspResourceWrapper getToResource() {
206
207        try {
208            return CmsJspElFunctions.convertResource(m_cms, m_object);
209        } catch (CmsException e) {
210            if (LOG.isInfoEnabled()) {
211                LOG.info("Failed to convert object \"" + getToString() + "\" to a resource.", e);
212            }
213            return null;
214        }
215    }
216
217    /**
218     * @see java.lang.Object#hashCode()
219     */
220    @Override
221    public int hashCode() {
222
223        if (m_object == null) {
224            return 0;
225        }
226        if (m_hashCode == 0) {
227            m_hashCode = m_object.toString().hashCode();
228        }
229        return m_hashCode;
230    }
231
232    /**
233     * @see java.lang.Object#toString()
234     */
235    @Override
236    public String toString() {
237
238        if (m_object == null) {
239            // this is the case for non existing values
240            return "";
241        } else {
242            return m_object.toString();
243        }
244    }
245}