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.cms.scripts;
017
018import java.util.Collection;
019import java.util.HashMap;
020import java.util.List;
021import java.util.Map;
022import java.util.Optional;
023import java.util.stream.Collectors;
024
025import org.apache.avalon.framework.service.ServiceException;
026import org.apache.avalon.framework.service.ServiceManager;
027
028import org.ametys.cms.repository.Content;
029import org.ametys.core.util.ServerCommHelper;
030import org.ametys.plugins.core.ui.script.StaticConfigurableScriptBinding;
031import org.ametys.plugins.repository.AmetysObjectResolver;
032import org.ametys.runtime.i18n.I18nizableText;
033
034import com.google.common.collect.ImmutableMap;
035
036/**
037 * CMS script binding, provides variables to the script tool, and a configuration file for the functions. 
038 */
039public class CmsScriptBinding extends StaticConfigurableScriptBinding
040{
041    private static final String REQUEST_PARAM_SELECTION = "selection";
042    private ServerCommHelper _serverCommHelper;
043    private AmetysObjectResolver _resolver;
044    
045    @Override
046    public void service(ServiceManager manager) throws ServiceException
047    {
048        super.service(manager);
049        _serverCommHelper = (ServerCommHelper) manager.lookup(ServerCommHelper.ROLE);
050        _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE);
051    }
052    
053    @Override
054    public Map<String, Object> getVariables()
055    {
056        Map<String, Object> variables = new HashMap<>();
057
058        Map<String, Object> jsParameters = _serverCommHelper.getJsParameters();
059
060        @SuppressWarnings("unchecked")
061        Collection<String> selection = Optional.ofNullable(jsParameters.getOrDefault("parameters", null))
062                .map(List.class::cast)
063                .map(l -> l.get(0))
064                .map(Map.class::cast)
065                .map(map -> (Collection<String>) map.getOrDefault(REQUEST_PARAM_SELECTION, null))
066                .orElse(null);
067
068        if (selection != null)
069        {
070            List<Content> contents = selection.stream()
071                                              .map(id -> _resolver.resolveById(id))
072                                              .filter(Content.class::isInstance)
073                                              .map(Content.class::cast)
074                                              .collect(Collectors.toList());
075            variables.put("selection", contents);
076        }
077        
078        return variables;
079    }
080    
081    @Override
082    public Map<String, I18nizableText> getVariablesDescriptions()
083    {
084        return ImmutableMap.of("selection", new I18nizableText("plugin.cms", "PLUGINS_CMS_SCRIPT_VAR_SELECTION"));
085    }
086
087}