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

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

The PreviewButton's response had not been updated to load the page of a collection if this was nested in a collectiongroup. Adjusted relevant classes to make the PreviewButton work in the collection group case as well.

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