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.web.service;
017
018import java.util.ArrayList;
019import java.util.Iterator;
020import java.util.List;
021
022import org.apache.commons.lang3.StringUtils;
023import org.slf4j.Logger;
024import org.slf4j.LoggerFactory;
025
026import org.ametys.runtime.model.ElementDefinition;
027
028/**
029 * The definition of a service parameter
030 * @param <T> Type of the parameter value
031 */
032public class ServiceParameter<T> extends ElementDefinition<T>
033{
034    private Logger _logger = LoggerFactory.getLogger(this.getClass());
035    
036    @Override
037    public T getDefaultValue()
038    {
039        T defaultValue = super.getDefaultValue();
040        
041        if (defaultValue != null && getEnumerator() != null)
042        {
043            try
044            {
045                if (defaultValue instanceof String && isMultiple())
046                {
047                    return _multipleDefaultValuesFromEnumeration((String) defaultValue);
048                }
049                
050                if (getEnumerator().getEntry(defaultValue) == null)
051                {
052                    // default value does not exist
053                    Iterator<T> it = getEnumerator().getTypedEntries().keySet().iterator();
054                    if (it.hasNext())
055                    {
056                        return it.next();
057                    }
058                }
059            }
060            catch (Exception e)
061            {
062                _logger.error("Cannot test if default exists or cannot replace unexisting default value", e);
063            }
064        }
065
066        return defaultValue;
067    }
068    
069    @SuppressWarnings("unchecked")
070    private T _multipleDefaultValuesFromEnumeration(String defaultValue) throws Exception
071    {
072        String[] declaredDefaultValues = defaultValue.split(",");
073        List<String> existingDefaultValues = new ArrayList<>();
074        for (String declaredDefaultValue : declaredDefaultValues)
075        {
076            if (getEnumerator().getEntry((T) declaredDefaultValue) != null)
077            {
078                existingDefaultValues.add(declaredDefaultValue);
079            }
080        }
081        
082        if (existingDefaultValues.isEmpty())
083        {
084            // none of the declared default values exists => return first one
085            Iterator<T> it = getEnumerator().getTypedEntries().keySet().iterator();
086            if (it.hasNext())
087            {
088                return it.next();
089            }
090        }
091        else
092        {
093            return (T) StringUtils.join(existingDefaultValues, ',');
094        }
095        
096        return (T) defaultValue;
097    }
098    
099    @Override
100    public String toString()
101    {
102        return "'" + getName() + "' (type:    " + getType().getId() + ", label:    " + getLabel().toString() + ", " + (getDefaultValue() != null ? ("default value: " + getDefaultValue()) : "no default value")  + ", " + (getEnumerator() != null ? ("enumerator: " + getEnumerator()) : "no enumerator") + ")";
103    }
104}