62f00179d4bc65bd240fecea12c384fdf7824f65
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / util / ProgressMonitorClientManager.java
1 // $Id$
2 /**
3 * Copyright (C) 2015 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
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.
9 */
10 package eu.etaxonomy.taxeditor.util;
11
12 import java.text.DecimalFormat;
13 import java.util.UUID;
14
15 import org.apache.log4j.Logger;
16 import org.eclipse.core.runtime.IProgressMonitor;
17
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;
22
23 /**
24 * Manages client side progress monitors
25 *
26 * @author cmathew
27 * @date 23 Oct 2015
28 *
29 */
30
31 public class ProgressMonitorClientManager {
32 private static final Logger logger = Logger.getLogger(ProgressMonitorClientManager.class);
33
34 /**
35 * Polls the progress monitor service for the progress status of a monitor
36 * corresponding to the given uuid.
37 *
38 *
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
47 */
48 public IRemotingProgressMonitor pollMonitor(final String label,
49 final UUID uuid,
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);
55 try {
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++) {
59 Thread.sleep(1000);
60 logger.info("Waiting for monitered work to start ..");
61 remotingMonitor = progressMonitorService.getRemotingMonitor(uuid);
62 if(remotingMonitor.getTotalWork() > 0) {
63 break;
64 }
65 }
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");
70 }
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;
89 if(worked > 0) {
90 logger.info("Work done since last check: " + worked);
91 monitor.worked(worked);
92 }
93 editorTotalWorkDone = serverTotalWorkDone;
94 }
95 return remotingMonitor;
96 } finally {
97 if(postOp != null) {
98 postOp.postOperation(remotingMonitor);
99 }
100 }
101 }
102 }