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.group;
017
018import java.util.Map;
019import java.util.Set;
020
021import org.ametys.plugins.repository.data.UnknownDataException;
022import org.ametys.plugins.repository.data.holder.ModifiableDataHolder;
023import org.ametys.plugins.repository.data.repositorydata.ModifiableRepositoryData;
024
025/**
026 * Interface for modifiable repeaters
027 */
028public interface ModifiableRepeater extends Repeater
029{
030    @Override
031    public ModifiableRepeaterEntry getEntry(int position);
032    
033    /**
034     * Creates a Repeater entry at the last position.
035     * @return the created entry
036     */
037    public ModifiableRepeaterEntry addEntry();
038    
039    /**
040     * Creates a Repeater entry at the given position. The position starts at index 1.
041     * The position can be an integer between 1 and the repeater size + 1 to insert an entry from the beginning
042     * Or the position can an integer between 0 and - the repeater size to insert an entry from the end (0 means at the end, -1 means before the last one and so on)
043     * @param position The position of the new entry
044     * @return the created entry
045     * @throws IllegalArgumentException if the position is not between the negative and positive repeater size
046     */
047    public ModifiableRepeaterEntry addEntry(int position) throws IllegalArgumentException;
048    
049    /**
050     * Moves the entries according to the given position mapping.
051     * The given position mapping's key is the previous position and the value is the new one
052     * The existent entries with no corresponding entry in the position mapping will be removed
053     * the given size is taken into account to create new empty entries if needed
054     * @param positionsMapping the position mapping
055     * @param targetSize the target size of the repeater
056     * @return <code>true</code> if some entries have moved, <code>false</code> otherwise
057     */
058    public boolean moveEntries(Map<Integer, Integer> positionsMapping, int targetSize);
059    
060    /**
061     * Removes the repeater entries at the given positions. The position starts at index 1.
062     * The positions can be integers between 1 and the repeater size to remove an entry from the beginning
063     * Or the positions can integers between 0 and - the repeater size to remove an entry from the end (0 means the last entry, -1 means before the last one and so on)
064     * @param positions The positions of the entries to remove
065     * @throws UnknownDataException if there is no entry for one of the given positions
066     */
067    public void removeEntries(Set<Integer> positions) throws UnknownDataException;
068    
069    /**
070     * Removes the repeater entry at the given position. The position starts at index 1.
071     * The position can be an integer between 1 and the repeater size to remove an entry from the beginning
072     * Or the position can an integer between 0 and - the repeater size to remove an entry from the end (0 means the last entry, -1 means before the last one and so on)
073     * @param position The position of the entry to remove
074     * @throws UnknownDataException if there is no entry at the given position
075     */
076    public void removeEntry(int position) throws UnknownDataException;
077    
078    @Override
079    public ModifiableRepositoryData getRepositoryData();
080    
081    @Override
082    public ModifiableDataHolder getParentDataHolder();
083    
084    @Override
085    public ModifiableDataHolder getRootDataHolder();
086}