001/*
002 *  Copyright 2018 Anyware Services
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package org.ametys.plugins.repository.data.holder;
017
018import java.util.Collection;
019import java.util.Locale;
020
021import org.xml.sax.ContentHandler;
022import org.xml.sax.SAXException;
023
024import org.ametys.plugins.repository.data.holder.group.Composite;
025import org.ametys.plugins.repository.data.holder.group.Repeater;
026import org.ametys.plugins.repository.data.holder.impl.DataHolderHelper;
027import org.ametys.plugins.repository.metadata.MultilingualString;
028import org.ametys.runtime.model.exception.BadItemTypeException;
029import org.ametys.runtime.model.exception.NotUniqueTypeException;
030import org.ametys.runtime.model.exception.UndefinedItemPathException;
031import org.ametys.runtime.model.exception.UnknownTypeException;
032
033/**
034 * Interface for data containers
035 */
036public interface DataHolder
037{
038    /**
039     * Retrieves the composite at the given path
040     * @param compositePath path of the composite to retrieve
041     * @return the composite or <code>null</code> if not exists
042     * @throws IllegalArgumentException if the given composite path is null or empty
043     * @throws BadItemTypeException if the value stored in the repository at the given path is not a composite
044     */
045    public Composite getComposite(String compositePath) throws IllegalArgumentException, BadItemTypeException;
046    
047    /**
048     * Retrieves the repeater at the given path
049     * @param repeaterPath path of the repeater to retrieve
050     * @return the repeater or <code>null</code> if not exists
051     * @throws IllegalArgumentException if the given repeater path is null or empty
052     * @throws BadItemTypeException if the value stored in the repository at the given path is not a repeater
053     */
054    public Repeater getRepeater(String repeaterPath) throws IllegalArgumentException, BadItemTypeException;
055    
056    /**
057     * Checks if there is a value for the data at the given path
058     * @param dataPath path of the data
059     * @throws IllegalArgumentException if the given data path is null or empty
060     * @return <code>true</code> if there is value for the data, <code>false</code> otherwise
061     */
062    public boolean hasValue(String dataPath) throws IllegalArgumentException;
063    
064    /**
065     * Retrieves the names of data contained by this data holder
066     * @return the paths of all data contained by this data holder
067     */
068    public Collection<String> getDataNames();
069    
070    /**
071     * Copies the current {@link DataHolder} to the given {@link ModifiableModelAwareDataHolder}.
072     * @param dataHolder The destination dataHolder. Can not be null.
073     * @throws UndefinedItemPathException if one of the copied data is not defined by the model of the destination {@link ModifiableModelAwareDataHolder}
074     * @throws BadItemTypeException if the type defined by the model of the destination {@link ModifiableModelAwareDataHolder} doesn't match the copied value
075     * @throws UnknownTypeException if there is no available type compatible with the copied value for the type extension point of the destination {@link ModifiableModelLessDataHolder}
076     * @throws NotUniqueTypeException if there is more than one available types compatibles with the copied value for the type extension point of the destination {@link ModifiableModelLessDataHolder}
077     */
078    public default void copyTo(ModifiableDataHolder dataHolder) throws UndefinedItemPathException, BadItemTypeException, UnknownTypeException, NotUniqueTypeException
079    {
080        DataHolderHelper.copyDataHolder(this, dataHolder);
081    }
082    
083    /**
084     * Generates SAX events for current {@link DataHolder}
085     * @param contentHandler the {@link ContentHandler} that will receive the SAX events
086     * @throws SAXException if an error occurs during the SAX events generation
087     */
088    public default void toSAX(ContentHandler contentHandler) throws SAXException
089    {
090        toSAX(contentHandler, null);
091    }
092    
093    /**
094     * Generates SAX events for the current {@link DataHolder}
095     * @param contentHandler the {@link ContentHandler} that will receive the SAX events
096     * @param locale The locale to use for localized data, such as {@link MultilingualString}. Can be <code>null</code> to generate SAX events for all existing {@link Locale}s.
097     * @throws SAXException if an error occurs during the SAX events generation
098     */
099    public default void toSAX(ContentHandler contentHandler, Locale locale) throws SAXException
100    {
101        DataHolderHelper.dataHolderToSAX(this, contentHandler, locale);
102    }
103    
104    /**
105     * Generates SAX events for the data at the given data path in the current {@link DataHolder}
106     * Do not generate any event if there is no values at the given path
107     * @param contentHandler the {@link ContentHandler} that will receive the SAX events
108     * @param dataPath the path of the data to SAX
109     * @throws SAXException if an error occurs during the SAX events generation
110     */
111    public default void dataToSAX(ContentHandler contentHandler, String dataPath) throws SAXException
112    {
113        dataToSAX(contentHandler, dataPath, null, null);
114    }
115    
116    /**
117     * Generates SAX events for the data at the given data path in the current {@link DataHolder}
118     * Do not generate any event if there is no values at the given path
119     * @param contentHandler the {@link ContentHandler} that will receive the SAX events
120     * @param dataPath the path of the data to SAX
121     * @param tagName the tag name of the SAX event to generate. If <code>null</code> the name of the data is used
122     * @throws SAXException if an error occurs during the SAX events generation
123     */
124    public default void dataToSAX(ContentHandler contentHandler, String dataPath, String tagName) throws SAXException
125    {
126        dataToSAX(contentHandler, dataPath, tagName, null);
127    }
128    
129    /**
130     * Generates SAX events for the data at the given data path in the current {@link DataHolder}
131     * Do not generate any event if there is no values at the given path
132     * @param contentHandler the {@link ContentHandler} that will receive the SAX events
133     * @param dataPath the path of the data to SAX
134     * @param locale The locale to use for localized data, such as {@link MultilingualString}. Can be <code>null</code> to generate SAX events for all existing {@link Locale}s.
135     * @throws SAXException if an error occurs during the SAX events generation
136     */
137    public default void dataToSAX(ContentHandler contentHandler, String dataPath, Locale locale) throws SAXException
138    {
139        dataToSAX(contentHandler, dataPath, null, locale);
140    }
141    
142    /**
143     * Generates SAX events for the data at the given data path in the current {@link DataHolder}
144     * Do not generate any event if there is no values at the given path
145     * @param contentHandler the {@link ContentHandler} that will receive the SAX events
146     * @param dataPath the path of the data to SAX
147     * @param tagName the tag name of the SAX event to generate. If <code>null</code> the name of the data is used
148     * @param locale The locale to use for localized data, such as {@link MultilingualString}. Can be <code>null</code> to generate SAX events for all existing {@link Locale}s.
149     * @throws SAXException if an error occurs during the SAX events generation
150     */
151    public default void dataToSAX(ContentHandler contentHandler, String dataPath, String tagName, Locale locale) throws SAXException
152    {
153        DataHolderHelper.dataHolderValueToSAX(this, contentHandler, dataPath, tagName, locale);
154    }
155}