add ecxeptions to update result instead of throwing them in pollingRemotingProgressMo...
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / util / ProgressMonitorClientManager.java
1 /**
2 * Copyright (C) 2015 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
8 */
9 package eu.etaxonomy.taxeditor.util;
10
11 import java.text.DecimalFormat;
12 import java.util.Arrays;
13 import java.util.List;
14 import java.util.UUID;
15
16 import org.apache.log4j.Logger;
17 import org.eclipse.core.runtime.IProgressMonitor;
18
19 import eu.etaxonomy.cdm.api.application.CdmApplicationState;
20 import eu.etaxonomy.cdm.api.service.IProgressMonitorService;
21 import eu.etaxonomy.cdm.api.service.UpdateResult;
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 && remotingMonitor.isDone()) {
100 return remotingMonitor;
101 // throw new InterruptedException("Monitor has zero work units");
102 }else if (remotingMonitor.getTotalWork() == 0 ){
103 throw new InterruptedException("Monitor has zero work units");
104 }
105 // start the client monitor
106 monitor.beginTask(label, remotingMonitor.getTotalWork());
107 logger.info("Work to be done: " + remotingMonitor.getTotalWork());
108 int editorTotalWorkDone = 0;
109 int serverTotalWorkDone = 0;
110 // loop until the operation is done
111 int feedbackCount = 0;
112 while(!(remotingMonitor.isCanceled() || remotingMonitor.isFailed() || remotingMonitor.isDone())) {
113 // wait for pollInterval, then
114 // .... retrieve remoting monitor, then
115 // .... set client monitor info
116 Thread.sleep(pollInterval);
117 remotingMonitor = progressMonitorService.getRemotingMonitor(uuid);
118 // check if remoting monitor is waiting for feedback
119 if(remotingMonitor.getIsWaitingForFeedback()) {
120 if(feedbackGenerators != null) {
121 // if we have run out of feedback generators while
122 // the remoting monitor is waiting on feedback
123 // then throw exception
124 if(feedbackCount + 1 > feedbackGenerators.size()) {
125 IllegalStateException exception = new IllegalStateException("Remoting monitor waiting on feedback that does not exist");
126 Object result = remotingMonitor.getResult();
127 if (result instanceof UpdateResult){
128 ((UpdateResult)result).addException(exception);
129 }
130
131 }
132 feedbackGenerators.get(feedbackCount).setFeedbackForMonitor(uuid);
133 feedbackCount++;
134 }
135 }
136 serverTotalWorkDone = (int) remotingMonitor.getWorkDone();
137 logger.info("Work done from start: " + serverTotalWorkDone);
138 String percentage = new DecimalFormat("#.##").format(remotingMonitor.getPercentage());
139 // set dialog text
140 monitor.setTaskName(label + " " + percentage + "% done ");
141 monitor.subTask(remotingMonitor.getSubTask());
142 int worked = serverTotalWorkDone - editorTotalWorkDone;
143 if(worked > 0) {
144 logger.info("Work done since last check: " + worked);
145 monitor.worked(worked);
146 }
147 editorTotalWorkDone = serverTotalWorkDone;
148 }
149 if(remotingMonitor.getResult() instanceof Exception) {
150 UpdateResult result = new UpdateResult();
151 result.addException((Exception) remotingMonitor.getResult());
152 // throw new IllegalStateException((Exception)remotingMonitor.getResult());
153 }
154 return remotingMonitor;
155 } finally {
156 if(postOp != null && remotingMonitor.isDone()) {
157 postOp.postOperation(remotingMonitor);
158 }
159 }
160 }
161 }