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.io.IOException;
019import java.util.Collection;
020import java.util.Locale;
021
022import org.xml.sax.ContentHandler;
023import org.xml.sax.SAXException;
024
025import org.ametys.plugins.repository.data.holder.group.Composite;
026import org.ametys.plugins.repository.data.holder.impl.DataHolderHelper;
027import org.ametys.plugins.repository.data.repositorydata.RepositoryData;
028import org.ametys.plugins.repository.metadata.MultilingualString;
029import org.ametys.runtime.model.exception.BadItemTypeException;
030import org.ametys.runtime.model.exception.NotUniqueTypeException;
031import org.ametys.runtime.model.exception.UndefinedItemPathException;
032import org.ametys.runtime.model.exception.UnknownTypeException;
033
034/**
035 * Interface for data containers
036 */
037public interface DataHolder
038{
039    /**
040     * Retrieves the composite at the given path
041     * @param compositePath path of the composite to retrieve
042     * @return the composite or <code>null</code> if not exists or is empty
043     * @throws IllegalArgumentException if the given composite path is null or empty
044     * @throws BadItemTypeException if the stored value at the given path is not a composite
045     */
046    public Composite getComposite(String compositePath) throws IllegalArgumentException, BadItemTypeException;
047    
048    /**
049     * Checks if there is a value for the data at the given path
050     * @param dataPath path of the data
051     * @return <code>true</code> if there is value (even empty) for the data, <code>false</code> otherwise
052     * @throws IllegalArgumentException if the given data path is null or empty
053     */
054    public boolean hasValue(String dataPath) throws IllegalArgumentException;
055    
056    /**
057     * Retrieves the names of data contained by this data holder
058     * Retrieves only the data at first level, does not check composite data 
059     * @return the names of all data contained by this data holder
060     */
061    public Collection<String> getDataNames();
062    
063    /**
064     * Copies the current {@link DataHolder} to the given {@link ModifiableModelAwareDataHolder}.
065     * @param dataHolder The destination dataHolder. Can not be null.
066     * @throws UndefinedItemPathException if one of the copied data is not defined by the model of the destination {@link ModifiableModelAwareDataHolder}
067     * @throws BadItemTypeException if the type defined by the model of the destination {@link ModifiableModelAwareDataHolder} doesn't match the copied value
068     * @throws UnknownTypeException if there is no available type compatible with the copied value for the type extension point of the destination {@link ModifiableModelLessDataHolder}
069     * @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}
070     */
071    public default void copyTo(ModifiableDataHolder dataHolder) throws UndefinedItemPathException, BadItemTypeException, UnknownTypeException, NotUniqueTypeException
072    {
073        DataHolderHelper.copyTo(this, dataHolder);
074    }
075    
076    /**
077     * Generates SAX events for the data at the given data path in the current {@link DataHolder}
078     * Do not generate any event if there is no values at the given path
079     * @param contentHandler the {@link ContentHandler} that will receive the SAX events
080     * @param dataPath the path of the data to SAX
081     * @throws SAXException if an error occurs during the SAX events generation
082     * @throws IOException if an error occurs while reading a value using the I/O API
083     */
084    public default void dataToSAX(ContentHandler contentHandler, String dataPath) throws SAXException, IOException
085    {
086        dataToSAX(contentHandler, dataPath, null);
087    }
088    
089    /**
090     * Generates SAX events for the data at the given data path in the current {@link DataHolder}
091     * Do not generate any event if there is no values at the given path
092     * @param contentHandler the {@link ContentHandler} that will receive the SAX events
093     * @param dataPath the path of the data to SAX
094     * @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.
095     * @throws SAXException if an error occurs during the SAX events generation
096     * @throws IOException if an error occurs while reading a value using the I/O API
097     */
098    public void dataToSAX(ContentHandler contentHandler, String dataPath, Locale locale) throws SAXException, IOException;
099    
100    /**
101     * Retrieves the repository data used by this {@link DataHolder}
102     * @return the repository data used by this {@link DataHolder}
103     */
104    public RepositoryData getRepositoryData();
105}