View Javadoc
1   /*
2    * This file is part of ***  M y C o R e  ***
3    * See http://www.mycore.de/ for details.
4    *
5    * MyCoRe is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU General Public License as published by
7    * the Free Software Foundation, either version 3 of the License, or
8    * (at your option) any later version.
9    *
10   * MyCoRe is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with MyCoRe.  If not, see <http://www.gnu.org/licenses/>.
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   * Container class handled by hibernate to store and retrieve job information.
47   * 
48   * @author Ren\u00E9 Adler
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       * Returns the job Id.
79       * 
80       * @return the job Id
81       */
82      @Id
83      @GeneratedValue(strategy = GenerationType.IDENTITY)
84      @Column(name = "id")
85      public long getId() {
86          return id;
87      }
88  
89      /**
90       * Set the job Id.
91       * 
92       * @param id - the job id
93       */
94      protected void setId(long id) {
95          this.id = id;
96      }
97  
98      /**
99       * Returns the action class ({@link MCRJobAction}).
100      * 
101      * @return the action class
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      * Set the action class ({@link MCRJobAction}).
111      * 
112      * @param actionClass - the action class to set
113      */
114     public void setAction(Class<? extends MCRJobAction> actionClass) {
115         this.action = actionClass;
116     }
117 
118     /**
119      * Returns the current state ({@link MCRJobStatus}) of the job.
120      * 
121      */
122     @Column(name = "status", nullable = false)
123     @Enumerated(EnumType.STRING)
124     public MCRJobStatus getStatus() {
125         return status;
126     }
127 
128     /**
129      * Set the state ({@link MCRJobStatus}) of the job.
130      * 
131      * @param status - the job status
132      */
133     public void setStatus(MCRJobStatus status) {
134         this.status = status;
135     }
136 
137     /**
138      * Returns the adding date of job.
139      * 
140      * @return the add date of the job
141      */
142     @Column(name = "added", nullable = true)
143     public Date getAdded() {
144         return added;
145     }
146 
147     /**
148      * Set the adding date.
149      * 
150      */
151     public void setAdded(Date added) {
152         this.added = added;
153     }
154 
155     /**
156      * Returns the starting date of execution.
157      *  
158      * @return the job start date
159      */
160     @Column(name = "start", nullable = true)
161     public Date getStart() {
162         return start;
163     }
164 
165     /**
166      * Set the job starting date.
167      * 
168      * @param start - the starting date
169      */
170     public void setStart(Date start) {
171         this.start = start;
172     }
173 
174     /**
175      * Returns the finishing date of execution.
176      * 
177      * @return the finishing date
178      */
179     @Column(name = "finished", nullable = true)
180     public Date getFinished() {
181         return finished;
182     }
183 
184     /**
185      * Set the finishing date of execution.
186      * 
187      * @param finished - the finishing date
188      */
189     public void setFinished(Date finished) {
190         this.finished = finished;
191     }
192 
193     /**
194      * Returns all set parameters of the job.
195      * 
196      * @return the job parameters
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      * Set all job parameters.
208      * 
209      * @param parameters - the job parameters
210      */
211     public void setParameters(Map<String, String> parameters) {
212         this.parameters = parameters;
213     }
214 
215     /**
216      * Returns an single parameter by it's name.
217      * 
218      * @param key - the parameter name.
219      * @return the value of the parameter.
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      * Set an single parameter.
232      * 
233      * @param key - the parameter name
234      * @param value - the parameter value
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     /* (non-Javadoc)
245      * @see java.lang.Object#clone()
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     /* (non-Javadoc)
264      * @see java.lang.Object#toString()
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 }