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.time.LocalDateTime;
019import java.time.ZoneId;
020import java.time.ZonedDateTime;
021import java.time.format.DateTimeFormatter;
022import java.util.HashMap;
023import java.util.List;
024import java.util.Map;
025
026import org.ametys.core.user.UserIdentity;
027import org.ametys.core.util.StringUtils;
028import org.ametys.plugins.core.impl.schedule.DefaultRunnable;
029import org.ametys.runtime.i18n.I18nizableText;
030
031/**
032 * Runnable to schedule the depublication of a content
033 */
034public class UnpublishContentRunnable extends DefaultRunnable
035{
036    /**
037     * Constructor
038     * @param contentId The id of the content to target with the action
039     * @param contentTitle The title of the targeted content
040     * @param workflowAction The id of the action to execute
041     * @param userIdentity The {@link UserIdentity} to launch the runnable task
042     * @param date The date when to publish the page
043     */
044    public UnpublishContentRunnable(String contentId, String contentTitle, int workflowAction, UserIdentity userIdentity, ZonedDateTime date)
045    {
046        super(
047                getId(contentId),
048                new I18nizableText("plugin.cms", "PLUGINS_CMS_CONTENT_UNPUBLISH_RUNNABLE_LABEL", List.of(StringUtils.escapeHTML(contentTitle))),
049                new I18nizableText("plugin.cms", "PLUGINS_CMS_CONTENT_UNPUBLISH_RUNNABLE_DESC", List.of(StringUtils.escapeHTML(contentTitle))),
050                FireProcess.CRON,
051                _getCron(date),
052                ExecuteContentWorkflowActionSchedulable.ID,
053                false, // removable
054                false, // modifiable
055                false, // deactivable
056                MisfirePolicy.FIRE_ONCE,
057                false, // volatile
058                userIdentity,
059                _getParameterValues(contentId, workflowAction));
060    }
061    
062    /**
063     * Get the job name for a given content
064     * @param contentId the content identifier
065     * @return the name of the job to do an action
066     */
067    public static String getId(String contentId)
068    {
069        return UnpublishContentRunnable.class.getName() + "-" + contentId;
070    }
071    
072    private static String _getCron(ZonedDateTime date)
073    {
074        LocalDateTime localDate = date.withZoneSameInstant(ZoneId.systemDefault()).toLocalDateTime();
075        DateTimeFormatter cronFormatter = DateTimeFormatter.ofPattern("s m H d M '?' y");
076        return cronFormatter.format(localDate);
077    }
078    
079    private static Map<String, Object> _getParameterValues(String contentId, long workflowAction)
080    {
081        Map<String, Object> values = new HashMap<>();
082        values.put(ExecuteContentWorkflowActionSchedulable.CONTENT_ID_KEY, contentId);
083        values.put(ExecuteContentWorkflowActionSchedulable.ACTION_ID_KEY,  workflowAction);
084        return values;
085    }
086}