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

Last change on this file since 30854 was 30854, checked in by ak19, 8 years ago

As per Kathy's suggestion, after editing the collectionConfig file with the ConfigFileEditor, need to close and reopen the collection in order for the changes to take effect.

  • Property svn:keywords set to Author Date Id Revision
File size: 18.7 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 // return full file path of the collection object
238 public String getCollectionPath() {
239 return file.getAbsolutePath();
240 }
241
242 /** Determine if this collection has been saved since the last major change.
243 * @return <i>true</i> if it has been saved recently, <i>false</i> otherwise.
244 */
245 public boolean getSaved() {
246 return saved;
247 }
248
249 /** Determine if this collection has had its collection files changed since the last build.
250 * @return <i>true</i> if they have been saved, <i>false</i> otherwise.
251 */
252 public boolean getFilesChanged() {
253 return filesChanged;
254 }
255
256 /** Set the flag that marks weather or not the files has been changed since the last build.
257 @param changed if files has been added/removed, as a <strong>boolean</strong>
258 */
259 public void setFilesChanged(boolean changed) {
260 filesChanged = changed;
261 }
262
263 /** Determine if this collection has had metadata changed since the last build.
264 * @return <i>true</i> if it has been changed, <i>false</i> otherwise.
265 */
266 public boolean getMetadataChanged() {
267 return metadataChanged;
268 }
269
270 /** Set the flag that marks weather or not the metadata has been changed since the last build.
271 @param changed if metadata has been added/changed/removed, as a <strong>boolean</strong>
272 */
273 public void setMetadataChanged(boolean changed) {
274 metadataChanged = changed;
275 }
276
277 /** Retrieve the title of this collection.
278 * @return The title as a <strong>String</strong>.
279 */
280 public String getTitle() {
281 if(cdm == null) {
282 return StaticStrings.EMPTY_STR;
283 }
284 CollectionMeta collection_name_collectionmeta = cdm.collectionmeta_manager.getMetadatum(StaticStrings.COLLECTIONMETADATA_COLLECTIONNAME_STR);
285 return collection_name_collectionmeta.getValue(CollectionMeta.TEXT);
286 }
287
288 /** Save this xml document to the given file. */
289 public void save() {
290 XMLTools.writeXMLFile(file, document);
291 saved = true;
292 }
293
294 public void setBaseCollection(String base_collection) {
295 set(BASE_COLLECTION, base_collection);
296 }
297
298 /** Set the value of imported to the given value.
299 * @param value The new value for imported, <i>true</i> if the collection has been imported successfully, <i>false</i> otherwise.
300 */
301 public void setImported(boolean value) {
302 set(IMPORTED, value);
303 saved = false;
304 }
305
306 /** Set the value of saved to the given value.
307 * @param value The new value for saved, <i>true</i> if the collection has been saved recently, <i>false</i> otherwise.
308 */
309 public void setSaved(boolean value) {
310 saved = value;
311 }
312
313 /** Set the value of title to the given value.
314 * @param title The new <strong>String</strong> title.
315 */
316 public void setTitle(String title) {
317 if(cdm != null) {
318 CollectionMeta collection_name_collectionmeta = cdm.collectionmeta_manager.getMetadatum(StaticStrings.COLLECTIONMETADATA_COLLECTIONNAME_STR);
319 collection_name_collectionmeta.setValue(title);
320 }
321 }
322
323 /** Method called to return a textual representation of a class, which in this case is the collections title.
324 * @return A <strong>String</strong> containing the collections title.
325 */
326 public String toString() {
327 return getTitle();
328 }
329
330 public boolean toSkimFile(){
331 Element document_element = document.getDocumentElement();
332 NodeList arguments = document_element.getElementsByTagName(ARGUMENT);
333 for(int i = 0; i < arguments.getLength(); i++) {
334 Element argument_element = (Element) arguments.item(i);
335 if(argument_element.getAttribute(StaticStrings.NAME_ATTRIBUTE).equalsIgnoreCase(SKIM_FILE)) {
336 String skimfile = XMLTools.getValue(argument_element);
337 if (skimfile !=null && skimfile.trim().toLowerCase().equals("false")){
338 return false;
339 }
340 }
341
342 }
343
344 return true;
345 }
346
347
348 /** Get the value of a collection argument. */
349 private boolean get(String name) {
350 boolean result = false;
351 try {
352 Element document_element = document.getDocumentElement();
353 NodeList arguments = document_element.getElementsByTagName(ARGUMENT);
354 boolean found = false;
355 for(int i = 0; !found && i < arguments.getLength(); i++) {
356 Element argument_element = (Element) arguments.item(i);
357 if(argument_element.getParentNode() == document_element) {
358 if(argument_element.getAttribute(StaticStrings.NAME_ATTRIBUTE).equalsIgnoreCase(name)) {
359 String value = XMLTools.getValue(argument_element);
360 if(value.equalsIgnoreCase(StaticStrings.TRUE_STR)) {
361 result = true;
362 }
363 found = true;
364 value = null;
365 }
366 }
367 argument_element = null;
368 }
369 arguments = null;
370 document_element = null;
371 }
372 catch (Exception error) {
373 DebugStream.printStackTrace(error);
374 }
375 return result;
376 }
377
378 /** Get the value of a collection argument. */
379 private String getString(String name) {
380 String result = "";
381 try {
382 Element document_element = document.getDocumentElement();
383 NodeList arguments = document_element.getElementsByTagName(ARGUMENT);
384 boolean found = false;
385 for(int i = 0; !found && i < arguments.getLength(); i++) {
386 Element argument_element = (Element) arguments.item(i);
387 if(argument_element.getParentNode() == document_element) {
388 if(argument_element.getAttribute(StaticStrings.NAME_ATTRIBUTE).equalsIgnoreCase(name)) {
389 result = XMLTools.getValue(argument_element);
390 found = true;
391 }
392 }
393 argument_element = null;
394 }
395 arguments = null;
396 document_element = null;
397 }
398 catch (Exception error) {
399 DebugStream.printStackTrace(error);
400 }
401 return result;
402 }
403
404
405
406 /** Method to retrieve the current import/build/export/whatever options associated with this Collection. */
407 private Element getValues(String val_type) {
408 Element values_element = null;
409 try {
410 Element document_element = document.getDocumentElement();
411 Element config_element = (Element) XMLTools.getNodeFromNamed(document_element, BUILD_CONFIG);
412 values_element = (Element) XMLTools.getNodeFromNamed(config_element, val_type);
413 config_element = null;
414 document_element = null;
415 }
416 catch (Exception error) {
417 DebugStream.printStackTrace(error);
418 }
419 return values_element;
420 }
421
422
423 /** Method to retrieve the current build options associated with this Collection. */
424 private Element getBuildValues() {
425 Element build_values_element = null;
426 try {
427 Element document_element = document.getDocumentElement();
428 Element build_config_element = (Element) XMLTools.getNodeFromNamed(document_element, BUILD_CONFIG);
429 build_values_element = (Element) XMLTools.getNodeFromNamed(build_config_element, BUILD);
430 build_config_element = null;
431 document_element = null;
432 }
433 catch (Exception error) {
434 DebugStream.printStackTrace(error);
435 }
436 return build_values_element;
437 }
438
439 private Element getScheduleValues() {
440 Element schedule_values_element = null;
441 try {
442 Element document_element = document.getDocumentElement();
443 Element build_config_element = (Element) XMLTools.getNodeFromNamed(document_element, BUILD_CONFIG);
444 schedule_values_element = (Element) XMLTools.getNodeFromNamed(build_config_element, SCHEDULE);
445 build_config_element = null;
446 document_element = null;
447 }
448 catch (Exception error) {
449 DebugStream.printStackTrace(error);
450 }
451 return schedule_values_element;
452 }
453
454 /** Count either documents or folders, depending on the state of the given boolean. */
455 private int getCount(TreeNode node, boolean count_folders, boolean count_files) {
456 int count = 0;
457 File file = ((CollectionTreeNode)node).getFile();
458 if(file.isFile() && count_files) {
459 count++;
460 }
461 else if(file.isDirectory() && count_folders) {
462 count++;
463 }
464 for(int i = 0; !file.getName().equals("CVS") && i < node.getChildCount(); i++) {
465 count = count + getCount(node.getChildAt(i), count_folders, count_files);
466 }
467 return count;
468 }
469
470 /** Method to retrieve the current import options associated with this Collection. */
471 public Element getImportValues() {
472 Element import_values_element = null;
473 try {
474 Element document_element = document.getDocumentElement();
475 Element build_config_element = (Element) XMLTools.getNodeFromNamed(document_element, BUILD_CONFIG);
476 import_values_element = (Element) XMLTools.getNodeFromNamed(build_config_element, IMPORT);
477 build_config_element = null;
478 document_element = null;
479 }
480 catch (Exception error) {
481 DebugStream.printStackTrace(error);
482 }
483 return import_values_element;
484 }
485
486 /** Set the value of a collection argument. */
487 private void set(String name, boolean value) {
488 set(name, (value ? StaticStrings.TRUE_STR : StaticStrings.FALSE_STR));
489 }
490
491 private void set(String name, String value) {
492 try {
493 Element document_element = document.getDocumentElement();
494 NodeList arguments = document_element.getElementsByTagName(ARGUMENT);
495 boolean found = false;
496 for(int i = 0; !found && i < arguments.getLength(); i++) {
497 Element argument_element = (Element) arguments.item(i);
498 if(argument_element.getParentNode() == document_element) {
499 if(argument_element.getAttribute(StaticStrings.NAME_ATTRIBUTE).equalsIgnoreCase(name)) {
500 // Strip any current value nodes.
501 while(argument_element.hasChildNodes()) {
502 argument_element.removeChild(argument_element.getFirstChild());
503 }
504 // Append new value
505 argument_element.appendChild(document.createTextNode(value));
506 found = true;
507 }
508 }
509 argument_element = null;
510 }
511 // Append it
512 if(!found) {
513 Element argument_element = document.createElement(ARGUMENT);
514 argument_element.setAttribute(StaticStrings.NAME_ATTRIBUTE, name);
515 argument_element.appendChild(document.createTextNode(value));
516 document_element.appendChild(argument_element);
517 argument_element = null;
518 }
519 arguments = null;
520 document_element = null;
521 }
522 catch (Exception error) {
523 DebugStream.printStackTrace(error);
524 }
525 }
526}
Note: See TracBrowser for help on using the repository browser.