1
16
17 import org.as2lib.env.except.IllegalArgumentException;
18 import org.as2lib.app.exec.Process;
19 import org.as2lib.app.exec.BatchStartListener;
20 import org.as2lib.app.exec.BatchFinishListener;
21 import org.as2lib.app.exec.BatchErrorListener;
22 import org.as2lib.app.exec.BatchUpdateListener;
23 import org.as2lib.app.exec.ProcessErrorListener;
24 import org.as2lib.app.exec.ProcessPauseListener;
25 import org.as2lib.app.exec.ProcessFinishListener;
26 import org.as2lib.app.exec.ProcessResumeListener;
27 import org.as2lib.app.exec.ProcessUpdateListener;
28 import org.as2lib.app.exec.ProcessStartListener;
29 import org.as2lib.app.exec.AbstractProcess;
30 import org.as2lib.app.exec.Batch;
31
32
51 class org.as2lib.app.exec.BatchProcess extends AbstractProcess
52 implements Batch,
53 ProcessUpdateListener,
54 ProcessPauseListener,
55 ProcessResumeListener,
56 ProcessStartListener,
57 ProcessFinishListener,
58 ProcessErrorListener
59 {
60
61
62 private var list:Array;
63
64
65 private var percent:Number;
66
67
68 private var current:Number;
69
70
73 public function BatchProcess(Void) {
74 acceptListenerType(BatchStartListener);
75 acceptListenerType(BatchFinishListener);
76 acceptListenerType(BatchErrorListener);
77 acceptListenerType(BatchUpdateListener);
78 list = new Array();
79
80 percent = 0;
81 started = false;
82 finished = false;
83 }
84
85
92 public function setParentProcess(p:Process):Void {
93 parent = p;
94 do {
95 if(p == this) {
96 throw new IllegalArgumentException("You can not start a process with itself as super process.", this, arguments);
97 }
98 } while (p = p.getParentProcess());
99 }
100
101
106 public function getParentProcess(Void):Process {
107 return parent;
108 }
109
110
115 public function getCurrentProcess(Void):Process {
116 return list[current];
117 }
118
119
125 public function getAllAddedProcesses(Void):Array {
126 return list.concat();
127 }
128
129
134 public function onProcessFinish(info:Process):Void {
135 if (info == getCurrentProcess()) {
136 info.removeListener(this);
137 nextProcess();
138 } else {
139 var error:IllegalArgumentException = new IllegalArgumentException("Unexpected onFinishProcess occured from "+info+".", this, arguments);
140 publishError(error);
141 finish();
142 }
143 }
144
145
148 private function nextProcess(Void):Void {
149 if (current < list.length-1) {
150 updatePercent(100);
151 current ++;
152 var c:Process = list[current];
153 c.setParentProcess(this);
154 c.addListener(this);
155 sendUpdateEvent();
156 c.start();
157 } else {
158 finish();
159 }
160 }
161
162
167 public function onProcessStart(info:Process):Void {}
168
169
175 public function onProcessPause(info:Process):Void {
176 if (info == getCurrentProcess()) {
177 sendPauseEvent();
178 } else {
179 publishError(new IllegalArgumentException("Unexpected onPauseProcess occured from "+info+". Expected was "+getCurrentProcess(), this, arguments));
180 }
181 }
182
183
189 public function onProcessResume(info:Process):Void {
190 if (info == getCurrentProcess()) {
191 sendResumeEvent();
192 } else {
193 publishError(new IllegalArgumentException("Unexpected onResumeProcess occured from "+info+".", this, arguments));
194 }
195 }
196
197
203 public function onProcessError(info:Process, error):Boolean {
204 var result:Boolean = false;
205 if (info != getCurrentProcess()) {
206 error = new IllegalArgumentException("Unexpected onProcessError occured from "+info+".", this, arguments);
207 }
208
209 result = publishError(error);
210 if (!result) {
211 finish();
212 }
213 return result;
214 }
215
216
222 public function onProcessUpdate(info:Process):Void {
223 var p:Number = info.getPercentage();
224 if(p != null) {
225 updatePercent(p);
226 }
227 sendUpdateEvent();
228 }
229
230
233 public function start() {
234 if(!started) {
235 current = -1;
236 started = false;
237 finished = false;
238 working = true;
239 percent = 0;
240
241 delete endTime;
242 startTime = getTimer();
243 sendStartEvent();
244 started = true;
245 nextProcess();
246 }
247 }
248
249
255 public function addProcess(p:Process):Number {
256 if(p != this) {
257 list.push(p);
258 updatePercent(100);
259 return list.length-1;
260 }
261 }
262
263
272 public function removeProcess(p:Process):Void {
273 var i:Number = list.length;
274 while(--i-(-1)) {
275 if(list[i] == p) {
276 list.slice(i, i);
277 return;
278 }
279 }
280 }
281
282
291 public function removeProcessById(id:Number):Void {
292 list.splice(id, 1);
293 }
294
295
300 public function getPercentage(Void):Number {
301 return percent;
302 }
303
304
309 private function updatePercent(cP:Number):Void {
310 percent = 100/list.length*(current+(1/100*cP));
311 }
312
313
314
320 private function sendErrorEvent(error):Boolean {
321 var result:Boolean = false;
322 if (super.sendErrorEvent(error)) {
323 return true;
324 }
325 var errorDistributor:BatchErrorListener =
326 dC.getDistributor(BatchErrorListener);
327 return errorDistributor.onBatchError(this, error);
328 }
329
330
333 private function sendFinishEvent(Void):Void {
334 super.sendFinishEvent();
335 var finishDistributor:BatchFinishListener =
336 dC.getDistributor(BatchFinishListener);
337 finishDistributor.onBatchFinish(this);
338 }
339
340
343 private function sendPauseEvent(Void):Void {
344 sendUpdateEvent();
345 }
346
347
350 private function sendResumeEvent(Void):Void {
351 sendUpdateEvent();
352 }
353
354
357 private function sendUpdateEvent(Void):Void {
358 super.sendUpdateEvent();
359 var finishDistributor:BatchUpdateListener =
360 dC.getDistributor(BatchUpdateListener);
361 finishDistributor.onBatchUpdate(this);
362 }
363
364
367 private function sendStartEvent(Void):Void {
368 super.sendStartEvent();
369 var finishDistributor:BatchStartListener =
370 dC.getDistributor(BatchStartListener);
371 finishDistributor.onBatchStart(this);
372 }
373 }
374