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.cms.data.holder.group.impl;
017
018import java.util.Optional;
019
020import org.ametys.cms.data.holder.ModifiableIndexableDataHolder;
021import org.ametys.cms.data.holder.group.ModifiableIndexableRepeater;
022import org.ametys.cms.data.holder.group.ModifiableIndexableRepeaterEntry;
023import org.ametys.cms.data.holder.impl.DefaultModifiableModelAwareDataHolder;
024import org.ametys.plugins.repository.data.repositorydata.ModifiableRepositoryData;
025import org.ametys.plugins.repository.model.RepeaterDefinition;
026
027/**
028 * Class for modifiable model aware repeater entries
029 */
030public class DefaultModifiableModelAwareRepeaterEntry extends DefaultModelAwareRepeaterEntry implements ModifiableIndexableRepeaterEntry
031{
032    /** The modifiable repository data if the repeater entry is modifiable */
033    protected ModifiableRepositoryData _modifiableRepositoryData;
034    
035    /** the repeater containing this entry */
036    protected ModifiableIndexableRepeater _modifiableRepeater;
037    
038    /** the default implementation of a {@link ModifiableIndexableDataHolder} to use */
039    protected ModifiableIndexableDataHolder _modifiableDefaultDataHolder;
040    
041    /**
042     * Creates a modifiable model aware repeater entry
043     * @param repositoryData the repository data of the repeater entry
044     * @param repeater the repeater containing this entry
045     * @param definition the definition of the repeater
046     */
047    public DefaultModifiableModelAwareRepeaterEntry(ModifiableRepositoryData repositoryData, ModifiableIndexableRepeater repeater, RepeaterDefinition definition)
048    {
049        super(repositoryData, repeater, definition);
050        _modifiableDefaultDataHolder = new DefaultModifiableModelAwareDataHolder(repositoryData, Optional.of(repeater.getParentDataHolder()), Optional.of(repeater.getRootDataHolder()), definition);
051        _modifiableRepositoryData = repositoryData;
052        _modifiableRepeater = repeater;
053    }
054    
055    public void setPosition(int position) throws IllegalArgumentException
056    {
057        if (1 <= position && position <= _modifiableRepeater.getSize())
058        {
059            if (position != getPosition())
060            {
061                int oldPosition = getPosition();
062                _rename(String.valueOf(position) + "_tmp");
063                
064                // rename entries between the old and new position
065                if (position > oldPosition)
066                {
067                    for (int currentEntryPosition = oldPosition + 1; currentEntryPosition <= position; currentEntryPosition++)
068                    {
069                        DefaultModifiableModelAwareRepeaterEntry entry = (DefaultModifiableModelAwareRepeaterEntry) _modifiableRepeater.getEntry(currentEntryPosition);
070                        entry._rename(String.valueOf(currentEntryPosition - 1));
071                    }
072                }
073                else if (position < oldPosition)
074                {
075                    for (int currentEntryPosition = oldPosition - 1; currentEntryPosition >= position; currentEntryPosition--)
076                    {
077                        DefaultModifiableModelAwareRepeaterEntry entry = (DefaultModifiableModelAwareRepeaterEntry) _modifiableRepeater.getEntry(currentEntryPosition);
078                        entry._rename(String.valueOf(currentEntryPosition + 1));
079                    }
080                }
081                
082                _rename(String.valueOf(position));
083            }
084        
085        }
086        else if (-_modifiableRepeater.getSize() < position && position <= 0)
087        {
088            // Find the positive equivalent position and call the setPosition method with this position
089            setPosition(_modifiableRepeater.getSize() + position);
090        }
091        else
092        {
093            throw new IllegalArgumentException("The repeater named '" + _modifiableRepositoryData.getName() + "' has '" + _modifiableRepeater.getSize() + "' entries. You can not set an entry at position '" + position + "'.");
094        }
095    }
096    
097    private void _rename(String newName)
098    {
099        _modifiableRepositoryData.rename(newName);
100    }
101    
102    @Override
103    public ModifiableIndexableDataHolder getDefaultDataHolder()
104    {
105        return _modifiableDefaultDataHolder;
106    }
107    
108    @Override
109    public ModifiableRepositoryData getRepositoryData()
110    {
111        return _modifiableRepositoryData;
112    }
113
114    @Override
115    public ModifiableIndexableRepeater getHoldingRepeater()
116    {
117        return _modifiableRepeater;
118    }
119}