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.xml.types;
029
030import org.opencms.file.CmsObject;
031import org.opencms.i18n.CmsMessageContainer;
032import org.opencms.json.JSONObject;
033import org.opencms.widgets.serialdate.CmsSerialDateValue;
034import org.opencms.xml.I_CmsXmlDocument;
035import org.opencms.xml.xml2json.I_CmsJsonFormattableValue;
036
037import java.util.Locale;
038
039import org.dom4j.Element;
040
041/**
042 * Describes the XML content type "OpenCmsSerialDate".<p>
043 *
044 * @since 11.0.0
045 */
046public class CmsXmlSerialDateValue extends A_CmsXmlValueTextBase
047implements I_CmsXmlValidateWithMessage, I_CmsJsonFormattableValue {
048
049    /** The name of this type as used in the XML schema. */
050    public static final String TYPE_NAME = "OpenCmsSerialDate";
051
052    /**
053     * Creates a new, empty schema type descriptor of type "OpenCmsDateTime".<p>
054     */
055    public CmsXmlSerialDateValue() {
056
057        // empty constructor is required for class registration
058    }
059
060    /**
061     * Creates a new XML content value of type "OpenCmsSerialDate".<p>
062     *
063     * @param document the XML content instance this value belongs to
064     * @param element the XML element that contains this value
065     * @param locale the locale this value is created for
066     * @param type the type instance to create the value for
067     */
068    public CmsXmlSerialDateValue(I_CmsXmlDocument document, Element element, Locale locale, I_CmsXmlSchemaType type) {
069
070        super(document, element, locale, type);
071    }
072
073    /**
074     * Creates a new schema type descriptor for the type "OpenCmsDateTime".<p>
075     *
076     * @param name the name of the XML node containing the value according to the XML schema
077     * @param minOccurs minimum number of occurrences of this type according to the XML schema
078     * @param maxOccurs maximum number of occurrences of this type according to the XML schema
079     */
080    public CmsXmlSerialDateValue(String name, String minOccurs, String maxOccurs) {
081
082        super(name, minOccurs, maxOccurs);
083    }
084
085    /**
086     * @see org.opencms.xml.types.A_CmsXmlContentValue#createValue(I_CmsXmlDocument, org.dom4j.Element, Locale)
087     */
088    public I_CmsXmlContentValue createValue(I_CmsXmlDocument document, Element element, Locale locale) {
089
090        return new CmsXmlSerialDateValue(document, element, locale, this);
091    }
092
093    /**
094     * @see org.opencms.xml.types.A_CmsXmlContentValue#getDefault(Locale)
095     */
096    @Override
097    public String getDefault(Locale locale) {
098
099        if (m_defaultValue != null) {
100            return m_defaultValue;
101        }
102        return "";
103    }
104
105    /**
106     * @see org.opencms.xml.types.I_CmsXmlSchemaType#getSchemaDefinition()
107     */
108    public String getSchemaDefinition() {
109
110        return "<xsd:simpleType name=\"" + TYPE_NAME + "\"><xsd:restriction base=\"xsd:string\" /></xsd:simpleType>";
111    }
112
113    /**
114     * @see org.opencms.xml.types.A_CmsXmlContentValue#getTypeName()
115     */
116    public String getTypeName() {
117
118        return TYPE_NAME;
119    }
120
121    /**
122     * @see org.opencms.xml.types.A_CmsXmlContentValue#isSearchable()
123     */
124    @Override
125    public boolean isSearchable() {
126
127        // there is no point in searching date/time values
128        return false;
129    }
130
131    /**
132     * @see org.opencms.xml.types.A_CmsXmlContentValue#newInstance(java.lang.String, java.lang.String, java.lang.String)
133     */
134    public I_CmsXmlSchemaType newInstance(String name, String minOccurs, String maxOccurs) {
135
136        return new CmsXmlSerialDateValue(name, minOccurs, maxOccurs);
137    }
138
139    /**
140     * @see org.opencms.xml.xml2json.I_CmsJsonFormattableValue#toJson(org.opencms.file.CmsObject)
141     */
142    @SuppressWarnings("finally")
143    public Object toJson(CmsObject cms) {
144
145        JSONObject result = null;
146        try {
147            result = new JSONObject(getStringValue(cms));
148        } catch (Exception e) {
149            result = new JSONObject("{}");
150        } finally {
151            return result;
152        }
153    }
154
155    /**
156     * @see org.opencms.xml.types.I_CmsXmlSchemaType#validateValue(java.lang.String)
157     */
158    @Override
159    public boolean validateValue(String value) {
160
161        return null == validateWithMessage(value);
162    }
163
164    /**
165     * @see org.opencms.xml.types.I_CmsXmlValidateWithMessage#validateWithMessage(java.lang.String)
166     */
167    public CmsMessageContainer validateWithMessage(String value) {
168
169        CmsSerialDateValue wrapper = new CmsSerialDateValue(value);
170        return wrapper.validateWithMessage();
171    }
172}