c50686fa191d2779dd56624e9b7bb6c84c69170e
[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.Arrays;
14 import java.util.List;
15 import java.util.UUID;
16
17 import org.apache.log4j.Logger;
18 import org.eclipse.core.runtime.IProgressMonitor;
19
20 import eu.etaxonomy.cdm.api.application.CdmApplicationState;
21 import eu.etaxonomy.cdm.api.service.IProgressMonitorService;
22 import eu.etaxonomy.cdm.common.monitor.IRemotingProgressMonitor;
23 import eu.etaxonomy.taxeditor.operation.IFeedbackGenerator;
24 import eu.etaxonomy.taxeditor.operation.IPostMoniteredOperationEnabled;
25
26 /**
27 * Manages client side progress monitors
28 *
29 * @author cmathew
30 * @date 23 Oct 2015
31 *
32 */
33
34 public class ProgressMonitorClientManager {
35 private static final Logger logger = Logger.getLogger(ProgressMonitorClientManager.class);
36
37 /**
38 * Polls the progress monitor service for the progress status of a monitor
39 * corresponding to the given uuid.
40 *
41 * @param label for the operation
42 * @param uuid of the remoting monitor already started on the server
43 * @param pollInterval in milliseconds
44 * @param cancelable flag which determines whether the operation can be cancelled
45 * @param postOp callback for running post operation logic
46 * @param feedbackGenerator feedback generator corresponding to the
47 * 'wait on feedback' request made on the remoting monitor
48 * @param monitor to be updated
49 * @return a final progress monitor after the operation is completed
50 * @throws InterruptedException
51 */
52 public IRemotingProgressMonitor pollMonitor(final String label,
53 final UUID uuid,
54 final int pollInterval,
55 final IPostMoniteredOperationEnabled postOp,
56 IFeedbackGenerator feedbackGenerator,
57 IProgressMonitor monitor) throws InterruptedException {
58 return pollMonitor(label, uuid, pollInterval, postOp, Arrays.asList(feedbackGenerator), monitor);
59 }
60 /**
61 * Polls the progress monitor service for the progress status of a monitor
62 * corresponding to the given uuid.
63 *
64 *
65 * @param label for the operation
66 * @param uuid of the remoting monitor already started on the server
67 * @param pollInterval in milliseconds
68 * @param cancelable flag which determines whether the operation can be cancelled
69 * @param postOp callback for running post operation logic
70 * @param feedbackGenerators list of feedback generators corresponding to the
71 * size and exact order of the 'wait on feedback' requests made on the
72 * remoting monitor
73 * @param monitor to be updated
74 * @return a final progress monitor after the operation is completed
75 * @throws InterruptedException
76 */
77 public IRemotingProgressMonitor pollMonitor(final String label,
78 final UUID uuid,
79 final int pollInterval,
80 final IPostMoniteredOperationEnabled postOp,
81 List<IFeedbackGenerator> feedbackGenerators,
82 IProgressMonitor monitor) throws InterruptedException {
83 IProgressMonitorService progressMonitorService = CdmApplicationState.getCurrentAppConfig().getProgressMonitorService();
84 IRemotingProgressMonitor remotingMonitor = progressMonitorService.getRemotingMonitor(uuid);
85 try {
86 final int START_DELAY=10;
87 // wait about 10 seconds for the remoting monitor to be initialised
88 // (i.e. for the begin task method to be called ON THE REMOTING MONITOR)
89 for(int i=0;i<START_DELAY;i++) {
90 Thread.sleep(1000);
91 logger.info("Waiting for monitered work to start ..");
92 remotingMonitor = progressMonitorService.getRemotingMonitor(uuid);
93 if(remotingMonitor.getTotalWork() > 0) {
94 break;
95 }
96 }
97 // if the total work is still not been set then we assume that the
98 // operation has zero work units
99 if(remotingMonitor.getTotalWork() == 0) {
100 throw new InterruptedException("Monitor has zero work units");
101 }
102 // start the client monitor
103 monitor.beginTask(label, remotingMonitor.getTotalWork());
104 logger.info("Work to be done: " + remotingMonitor.getTotalWork());
105 int editorTotalWorkDone = 0;
106 int serverTotalWorkDone = 0;
107 // loop until the operation is done
108 int feedbackCount = 0;
109 while(!(remotingMonitor.isCanceled() || remotingMonitor.isFailed() || remotingMonitor.isDone())) {
110 // wait for pollInterval, then
111 // .... retrieve remoting monitor, then
112 // .... set client monitor info
113 Thread.sleep(pollInterval);
114 remotingMonitor = progressMonitorService.getRemotingMonitor(uuid);
115 // check if remoting monitor is waiting for feedback
116 if(remotingMonitor.isWaitingForFeedback()) {
117 if(feedbackGenerators != null) {
118 // if we have run out of feedback generators while
119 // the remoting monitor is waiting on feedback
120 // then throw exception
121 if(feedbackCount + 1 > feedbackGenerators.size()) {
122 throw new IllegalStateException("Remoting monitor waiting on feedback that does not exist");
123 }
124 progressMonitorService.setFeedback(uuid, feedbackGenerators.get(feedbackCount).generateFeedback());
125 feedbackCount++;
126 }
127 }
128 serverTotalWorkDone = (int) remotingMonitor.getWorkDone();
129 logger.info("Work done from start: " + serverTotalWorkDone);
130 String percentage = new DecimalFormat("#.##").format(remotingMonitor.getPercentage());
131 // set dialog text
132 monitor.setTaskName(label + " " + percentage + "% done ");
133 monitor.subTask(remotingMonitor.getSubTask());
134 int worked = serverTotalWorkDone - editorTotalWorkDone;
135 if(worked > 0) {
136 logger.info("Work done since last check: " + worked);
137 monitor.worked(worked);
138 }
139 editorTotalWorkDone = serverTotalWorkDone;
140 }
141 if(remotingMonitor.getResult() instanceof Exception) {
142 throw new IllegalStateException((Exception)remotingMonitor.getResult());
143 }
144 return remotingMonitor;
145 } finally {
146 if(postOp != null && remotingMonitor.isDone()) {
147 postOp.postOperation(remotingMonitor);
148 }
149 }
150 }
151 }