# Job.java

    /*
    ** Module   : Job.java
    ** Abstract : POJO defining a scheduled job.
    **
    ** Copyright (c) 2014-2021, Golden Code Development Corporation.
    **
    ** -#- -I- --Date-- --------------------------------------Description---------------------------------------
    ** 001 CA  20140206 First version.
    ** 002 CA  20161027 Allow Runnable instances to be configured as jobs, at runtime.
    ** 003 GES 20210415 Changed RECURRENT to RECURRING.
    */

    /*
    ** This program is free software: you can redistribute it and/or modify
    ** it under the terms of the GNU Affero General Public License as
    ** published by the Free Software Foundation, either version 3 of the
    ** License, or (at your option) any later version.
    **
    ** This program is distributed in the hope that it will be useful,
    ** but WITHOUT ANY WARRANTY; without even the implied warranty of
    ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    ** GNU Affero General Public License for more details.
    **
    ** You may find a copy of the GNU Affero GPL version 3 at the following
    ** location: https://www.gnu.org/licenses/agpl-3.0.en.html
    **
    ** Additional terms under GNU Affero GPL version 3 section 7:
    **
    **   Under Section 7 of the GNU Affero GPL version 3, the following additional
    **   terms apply to the works covered under the License.  These additional terms
    **   are non-permissive additional terms allowed under Section 7 of the GNU
    **   Affero GPL version 3 and may not be removed by you.
    **
    **   0. Attribution Requirement.
    **
    **     You must preserve all legal notices or author attributions in the covered
    **     work or Appropriate Legal Notices displayed by works containing the covered
    **     work.  You may not remove from the covered work any author or developer
    **     credit already included within the covered work.
    **
    **   1. No License To Use Trademarks.
    **
    **     This license does not grant any license or rights to use the trademarks
    **     Golden Code, FWD, any Golden Code or FWD logo, or any other trademarks
    **     of Golden Code Development Corporation. You are not authorized to use the
    **     name Golden Code, FWD, or the names of any author or contributor, for
    **     publicity purposes without written authorization.
    **
    **   2. No Misrepresentation of Affiliation.
    **
    **     You may not represent yourself as Golden Code Development Corporation or FWD.
    **
    **     You may not represent yourself for publicity purposes as associated with
    **     Golden Code Development Corporation, FWD, or any author or contributor to
    **     the covered work, without written authorization.
    **
    **   3. No Misrepresentation of Source or Origin.
    **
    **     You may not represent the covered work as solely your work.  All modified
    **     versions of the covered work must be marked in a reasonable way to make it
    **     clear that the modified work is not originating from Golden Code Development
    **     Corporation or FWD.  All modified versions must contain the notices of
    **     attribution required in this license.
    */

    package com.goldencode.p2j.scheduler;

    import org.quartz.*;

    /**
     * Definition of a {@link Scheduler} job.
     * <p>
     * For each job, the following can be defined:
     * <ul>
     *    <li>{@link #name} - the job name; must be unique and is case insensitive.</li>
     *    <li>{@link #type} - the kind of the job being started:
     *       <p> {@link JobType#PROCESS} - start a new P2J process.
     *       <p>-{@link JobType#CLASS} - execute a certain java class which implements
     *           {@link Runnable}.
     *       <p>-{@link JobType#METHOD} - execute a certain java method.
     *       <p>-{@link JobType#INSTANCE} - execute a given {@link Runnable} instance.  This job
     *           can't be configured in the directory.
     *    </li>
     *    <li>{@link #target}
     *       <p>- in case of {@link JobType#PROCESS}, is the subject ID of a P2J process.
     *       <p>- in case of {@link JobType#CLASS}, is the qualified name of a Java class which
     *            implements the {@link Runnable} interface
     *       <p>- in case of {@link JobType#METHOD}, is the qualified name of a Java class followed by
     *            the name of a no-arguments method, i.e. {@code foo.bar.Something.execute}
     *       <p>- in case of {@link JobType#INSTANCE}, this field will be <code>null</code>.
     *    </li>
     *    <li>{@link #instance} - the instance to be ran, only for {@link JobType#INSTANCE}</li>
     *    <li>{@link #enabled} - flag indicating if this job is enabled or not.</li>
     *    <li>{@link #mode} - the job mode. One of the following values:
     *       <p>- {@link JobMode#NOW} - for jobs being executed immediately
     *       <p>- {@link JobMode#ONE_TIME} - for jobs being executed at a certain time/date,
     *           a single time.
     *       <p>- {@link JobMode#RECURRING} - for recurrent jobs executed at a certain time/date.
     *    </li>
     *    <li>The {@link #second}, {@link #minute}, {@link #hour}, {@link #dayOfMonth},
     *        {@link #dayOfWeek}, {@link #month} and {@link #year} need to be specified only for
     *        {@link JobMode#RECURRING} or {@link JobMode#ONE_TIME} jobs.  They follow the
     *        {@link CronExpression} specification for each cron field.
     *     </li>
     * </ul>
     */
    public class Job
    {
       /** The name of this job. */
       private final String name;

       /** The type of this job, one of the {@link JobType} values. */
       private JobType type;

       /** The target of this job. */
       private String target;

       /** The instance of this job, only with {@link JobType#INSTANCE}. */
       private Runnable instance;

       /** Flag indicating if this job is enabled. */
       private boolean enabled;

       /** The execution mode of this job, one of the {@link JobMode} values. */
       private JobMode mode;

       /** The second expression when the job needs to be executed. */
       private String second;

       /** The minute expression when the job needs to be executed. */
       private String minute;

       /** The hour expression when the job needs to be executed. */
       private String hour;

       /** The day the month expression when the job needs to be executed. */
       private String dayOfMonth;

       /** The day of the week expression when the job needs to be executed. */
       private String dayOfWeek;

       /** The month expression when the job needs to be executed. */
       private String month;

       /** The year expression when the job needs to be executed. */
       private String year;

       /**
        * Create a new job.
        *
        * @param    name
        *           The name of this job.
        * @param    type
        *           The job's {@link JobType type}.
        * @param    target
        *           The target of this job.
        * @param    enabled
        *           Flag indicating if this job is enabled.
        * @param    mode
        *           The execution mode of this job, one of the {@link JobMode} values.
        */
       public Job(String name, JobType type, String target, boolean enabled, JobMode mode)
       {
          this.name = name;
          this.type = type;
          this.target = target;
          this.enabled = enabled;
          this.mode = mode;
       }

       /**
        * Create a new job, with type {@link JobType#INSTANCE}
        *
        * @param    name
        *           The name of this job.
        * @param    instance
        *           The instance of this job.
        * @param    enabled
        *           Flag indicating if this job is enabled.
        * @param    mode
        *           The execution mode of this job, one of the {@link JobMode} values.
        */
       public Job(String name, Runnable instance, boolean enabled, JobMode mode)
       {
          this.type = JobType.INSTANCE;
          this.name = name;
          this.instance = instance;
          this.enabled = enabled;
          this.mode = mode;
       }

       /**
        * Get the instance of this job.
        *
        * @return   The job's {@link #instance}, to be executed.
        */
       public Runnable getInstance()
       {
          return instance;
       }

       /**
        * Get the job's {@link #name}.
        *
        * @return   See above.
        */
       public String getName()
       {
          return name;
       }

       /**
        * Get the job's {@link #type}.
        *
        * @return   See above.
        */
       public JobType getType()
       {
          return type;
       }

       /**
        * Get the job's {@link #target}.
        *
        * @return   See above.
        */
       public String getTarget()
       {
          return target;
       }

       /**
        * Get the job's {@link #enabled} state.
        *
        * @return   See above.
        */
       public boolean isEnabled()
       {
          return enabled;
       }

       /**
        * Get the job's {@link #mode}.
        *
        * @return   See above.
        */
       public JobMode getMode()
       {
          return mode;
       }

       /**
        * Get the job's {@link #second}.
        *
        * @return   See above.
        */
       public String getSecond()
       {
          return second;
       }

       /**
        * Set the job's execution {@link #second}.
        *
        * @param    second
        *           The second, {@code null} or a value following the {@link CronExpression} syntax.
        */
       public void setSecond(String second)
       {
          this.second = second;
       }

       /**
        * Get the job's {@link #minute}.
        *
        * @return   See above.
        */
       public String getMinute()
       {
          return minute;
       }

       /**
        * Set the job's execution {@link #minute}.
        *
        * @param    minute
        *           The minute, {@code null} or a value following the {@link CronExpression} syntax.
        */
       public void setMinute(String minute)
       {
          this.minute = minute;
       }

       /**
        * Get the job's {@link #hour}.
        *
        * @return   See above.
        */
       public String getHour()
       {
          return hour;
       }

       /**
        * Set the job's execution {@link #hour}.
        *
        * @param    hour
        *           The hour, {@code null} or a value following the {@link CronExpression} syntax.
        */
       public void setHour(String hour)
       {
          this.hour = hour;
       }

       /**
        * Get the job's {@link #dayOfMonth}.
        *
        * @return   See above.
        */
       public String getDayOfMonth()
       {
          return dayOfMonth;
       }

       /**
        * Set the job's execution {@link #dayOfMonth}.
        *
        * @param    dayOfMonth
        *           The day of the month, {@code null} or a value following the {@link CronExpression}
        *           syntax.
        */
       public void setDayOfMonth(String dayOfMonth)
       {
          this.dayOfMonth = dayOfMonth;
       }

       /**
        * Get the job's {@link #dayOfWeek}.
        *
        * @return   See above.
        */
       public String getDayOfWeek()
       {
          return dayOfWeek;
       }

       /**
        * Set the job's execution {@link #dayOfWeek}.
        *
        * @param    dayOfWeek
        *           The day of the week, {@code null} or a value following the {@link CronExpression}
        *           syntax.
        */
       public void setDayOfWeek(String dayOfWeek)
       {
          this.dayOfWeek = dayOfWeek;
       }

       /**
        * Get the job's {@link #month}.
        *
        * @return   See above.
        */
       public String getMonth()
       {
          return month;
       }

       /**
        * Set the job's execution {@link #month}.
        *
        * @param    month
        *           The month, {@code null} or a value following the {@link CronExpression} syntax.
        */
       public void setMonth(String month)
       {
          this.month = month;
       }

       /**
        * Get the job's {@link #year}.
        *
        * @return   See above.
        */
       public String getYear()
       {
          return year;
       }

       /**
        * Set the job's execution {@link #year}.
        *
        * @param    year
        *           The year, {@code null} or a year following the {@link CronExpression} syntax.
        */
       public void setYear(String year)
       {
          this.year = year;
       }

       /**
        * Get the string representation of this job, using the
        * {@code "<name> [<type>:<mode>] - <target>"} format.
        */
       @Override
       public String toString()
       {
          return String.format("%s [%s:%s] - %s", this.name, this.type, this.mode, this.target);
       }
    }