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.Optional;
019
020import org.xml.sax.ContentHandler;
021import org.xml.sax.SAXException;
022
023import org.ametys.plugins.repository.data.UnknownDataException;
024import org.ametys.plugins.repository.data.holder.group.impl.ModelLessComposite;
025import org.ametys.plugins.repository.data.type.RepositoryModelItemType;
026import org.ametys.runtime.model.exception.BadDataPathCardinalityException;
027import org.ametys.runtime.model.exception.BadItemTypeException;
028import org.ametys.runtime.model.exception.NotUniqueTypeException;
029import org.ametys.runtime.model.exception.UnknownTypeException;
030import org.ametys.runtime.model.type.DataContext;
031
032/**
033 * Interface for data containers without models
034 */
035public interface ModelLessDataHolder extends DataHolder
036{
037    /**
038     * {@inheritDoc}
039     * @throws BadDataPathCardinalityException if the value of a part of the data path is multiple. Only the last part can be multiple
040     */
041    @Override
042    public ModelLessComposite getComposite(String compositePath) throws IllegalArgumentException, BadItemTypeException, BadDataPathCardinalityException;
043    
044    /**
045     * {@inheritDoc}
046     * @throws BadDataPathCardinalityException if the value of a part of the data path is multiple. Only the last part can be multiple
047     */
048    @Override
049    public boolean hasValue(String dataPath) throws IllegalArgumentException, BadDataPathCardinalityException;
050    
051    /**
052     * {@inheritDoc}
053     * @throws BadDataPathCardinalityException if the value of a part of the data path is multiple. Only the last part can be multiple
054     */
055    @Override
056    public boolean hasValueOrEmpty(String dataPath) throws IllegalArgumentException, BadDataPathCardinalityException;
057    
058    /**
059     * Retrieves the value of the data at the given path
060     * The type of the value will be deduced from the stored data.
061     * In some cases, the type can be wrong. For example, it is impossible to know if a stored date is a date or a date time
062     * @param <T> type of the value to retrieve. Should match the given data type
063     * @param dataPath path of the data
064     * @return the value of the data or <code>null</code> if not exists or is empty. The object returned may be of a generic class defined by the storage. For example, an url may be returned as a String. Use the 2 arguments version to ensure to get the right kind of Object.
065     * @throws IllegalArgumentException if the given data path is null or empty
066     * @throws UnknownTypeException if there is no compatible type with the data at the given data path
067     * @throws NotUniqueTypeException if there are many compatible types (there is no way to determine which type is the good one)
068     * @throws BadDataPathCardinalityException if the value of a part of the data path is multiple. Only the last part can be multiple
069     */
070    public <T> T getValue(String dataPath) throws IllegalArgumentException, UnknownTypeException, NotUniqueTypeException, BadDataPathCardinalityException;
071    
072    /**
073     * Retrieves the value of the data at the given path if exists and is not empty, or the default value
074     * @param <T> type of the value to retrieve. Should match the given data type
075     * @param dataPath path of the data
076     * @param defaultValue default value
077     * @return the value of the data, <code>null</code> if the data exists but is empty, or the given default value
078     * @throws IllegalArgumentException if the given data path is null or empty
079     * @throws UnknownTypeException if there is no compatible type with the data at the given data path
080     * @throws NotUniqueTypeException if there are many compatible types (there is no way to determine which type is the good one)
081     * @throws BadDataPathCardinalityException if the value of a part of the data path is multiple. Only the last part can be multiple
082     */
083    public <T> T getValue(String dataPath, T defaultValue) throws IllegalArgumentException, UnknownTypeException, NotUniqueTypeException, BadDataPathCardinalityException;
084    
085    /**
086     * Retrieves the value of the data at the given path
087     * @param <T> type of the value to retrieve. Should match the given data type
088     * @param dataPath path of the data
089     * @param dataTypeId type identifier of the data
090     * @return the value of the data or <code>null</code> if not exists or is empty
091     * @throws IllegalArgumentException if the given data path is null or empty
092     * @throws UnknownTypeException if the given type isn't available for this data holder's type extension point
093     * @throws BadItemTypeException if the given type doesn't match the type of the stored value at the given path
094     * @throws BadDataPathCardinalityException if the value of a part of the data path is multiple. Only the last part can be multiple
095     */
096    public <T> T getValueOfType(String dataPath, String dataTypeId) throws IllegalArgumentException, UnknownTypeException, BadItemTypeException, BadDataPathCardinalityException;
097    
098    /**
099     * Retrieves the value of the data at the given path if exists and is not empty, or the default value
100     * @param <T> type of the value to retrieve. Should match the given data type
101     * @param dataPath path of the data
102     * @param dataTypeId type identifier of the data
103     * @param defaultValue default value
104     * @return the value of the data, <code>null</code> if the data exists but is empty, or the given default value
105     * @throws IllegalArgumentException if the given data path is null or empty
106     * @throws UnknownTypeException if the given type isn't available for this data holder's type extension point
107     * @throws BadItemTypeException if the given type doesn't match the type of the stored value at the given path
108     * @throws BadDataPathCardinalityException if the value of a part of the data path is multiple. Only the last part can be multiple
109     */
110    public <T> T getValueOfType(String dataPath, String dataTypeId, T defaultValue) throws IllegalArgumentException, UnknownTypeException, BadItemTypeException, BadDataPathCardinalityException;
111    
112    /**
113     * Checks if the value of the data at the given path is multiple
114     * @param dataPath path of the data to check
115     * @return <code>true</code> if the value of the data is multiple, <code>false</code> otherwise
116     * @throws IllegalArgumentException if the given data path is null or empty
117     * @throws UnknownDataException the data at the given path does not exist
118     * @throws NotUniqueTypeException if there are many compatible types (there is no way to determine which type is the good one)
119     * @throws BadDataPathCardinalityException if the value of a part of the data path is multiple. Only the last part can be multiple
120     */
121    public boolean isMultiple(String dataPath) throws IllegalArgumentException, UnknownDataException, NotUniqueTypeException, BadDataPathCardinalityException;
122    
123    /**
124     * Retrieves the type of the data at the given path
125     * @param dataPath path of the data
126     * @return the type of the data
127     * @throws IllegalArgumentException if the given data path is null or empty
128     * @throws UnknownDataException if there is no data stored at the given path
129     * @throws UnknownTypeException if there is no compatible type with the data at the given data path or if the data is a repeater entry but the composite type is not available
130     * @throws NotUniqueTypeException if there are many compatible types (there is no way to determine which type is the good one)
131     * @throws BadDataPathCardinalityException if the value of a part of the data path is multiple. Only the last part can be multiple
132     */
133    public RepositoryModelItemType getType(String dataPath) throws IllegalArgumentException, UnknownDataException, UnknownTypeException, NotUniqueTypeException, BadDataPathCardinalityException;
134    
135    /**
136     * Generates SAX events for data contained in this {@link DataHolder}
137     * @param contentHandler the {@link ContentHandler} that will receive the SAX events
138     * @throws SAXException if an error occurs during the SAX events generation
139     * @throws UnknownTypeException if there is no compatible type with the saxed value
140     * @throws NotUniqueTypeException if there are many compatible types (there is no way to determine which type is the good one) with the saxed value
141     */
142    public default void dataToSAX(ContentHandler contentHandler) throws SAXException, UnknownTypeException, NotUniqueTypeException
143    {
144        dataToSAX(contentHandler, DataContext.newInstance());
145    }
146    
147    /**
148     * Generates SAX events for data contained in this {@link DataHolder}
149     * @param contentHandler the {@link ContentHandler} that will receive the SAX events
150     * @param context The context of the data to SAX
151     * @throws SAXException if an error occurs during the SAX events generation
152     * @throws UnknownTypeException if there is no compatible type with the saxed value
153     * @throws NotUniqueTypeException if there are many compatible types (there is no way to determine which type is the good one) with the saxed value
154     */
155    public void dataToSAX(ContentHandler contentHandler, DataContext context) throws SAXException, UnknownTypeException, NotUniqueTypeException;
156    
157    @Override
158    public Optional<? extends ModelLessDataHolder> getParentDataHolder();
159    
160    @Override
161    public ModelLessDataHolder getRootDataHolder();
162}