001/*
002 *  Copyright 2010 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.Iterator;
019
020import org.slf4j.Logger;
021import org.slf4j.LoggerFactory;
022
023import org.ametys.runtime.parameter.Parameter;
024import org.ametys.runtime.parameter.ParameterHelper.ParameterType;
025
026/**
027 * Definition of a {@link Service} parameter.
028 */
029public class ServiceParameter extends Parameter<ParameterType> implements ServiceParameterOrRepeater
030{
031    private boolean _isMultiple;
032    
033    private Logger _logger = LoggerFactory.getLogger(this.getClass());
034    
035    /**
036     * Tests if the parameter is multiple.
037     * @return <code>true</code> if the parameter is multiple.
038     */
039    public boolean isMultiple()
040    {
041        return _isMultiple;
042    }
043    
044    /**
045     * Set the parameter multiple status.
046     * @param isMultiple the parameter multiple status.
047     */
048    public void setMultiple(boolean isMultiple)
049    {
050        _isMultiple = isMultiple;
051    }
052        
053    @Override
054    public Object getDefaultValue()
055    {
056        Object defaultValue = super.getDefaultValue();
057        
058        if (defaultValue != null && getEnumerator() != null)
059        {
060            try
061            {
062                if (getEnumerator().getEntry(defaultValue.toString()) == null)
063                {
064                    // default value does not exist
065                    Iterator<Object> it = getEnumerator().getEntries().keySet().iterator();
066                    if (it.hasNext())
067                    {
068                        return it.next();
069                    }
070                }
071            }
072            catch (Exception e)
073            {
074                _logger.error("Cannot test if default exists or cannot replace unexisting default value", e);
075            }
076        }
077
078        return defaultValue;
079    }
080    
081    @Override
082    public String toString()
083    {
084        return "'" + getId() + "' (type:    " + getType().name() + ", label:    " + getLabel().toString() + ", " + (getDefaultValue() != null ? ("default value: " + getDefaultValue()) : "no default value")  + ", " + (getEnumerator() != null ? ("enumerator: " + getEnumerator()) : "no enumerator") + ")";
085    }
086    
087}