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