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