1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.mycore.services.queuedjob;
20
21 import java.text.MessageFormat;
22 import java.util.Date;
23 import java.util.HashMap;
24 import java.util.Locale;
25 import java.util.Map;
26
27 import jakarta.persistence.CollectionTable;
28 import jakarta.persistence.Column;
29 import jakarta.persistence.Convert;
30 import jakarta.persistence.ElementCollection;
31 import jakarta.persistence.Entity;
32 import jakarta.persistence.EnumType;
33 import jakarta.persistence.Enumerated;
34 import jakarta.persistence.FetchType;
35 import jakarta.persistence.GeneratedValue;
36 import jakarta.persistence.GenerationType;
37 import jakarta.persistence.Id;
38 import jakarta.persistence.JoinColumn;
39 import jakarta.persistence.MapKeyColumn;
40 import jakarta.persistence.NamedQueries;
41 import jakarta.persistence.NamedQuery;
42 import jakarta.persistence.Table;
43 import jakarta.persistence.Transient;
44
45
46
47
48
49
50 @Entity
51 @NamedQueries({
52 @NamedQuery(name = "mcrjob.classes",
53 query = "select DISTINCT(o.action) from MCRJob o") })
54 @Table(name = "MCRJob")
55 public class MCRJob implements Cloneable {
56 private long id;
57
58 private Class<? extends MCRJobAction> action;
59
60 private MCRJobStatus status;
61
62 private Date added;
63
64 private Date start;
65
66 private Date finished;
67
68 private Map<String, String> parameters;
69
70 protected MCRJob() {
71 }
72
73 public MCRJob(Class<? extends MCRJobAction> actionClass) {
74 action = actionClass;
75 }
76
77
78
79
80
81
82 @Id
83 @GeneratedValue(strategy = GenerationType.IDENTITY)
84 @Column(name = "id")
85 public long getId() {
86 return id;
87 }
88
89
90
91
92
93
94 protected void setId(long id) {
95 this.id = id;
96 }
97
98
99
100
101
102
103 @Column(name = "action", nullable = false)
104 @Convert(converter = MCRJobActionConverter.class)
105 public Class<? extends MCRJobAction> getAction() {
106 return action;
107 }
108
109
110
111
112
113
114 public void setAction(Class<? extends MCRJobAction> actionClass) {
115 this.action = actionClass;
116 }
117
118
119
120
121
122 @Column(name = "status", nullable = false)
123 @Enumerated(EnumType.STRING)
124 public MCRJobStatus getStatus() {
125 return status;
126 }
127
128
129
130
131
132
133 public void setStatus(MCRJobStatus status) {
134 this.status = status;
135 }
136
137
138
139
140
141
142 @Column(name = "added", nullable = true)
143 public Date getAdded() {
144 return added;
145 }
146
147
148
149
150
151 public void setAdded(Date added) {
152 this.added = added;
153 }
154
155
156
157
158
159
160 @Column(name = "start", nullable = true)
161 public Date getStart() {
162 return start;
163 }
164
165
166
167
168
169
170 public void setStart(Date start) {
171 this.start = start;
172 }
173
174
175
176
177
178
179 @Column(name = "finished", nullable = true)
180 public Date getFinished() {
181 return finished;
182 }
183
184
185
186
187
188
189 public void setFinished(Date finished) {
190 this.finished = finished;
191 }
192
193
194
195
196
197
198 @ElementCollection(fetch = FetchType.EAGER)
199 @CollectionTable(name = "MCRJobParameter", joinColumns = @JoinColumn(name = "jobID"))
200 @MapKeyColumn(name = "paramKey", length = 128)
201 @Column(name = "paramValue", length = 255)
202 public Map<String, String> getParameters() {
203 return parameters;
204 }
205
206
207
208
209
210
211 public void setParameters(Map<String, String> parameters) {
212 this.parameters = parameters;
213 }
214
215
216
217
218
219
220
221 @Transient
222 public String getParameter(String key) {
223 if (parameters == null) {
224 return null;
225 }
226
227 return parameters.get(key);
228 }
229
230
231
232
233
234
235
236 public void setParameter(String key, String value) {
237 if (parameters == null) {
238 parameters = new HashMap<>();
239 }
240
241 parameters.put(key, value);
242 }
243
244
245
246
247 @Override
248 public MCRJob clone() {
249 MCRJob clone = new MCRJob();
250 clone.setAction(getAction());
251 clone.setAdded(getAdded());
252 clone.setFinished(getFinished());
253 clone.setId(getId());
254 clone.setStart(getStart());
255 clone.setStatus(getStatus());
256
257 Map<String, String> map = new HashMap<>(getParameters());
258 clone.setParameters(map);
259
260 return clone;
261 }
262
263
264
265
266 @Override
267 public String toString() {
268 return new MessageFormat("MCRJob [id:{0}, action:{1}, status:{2}, added:{3}, parameters:{4}]", Locale.ROOT)
269 .format(
270 new Object[] { getId(), getAction().getName(), getStatus(), getAdded(), getParameters() });
271 }
272
273 }