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.impl;
017
018import java.util.List;
019
020import org.ametys.plugins.repository.data.repositorydata.RepositoryData;
021import org.ametys.plugins.repository.model.RepeaterDefinition;
022
023/**
024 * Class for model aware repeaters
025 */
026public class ModelAwareRepeater extends AbstractRepeater
027{    
028    /** Definition of this repeater */
029    protected RepeaterDefinition _definition;
030    
031    /**
032     * Creates a model aware repeater
033     * @param repositoryData the repository data of the repeater
034     * @param definition the definition of the repeater
035     */
036    public ModelAwareRepeater(RepositoryData repositoryData, RepeaterDefinition definition)
037    {
038        super(repositoryData);
039        _definition = definition;
040    }
041    
042    @SuppressWarnings("unchecked")
043    @Override
044    public List<? extends ModelAwareRepeaterEntry> getEntries()
045    {
046        return (List< ? extends ModelAwareRepeaterEntry>) super.getEntries();
047    }
048    
049    public ModelAwareRepeaterEntry getEntry(int position)
050    {
051        if (1 <= position && position <= getSize())
052        {
053            RepositoryData entryRepositoryData = _repositoryData.getRepositoryData(String.valueOf(position));
054            return new ModelAwareRepeaterEntry(entryRepositoryData, _definition);
055        }
056        else if (-getSize() < position && position <= 0)
057        {
058            // Find the positive equivalent position and call the getEntry method with this position
059            return getEntry(getSize() + position);
060        }
061        else
062        {
063            return null;
064        }
065    }
066}