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