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.cms.scripts;
017
018import java.util.Collection;
019import java.util.Map;
020import java.util.Optional;
021
022import org.ametys.plugins.core.ui.script.ScriptExecArguments;
023
024/**
025 * Enriches {@link ScriptExecArguments} with some arguments needed by {@link CmsScriptHandler}
026 */
027public interface CmsScriptExecArguments extends ScriptExecArguments
028{
029    /**
030     * The id of search model for searching contents in CMS ScriptTool
031     * @return The id of search model for searching contents in CMS ScriptTool
032     */
033    Optional<String> searchModelId();
034    
035    /**
036     * The current selection of contents
037     * @return The current selection of contents
038     */
039    Optional<Collection<String>> selection();
040    
041    /**
042     * The default implementation for {@link CmsScriptExecArguments}
043     * <br>Must be constructed from a {@link Map} containing at least the key "script", and optionally the keys "model" and "selection"
044     */
045    static class Impl implements CmsScriptExecArguments
046    {
047        private Map<String, Object> _arguments;
048        Impl(Map<String, Object> arguments)
049        {
050            _arguments = arguments;
051        }
052        
053        @Override
054        public String script()
055        {
056            return (String) _arguments.get("script");
057        }
058
059        @Override
060        public Optional<String> searchModelId()
061        {
062            return Optional.of(_arguments)
063                    .map(args -> args.get("model"))
064                    .filter(String.class::isInstance)
065                    .map(String.class::cast);
066        }
067
068        @Override
069        public Optional<Collection<String>> selection()
070        {
071            return Optional.of(_arguments)
072                    .map(args -> args.get("selection"))
073                    .filter(Collection.class::isInstance)
074                    .map(Collection.class::cast);
075        }
076    }
077}