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 "progressionTracker", "model" and "selection"
044     */
045    static class Impl extends ScriptExecArguments.Impl implements CmsScriptExecArguments
046    {
047        protected Impl(Map<String, Object> arguments)
048        {
049            super(arguments);
050        }
051        
052        public Optional<String> searchModelId()
053        {
054            return Optional.of(_arguments)
055                    .map(args -> args.get("model"))
056                    .filter(String.class::isInstance)
057                    .map(String.class::cast);
058        }
059        
060        public Optional<Collection<String>> selection()
061        {
062            return Optional.of(_arguments)
063                    .map(args -> args.get("selection"))
064                    .filter(Collection.class::isInstance)
065                    .map(Collection.class::cast);
066        }
067    }
068}