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) map.get("type"), (String) map.get("description"));
25      this.putAll(map);
26      // validate the priority
27      this.setPriority((Integer) map.getOrDefault("priority", Trigger.DEFAULT_PRIORITY));
28    }
29  
30    /**
31     * Create a new instance of TMJobDataMap with a status of {@code Trigger.TriggerState.NONE} and a
32     * default priority.
33     *
34     * @param type the type of the job
35     * @param description a description of the job
36     */
37    public TMJobDataMap(@NotNull String type, @NotNull String description) {
38      this(type, description, Trigger.TriggerState.NONE);
39    }
40  
41    /**
42     * Create a new instance of TMJobDataMap with default priority.
43     *
44     * @param type the type of the job
45     * @param description a description of the job
46     * @param status the status of the job
47     */
48    public TMJobDataMap(
49        @NotNull String type, @NotNull String description, @NotNull Trigger.TriggerState status) {
50      this(type, description, status, Trigger.DEFAULT_PRIORITY);
51    }
52  
53    /**
54     * Create a new instance of TMJobDataMap.
55     *
56     * @param type the type of the job
57     * @param description a description of the job
58     * @param status the status of the job
59     * @param priority the priority of the job, an integer value equal or greater than 0
60     */
61    public TMJobDataMap(
62        @NotNull String type,
63        @NotNull String description,
64        @NotNull Trigger.TriggerState status,
65        int priority) {
66      super();
67      // Check if the map contains the required parameters
68      Assert.notNull(type, "type must not be null");
69      Assert.notNull(description, "description must not be null");
70      Assert.notNull(status, "status must not be null");
71      super.put("type", type);
72      super.put("description", description);
73      super.put("status", status);
74      setPriority(priority);
75    }
76  
77    @NotNull
78    public String getType() {
79      return super.get("type").toString();
80    }
81  
82    @NotNull
83    public String getDescription() {
84      return super.get("description").toString();
85    }
86  
87    @NotNull
88    public Trigger.TriggerState getStatus() {
89      return (Trigger.TriggerState) super.get("status");
90    }
91  
92    public void setStatus(Trigger.TriggerState status) {
93      if (null == status) {
94        status = Trigger.TriggerState.NONE;
95      }
96      super.put("status", status);
97    }
98  
99    /**
100    * Set the priority of the job.
101    *
102    * @param priority the priority of the job, an integer value equal or greater than 0
103    */
104   public void setPriority(int priority) {
105     if (priority < 0) {
106       priority = 0;
107     }
108     super.put("priority", priority);
109   }
110 
111   public int getPriority() {
112     return (int) super.get("priority");
113   }
114 }