source: trunk/gli/src/org/greenstone/gatherer/shell/GImportProgressMonitor.java@ 8622

Last change on this file since 8622 was 8243, checked in by mdewsnip, 20 years ago

Removed all occurrences of classes explicitly importing other classes in the same package.

  • Property svn:keywords set to Author Date Id Revision
File size: 17.1 KB
Line 
1/**
2 *#########################################################################
3 *
4 * A component of the Gatherer application, part of the Greenstone digital
5 * library suite from the New Zealand Digital Library Project at the
6 * University of Waikato, New Zealand.
7 *
8 * <BR><BR>
9 *
10 * Author: John Thompson, Greenstone Digital Library, University of Waikato
11 *
12 * <BR><BR>
13 *
14 * Copyright (C) 1999 New Zealand Digital Library Project
15 *
16 * <BR><BR>
17 *
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 2 of the License, or
21 * (at your option) any later version.
22 *
23 * <BR><BR>
24 *
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
29 *
30 * <BR><BR>
31 *
32 * You should have received a copy of the GNU General Public License
33 * along with this program; if not, write to the Free Software
34 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
35 *########################################################################
36 */
37package org.greenstone.gatherer.shell;
38
39import java.awt.Component;
40import java.util.*;
41import javax.swing.JProgressBar;
42import org.greenstone.gatherer.Configuration;
43import org.greenstone.gatherer.DebugStream;
44import org.greenstone.gatherer.Dictionary;
45import org.greenstone.gatherer.Gatherer;
46import org.greenstone.gatherer.util.StaticStrings;
47
48/** This implementation of <i>GShellProgressMonitor</i> is designed to parse and translate the progress of a import.pl call.
49 * @author John Thompson, Greenstone Digital Library, University of Waikato
50 * @version 2.1
51 */
52public class GImportProgressMonitor
53 implements GShellProgressMonitor {
54 /** Indicates if the progress bar is currently showing a string. */
55 private boolean showing_string = false;
56 /** Indicates if the GUI has asked the process this object monitors to stop. */
57 private boolean stop = false;
58 /** A count of the extracted files processed so far. */
59 private int extracted_file_count;
60 /** The number of documents processed (or rejected) so far. */
61 private int file_count;
62 /** The next value to be set for the progress bar - I use this rather than a compounding progress measure to try to limit rounding inaccuracies (nothing looks worse than the progress bar having to jump the last 10-15%) */
63 private int next_progress_value;
64 /** The number of files expected to be scanned by this import process. */
65 private int num_expected_docs;
66 /** This holds the number of documents actually processed by the import command, as garnered from the final block of text output. */
67 private int num_docs;
68
69 private int threshold = Configuration.SYSTEMS_MODE;
70 /** The progress bar this monitor updates. */
71 private JProgressBar progress_bar;
72
73 /** The progress bar that is shared with build monitor. */
74 private JProgressBar shared_progress_bar;
75
76 /** */
77 static final private String BLOCKED_ATTRIBUTE = "blocked";
78 static final private String CONSIDERED_ATTRIBUTE = "considered";
79 static final private String FAILED_ATTRIBUTE = "failed";
80 static final private String IGNORED_ATTRIBUTE = "ignored";
81 static final private String IMPORT_ELEMENT = "Import";
82 static final private String PROCESSED_ATTRIBUTE = "processed";
83 /** */
84 static final private String NAME_ATTRIBUTE = "n";
85 /** */
86 static final private String PLUGIN_ATTRIBUTE = "p";
87 /** The fixed portion of the progress bar used for the calculating of file size and other pre-import functions. */
88 static final private int CALCULATION = 50000;
89 /** The fixed portion of the progress bar used for extracted metadata. */
90 static final private int EXTRACTED = 200000;
91 /** The minimum value for this progress bar. */
92 static final private int MIN = 0;
93 /** The fixed portion of the progress bar used for processed documents. */
94 static final private int PROCESSED = 750000;
95 /** The element name of a file detected message. */
96 static final private String FILE_ELEMENT = "File";
97 /** The element name of a file processing message. */
98 static final private String PROCESSING_ELEMENT = "Processing";
99 /** The element name of a processing error message. */
100 static final private String PROCESSINGERROR_ELEMENT = "ProcessingError";
101 /** The element name of an import complete message. */
102 static final private String IMPORTCOMPLETE_ELEMENT = "ImportComplete";
103 /** The element name of a file not processed. */
104 static final private String NONPROCESSEDFILE_ELEMENT = "NonProcessedFile";
105 /** The element name of a file not recognised. */
106 static final private String NONRECOGNISEDFILE_ELEMENT = "NonRecognisedFile";
107
108
109 public GImportProgressMonitor() {
110 progress_bar = new JProgressBar();
111 progress_bar.setIndeterminate(false);
112 progress_bar.setMaximum(CALCULATION + PROCESSED + EXTRACTED);
113 progress_bar.setMinimum(MIN);
114 progress_bar.setString(null);
115 progress_bar.setStringPainted(true);
116 next_progress_value = CALCULATION;
117 shared_progress_bar = new JProgressBar();
118 shared_progress_bar.setIndeterminate(false);
119 shared_progress_bar.setMaximum((CALCULATION + PROCESSED + EXTRACTED) * 2);
120 shared_progress_bar.setMinimum(MIN);
121 shared_progress_bar.setString(null); // Default string
122 shared_progress_bar.setStringPainted(true);
123 setValue(MIN);
124 }
125
126 /** Method to register a new progress bar with this monitor.
127 * @param progress_bar The new <strong>JProgressBar</strong>.
128 */
129 public void addProgressBar(JProgressBar progress_bar) {
130 this.progress_bar = progress_bar;
131 progress_bar.setMaximum(CALCULATION + PROCESSED + EXTRACTED);
132 progress_bar.setMinimum(MIN);
133 setValue(MIN);
134 next_progress_value = CALCULATION;
135 }
136
137 /** Determine the script exit value according to the progress monitor. This gets around a problem where several script failures actually result with a successful exit value.
138 * @return A <i>int</i> with a value of zero if and only if the script was successful.
139 */
140 public int exitValue() {
141 if(num_docs > 0) {
142 return 0;
143 }
144 return 1;
145 }
146
147 /** Retrieve the number of documents recorded by the import monitor during the execution of the import scripts.
148 * @return An <i>int</i> giving the document number.
149 */
150 public int getNumberOfDocuments() {
151 return num_docs;
152 }
153
154 /** Method to retrieve whatever control is being used as the progress indicator. Usually a <strong>JProgressBar</strong> but there may be others implemented later.
155 * @return A <strong>Component</strong> on which the progress of the process is being displayed.
156 */
157 public Component getProgress() {
158 return progress_bar;
159 }
160
161 public JProgressBar getSharedProgress() {
162 return shared_progress_bar;
163 }
164
165 public void messageOnProgressBar(String message)
166 {
167 if ((message!=null) & (message!="")) {
168 progress_bar.setString(message);
169 shared_progress_bar.setString(message);
170 showing_string = true;
171 }
172 else {
173 progress_bar.setString(null);
174 shared_progress_bar.setString(null);
175 showing_string = false;
176 }
177 }
178
179 /** Method to determine the state of the stop flag, which may be set by the visual component that created this monitor.
180 * @return A <strong>boolean</strong> indicating if the process has been asked to stop.
181 */
182 public synchronized boolean hasSignalledStop() {
183 return stop;
184 }
185
186 /** Inform the progress bar that it should programatically increment progress by one step. This is only called during the metadata archive extraction so each step should be (1000000 / 5) / num_docs. */
187 public void increment() {
188 extracted_file_count++;
189 // The current progress is calculated to be:
190 // The fixed calculation value plus the fixed processed value plus some portion of the fixed extracted value. This portion is the extracted_file_count over the total number of documents available. Note that this breaks badly for bibliographical files (for now).
191 setValue(CALCULATION + PROCESSED + ((EXTRACTED * extracted_file_count) / num_docs));
192 }
193
194 /** This method is used to 'feed in' a line of text captured from the process.
195 * @param queue a queue which at the moment should contain a single GShellEvent
196 */
197 public void process(ArrayList queue) {
198 // Retrieve the first event.
199 GShellEvent event = (GShellEvent) queue.get(0);
200 // Remove 'import.pl> ' bit
201 String line = event.getMessage();
202 line = line.substring(line.indexOf(StaticStrings.GREATER_THAN_CHARACTER) + 1);
203 line = line.trim();
204 // System.err.println("**** line = " + line);
205
206 ///ystem.err.println("Process: " + line);
207 if(line.startsWith(StaticStrings.LESS_THAN_CHARACTER) && line.endsWith(StaticStrings.GREATER_THAN_CHARACTER)) {
208 // No original event is passed on, even in the lower modes
209 event.veto();
210 // Create a new element from it
211 GShellElement element = new GShellElement(line);
212 String name = element.getElementName();
213 ///ystem.err.println("Gatherer tag: " + name);
214 // We may be reading a file. Remember we have to offset process as we recieve this message 'before' a document is processed. Hence the use of 'next_progress_value'
215 if(name.equals(IMPORT_ELEMENT)) {
216 ///ystem.err.println("#Import");
217 // We're into parsing output, so we don't need the 'calculating file size' etc string.
218 if(showing_string) {
219 progress_bar.setString(null);
220 showing_string = false;
221 }
222 if(Configuration.getMode() <= threshold) {
223 queue.add(new GShellEvent(event.getSource(), 0, event.getType(), Dictionary.get("GShell.Import.ImportBegun1"), event.getStatus()));
224 }
225 }
226 else if(name.equals(FILE_ELEMENT)) {
227 ///ystem.err.println("#File");
228 file_count++;
229 // Set the next progress
230 if(next_progress_value > 0) {
231 setValue(next_progress_value);
232 }
233 // Now we calculate the next progress value
234 next_progress_value = CALCULATION + ((PROCESSED * file_count) / num_expected_docs);
235 event.veto(); // Unconditional veto
236 }
237 // Or we're being told what plugin is actually processing the file
238 else if(name.equals(PROCESSING_ELEMENT)) {
239 ///ystem.err.println("#FileProcessing");
240 // If we are at lower mode settings fire a new 'dumbed down' event
241 if(Configuration.getMode() <= threshold) {
242 String args[] = new String[2];
243 args[0] = element.getAttribute(NAME_ATTRIBUTE);
244 args[1] = element.getAttribute(PLUGIN_ATTRIBUTE);
245 queue.add(new GShellEvent(event.getSource(), 0, event.getType(), Dictionary.get("GShell.Import.FileProcessing", args), event.getStatus()));
246 args = null;
247 }
248 }
249 // processing error
250 else if (name.equals(PROCESSINGERROR_ELEMENT)) {
251 if(Configuration.getMode() <= threshold) {
252 String args[] = new String[1];
253 args[0] = element.getAttribute(NAME_ATTRIBUTE);
254 queue.add(new GShellEvent(event.getSource(), 0, event.getType(), Dictionary.get("GShell.Import.FileProcessingError", args), event.getStatus()));
255 args = null;
256 }
257 }
258 // unrecognised file
259 else if (name.equals(NONRECOGNISEDFILE_ELEMENT)) {
260 if(Configuration.getMode() <= threshold) {
261 String args[] = new String[1];
262 args[0] =element.getAttribute(NAME_ATTRIBUTE);
263 queue.add(new GShellEvent(event.getSource(), 0, event.getType(), Dictionary.get("GShell.Import.FileNotRecognised", args), event.getStatus()));
264 args = null;
265 }
266 }
267 // unprocessed file
268 else if (name.equals(NONPROCESSEDFILE_ELEMENT)) {
269 if(Configuration.getMode() <= threshold) {
270 String args[] = new String[1];
271 args[0] =element.getAttribute(NAME_ATTRIBUTE);
272 queue.add(new GShellEvent(event.getSource(), 0, event.getType(), Dictionary.get("GShell.Import.FileNotProcessed", args), event.getStatus()));
273 args = null;
274 }
275 }
276 // Or the import complete element
277 else if(name.equals(IMPORTCOMPLETE_ELEMENT)) {
278 // Set the next progress
279 setValue(next_progress_value);
280 // If we are at lower mode settings fire a new 'dumbed down' event
281 if(Configuration.getMode() <= threshold) {
282 queue.add(new GShellEvent(event.getSource(), 0, event.getType(), Dictionary.get("GShell.Import.ImportComplete1"), event.getStatus()));
283 String considered_str = element.getAttribute(CONSIDERED_ATTRIBUTE);
284
285 if (considered_str.equals(StaticStrings.ONE_CHARACTER)) {
286 queue.add(new GShellEvent(event.getSource(), 0, event.getType(), Dictionary.get("GShell.Import.ImportComplete2.1"), event.getStatus()));
287 } else {
288 queue.add(new GShellEvent(event.getSource(), 0, event.getType(), Dictionary.get("GShell.Import.ImportComplete2", considered_str), event.getStatus()));
289 }
290 considered_str = null;
291 // The number of documents processed
292 String processed_str = element.getAttribute(PROCESSED_ATTRIBUTE);
293 if(!processed_str.equals(StaticStrings.ZERO_CHARACTER)) {
294 if(processed_str.equals(StaticStrings.ONE_CHARACTER)) {
295 queue.add(new GShellEvent(event.getSource(), 0, event.getType(), " "+Dictionary.get("GShell.Import.ImportComplete.Processed.1"), event.getStatus()));
296 }
297 else {
298 queue.add(new GShellEvent(event.getSource(), 0, event.getType(), " "+Dictionary.get("GShell.Import.ImportComplete.Processed", processed_str), event.getStatus()));
299 }
300 }
301 // Try to set num docs
302 try {
303 num_docs = Integer.parseInt(processed_str);
304 }
305 catch(Exception exception) {
306 num_docs = 0;
307 }
308 processed_str = null;
309
310 // The number of documents blocked
311 String blocked_str = element.getAttribute(BLOCKED_ATTRIBUTE);
312 if(!blocked_str.equals(StaticStrings.ZERO_CHARACTER)) {
313 if (blocked_str.equals(StaticStrings.ONE_CHARACTER)) {
314 queue.add(new GShellEvent(event.getSource(), 0, event.getType(), " "+Dictionary.get("GShell.Import.ImportComplete.Blocked.1"), event.getStatus()));
315 } else {
316 queue.add(new GShellEvent(event.getSource(), 0, event.getType(), " "+Dictionary.get("GShell.Import.ImportComplete.Blocked", blocked_str), event.getStatus()));
317 }
318 }
319 blocked_str = null;
320
321 // The number of documents ignored
322 String ignored_str = element.getAttribute(IGNORED_ATTRIBUTE);
323 if(!ignored_str.equals(StaticStrings.ZERO_CHARACTER)) {
324 if(ignored_str.equals(StaticStrings.ONE_CHARACTER)) {
325 queue.add(new GShellEvent(event.getSource(), 0, event.getType(), " "+Dictionary.get("GShell.Import.ImportComplete.Ignored.1"), event.getStatus()));
326 } else {
327 queue.add(new GShellEvent(event.getSource(), 0, event.getType(), " "+Dictionary.get("GShell.Import.ImportComplete.Ignored", ignored_str), event.getStatus()));
328 }
329 }
330 ignored_str = null;
331
332 // The number of documents failed
333 String failed_str = element.getAttribute(FAILED_ATTRIBUTE);
334 if(!failed_str.equals(StaticStrings.ZERO_CHARACTER)) {
335 if(failed_str.equals(StaticStrings.ONE_CHARACTER)) {
336 queue.add(new GShellEvent(event.getSource(), 0, event.getType(), " "+Dictionary.get("GShell.Import.ImportComplete.Failed.1"), event.getStatus()));
337 } else {
338 queue.add(new GShellEvent(event.getSource(), 0, event.getType(), " "+Dictionary.get("GShell.Import.ImportComplete.Failed", failed_str), event.getStatus()));
339 }
340 }
341 failed_str = null;
342 // Message is finally ready
343 queue.add(new GShellEvent(event.getSource(), 0, event.getType(), Dictionary.get("GShell.Import.ImportComplete3"), event.getStatus()));
344 } // if mode below threshhold
345 else {
346 // Try to set num docs
347 String processed_str = element.getAttribute(PROCESSED_ATTRIBUTE);
348 try {
349 num_docs = Integer.parseInt(processed_str);
350 }
351 catch(Exception exception) {
352 num_docs = 0;
353 }
354 }
355 }
356 else {
357 // Veto it
358 event.veto();
359 }
360 } // GLI output
361 else if(Configuration.getMode() <= threshold) {
362 event.veto();
363 }
364 }
365
366 public void reset() {
367 setValue(MIN);
368 progress_bar.setString(null);
369 progress_bar.updateUI();
370 }
371
372 public void saving() {
373 progress_bar.setString(Dictionary.get("SaveProgressDialog.Title"));
374 showing_string = true;
375 }
376
377 /** Since the creator of this process monitor is actually in the GUI, this class provides the simpliest way to send a cancel process message between the two.
378 * @param state The desired state of the stop flag as a <strong>boolean</strong>.
379 */
380 public synchronized void setStop(boolean state) {
381 this.stop = state;
382 }
383
384 /** This method resets this monitor to the start, reseting the process parsing and progress bar.
385 */
386 public void start() {
387 stop = false;
388 setValue(MIN);
389 progress_bar.setString(Dictionary.get("FileActions.Calculating_Size"));
390 showing_string = true;
391 extracted_file_count = 0;
392 file_count = 0;
393 next_progress_value = -1;
394 num_docs = -1;
395 num_expected_docs = Gatherer.c_man.getCollection().getCount();
396 }
397
398 /** This method indicates the process is complete.
399 */
400 public void stop() {
401 setValue(CALCULATION + PROCESSED + EXTRACTED);
402 }
403
404 int previous_value = -1;
405 private void setValue(int value) {
406 progress_bar.setValue(value);
407 shared_progress_bar.setValue(value);
408 if(value != 0 && value < previous_value) {
409 DebugStream.println("Progress is decreasing! " + previous_value + " -> " + value);
410 }
411 previous_value = value;
412 }
413}
414
Note: See TracBrowser for help on using the repository browser.