View Javadoc
1   package net.sf.appstatus.batch;
2   
3   import java.util.ArrayList;
4   import java.util.Date;
5   import java.util.List;
6   
7   import net.sf.appstatus.core.batch.IBatch;
8   import net.sf.appstatus.core.batch.IBatchProgressMonitor;
9   
10  /**
11   * Bean batch implementation,
12   * <p>
13   * It uses a progress monitor to track the batch status informations.
14   *
15   */
16  public class Batch implements IBatch {
17  
18  	private final String group;
19  	private InProcessBatchProgressMonitor monitor;
20  
21  	private final String name;
22  
23  	/**
24  	 * Unique ID for this batch instance.
25  	 */
26  	private final String uuid;
27  	private int zombieInterval = 1000 * 60 * 10;
28  
29  	/**
30  	 * Creates a new Batch.
31  	 * <p>
32  	 * This method is not intended to be used directly.
33  	 *
34  	 * @param uuid
35  	 *            unique batch identifier
36  	 */
37  	public Batch(String uuid) {
38  		this(uuid, null, null);
39  	}
40  
41  	/**
42  	 * Creates a new Batch.
43  	 * <p>
44  	 * This method is not intended to be used directly.
45  	 *
46  	 * @param uuid
47  	 *            unique batch identifier
48  	 * @param name
49  	 *            batch name
50  	 * @param group
51  	 *            batch group name
52  	 */
53  	public Batch(String uuid, String name, String group) {
54  		if (uuid == null) {
55  			throw new IllegalArgumentException("Batch uuid cannot be null");
56  		}
57  		this.uuid = uuid;
58  		this.name = name;
59  		this.group = group;
60  	}
61  
62  	/*
63  	 * (non-Javadoc)
64  	 * 
65  	 * @see java.lang.Object#equals(java.lang.Object)
66  	 */
67  	@Override
68  	public boolean equals(Object obj) {
69  		if (this == obj) {
70  			return true;
71  		}
72  		if (!(obj instanceof Batch)) {
73  			return false;
74  		}
75  
76  		return uuid.equals(((Batch) obj).getUuid());
77  	}
78  
79  	/**
80  	 * Retrieve the current processed item.
81  	 */
82  	public String getCurrentItem() {
83  		if (monitor == null) {
84  			return null;
85  		}
86  
87  		Object currentItem = monitor.getCurrentItem();
88  		if (currentItem == null) {
89  			return null;
90  		}
91  		return currentItem.toString();
92  	}
93  
94  	/**
95  	 * Retrieve the current task.
96  	 */
97  	public String getCurrentTask() {
98  		if (monitor == null) {
99  			return null;
100 		}
101 		return monitor.getTaskName();
102 	}
103 
104 	public Date getEndDate() {
105 		if (monitor == null) {
106 			return null;
107 		}
108 		return monitor.getEndDate();
109 	}
110 
111 	public String getGroup() {
112 		return group;
113 	}
114 
115 	public long getItemCount() {
116 		if (monitor == null) {
117 			return 0;
118 		}
119 		return monitor.getItemCount();
120 	}
121 
122 	public String getLastMessage() {
123 		if (monitor == null) {
124 			return null;
125 		}
126 		return monitor.getLastMessage();
127 	}
128 
129 	public Date getLastUpdate() {
130 		if (monitor == null) {
131 			return null;
132 		}
133 		return monitor.getLastUpdate();
134 	}
135 
136 	public String getName() {
137 		return name;
138 	}
139 
140 	public IBatchProgressMonitor getProgressMonitor() {
141 		return monitor;
142 	}
143 
144 	public float getProgressStatus() {
145 		if (monitor == null || monitor.getTotalWork() == IBatchProgressMonitor.UNKNOW || monitor.getTotalWork() == 0) {
146 			return IBatchProgressMonitor.UNKNOW;
147 		}
148 
149 		return monitor.getProgress() * 100f / monitor.getTotalWork();
150 	}
151 
152 	public List<String> getRejectedItemsId() {
153 		if (monitor == null) {
154 			return new ArrayList<String>();
155 		}
156 		return monitor.getRejectedItems();
157 	}
158 
159 	public Date getStartDate() {
160 		if (monitor == null) {
161 			return null;
162 		}
163 		return monitor.getStartDate();
164 	}
165 
166 	public String getStatus() {
167 		if (monitor == null) {
168 			return null;
169 		}
170 
171 		if (!monitor.isDone()) {
172 
173 			// If batch was not updated since 1 hour, report as Zombie.
174 			if (new Date().getTime() - monitor.getLastUpdate().getTime() > zombieInterval) {
175 				return STATUS_ZOMBIE;
176 			}
177 
178 			return STATUS_RUNNING;
179 		}
180 
181 		if (monitor.isSuccess()) {
182 			return STATUS_SUCCESS;
183 		}
184 
185 		return STATUS_FAILURE;
186 	}
187 
188 	public String getUuid() {
189 		return uuid;
190 	}
191 
192 	public boolean isSuccess() {
193 		if (monitor == null) {
194 			return false;
195 		}
196 		return monitor.isSuccess();
197 	}
198 
199 	/**
200 	 * @inheritDoc
201 	 *
202 	 * @see net.sf.appstatus.core.batch.IBatch#setProgressMonitor(net.sf.appstatus.core.batch.IBatchProgressMonitor)
203 	 */
204 	public void setProgressMonitor(IBatchProgressMonitor monitor) {
205 		this.monitor = (InProcessBatchProgressMonitor) monitor;
206 	}
207 
208 	public void setZombieInterval(int zombieInterval) {
209 		this.zombieInterval = zombieInterval;
210 	}
211 }