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

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

Replaced all Gatherer.print* with DebugStream.print*.

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