001/*
002 *  Copyright 2019 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.type;
017
018import org.ametys.plugins.repository.RepositoryConstants;
019import org.ametys.plugins.repository.data.UnknownDataException;
020import org.ametys.plugins.repository.data.repositorydata.ModifiableRepositoryData;
021import org.ametys.plugins.repository.data.repositorydata.RepositoryData;
022import org.ametys.runtime.model.exception.BadItemTypeException;
023import org.ametys.runtime.model.type.ModelItemType;
024
025/**
026 * Interface for types of model items stored in the repository
027 */
028public interface RepositoryModelItemType extends ModelItemType
029{
030    /** Suffix of the metadata used to check if a data is present but empty */
031    public static final String EMPTY_METADATA_SUFFIX = "__isEmpty";
032    
033    /**
034     * Checks if the value in the given repository data is compatible with the item type 
035     * @param parentData repository data containing the data to check
036     * @param name the name of the data to check
037     * @return <code>true</code> if the data type matches the current type, <code>false</code> otherwise
038     * @throws UnknownDataException if there is no data in the parent repository data with this name
039     */
040    public default boolean isCompatible(RepositoryData parentData, String name) throws UnknownDataException
041    {
042        return parentData.hasValue(name + EMPTY_METADATA_SUFFIX, RepositoryConstants.NAMESPACE_PREFIX_INTERNAL) || getRepositoryDataType().equals(parentData.getType(name));
043    }
044    
045    /**
046     * Retrieves the type of repository data used for this type
047     * @return the type of repository data used for this type
048     */
049    public String getRepositoryDataType();
050    
051    /**
052     * Checks if the value in the given repository data is multiple 
053     * @param parentData repository data containing the data to check
054     * @param name the name of the data to check
055     * @return <code>true</code> if the stored data is multiple, <code>false</code> otherwise
056     * @throws UnknownDataException if there is no data in the parent repository data with this name
057     */
058    public default boolean isMultiple(RepositoryData parentData, String name) throws UnknownDataException
059    {
060        return parentData.isMultiple(name);
061    }
062    
063    /**
064     * Check if there is a value, even empty, in the given repository data
065     * @param parentData repository data containing the value
066     * @param name the name of the element to check
067     * @return <code>true</code> if there is value, <code>false</code> otherwise
068     * @throws BadItemTypeException if the reading value doesn't match this element type
069     */
070    public default boolean hasValue(RepositoryData parentData, String name) throws BadItemTypeException
071    {
072        return parentData.hasValue(name) && isCompatible(parentData, name);
073    }
074    
075    /**
076     * Check if there is a non empty value in the given repository data
077     * @param parentData repository data containing the value
078     * @param name the name of the element to check
079     * @return <code>true</code> if there is a non empty value, <code>false</code> otherwise
080     * @throws BadItemTypeException if the reading value doesn't match this element type
081     */
082    public boolean hasNonEmptyValue(RepositoryData parentData, String name) throws BadItemTypeException;
083    
084    /**
085     * Remove the value into the given repository data
086     * @param parentData repository where the value to remove is stored.
087     * @param name the name of the element to remove
088     * @throws UnknownDataException if the value with the given name does not exist
089     */
090    public default void remove(ModifiableRepositoryData parentData, String name) throws UnknownDataException
091    {
092        parentData.removeValue(name);
093    }
094}