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

Last change on this file since 19203 was 19203, checked in by ak19, 15 years ago

Undoing the changes where the Collection.getName() always returned the groupQualifiedName(). Now it returns the collection's subname again.

  • 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. Always qualified by the collectionGroup, if any.
180 * @return The name as a <strong>String</strong>.
181 */
182 public String getName() {
183 return getCollectionSubName();
184 //return getGroupQualifiedName(true);
185 }
186
187 /** @return just the sub-name of the collection: without any collection group prefix */
188 public String getCollectionSubName() {
189 return file.getParentFile().getName();
190 }
191
192 /** @return just the name of the collection group if any, else returns "" */
193 public String getCollectionGroupName() {
194 String groupAndColName = getGroupQualifiedName(true);
195
196 int slash = groupAndColName.indexOf('/');
197 if(slash == -1) {
198 return "";
199 } else { // remove the colName from the end, to get the groupName
200 return groupAndColName.substring(0, slash);
201 }
202 }
203
204 /**
205 * If the collection is part of a collection group, returns the
206 * colgroupname/colname, else returns the colname.
207 * If url = true, then returns the sub-path as a URL (containing / only),
208 * and if url = false, then the sub-path is returned in filepath form
209 * (\ or /, depending on the OS).
210 * @return collection-group-name/collection-name
211 */
212 public String getGroupQualifiedName(boolean url) {
213 // Subtracting collect dir's path from the current collection's path
214
215 String groupAndCol;
216 String collectDir = Gatherer.getCollectDirectoryPath();
217 String currentCollDir = file.getParentFile().getAbsolutePath();
218
219 if(currentCollDir.startsWith(collectDir)) {
220 groupAndCol = currentCollDir.substring(collectDir.length());
221 } else {
222 // shouldn't happen, but in case it does, just return the collection name.
223 System.err.println("Current collection " + currentCollDir + " is not located inside collectdir " + "collectDir");
224 groupAndCol = getCollectionSubName();
225 }
226
227 if(url) {
228 groupAndCol = groupAndCol.replace('\\', '/');
229 }
230
231 return groupAndCol;
232 }
233
234 public File getCollectionDirectory() {
235 return file.getParentFile();
236 }
237
238 /** Determine if this collection has been saved since the last major change.
239 * @return <i>true</i> if it has been saved recently, <i>false</i> otherwise.
240 */
241 public boolean getSaved() {
242 return saved;
243 }
244
245 /** Determine if this collection has had its collection files changed since the last build.
246 * @return <i>true</i> if they have been saved, <i>false</i> otherwise.
247 */
248 public boolean getFilesChanged() {
249 return filesChanged;
250 }
251
252 /** Set the flag that marks weather or not the files has been changed since the last build.
253 @param changed if files has been added/removed, as a <strong>boolean</strong>
254 */
255 public void setFilesChanged(boolean changed) {
256 filesChanged = changed;
257 }
258
259 /** Determine if this collection has had metadata changed since the last build.
260 * @return <i>true</i> if it has been changed, <i>false</i> otherwise.
261 */
262 public boolean getMetadataChanged() {
263 return metadataChanged;
264 }
265
266 /** Set the flag that marks weather or not the metadata has been changed since the last build.
267 @param changed if metadata has been added/changed/removed, as a <strong>boolean</strong>
268 */
269 public void setMetadataChanged(boolean changed) {
270 metadataChanged = changed;
271 }
272
273 /** Retrieve the title of this collection.
274 * @return The title as a <strong>String</strong>.
275 */
276 public String getTitle() {
277 if(cdm == null) {
278 return StaticStrings.EMPTY_STR;
279 }
280 CollectionMeta collection_name_collectionmeta = cdm.collectionmeta_manager.getMetadatum(StaticStrings.COLLECTIONMETADATA_COLLECTIONNAME_STR);
281 return collection_name_collectionmeta.getValue(CollectionMeta.TEXT);
282 }
283
284 /** Save this xml document to the given file. */
285 public void save() {
286 XMLTools.writeXMLFile(file, document);
287 saved = true;
288 }
289
290 public void setBaseCollection(String base_collection) {
291 set(BASE_COLLECTION, base_collection);
292 }
293
294 /** Set the value of imported to the given value.
295 * @param value The new value for imported, <i>true</i> if the collection has been imported successfully, <i>false</i> otherwise.
296 */
297 public void setImported(boolean value) {
298 set(IMPORTED, value);
299 saved = false;
300 }
301
302 /** Set the value of saved to the given value.
303 * @param value The new value for saved, <i>true</i> if the collection has been saved recently, <i>false</i> otherwise.
304 */
305 public void setSaved(boolean value) {
306 saved = value;
307 }
308
309 /** Set the value of title to the given value.
310 * @param title The new <strong>String</strong> title.
311 */
312 public void setTitle(String title) {
313 if(cdm != null) {
314 CollectionMeta collection_name_collectionmeta = cdm.collectionmeta_manager.getMetadatum(StaticStrings.COLLECTIONMETADATA_COLLECTIONNAME_STR);
315 collection_name_collectionmeta.setValue(title);
316 }
317 }
318
319 /** Method called to return a textual representation of a class, which in this case is the collections title.
320 * @return A <strong>String</strong> containing the collections title.
321 */
322 public String toString() {
323 return getTitle();
324 }
325
326 public boolean toSkimFile(){
327 Element document_element = document.getDocumentElement();
328 NodeList arguments = document_element.getElementsByTagName(ARGUMENT);
329 for(int i = 0; i < arguments.getLength(); i++) {
330 Element argument_element = (Element) arguments.item(i);
331 if(argument_element.getAttribute(StaticStrings.NAME_ATTRIBUTE).equalsIgnoreCase(SKIM_FILE)) {
332 String skimfile = XMLTools.getValue(argument_element);
333 if (skimfile !=null && skimfile.trim().toLowerCase().equals("false")){
334 return false;
335 }
336 }
337
338 }
339
340 return true;
341 }
342
343
344 /** Get the value of a collection argument. */
345 private boolean get(String name) {
346 boolean result = false;
347 try {
348 Element document_element = document.getDocumentElement();
349 NodeList arguments = document_element.getElementsByTagName(ARGUMENT);
350 boolean found = false;
351 for(int i = 0; !found && i < arguments.getLength(); i++) {
352 Element argument_element = (Element) arguments.item(i);
353 if(argument_element.getParentNode() == document_element) {
354 if(argument_element.getAttribute(StaticStrings.NAME_ATTRIBUTE).equalsIgnoreCase(name)) {
355 String value = XMLTools.getValue(argument_element);
356 if(value.equalsIgnoreCase(StaticStrings.TRUE_STR)) {
357 result = true;
358 }
359 found = true;
360 value = null;
361 }
362 }
363 argument_element = null;
364 }
365 arguments = null;
366 document_element = null;
367 }
368 catch (Exception error) {
369 DebugStream.printStackTrace(error);
370 }
371 return result;
372 }
373
374 /** Get the value of a collection argument. */
375 private String getString(String name) {
376 String result = "";
377 try {
378 Element document_element = document.getDocumentElement();
379 NodeList arguments = document_element.getElementsByTagName(ARGUMENT);
380 boolean found = false;
381 for(int i = 0; !found && i < arguments.getLength(); i++) {
382 Element argument_element = (Element) arguments.item(i);
383 if(argument_element.getParentNode() == document_element) {
384 if(argument_element.getAttribute(StaticStrings.NAME_ATTRIBUTE).equalsIgnoreCase(name)) {
385 result = XMLTools.getValue(argument_element);
386 found = true;
387 }
388 }
389 argument_element = null;
390 }
391 arguments = null;
392 document_element = null;
393 }
394 catch (Exception error) {
395 DebugStream.printStackTrace(error);
396 }
397 return result;
398 }
399
400
401
402 /** Method to retrieve the current import/build/export/whatever options associated with this Collection. */
403 private Element getValues(String val_type) {
404 Element values_element = null;
405 try {
406 Element document_element = document.getDocumentElement();
407 Element config_element = (Element) XMLTools.getNodeFromNamed(document_element, BUILD_CONFIG);
408 values_element = (Element) XMLTools.getNodeFromNamed(config_element, val_type);
409 config_element = null;
410 document_element = null;
411 }
412 catch (Exception error) {
413 DebugStream.printStackTrace(error);
414 }
415 return values_element;
416 }
417
418
419 /** Method to retrieve the current build options associated with this Collection. */
420 private Element getBuildValues() {
421 Element build_values_element = null;
422 try {
423 Element document_element = document.getDocumentElement();
424 Element build_config_element = (Element) XMLTools.getNodeFromNamed(document_element, BUILD_CONFIG);
425 build_values_element = (Element) XMLTools.getNodeFromNamed(build_config_element, BUILD);
426 build_config_element = null;
427 document_element = null;
428 }
429 catch (Exception error) {
430 DebugStream.printStackTrace(error);
431 }
432 return build_values_element;
433 }
434
435 private Element getScheduleValues() {
436 Element schedule_values_element = null;
437 try {
438 Element document_element = document.getDocumentElement();
439 Element build_config_element = (Element) XMLTools.getNodeFromNamed(document_element, BUILD_CONFIG);
440 schedule_values_element = (Element) XMLTools.getNodeFromNamed(build_config_element, SCHEDULE);
441 build_config_element = null;
442 document_element = null;
443 }
444 catch (Exception error) {
445 DebugStream.printStackTrace(error);
446 }
447 return schedule_values_element;
448 }
449
450 /** Count either documents or folders, depending on the state of the given boolean. */
451 private int getCount(TreeNode node, boolean count_folders, boolean count_files) {
452 int count = 0;
453 File file = ((CollectionTreeNode)node).getFile();
454 if(file.isFile() && count_files) {
455 count++;
456 }
457 else if(file.isDirectory() && count_folders) {
458 count++;
459 }
460 for(int i = 0; !file.getName().equals("CVS") && i < node.getChildCount(); i++) {
461 count = count + getCount(node.getChildAt(i), count_folders, count_files);
462 }
463 return count;
464 }
465
466 /** Method to retrieve the current import options associated with this Collection. */
467 public Element getImportValues() {
468 Element import_values_element = null;
469 try {
470 Element document_element = document.getDocumentElement();
471 Element build_config_element = (Element) XMLTools.getNodeFromNamed(document_element, BUILD_CONFIG);
472 import_values_element = (Element) XMLTools.getNodeFromNamed(build_config_element, IMPORT);
473 build_config_element = null;
474 document_element = null;
475 }
476 catch (Exception error) {
477 DebugStream.printStackTrace(error);
478 }
479 return import_values_element;
480 }
481
482 /** Set the value of a collection argument. */
483 private void set(String name, boolean value) {
484 set(name, (value ? StaticStrings.TRUE_STR : StaticStrings.FALSE_STR));
485 }
486
487 private void set(String name, String value) {
488 try {
489 Element document_element = document.getDocumentElement();
490 NodeList arguments = document_element.getElementsByTagName(ARGUMENT);
491 boolean found = false;
492 for(int i = 0; !found && i < arguments.getLength(); i++) {
493 Element argument_element = (Element) arguments.item(i);
494 if(argument_element.getParentNode() == document_element) {
495 if(argument_element.getAttribute(StaticStrings.NAME_ATTRIBUTE).equalsIgnoreCase(name)) {
496 // Strip any current value nodes.
497 while(argument_element.hasChildNodes()) {
498 argument_element.removeChild(argument_element.getFirstChild());
499 }
500 // Append new value
501 argument_element.appendChild(document.createTextNode(value));
502 found = true;
503 }
504 }
505 argument_element = null;
506 }
507 // Append it
508 if(!found) {
509 Element argument_element = document.createElement(ARGUMENT);
510 argument_element.setAttribute(StaticStrings.NAME_ATTRIBUTE, name);
511 argument_element.appendChild(document.createTextNode(value));
512 document_element.appendChild(argument_element);
513 argument_element = null;
514 }
515 arguments = null;
516 document_element = null;
517 }
518 catch (Exception error) {
519 DebugStream.printStackTrace(error);
520 }
521 }
522}
Note: See TracBrowser for help on using the repository browser.