001/*
002 *  Copyright 2026 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.workflow.schedule;
017
018import java.util.HashMap;
019import java.util.Map;
020
021import org.apache.avalon.framework.service.ServiceException;
022import org.apache.avalon.framework.service.ServiceManager;
023import org.quartz.JobDataMap;
024import org.quartz.JobExecutionContext;
025
026import org.ametys.cms.repository.WorkflowAwareContent;
027import org.ametys.cms.workflow.ContentWorkflowHelper;
028import org.ametys.core.observation.ObservationManager;
029import org.ametys.core.schedule.progression.ContainerProgressionTracker;
030import org.ametys.core.user.CurrentUserProvider;
031import org.ametys.plugins.core.impl.schedule.AbstractStaticSchedulable;
032import org.ametys.plugins.core.schedule.Scheduler;
033import org.ametys.plugins.repository.AmetysObjectResolver;
034
035/**
036 * Schedulable to publish/unpublish a content
037 */
038public class ExecuteContentWorkflowActionSchedulable extends AbstractStaticSchedulable
039{
040    /** the extension id */
041    public static final String ID = ExecuteContentWorkflowActionSchedulable.class.getName();
042    
043    /** The key for the id of the content to transition */
044    public static final String CONTENT_ID_KEY = "contentId";
045    /** The key for the id of the action to execute */
046    public static final String ACTION_ID_KEY = "actionId";
047    private static final String _JOBDATAMAP_CONTENT_ID_KEY = Scheduler.PARAM_VALUES_PREFIX + CONTENT_ID_KEY;
048    private static final String _JOBDATAMAP_ACTION_ID_KEY = Scheduler.PARAM_VALUES_PREFIX + ACTION_ID_KEY;
049    /** The observation manager */
050    protected ObservationManager _observationManager;
051    /** The Ametys object resolver */
052    protected AmetysObjectResolver _resolver;
053    /** The current user provider */
054    protected CurrentUserProvider _currentUserProvider;
055    /** The content workflow helper */
056    protected ContentWorkflowHelper _workflowHelper;
057    
058    @Override
059    public void service(ServiceManager manager) throws ServiceException
060    {
061        super.service(manager);
062        _observationManager = (ObservationManager) manager.lookup(ObservationManager.ROLE);
063        _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE);
064        _currentUserProvider = (CurrentUserProvider) manager.lookup(CurrentUserProvider.ROLE);
065        _workflowHelper = (ContentWorkflowHelper) manager.lookup(ContentWorkflowHelper.ROLE);
066    }
067
068    @Override
069    public void execute(JobExecutionContext context, ContainerProgressionTracker progressionTracker) throws Exception
070    {
071        JobDataMap jobDataMap = context.getJobDetail().getJobDataMap();
072        
073        String contentId = (String) jobDataMap.get(_JOBDATAMAP_CONTENT_ID_KEY);
074        WorkflowAwareContent content = _resolver.resolveById(contentId);
075        Long actionId = (Long) jobDataMap.get(_JOBDATAMAP_ACTION_ID_KEY);
076        
077        getLogger().info("Trying to execute scheduled action on content '{}'", contentId);
078        
079        Map<String, Object> inputs = new HashMap<>();
080        inputs.put(ScheduledPublicationCondition.ALLOW_SCHEDULED_ACTION, true);
081        _workflowHelper.doAction(content, actionId.intValue(), inputs);
082    }
083}