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.data.UnknownDataException;
019import org.ametys.plugins.repository.data.repositorydata.ModifiableRepositoryData;
020import org.ametys.plugins.repository.data.repositorydata.RepositoryData;
021import org.ametys.runtime.model.exception.BadItemTypeException;
022import org.ametys.runtime.model.type.ModelItemGroupType;
023
024/**
025 * Interface for types of model item groups stored in the repository
026 */
027public interface RepositoryModelItemGroupType extends RepositoryModelItemType, ModelItemGroupType
028{
029    /**
030     * Read the group in the given repository data
031     * @param parentData repository data containing the group
032     * @param name the name of the group to read
033     * @return the repository data representing the read group
034     * @throws BadItemTypeException if the reading value doesn't match this element type
035     */
036    public default RepositoryData read(RepositoryData parentData, String name) throws BadItemTypeException
037    {
038        if (!parentData.hasValue(name))
039        {
040            return null;
041        }
042        
043        if (!isCompatible(parentData, name))
044        {
045            throw new BadItemTypeException("Try to get " + getId() + " value from the non '" + getId() + "' data '" + name + "' on '" + parentData + "'");
046        }
047        
048        return parentData.getRepositoryData(name);
049    }
050    
051    public default boolean hasNonEmptyValue(RepositoryData parentData, String name) throws BadItemTypeException
052    {
053        if (!parentData.hasValue(name))
054        {
055            return false;
056        }
057        
058        if (!isCompatible(parentData, name))
059        {
060            throw new BadItemTypeException("Try to check " + getId() + " value from the non '" + getId() + "' data '" + name + "' on '" + parentData + "'");
061        }
062        
063        return true;
064    }
065    
066    /**
067     * Add a group into the given repository data
068     * @param parentData repository where to add the group
069     * @param name the name of the group to add
070     * @return the repository data representing the added group
071     */
072    public default ModifiableRepositoryData add(ModifiableRepositoryData parentData, String name)
073    {
074        if (parentData.hasValue(name))
075        {
076            parentData.removeValue(name);
077        }
078        
079        return parentData.addRepositoryData(name, getRepositoryDataType());
080    }
081    
082    default boolean isMultiple(RepositoryData parentData, String name) throws UnknownDataException
083    {
084        return false;
085    }
086}