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.CmsFile;
031import org.opencms.file.CmsObject;
032import org.opencms.json.JSONArray;
033import org.opencms.main.CmsIllegalArgumentException;
034import org.opencms.main.CmsRuntimeException;
035import org.opencms.relations.CmsCategory;
036import org.opencms.relations.CmsCategoryService;
037import org.opencms.xml.I_CmsXmlDocument;
038import org.opencms.xml.content.CmsXmlContent;
039import org.opencms.xml.xml2json.I_CmsJsonFormattableValue;
040
041import java.util.List;
042import java.util.Locale;
043
044import org.dom4j.Element;
045
046/**
047 * Describes the XML content type "OpenCmsVfsFile".<p>
048 *
049 * This type allows links to internal VFS resources only.<p>
050 *
051 * @since 7.0.0
052 */
053public class CmsXmlDynamicCategoryValue extends A_CmsXmlContentValue implements I_CmsJsonFormattableValue {
054
055    /** Temporary element used for storing the categories as a string in the XML for validation purposes. This is thrown out before the file is actually saved to the database. */
056    public static final String N_CATEGORY_STRING = "category-string";
057
058    /** The name of this type as used in the XML schema. */
059    public static final String TYPE_NAME = "OpenCmsDynamicCategory";
060
061    /** The schema definition String is located in a text for easier editing. */
062    private static String m_schemaDefinition;
063
064    /**
065     * Creates a new, empty schema type descriptor of type "OpenCmsCategoryValue".<p>
066     */
067    public CmsXmlDynamicCategoryValue() {
068
069        // empty constructor is required for class registration
070    }
071
072    /**
073     * Creates a new XML content value of type "OpenCmsCategoryValue".<p>
074     *
075     * @param document the XML content instance this value belongs to
076     * @param element the XML element that contains this value
077     * @param locale the locale this value is created for
078     * @param type the type instance to create the value for
079     */
080    public CmsXmlDynamicCategoryValue(
081        I_CmsXmlDocument document,
082        Element element,
083        Locale locale,
084        I_CmsXmlSchemaType type) {
085
086        super(document, element, locale, type);
087    }
088
089    /**
090     * Creates a new schema type descriptor for the type "OpenCmsCategoryValue".<p>
091     *
092     * @param name the name of the XML node containing the value according to the XML schema
093     * @param minOccurs minimum number of occurrences of this type according to the XML schema
094     * @param maxOccurs maximum number of occurrences of this type according to the XML schema
095     */
096    public CmsXmlDynamicCategoryValue(String name, String minOccurs, String maxOccurs) {
097
098        super(name, minOccurs, maxOccurs);
099    }
100
101    /**
102     * @see org.opencms.xml.types.A_CmsXmlContentValue#createValue(I_CmsXmlDocument, org.dom4j.Element, Locale)
103     */
104    public I_CmsXmlContentValue createValue(I_CmsXmlDocument document, Element element, Locale locale) {
105
106        return new CmsXmlDynamicCategoryValue(document, element, locale, this);
107    }
108
109    /**
110     * @see org.opencms.xml.types.I_CmsXmlSchemaType#generateXml(org.opencms.file.CmsObject, org.opencms.xml.I_CmsXmlDocument, org.dom4j.Element, java.util.Locale)
111     */
112    @Override
113    public Element generateXml(CmsObject cms, I_CmsXmlDocument document, Element root, Locale locale) {
114
115        Element element = root.addElement(getName());
116        element.addComment("Categories are read dynamically");
117
118        return element;
119    }
120
121    /**
122     * @see org.opencms.xml.types.I_CmsXmlContentValue#getPlainText(org.opencms.file.CmsObject)
123     */
124    @Override
125    public String getPlainText(CmsObject cms) {
126
127        return getStringValue(cms);
128    }
129
130    /**
131     * @see org.opencms.xml.types.I_CmsXmlSchemaType#getSchemaDefinition()
132     */
133    public String getSchemaDefinition() {
134
135        if (m_schemaDefinition == null) {
136            m_schemaDefinition = readSchemaDefinition("org/opencms/xml/types/XmlDynamicCategoryValue.xsd");
137        }
138        return m_schemaDefinition;
139    }
140
141    /**
142     * @see org.opencms.xml.types.I_CmsXmlContentValue#getStringValue(CmsObject)
143     */
144    public String getStringValue(CmsObject cms) throws CmsRuntimeException {
145
146        Element categoryElement = categoryStringElem(false);
147        if (categoryElement == null) {
148            return "";
149        }
150        return categoryElement.getText();
151    }
152
153    /**
154     * @see org.opencms.xml.types.A_CmsXmlContentValue#getTypeName()
155     */
156    public String getTypeName() {
157
158        return TYPE_NAME;
159    }
160
161    /**
162     * @see org.opencms.xml.types.A_CmsXmlContentValue#isSearchable()
163     */
164    @Override
165    public boolean isSearchable() {
166
167        // there is no point in an empty node
168        return false;
169    }
170
171    /**
172     * @see org.opencms.xml.types.A_CmsXmlContentValue#newInstance(java.lang.String, java.lang.String, java.lang.String)
173     */
174    public I_CmsXmlSchemaType newInstance(String name, String minOccurs, String maxOccurs) {
175
176        return new CmsXmlDynamicCategoryValue(name, minOccurs, maxOccurs);
177    }
178
179    /**
180     * @see org.opencms.xml.types.A_CmsXmlContentValue#setStringValue(org.opencms.file.CmsObject, java.lang.String)
181     */
182    public void setStringValue(CmsObject cms, String value) throws CmsIllegalArgumentException {
183
184        categoryStringElem(true).setText(value);
185    }
186
187    /**
188     * Gets the category-string subelement, creating it if necessary.
189     *
190     * @param create if true, the category string element is created if it doesn't exist; if false, null is returned in that case.
191     * @return the category-string subelement
192     */
193    Element categoryStringElem(boolean create) {
194
195        Element result = m_element.element(N_CATEGORY_STRING);
196        if ((result == null) && create) {
197            result = m_element.addElement(N_CATEGORY_STRING);
198            result.detach();
199            m_element.elements().add(0, result);
200        }
201        return result;
202    }
203
204    /**
205     * @see org.opencms.xml.xml2json.I_CmsJsonFormattableValue#toJson(org.opencms.file.CmsObject)
206     */
207    public Object toJson(CmsObject cms) {
208
209        CmsXmlContent content = (CmsXmlContent)getDocument();
210        JSONArray array = new JSONArray();
211        try {
212            CmsFile file = content.getFile();
213            List<CmsCategory> categories = CmsCategoryService.getInstance().readResourceCategories(cms, file);
214            for (CmsCategory cat : categories) {
215                array.put(cat.getPath());
216            }
217            return array;
218        } catch (Exception e) {
219            return array;
220        }
221    }
222
223}