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