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.core.schedule;
017
018import java.util.Map;
019
020import org.quartz.CronScheduleBuilder;
021import org.quartz.CronTrigger;
022import org.quartz.Trigger;
023
024import org.ametys.core.user.UserIdentity;
025import org.ametys.runtime.i18n.I18nizableText;
026
027/**
028 * This interface represents the entity by which a {@link Schedulable} can be scheduled.
029 */
030public interface Runnable
031{
032    /** The possible ways to fire the runnable */
033    public static enum FireProcess
034    {
035        /**
036         * Never fired
037         */
038        NEVER,
039        /**
040         * Fired during the next application startup
041         */
042        STARTUP,
043        /**
044         * Fired once as soon as possible.
045         */
046        NOW,
047        /**
048         * Based on a cron expression.
049         */
050        CRON
051    }
052    
053    /** The possible misfire policies */
054    public static enum MisfirePolicy
055    {
056        /** 
057         * Ignore that there were misfired triggers, and try to fire them all as soon as it can.
058         * See {@link CronScheduleBuilder#withMisfireHandlingInstructionIgnoreMisfires()} and {@link Trigger#MISFIRE_INSTRUCTION_IGNORE_MISFIRE_POLICY}
059         */
060        IGNORE,
061        /** 
062         * Try to fire one of them as soon as it can.
063         * See {@link CronScheduleBuilder#withMisfireHandlingInstructionFireAndProceed()} and {@link CronTrigger#MISFIRE_INSTRUCTION_FIRE_ONCE_NOW}
064         */
065        FIRE_ONCE,
066        /** 
067         * The misfired triggers will never be fired and the next trigger to be fired will be the next one.
068         * See {@link CronScheduleBuilder#withMisfireHandlingInstructionDoNothing()} and {@link CronTrigger#MISFIRE_INSTRUCTION_DO_NOTHING}
069         */
070        DO_NOTHING
071    }
072    
073    /**
074     * Returns the id
075     * @return the id
076     */
077    public String getId();
078    
079    /**
080     * Returns the label
081     * @return the i18n label
082     */
083    public I18nizableText getLabel();
084    
085    /**
086     * Returns the description
087     * @return the i18n description
088     */
089    public I18nizableText getDescription();
090    
091    /**
092     * Gets the process of firing, i.e. the way the task will be scheduled (fire now, fire at next stratup, schedule it based on a cron expression...).
093     * @return the fire process
094     */
095    public FireProcess getFireProcess();
096    
097    /**
098     * Returns the cron expression to base the schedule on. Ignored if {@link #getFireProcess()} is different from {@link FireProcess#CRON}.
099     * @return the cron expression
100     */
101    public String getCronExpression();
102    
103    /**
104     * Gets the identifier of {@link Schedulable} to execute
105     * @return the identifier of {@link Schedulable}
106     */
107    public String getSchedulableId();
108    
109    /**
110     * Determines if this runnable can be removed
111     * @return <code>true</code> if this runnable is removable
112     */
113    public boolean isRemovable();
114    
115    /**
116     * Determines if this runnable can be modified
117     * @return <code>true</code> if this runnable is modifiable
118     */
119    public boolean isModifiable();
120    
121    /**
122     * Determines if this runnable can be activate or deactivate
123     * @return <code>true</code> if this runnable is deactivatable
124     */
125    public boolean isDeactivatable();
126    
127    /**
128     * Gets the misfire policy, i.e. what the runnable must do if it missed a trigger. Ignored if {@link #getFireProcess()} is different from {@link FireProcess#CRON}.
129     * @return The misfire policy
130     */
131    public MisfirePolicy getMisfirePolicy();
132    
133    /**
134     * Determines if the runnable must not survive to a server restart
135     * @return true if the runnable must not survive to a server restart
136     */
137    public boolean isVolatile();
138    
139    /**
140     * Gets the values of the parameters (from the linked {@link Schedulable})
141     * @return the parameter values
142     */
143    public Map<String, Object> getParameterValues();
144    
145    /**
146     * Gets the user which should be used to launch the runnable task.
147     * @return the {@link UserIdentity} of the user
148     */
149    public UserIdentity getUserIdentity();
150}