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

Last change on this file since 7357 was 7357, checked in by kjdon, 20 years ago

tidied up the import log handling

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