001/*
002 *  Copyright 2019 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.repository;
017
018import java.util.HashMap;
019import java.util.List;
020import java.util.Map;
021import java.util.stream.Collectors;
022
023import org.ametys.plugins.repository.AmetysRepositoryException;
024import org.ametys.plugins.repository.data.holder.ModelAwareDataHolder;
025import org.ametys.plugins.repository.data.holder.group.ModelAwareRepeater;
026import org.ametys.plugins.repository.model.RepeaterDefinition;
027import org.ametys.runtime.model.ElementDefinition;
028import org.ametys.runtime.model.ModelItem;
029import org.ametys.web.repository.page.ZoneItem;
030import org.ametys.web.service.Service;
031
032/**
033 * Helper for page generator
034 */
035public final class PageGeneratorHelper
036{
037
038    private PageGeneratorHelper()
039    {
040        // Empty constructor
041    }
042    
043    /**
044     * Retrieve the parameter values of the given service
045     * @param service the service's model
046     * @param zoneItem the zone item containing the service's instance
047     * @return the service's parameter values indexed by their name
048     * @throws AmetysRepositoryException - to remove
049     */
050    public static Map<String, Object> getParameters(Service service, ZoneItem zoneItem) throws AmetysRepositoryException
051    {
052        ModelAwareDataHolder dataHolder = zoneItem.getServiceParameters();
053        return _getParameters(service.getParameters(), dataHolder);
054    }
055
056    /**
057     * Recursive method to retrieve parameter values
058     * @param definitions definitions of the parameters to retrieve, indexed by their name
059     * @param dataHolder data holder containing the parameter values
060     * @return the parameter values indexed by their name
061     */
062    private static Map<String, Object> _getParameters(Map<String, ModelItem> definitions, ModelAwareDataHolder dataHolder)
063    {
064        Map<String, Object> values = new HashMap<>();
065        
066        for (Map.Entry<String, ModelItem> definition : definitions.entrySet())
067        {
068            String parameterName = definition.getKey();
069            if (dataHolder.hasValueOrEmpty(parameterName))
070            {
071                if (definition.getValue() instanceof ElementDefinition)
072                {
073                    values.put(parameterName, dataHolder.getValue(parameterName));
074                }
075                else if (definition.getValue() instanceof RepeaterDefinition)
076                {
077                    RepeaterDefinition repeaterDefinition = (RepeaterDefinition) definition.getValue();
078                    Map<String, ModelItem> childrenDefinitions = repeaterDefinition.getChildren()
079                                                                                   .stream()
080                                                                                   .collect(Collectors.toMap(ModelItem::getName, child -> child));
081                    
082                    ModelAwareRepeater repeater = dataHolder.getRepeater(parameterName);
083                    List<Map<String, Object>> repeaterValues = repeater.getEntries()
084                                                                       .stream()
085                                                                       .map(entry -> _getParameters(childrenDefinitions, entry))
086                                                                       .collect(Collectors.toList());
087                    
088                    if (!repeaterValues.isEmpty())
089                    {
090                        values.put(parameterName, repeaterValues);
091                    }
092                }
093            }
094        }
095
096        return values;
097    }
098}