Project

General

Profile

Download (6.85 KB) Statistics
| Branch: | Tag: | Revision:
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.common.monitor.IRemotingProgressMonitor;
22
import eu.etaxonomy.taxeditor.operation.IFeedbackGenerator;
23
import eu.etaxonomy.taxeditor.operation.IPostMoniteredOperationEnabled;
24

    
25
/**
26
 * Manages client side progress monitors
27
 *
28
 * @author cmathew
29
 * @date 23 Oct 2015
30
 *
31
 */
32

    
33
public class ProgressMonitorClientManager {
34
    private static final Logger logger = Logger.getLogger(ProgressMonitorClientManager.class);
35

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