3 * Copyright (C) 2015 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
10 package eu
.etaxonomy
.taxeditor
.util
;
12 import java
.text
.DecimalFormat
;
13 import java
.util
.UUID
;
15 import org
.apache
.log4j
.Logger
;
16 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
18 import eu
.etaxonomy
.cdm
.api
.application
.CdmApplicationState
;
19 import eu
.etaxonomy
.cdm
.api
.service
.IProgressMonitorService
;
20 import eu
.etaxonomy
.cdm
.common
.monitor
.IRemotingProgressMonitor
;
21 import eu
.etaxonomy
.taxeditor
.operation
.IPostMoniteredOperationEnabled
;
24 * Manages client side progress monitors
31 public class ProgressMonitorClientManager
{
32 private static final Logger logger
= Logger
.getLogger(ProgressMonitorClientManager
.class);
35 * Polls the progress monitor service for the progress status of a monitor
36 * corresponding to the given uuid.
39 * @param label for the operation
40 * @param uuid of the remoting monitor already started on the server
41 * @param pollInterval in milliseconds
42 * @param cancelable flag which determines whether the operation can be cancelled
43 * @param postOp callback for running post operation logic
44 * @param monitor to be updated
45 * @return a final progress monitor after the operation is completed
46 * @throws InterruptedException
48 public IRemotingProgressMonitor
pollMonitor(final String label
,
50 final int pollInterval
,
51 final IPostMoniteredOperationEnabled postOp
,
52 IProgressMonitor monitor
) throws InterruptedException
{
53 IProgressMonitorService progressMonitorService
= CdmApplicationState
.getCurrentAppConfig().getProgressMonitorService();
54 IRemotingProgressMonitor remotingMonitor
= progressMonitorService
.getRemotingMonitor(uuid
);
56 final int START_DELAY
=10;
57 // wait about 10 seconds for the remoting monitor to be initialised (i.e. for the begin task method to be called)
58 for(int i
=0;i
<START_DELAY
;i
++) {
60 logger
.info("Waiting for monitered work to start ..");
61 remotingMonitor
= progressMonitorService
.getRemotingMonitor(uuid
);
62 if(remotingMonitor
.getTotalWork() > 0) {
66 // if the total work is still not been set then we assume that the
67 // operation has zero work units
68 if(remotingMonitor
.getTotalWork() == 0) {
69 throw new InterruptedException("Monitor has zero work units");
71 // start the client monitor
72 monitor
.beginTask(label
, remotingMonitor
.getTotalWork());
73 logger
.info("Work to be done: " + remotingMonitor
.getTotalWork());
74 int editorTotalWorkDone
= 0;
75 int serverTotalWorkDone
= 0;
76 // loop until the operation is done
77 while(!(remotingMonitor
.isCanceled() || remotingMonitor
.isFailed() || remotingMonitor
.isDone())) {
78 // wait for pollInterval, then
79 // .... retrieve remoting monitor, then
80 // .... set client monitor info
81 Thread
.sleep(pollInterval
);
82 remotingMonitor
= progressMonitorService
.getRemotingMonitor(uuid
);
83 serverTotalWorkDone
= (int) remotingMonitor
.getWorkDone();
84 logger
.warn("Work done from start: " + serverTotalWorkDone
);
85 String percentage
= new DecimalFormat("#.##").format(remotingMonitor
.getPercentage());
86 monitor
.setTaskName(label
+ " " + percentage
+ "% done ");
87 monitor
.subTask(remotingMonitor
.getSubTask());
88 int worked
= serverTotalWorkDone
- editorTotalWorkDone
;
90 logger
.info("Work done since last check: " + worked
);
91 monitor
.worked(worked
);
93 editorTotalWorkDone
= serverTotalWorkDone
;
95 return remotingMonitor
;
98 postOp
.postOperation(remotingMonitor
);