View Javadoc
1   /*
2    * Copyright (C) 2024 B3Partners B.V.
3    *
4    * SPDX-License-Identifier: MIT
5    */
6   package org.tailormap.api.scheduling;
7   
8   import jakarta.validation.constraints.NotNull;
9   import java.util.HashMap;
10  import java.util.Map;
11  import org.quartz.Trigger;
12  import org.springframework.util.Assert;
13  
14  /** Define a map with minimally required job data for the TailorMap scheduler. */
15  public class TMJobDataMap extends HashMap<String, Object> {
16  
17    /**
18     * Create a new instance of TMJobDataMap.
19     *
20     * @param map the map with job data, must have values for the required parameters {@code type} and
21     *     {@code description}
22     */
23    public TMJobDataMap(Map<String, Object> map) {
24      this(String.valueOf(map.get(Task.TYPE_KEY)), (String) map.get(Task.DESCRIPTION_KEY));
25      this.putAll(map);
26      // validate the priority
27      if (map.containsKey(Task.PRIORITY_KEY)) {
28        this.setPriority((Integer) map.getOrDefault(Task.PRIORITY_KEY, Trigger.DEFAULT_PRIORITY));
29      } else {
30        this.setPriority(Trigger.DEFAULT_PRIORITY);
31      }
32    }
33  
34    /**
35     * Create a new instance of TMJobDataMap with a status of {@code Trigger.TriggerState.NONE} and a
36     * default priority.
37     *
38     * @param type the type of the job
39     * @param description a description of the job
40     */
41    public TMJobDataMap(@NotNull String type, @NotNull String description) {
42      this(type, description, Trigger.TriggerState.NONE);
43    }
44  
45    /**
46     * Create a new instance of TMJobDataMap with default priority.
47     *
48     * @param type the type of the job
49     * @param description a description of the job
50     * @param state the state of the job
51     */
52    public TMJobDataMap(
53        @NotNull String type, @NotNull String description, @NotNull Trigger.TriggerState state) {
54      this(type, description, state, Trigger.DEFAULT_PRIORITY);
55    }
56  
57    /**
58     * Create a new instance of TMJobDataMap.
59     *
60     * @param type the type of the job
61     * @param description a description of the job
62     * @param state the state of the job
63     * @param priority the priority of the job, an integer value equal or greater than 0
64     */
65    public TMJobDataMap(
66        @NotNull String type,
67        @NotNull String description,
68        @NotNull Trigger.TriggerState state,
69        int priority) {
70      super();
71      // Check if the map contains the required parameters
72      Assert.notNull(type, "type must not be null");
73      Assert.doesNotContain(type, "null", "type must not be null");
74      Assert.notNull(description, "description must not be null");
75      Assert.notNull(state, "state must not be null");
76      super.put(Task.TYPE_KEY, type);
77      super.put(Task.DESCRIPTION_KEY, description);
78      super.put(Task.STATE_KEY, state);
79      setPriority(priority);
80    }
81  
82    @NotNull
83    public String getType() {
84      return super.get(Task.TYPE_KEY).toString();
85    }
86  
87    @NotNull
88    public String getDescription() {
89      return super.get(Task.DESCRIPTION_KEY).toString();
90    }
91  
92    @NotNull
93    public Trigger.TriggerState getState() {
94      return (Trigger.TriggerState) super.get(Task.STATE_KEY);
95    }
96  
97    public void setState(Trigger.TriggerState state) {
98      if (null == state) {
99        state = Trigger.TriggerState.NONE;
100     }
101     super.put(Task.STATE_KEY, state);
102   }
103 
104   /**
105    * Set the priority of the job. Using this method will ensure that the priority is equal or
106    * greater than 0.
107    *
108    * @param priority the priority of the job, an integer value equal or greater than 0
109    */
110   public void setPriority(int priority) {
111     if (priority < 0) {
112       priority = 0;
113     }
114     super.put(Task.PRIORITY_KEY, priority);
115   }
116 
117   public int getPriority() {
118     return (int) super.get(Task.PRIORITY_KEY);
119   }
120 }