001/*
002 *  Copyright 2025 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.odf.schedulable;
017
018import java.util.HashMap;
019import java.util.List;
020import java.util.Map;
021import java.util.Optional;
022
023import org.apache.cocoon.environment.Request;
024
025import org.ametys.runtime.model.DefaultElementDefinition;
026import org.ametys.runtime.model.ElementDefinition;
027import org.ametys.runtime.workspace.WorkspaceMatcher;
028
029/**
030 * Helper for ODF scheduable
031 */
032public final class ODFSchedulableHelper
033{
034    private static final List<String> __UNSUPPORTED_WIDGETS_FOR_ADMIN = List.of("edition.select-content", "edition.select-orgunit", "widget.select-in-content-attribute-values", "edition.select-referencetable-content");
035    
036    private ODFSchedulableHelper() 
037    {
038        // Empty
039    }
040    
041    /**
042     * Get the parameters for job execution taking into account the supported widgets in the current context
043     * @param request The request
044     * @param initialParameters The initial parameters of scheduable
045     * @return the parameters 
046     */
047    @SuppressWarnings("unchecked")
048    public static Map<String, ElementDefinition> cleanUnsupportedWidgets(Request request, Map<String, ElementDefinition> initialParameters)
049    {
050        // FIXME CMS-9980
051        if (_isAdminContext(request))
052        {
053            // Remove unsupported widgets in admin context
054            Map<String, ElementDefinition> copiedParameters = new HashMap<>();
055            
056            initialParameters.entrySet().stream().forEach(e -> {
057                ElementDefinition def = e.getValue();
058                String name = e.getKey();
059                
060                if (def.getWidget() != null && __UNSUPPORTED_WIDGETS_FOR_ADMIN.contains(def.getWidget()))
061                {
062                    DefaultElementDefinition newDef = new DefaultElementDefinition(def);
063                    newDef.setWidget(null);
064                    newDef.setWidgetParameters(Map.of());
065                    copiedParameters.put(name, newDef);
066                }
067                else
068                {
069                    copiedParameters.put(name, def);
070                }
071            });
072            
073            return copiedParameters;
074        }
075        else
076        {
077            return initialParameters;
078        }
079    }
080    
081    private static boolean _isAdminContext(Request request)
082    {
083        return Optional.ofNullable(request)
084                .map(r -> r.getAttribute(WorkspaceMatcher.WORKSPACE_NAME))
085                .map("admin"::equals)
086                .orElse(false);
087    }
088}