001/*
002 *  Copyright 2012 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.web.service;
017
018import java.util.ArrayList;
019import java.util.Collections;
020import java.util.List;
021
022/**
023 * Class representing a service parameter group.
024 */
025public class ServiceParameterGroup
026{
027    
028    /** The group's parameters. */
029    protected List<ServiceParameterOrRepeater> _parameters;
030    
031    /**
032     * Build a new service parameter group.
033     */
034    public ServiceParameterGroup()
035    {
036        _parameters = new ArrayList<>();
037    }
038    
039    /**
040     * Get the group's parameters.
041     * @return the group's parameters.
042     */
043    public List<ServiceParameterOrRepeater> getParameters()
044    {
045        return Collections.unmodifiableList(_parameters);
046    }
047    
048    /**
049     * Set the group's parameters.
050     * @param parameters the group's parameters.
051     */
052    public void setParameters(List<ServiceParameterOrRepeater> parameters)
053    {
054        _parameters = parameters;
055    }
056    
057    /**
058     * Add a parameter to the group.
059     * @param parameter the parameter to add.
060     */
061    public void add(ServiceParameterOrRepeater parameter)
062    {
063        _parameters.add(parameter);
064    }
065    
066    /**
067     * Insert a parameter to the group, just after the given parameter
068     * @param parameter The parameter to insert
069     * @param paramName The name of parameter to insert after
070     */
071    public void insertAfter(ServiceParameterOrRepeater parameter, String paramName)
072    {
073        int index = _parameters.size() - 1; // last position by default
074        
075        if (paramName != null)
076        {
077            int count = 0;
078            for (ServiceParameterOrRepeater param : _parameters)
079            {
080                if (param.getId().equals(paramName))
081                {
082                    index = count;
083                    break;
084                }
085                count++;
086            }
087        }
088        
089        _parameters.add(index + 1, parameter);
090    }
091    
092}