source: gli/trunk/src/org/greenstone/gatherer/collection/Collection.java@ 19944

Last change on this file since 19944 was 19944, checked in by kjdon, 15 years ago

fixed a typo on a debug statement

  • Property svn:keywords set to Author Date Id Revision
File size: 18.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.collection;
38
39import java.io.*;
40import java.util.*;
41import javax.swing.*;
42import javax.swing.tree.*;
43import org.greenstone.gatherer.Configuration;
44import org.greenstone.gatherer.DebugStream;
45import org.greenstone.gatherer.Gatherer;
46import org.greenstone.gatherer.cdm.CollectionDesignManager;
47import org.greenstone.gatherer.cdm.CollectionMeta;
48import org.greenstone.gatherer.cdm.CollectionMetaManager;
49import org.greenstone.gatherer.util.StaticStrings;
50import org.greenstone.gatherer.util.XMLTools;
51import org.w3c.dom.*;
52
53/** Collection provides a common collection point for all the information about a certain collection build session. It keeps a record of several other managers that actually handle the content of the collection, such as metadata sets and metadata itself.
54 * @author John Thompson, Greenstone Digital Library, University of Waikato
55 * @version 2.3c
56 */
57public class Collection {
58 /** A reference to the Collection Design Manager. */
59 public CollectionDesignManager cdm;
60 /** A reference to the build ScriptOptions. */
61 public ScriptOptions build_options;
62 /** A reference to the import ScriptOptions. */
63 public ScriptOptions import_options;
64 /** A reference to the schedule ScriptOptions */
65 public ScriptOptions schedule_options;
66 /** true if an error has occurred during construction */
67 public boolean error = false;
68 /** <i>true</i> if the currently loaded collection has been saved since the last significant change, <i>false</i> otherwise. */
69 private boolean saved = false;
70 /*<i>true</i> if the currently loaded collection has had files added since its last build */
71 private boolean filesChanged = false;
72 /*<i>true</i> if the currently loaded collection has had metadata added since its last build */
73 private boolean metadataChanged = false;
74 /** The document around which this collection class is based. */
75 private Document document;
76
77 /** The file the collection is in (the file may not actually exist, such in the case of a legacy collection)! */
78 private File file;
79 /** The name of the argument element. */
80 static final private String ARGUMENT = "Argument";
81 static final private String BASE_COLLECTION = "base_collection";
82 /** The name of the build element. */
83 static final private String BUILD = "Build";
84 /** The name of the build config element. */
85 static final private String BUILD_CONFIG = "BuildConfig";
86 /** The name of the collection xml template. */
87 static final private String COLLECTION_XML_TEMPLATE = "xml/template.col";
88 /** The name of the import element. */
89 static final private String IMPORT = "Import";
90 /** The name of the schedule element. */
91 static final private String SCHEDULE = "Schedule";
92 /** The name of the export element. */
93 static final private String EXPORT = "Export";
94 /** The name of the imported attribute. */
95 static final private String IMPORTED = "imported";
96 /** The name of the imported attribute. */
97 static final private String SKIM_FILE = "skimfile";
98
99
100 /** Constructor. */
101 public Collection(File collection_xml) {
102 this.file = collection_xml;
103 // Try to load this collections details.
104 document = XMLTools.parseXMLFile(collection_xml);
105 // If that fails load the default settings for a collection.
106 if(document == null) {
107 document = XMLTools.parseXMLFile(COLLECTION_XML_TEMPLATE, true);
108 }
109 if (document == null) {
110 error = true;
111 return;
112 }
113 // Point the Configuration class at our gatherer config arguments.
114 Configuration.setCollectionConfiguration(document);
115 if (Gatherer.g_man != null) {
116 Gatherer.g_man.updateUI();
117 }
118 // Finally create all of the child managers that are directly dependant on a collection
119
120 if (Configuration.fedora_info.isActive()) {
121 build_options = new ScriptOptions(getValues(BUILD), "g2f-buildcol.pl");
122 import_options = new ScriptOptions(getValues(IMPORT), "g2f-import.pl");
123 }
124 else {
125 build_options = new ScriptOptions(getBuildValues(), "buildcol.pl");
126 import_options = new ScriptOptions(getImportValues(), "import.pl");
127 schedule_options = new ScriptOptions(getScheduleValues(), "schedule.pl");
128 }
129 }
130
131 /** Destructor.
132 * @see org.greenstone.gatherer.Configuration
133 * @see org.greenstone.gatherer.Gatherer
134 * @see org.greenstone.gatherer.cdm.CollectionDesignManager
135 */
136 public void destroy() {
137 cdm.destroy();
138 Configuration.setCollectionConfiguration(null);
139 // this is called after calling collection.destroy, so we shouldn't need it here too.
140// if (Gatherer.g_man != null) {
141// Gatherer.g_man.updateUI();
142// }
143 cdm = null;
144 document = null;
145 }
146
147 /** Determine the number of documents and folders in this collection. */
148 public int getCount() {
149 return getCount((TreeNode)Gatherer.c_man.getCollectionTreeModel().getRoot(), true, true);
150 }
151
152 /** Calculates the number of documents in this collection. */
153 public int getDocumentCount() {
154 return getCount((TreeNode)Gatherer.c_man.getCollectionTreeModel().getRoot(), false, true);
155 }
156
157 /** Retrieve the description of this collection.
158 * @return a String
159 */
160 public String getDescription() {
161 if(cdm == null) {
162 return StaticStrings.EMPTY_STR;
163 }
164 CollectionMeta collection_extra_collectionmeta = cdm.collectionmeta_manager.getMetadatum(StaticStrings.COLLECTIONMETADATA_COLLECTIONEXTRA_STR);
165 return collection_extra_collectionmeta.getValue(CollectionMeta.TEXT);
166 }
167
168 /** Retrieve the authors email for this collection.
169 * @return The email as a <strong>String</strong>.
170 */
171 public String getEmail() {
172 if(cdm == null) {
173 return StaticStrings.EMPTY_STR;
174 }
175 CollectionMeta creator_collectionmeta = new CollectionMeta(CollectionDesignManager.collect_config.getCreator());
176 return creator_collectionmeta.getValue(CollectionMeta.TEXT);
177 }
178
179 /** Retrieve the short name for this collection.
180 * @return The name as a <strong>String</strong>.
181 */
182 public String getName() {
183 return getCollectionTailName();
184 }
185
186 /** @return just the tail-name of the collection: without any collection group prefix */
187 public String getCollectionTailName() {
188 return file.getParentFile().getName();
189 }
190
191 /** @return just the name of the collection group if any, else returns "" */
192 public String getCollectionGroupName() {
193 String groupAndColName = getGroupQualifiedName(true);
194
195 int slash = groupAndColName.indexOf('/');
196 if(slash == -1) {
197 return "";
198 } else { // remove the colName from the end, to get the groupName
199 return groupAndColName.substring(0, slash);
200 }
201 }
202
203 /**
204 * If the collection is part of a collection group, returns the
205 * colgroupname/colname, else returns the colname.
206 * If url = true, then returns the sub-path as a URL (containing / only),
207 * and if url = false, then the sub-path is returned in filepath form
208 * (\ or /, depending on the OS).
209 * @return collection-group-name/collection-name
210 */
211 public String getGroupQualifiedName(boolean url) {
212 // Subtracting collect dir's path from the current collection's path
213
214 String groupAndCol;
215 String collectDir = Gatherer.getCollectDirectoryPath();
216 String currentCollDir = file.getParentFile().getAbsolutePath();
217
218 if(currentCollDir.startsWith(collectDir)) {
219 groupAndCol = currentCollDir.substring(collectDir.length());
220 } else {
221 // shouldn't happen, but in case it does, just return the collection name.
222 System.err.println("Current collection " + currentCollDir + " is not located inside collectdir " + collectDir);
223 groupAndCol = getCollectionTailName();
224 }
225
226 if(url) {
227 groupAndCol = groupAndCol.replace('\\', '/');
228 }
229
230 return groupAndCol;
231 }
232
233 public File getCollectionDirectory() {
234 return file.getParentFile();
235 }
236
237 /** Determine if this collection has been saved since the last major change.
238 * @return <i>true</i> if it has been saved recently, <i>false</i> otherwise.
239 */
240 public boolean getSaved() {
241 return saved;
242 }
243
244 /** Determine if this collection has had its collection files changed since the last build.
245 * @return <i>true</i> if they have been saved, <i>false</i> otherwise.
246 */
247 public boolean getFilesChanged() {
248 return filesChanged;
249 }
250
251 /** Set the flag that marks weather or not the files has been changed since the last build.
252 @param changed if files has been added/removed, as a <strong>boolean</strong>
253 */
254 public void setFilesChanged(boolean changed) {
255 filesChanged = changed;
256 }
257
258 /** Determine if this collection has had metadata changed since the last build.
259 * @return <i>true</i> if it has been changed, <i>false</i> otherwise.
260 */
261 public boolean getMetadataChanged() {
262 return metadataChanged;
263 }
264
265 /** Set the flag that marks weather or not the metadata has been changed since the last build.
266 @param changed if metadata has been added/changed/removed, as a <strong>boolean</strong>
267 */
268 public void setMetadataChanged(boolean changed) {
269 metadataChanged = changed;
270 }
271
272 /** Retrieve the title of this collection.
273 * @return The title as a <strong>String</strong>.
274 */
275 public String getTitle() {
276 if(cdm == null) {
277 return StaticStrings.EMPTY_STR;
278 }
279 CollectionMeta collection_name_collectionmeta = cdm.collectionmeta_manager.getMetadatum(StaticStrings.COLLECTIONMETADATA_COLLECTIONNAME_STR);
280 return collection_name_collectionmeta.getValue(CollectionMeta.TEXT);
281 }
282
283 /** Save this xml document to the given file. */
284 public void save() {
285 XMLTools.writeXMLFile(file, document);
286 saved = true;
287 }
288
289 public void setBaseCollection(String base_collection) {
290 set(BASE_COLLECTION, base_collection);
291 }
292
293 /** Set the value of imported to the given value.
294 * @param value The new value for imported, <i>true</i> if the collection has been imported successfully, <i>false</i> otherwise.
295 */
296 public void setImported(boolean value) {
297 set(IMPORTED, value);
298 saved = false;
299 }
300
301 /** Set the value of saved to the given value.
302 * @param value The new value for saved, <i>true</i> if the collection has been saved recently, <i>false</i> otherwise.
303 */
304 public void setSaved(boolean value) {
305 saved = value;
306 }
307
308 /** Set the value of title to the given value.
309 * @param title The new <strong>String</strong> title.
310 */
311 public void setTitle(String title) {
312 if(cdm != null) {
313 CollectionMeta collection_name_collectionmeta = cdm.collectionmeta_manager.getMetadatum(StaticStrings.COLLECTIONMETADATA_COLLECTIONNAME_STR);
314 collection_name_collectionmeta.setValue(title);
315 }
316 }
317
318 /** Method called to return a textual representation of a class, which in this case is the collections title.
319 * @return A <strong>String</strong> containing the collections title.
320 */
321 public String toString() {
322 return getTitle();
323 }
324
325 public boolean toSkimFile(){
326 Element document_element = document.getDocumentElement();
327 NodeList arguments = document_element.getElementsByTagName(ARGUMENT);
328 for(int i = 0; i < arguments.getLength(); i++) {
329 Element argument_element = (Element) arguments.item(i);
330 if(argument_element.getAttribute(StaticStrings.NAME_ATTRIBUTE).equalsIgnoreCase(SKIM_FILE)) {
331 String skimfile = XMLTools.getValue(argument_element);
332 if (skimfile !=null && skimfile.trim().toLowerCase().equals("false")){
333 return false;
334 }
335 }
336
337 }
338
339 return true;
340 }
341
342
343 /** Get the value of a collection argument. */
344 private boolean get(String name) {
345 boolean result = false;
346 try {
347 Element document_element = document.getDocumentElement();
348 NodeList arguments = document_element.getElementsByTagName(ARGUMENT);
349 boolean found = false;
350 for(int i = 0; !found && i < arguments.getLength(); i++) {
351 Element argument_element = (Element) arguments.item(i);
352 if(argument_element.getParentNode() == document_element) {
353 if(argument_element.getAttribute(StaticStrings.NAME_ATTRIBUTE).equalsIgnoreCase(name)) {
354 String value = XMLTools.getValue(argument_element);
355 if(value.equalsIgnoreCase(StaticStrings.TRUE_STR)) {
356 result = true;
357 }
358 found = true;
359 value = null;
360 }
361 }
362 argument_element = null;
363 }
364 arguments = null;
365 document_element = null;
366 }
367 catch (Exception error) {
368 DebugStream.printStackTrace(error);
369 }
370 return result;
371 }
372
373 /** Get the value of a collection argument. */
374 private String getString(String name) {
375 String result = "";
376 try {
377 Element document_element = document.getDocumentElement();
378 NodeList arguments = document_element.getElementsByTagName(ARGUMENT);
379 boolean found = false;
380 for(int i = 0; !found && i < arguments.getLength(); i++) {
381 Element argument_element = (Element) arguments.item(i);
382 if(argument_element.getParentNode() == document_element) {
383 if(argument_element.getAttribute(StaticStrings.NAME_ATTRIBUTE).equalsIgnoreCase(name)) {
384 result = XMLTools.getValue(argument_element);
385 found = true;
386 }
387 }
388 argument_element = null;
389 }
390 arguments = null;
391 document_element = null;
392 }
393 catch (Exception error) {
394 DebugStream.printStackTrace(error);
395 }
396 return result;
397 }
398
399
400
401 /** Method to retrieve the current import/build/export/whatever options associated with this Collection. */
402 private Element getValues(String val_type) {
403 Element values_element = null;
404 try {
405 Element document_element = document.getDocumentElement();
406 Element config_element = (Element) XMLTools.getNodeFromNamed(document_element, BUILD_CONFIG);
407 values_element = (Element) XMLTools.getNodeFromNamed(config_element, val_type);
408 config_element = null;
409 document_element = null;
410 }
411 catch (Exception error) {
412 DebugStream.printStackTrace(error);
413 }
414 return values_element;
415 }
416
417
418 /** Method to retrieve the current build options associated with this Collection. */
419 private Element getBuildValues() {
420 Element build_values_element = null;
421 try {
422 Element document_element = document.getDocumentElement();
423 Element build_config_element = (Element) XMLTools.getNodeFromNamed(document_element, BUILD_CONFIG);
424 build_values_element = (Element) XMLTools.getNodeFromNamed(build_config_element, BUILD);
425 build_config_element = null;
426 document_element = null;
427 }
428 catch (Exception error) {
429 DebugStream.printStackTrace(error);
430 }
431 return build_values_element;
432 }
433
434 private Element getScheduleValues() {
435 Element schedule_values_element = null;
436 try {
437 Element document_element = document.getDocumentElement();
438 Element build_config_element = (Element) XMLTools.getNodeFromNamed(document_element, BUILD_CONFIG);
439 schedule_values_element = (Element) XMLTools.getNodeFromNamed(build_config_element, SCHEDULE);
440 build_config_element = null;
441 document_element = null;
442 }
443 catch (Exception error) {
444 DebugStream.printStackTrace(error);
445 }
446 return schedule_values_element;
447 }
448
449 /** Count either documents or folders, depending on the state of the given boolean. */
450 private int getCount(TreeNode node, boolean count_folders, boolean count_files) {
451 int count = 0;
452 File file = ((CollectionTreeNode)node).getFile();
453 if(file.isFile() && count_files) {
454 count++;
455 }
456 else if(file.isDirectory() && count_folders) {
457 count++;
458 }
459 for(int i = 0; !file.getName().equals("CVS") && i < node.getChildCount(); i++) {
460 count = count + getCount(node.getChildAt(i), count_folders, count_files);
461 }
462 return count;
463 }
464
465 /** Method to retrieve the current import options associated with this Collection. */
466 public Element getImportValues() {
467 Element import_values_element = null;
468 try {
469 Element document_element = document.getDocumentElement();
470 Element build_config_element = (Element) XMLTools.getNodeFromNamed(document_element, BUILD_CONFIG);
471 import_values_element = (Element) XMLTools.getNodeFromNamed(build_config_element, IMPORT);
472 build_config_element = null;
473 document_element = null;
474 }
475 catch (Exception error) {
476 DebugStream.printStackTrace(error);
477 }
478 return import_values_element;
479 }
480
481 /** Set the value of a collection argument. */
482 private void set(String name, boolean value) {
483 set(name, (value ? StaticStrings.TRUE_STR : StaticStrings.FALSE_STR));
484 }
485
486 private void set(String name, String value) {
487 try {
488 Element document_element = document.getDocumentElement();
489 NodeList arguments = document_element.getElementsByTagName(ARGUMENT);
490 boolean found = false;
491 for(int i = 0; !found && i < arguments.getLength(); i++) {
492 Element argument_element = (Element) arguments.item(i);
493 if(argument_element.getParentNode() == document_element) {
494 if(argument_element.getAttribute(StaticStrings.NAME_ATTRIBUTE).equalsIgnoreCase(name)) {
495 // Strip any current value nodes.
496 while(argument_element.hasChildNodes()) {
497 argument_element.removeChild(argument_element.getFirstChild());
498 }
499 // Append new value
500 argument_element.appendChild(document.createTextNode(value));
501 found = true;
502 }
503 }
504 argument_element = null;
505 }
506 // Append it
507 if(!found) {
508 Element argument_element = document.createElement(ARGUMENT);
509 argument_element.setAttribute(StaticStrings.NAME_ATTRIBUTE, name);
510 argument_element.appendChild(document.createTextNode(value));
511 document_element.appendChild(argument_element);
512 argument_element = null;
513 }
514 arguments = null;
515 document_element = null;
516 }
517 catch (Exception error) {
518 DebugStream.printStackTrace(error);
519 }
520 }
521}
Note: See TracBrowser for help on using the repository browser.