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.lang.reflect.Array;
019import java.util.ArrayList;
020import java.util.List;
021import java.util.Map;
022
023import org.ametys.runtime.i18n.I18nizableText;
024import org.ametys.runtime.model.ElementDefinition;
025
026/**
027 * The definition of a service parameter
028 * @param <T> Type of the parameter value
029 */
030public class ServiceParameter<T> extends ElementDefinition<T>
031{
032    @SuppressWarnings("unchecked")
033    @Override
034    public <X> X getDefaultValue()
035    {
036        X defaultValue = super.getDefaultValue();
037        
038        if (defaultValue != null && getEnumerator() != null)
039        {
040            try
041            {
042                if (isMultiple())
043                {
044                    return _multipleDefaultValuesFromEnumeration((T[]) defaultValue);
045                }
046                else
047                {
048                    if (getEnumerator().getEntry((T) defaultValue) == null)
049                    {
050                        // default value does not exist
051                        return (X) _getFirstEnumeratorEntry();
052                    }
053                }
054            }
055            catch (Exception e)
056            {
057                _logger.error("Cannot test if default value exists", e);
058            }
059        }
060
061        return defaultValue;
062    }
063    
064    @SuppressWarnings("unchecked")
065    private <X> X _multipleDefaultValuesFromEnumeration(T[] declaredDefaultValues) throws Exception
066    {
067        List<T> existingDefaultValues = new ArrayList<>();
068        for (T declaredDefaultValue : declaredDefaultValues)
069        {
070            if (getEnumerator().getEntry(declaredDefaultValue) != null)
071            {
072                existingDefaultValues.add(declaredDefaultValue);
073            }
074        }
075        
076        if (existingDefaultValues.isEmpty())
077        {
078            T firstEnumeratorEntry = _getFirstEnumeratorEntry();
079            if (firstEnumeratorEntry != null)
080            {
081                existingDefaultValues.add(firstEnumeratorEntry);
082            }
083        }
084
085        X defaultValuesAsArray = (X) Array.newInstance(getType().getManagedClass(), existingDefaultValues.size());
086        for (int i = 0; i < existingDefaultValues.size(); i++)
087        {
088            Array.set(defaultValuesAsArray, i, existingDefaultValues.get(i));
089        }
090
091        return defaultValuesAsArray;
092        
093    }
094    
095    private T _getFirstEnumeratorEntry()
096    {
097        if (getEnumerator() != null)
098        {
099            try
100            {
101                Map<T, I18nizableText> entries = getEnumerator().getTypedEntries();
102                if (!entries.isEmpty())
103                {
104                    return entries.keySet().iterator().next();
105                }
106            }
107            catch (Exception e)
108            {
109                _logger.error("Cannot find the first entry of the enumerator", e);
110            }
111        }
112        
113        // No enumerator entry has been found, retrieve null
114        return null;
115    }
116    
117    @Override
118    public String toString()
119    {
120        return "'" + getName() + "' (type:    " + getType().getId() + ", label:    " + getLabel().toString() + ", " + (getDefaultValue() != null ? ("default value: " + getDefaultValue()) : "no default value")  + ", " + (getEnumerator() != null ? ("enumerator: " + getEnumerator()) : "no enumerator") + ")";
121    }
122}