001/*
002 *  Copyright 2024 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.plugins.queriesdirectory.scripts;
017
018import java.util.HashMap;
019import java.util.Map;
020
021import org.apache.avalon.framework.service.ServiceException;
022import org.apache.avalon.framework.service.ServiceManager;
023
024import org.ametys.cms.scripts.CmsScriptHandler;
025import org.ametys.core.ui.Callable;
026import org.ametys.core.util.JSONUtils;
027import org.ametys.plugins.queriesdirectory.QueriesDirectoryRightAssignmentContext;
028import org.ametys.plugins.queriesdirectory.Query;
029import org.ametys.plugins.repository.AmetysObjectResolver;
030
031/**
032 * The CmsScriptHandler that only executes scripts for the Queries Directory plugin
033 */
034public class QueriesDirectoryScriptHandler extends CmsScriptHandler
035{
036    private AmetysObjectResolver _ametysObjectResover;
037    private JSONUtils _jsonUtils;
038
039    @Override
040    public void service(ServiceManager serviceManager) throws ServiceException
041    {
042        super.service(serviceManager);
043        
044        _ametysObjectResover = (AmetysObjectResolver) serviceManager.lookup(AmetysObjectResolver.ROLE);
045        _jsonUtils = (JSONUtils) serviceManager.lookup(JSONUtils.ROLE);
046    }
047    
048    /**
049     * Execute a script saved in the queries directory.
050     * @param queryId The id of the query to execute.
051     * @param arguments The arguments to pass to the executeScript method.
052     * @return A map of information on the script execution.
053     */
054    @SuppressWarnings("unchecked")
055    @Callable(rights = Callable.READ_ACCESS, paramIndex = 0, rightContext = QueriesDirectoryRightAssignmentContext.ID)
056    public Map<String, Object> executeScript(String queryId, Map<String, Object> arguments)
057    {
058        Query query = _ametysObjectResover.resolveById(queryId);
059        String queryContentAsString = query.getContent();
060        Map<String, Object> queryContentAsMap = _jsonUtils.convertJsonToMap(queryContentAsString);
061        Map<String, Object> toolParams = (Map<String, Object>) queryContentAsMap.get("toolParams");
062        Map<String, Object> values = (Map<String, Object>) toolParams.get("values");
063        String script = (String) values.get("script");
064        
065        Map<String, Object> modifiedArguments = new HashMap<>(arguments);
066        modifiedArguments.put("script", script);
067        return super.executeScript(modifiedArguments);
068    }
069}