KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > riot > job > persistence > JobDetail


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1
3  * The contents of this file are subject to the Mozilla Public License Version
4  * 1.1 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  * http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the
11  * License.
12  *
13  * The Original Code is Riot.
14  *
15  * The Initial Developer of the Original Code is
16  * Neteye GmbH.
17  * Portions created by the Initial Developer are Copyright (C) 2006
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * Felix Gnass [fgnass at neteye dot de]
22  *
23  * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.riot.job.persistence;
25
26 import java.util.Date JavaDoc;
27
28 import org.riotfamily.common.util.FormatUtils;
29 import org.riotfamily.riot.job.JobDescription;
30
31 /**
32  * Detailed information about a job.
33  */

34 public class JobDetail {
35
36     public static final int NEW = -1;
37     
38     public static final int INITIALIZED = 0;
39     
40     public static final int STARTED = 1;
41     
42     public static final int INTERRUPTED = 2;
43     
44     public static final int CANCELED = 3;
45     
46     public static final int COMPLETED = 4;
47     
48     private Long JavaDoc id;
49     
50     private String JavaDoc type;
51     
52     private String JavaDoc name;
53     
54     private String JavaDoc description;
55     
56     private int state = NEW;
57     
58     private Date JavaDoc startDate;
59     
60     private Date JavaDoc endDate;
61     
62     private int stepsTotal;
63     
64     private int stepsCompleted;
65     
66     private long lastStepTime;
67     
68     private long executionTime;
69     
70     private int averageStepTime;
71     
72     private String JavaDoc objectId;
73     
74     
75     public JobDetail() {
76     }
77
78     public JobDetail(String JavaDoc type, String JavaDoc objectId, int averageStepTime) {
79         this.type = type;
80         this.objectId = objectId;
81         this.averageStepTime = averageStepTime;
82     }
83     
84     public void init(JobDescription desc) {
85         this.name = desc.getName();
86         this.description = desc.getDescription();
87         this.stepsTotal = desc.getSteps();
88         this.state = INITIALIZED;
89     }
90     
91     public Long JavaDoc getId() {
92         return this.id;
93     }
94
95     public void setId(Long JavaDoc id) {
96         this.id = id;
97     }
98     
99     /**
100      * Returns the job type. The returned String can be used to lookup a
101      * Job implementation using the JobManager.
102      */

103     public String JavaDoc getType() {
104         return this.type;
105     }
106
107     public void setType(String JavaDoc type) {
108         this.type = type;
109     }
110     
111     public String JavaDoc getName() {
112         return this.name;
113     }
114
115     public void setName(String JavaDoc name) {
116         this.name = name;
117     }
118     
119     public String JavaDoc getDescription() {
120         return this.description;
121     }
122
123     public void setDescription(String JavaDoc description) {
124         this.description = description;
125     }
126
127     public int getState() {
128         return this.state;
129     }
130
131     public void setState(int state) {
132         this.state = state;
133     }
134
135     public String JavaDoc getObjectId() {
136         return this.objectId;
137     }
138
139     public void setObjectId(String JavaDoc objectId) {
140         this.objectId = objectId;
141     }
142
143     public Date JavaDoc getStartDate() {
144         return this.startDate;
145     }
146
147     public void setStartDate(Date JavaDoc startDate) {
148         this.startDate = startDate;
149     }
150     
151     /**
152      * Returns the date when the job was completed or <code>null</code> if
153      * the job is still pending.
154      */

155     public Date JavaDoc getEndDate() {
156         return this.endDate;
157     }
158
159     public void setEndDate(Date JavaDoc endDate) {
160         this.endDate = endDate;
161     }
162     
163     public int getStepsTotal() {
164         return this.stepsTotal;
165     }
166
167     public void setStepsTotal(int stepsTotal) {
168         this.stepsTotal = stepsTotal;
169     }
170     
171     public int getStepsCompleted() {
172         return this.stepsCompleted;
173     }
174
175     public void setStepsCompleted(int stepsCompleted) {
176         this.stepsCompleted = stepsCompleted;
177     }
178     
179     public void stepCompleted() {
180         stepsCompleted++;
181         updateExecutionTime();
182     }
183
184     
185     public long getExecutionTime() {
186         return this.executionTime;
187     }
188
189     public void setExecutionTime(long executionTime) {
190         this.executionTime = executionTime;
191     }
192     
193     public int getAverageStepTime() {
194         return this.averageStepTime;
195     }
196
197     public void setAverageStepTime(int averageStepTime) {
198         this.averageStepTime = averageStepTime;
199     }
200
201     public void updateExecutionTime() {
202         long now = System.currentTimeMillis();
203         if (lastStepTime > 0) {
204             long millis = now - lastStepTime;
205             this.executionTime += millis;
206         }
207         lastStepTime = now;
208         if (stepsTotal > 0 && stepsCompleted > 0) {
209             averageStepTime = (int) (executionTime / stepsCompleted);
210         }
211     }
212
213     public String JavaDoc getElapsedTime() {
214         return FormatUtils.formatMillis(executionTime);
215     }
216     
217     public String JavaDoc getEstimatedTime() {
218         if (averageStepTime > 0 && stepsTotal > 0) {
219             long eta = averageStepTime * stepsTotal - executionTime;
220             return FormatUtils.formatMillis(eta);
221         }
222         return null;
223     }
224     
225     public int getProgress() {
226         if (stepsTotal > 0) {
227             return stepsCompleted * 100 / stepsTotal;
228         }
229         return -1;
230     }
231     
232     public String JavaDoc toString() {
233         return "Job " + id + ": " + name;
234     }
235     
236 }
237
Popular Tags