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 default priority.
36     *
37     * @param type the type of the job
38     * @param description a description of the job
39     */
40    public TMJobDataMap(@NotNull String type, @NotNull String description) {
41      this(type, description, Trigger.TriggerState.NONE);
42    }
43  
44    /**
45     * Create a new instance of TMJobDataMap with default priority.
46     *
47     * @param type the type of the job
48     * @param description a description of the job
49     * @param state the state of the job
50     */
51    public TMJobDataMap(@NotNull String type, @NotNull String description, @NotNull Trigger.TriggerState state) {
52      this(type, description, state, Trigger.DEFAULT_PRIORITY);
53    }
54  
55    /**
56     * Create a new instance of TMJobDataMap.
57     *
58     * @param type the type of the job
59     * @param description a description of the job
60     * @param state the state of the job
61     * @param priority the priority of the job, an integer value equal or greater than 0
62     */
63    public TMJobDataMap(
64        @NotNull String type, @NotNull String description, @NotNull Trigger.TriggerState state, int priority) {
65      super();
66      // Check if the map contains the required parameters
67      Assert.notNull(type, "type must not be null");
68      Assert.doesNotContain(type, "null", "type must not be null");
69      Assert.notNull(description, "description must not be null");
70      Assert.notNull(state, "state must not be null");
71      super.put(Task.TYPE_KEY, type);
72      super.put(Task.DESCRIPTION_KEY, description);
73      super.put(Task.STATE_KEY, state);
74      setPriority(priority);
75    }
76  
77    @NotNull public String getType() {
78      return super.get(Task.TYPE_KEY).toString();
79    }
80  
81    @NotNull public String getDescription() {
82      return super.get(Task.DESCRIPTION_KEY).toString();
83    }
84  
85    @NotNull public Trigger.TriggerState getState() {
86      return (Trigger.TriggerState) super.get(Task.STATE_KEY);
87    }
88  
89    public void setState(Trigger.TriggerState state) {
90      if (null == state) {
91        state = Trigger.TriggerState.NONE;
92      }
93      super.put(Task.STATE_KEY, state);
94    }
95  
96    /**
97     * Set the priority of the job. Using this method will ensure that the priority is equal or greater than 0.
98     *
99     * @param priority the priority of the job, an integer value equal or greater than 0
100    */
101   public void setPriority(int priority) {
102     if (priority < 0) {
103       priority = 0;
104     }
105     super.put(Task.PRIORITY_KEY, priority);
106   }
107 
108   public int getPriority() {
109     return (int) super.get(Task.PRIORITY_KEY);
110   }
111 }