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