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.plugins.core.impl.schedule;
017
018import java.util.Map;
019
020import org.ametys.core.schedule.Runnable;
021import org.ametys.core.schedule.Runnable.MisfirePolicy;
022import org.ametys.core.user.UserIdentity;
023import org.ametys.core.schedule.Schedulable;
024import org.ametys.runtime.i18n.I18nizableText;
025
026/**
027 * Implementation of {@link Runnable} which can be created by the UI.
028 */
029public class DefaultRunnable implements Runnable
030{
031    /** The id */
032    protected String _id;
033    /** The label */
034    protected I18nizableText _label;
035    /** The description */
036    protected I18nizableText _description;
037    /** The fire process */
038    protected FireProcess _fireProcess;
039    /** The CRON expression */
040    protected String _cron;
041    /** The id of the linked {@link Schedulable} */
042    protected String _schedulableId;
043    /** true if it can be removed */
044    protected boolean _removable;
045    /** true if it can be edited */
046    protected boolean _modifiable;
047    /** true if it can be deactivated */
048    protected boolean _deactivatable;
049    /** The parameter values */
050    protected Map<String, Object> _parameterValues;
051    /** The misfire policy */
052    protected MisfirePolicy _misfirePolicy;
053    /** true if volatile */
054    protected boolean _volatile;
055    /** UserIdentity should be used to launch the Runnable */
056    protected UserIdentity _userIdentity;
057
058    /**
059     * Constructor
060     * @param id the id
061     * @param label the label
062     * @param description the descritpion
063     * @param fireProcess the fire process
064     * @param cron the cron expression
065     * @param schedulableId the id of the linked {@link Schedulable}
066     * @param removable true if it can be removed
067     * @param modifiable true if it can be edited
068     * @param deactivatable true if it can be deactivated
069     * @param misfirePolicy The misfire policy. Can be null, the default value is {@link MisfirePolicy#DO_NOTHING}
070     * @param isVolatile true if it is volatile, i.e. if it must not survive to a server restart
071     * @param userIdentity The user used to launch the runnable task
072     * @param parameters the parameter values
073     */
074    public DefaultRunnable(String id,
075                           I18nizableText label,
076                           I18nizableText description,
077                           FireProcess fireProcess,
078                           String cron,
079                           String schedulableId,
080                           boolean removable,
081                           boolean modifiable,
082                           boolean deactivatable,
083                           MisfirePolicy misfirePolicy,
084                           boolean isVolatile,
085                           UserIdentity userIdentity,
086                           Map<String, Object> parameters)
087    {
088        _id = id;
089        _label = label;
090        _description = description;
091        _fireProcess = fireProcess;
092        _cron = cron;
093        _schedulableId = schedulableId;
094        _removable = removable;
095        _modifiable = modifiable;
096        _deactivatable = deactivatable;
097        _misfirePolicy = misfirePolicy != null ? misfirePolicy : MisfirePolicy.DO_NOTHING;
098        _volatile = isVolatile;
099        _userIdentity = userIdentity;
100        _parameterValues = parameters;
101    }
102    
103    @Override
104    public String getId()
105    {
106        return _id;
107    }
108
109    @Override
110    public I18nizableText getLabel()
111    {
112        return _label;
113    }
114
115    @Override
116    public I18nizableText getDescription()
117    {
118        return _description;
119    }
120
121    @Override
122    public FireProcess getFireProcess()
123    {
124        return _fireProcess;
125    }
126
127    @Override
128    public String getCronExpression()
129    {
130        return _cron;
131    }
132
133    @Override
134    public String getSchedulableId()
135    {
136        return _schedulableId;
137    }
138
139    @Override
140    public boolean isRemovable()
141    {
142        return _removable;
143    }
144
145    @Override
146    public boolean isModifiable()
147    {
148        return _modifiable;
149    }
150
151    @Override
152    public boolean isDeactivatable()
153    {
154        return _deactivatable;
155    }
156
157    @Override
158    public MisfirePolicy getMisfirePolicy()
159    {
160        return _misfirePolicy;
161    }
162    
163    @Override
164    public boolean isVolatile()
165    {
166        return _volatile;
167    }
168
169    @Override
170    public Map<String, Object> getParameterValues()
171    {
172        return _parameterValues;
173    }
174
175    @Override
176    public UserIdentity getUserIdentity()
177    {
178        return _userIdentity;
179    }
180}