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

Last change on this file since 14974 was 14974, checked in by davidb, 16 years ago

Changes to GLI to support export into Fedora. New utility called flisvn diff gems/MetadataSetManager.java

  • Property svn:keywords set to Author Date Id Revision
File size: 16.1 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 /** true if an error has occurred during construction */
65 public boolean error = false;
66 /** <i>true</i> if the currently loaded collection has been saved since the last significant change, <i>false</i> otherwise. */
67 private boolean saved = false;
68 /*<i>true</i> if the currently loaded collection has had files added since its last build */
69 private boolean filesChanged = false;
70 /*<i>true</i> if the currently loaded collection has had metadata added since its last build */
71 private boolean metadataChanged = false;
72 /** The document around which this collection class is based. */
73 private Document document;
74
75 /** The file the collection is in (the file may not actually exist, such in the case of a legacy collection)! */
76 private File file;
77 /** The name of the argument element. */
78 static final private String ARGUMENT = "Argument";
79 static final private String BASE_COLLECTION = "base_collection";
80 /** The name of the build element. */
81 static final private String BUILD = "Build";
82 /** The name of the build config element. */
83 static final private String BUILD_CONFIG = "BuildConfig";
84 /** The name of the collection xml template. */
85 static final private String COLLECTION_XML_TEMPLATE = "xml/template.col";
86 /** The name of the import element. */
87 static final private String IMPORT = "Import";
88 /** The name of the import element. */
89 static final private String EXPORT = "Export";
90 /** The name of the imported attribute. */
91 static final private String IMPORTED = "imported";
92 /** The name of the imported attribute. */
93 static final private String SKIM_FILE = "skimfile";
94
95
96 /** Constructor. */
97 public Collection(File collection_xml) {
98 this.file = collection_xml;
99 // Try to load this collections details.
100 document = XMLTools.parseXMLFile(collection_xml);
101 // If that fails load the default settings for a collection.
102 if(document == null) {
103 document = XMLTools.parseXMLFile(COLLECTION_XML_TEMPLATE, true);
104 }
105 if (document == null) {
106 error = true;
107 return;
108 }
109 // Point the Configuration class at our gatherer config arguments.
110 Configuration.setCollectionConfiguration(document);
111 if (Gatherer.g_man != null) {
112 Gatherer.g_man.updateUI();
113 }
114 // Finally create all of the child managers that are directly dependant on a collection
115
116 if (Configuration.fedora_info.isActive()) {
117 build_options = new ScriptOptions(getValues(BUILD), "g2f-buildcol.pl");
118 import_options = new ScriptOptions(getValues(IMPORT), "g2f-import.pl");
119 }
120 else {
121 build_options = new ScriptOptions(getBuildValues(), "buildcol.pl");
122 import_options = new ScriptOptions(getImportValues(), "import.pl");
123 }
124 }
125
126 /** Destructor.
127 * @see org.greenstone.gatherer.Configuration
128 * @see org.greenstone.gatherer.Gatherer
129 * @see org.greenstone.gatherer.cdm.CollectionDesignManager
130 */
131 public void destroy() {
132 cdm.destroy();
133 Configuration.setCollectionConfiguration(null);
134 // this is called after calling collection.destroy, so we shouldn't need it here too.
135// if (Gatherer.g_man != null) {
136// Gatherer.g_man.updateUI();
137// }
138 cdm = null;
139 document = null;
140 }
141
142 /** Determine the number of documents and folders in this collection. */
143 public int getCount() {
144 return getCount((TreeNode)Gatherer.c_man.getCollectionTreeModel().getRoot(), true, true);
145 }
146
147 /** Calculates the number of documents in this collection. */
148 public int getDocumentCount() {
149 return getCount((TreeNode)Gatherer.c_man.getCollectionTreeModel().getRoot(), false, true);
150 }
151
152 /** Retrieve the description of this collection.
153 * @return a String
154 */
155 public String getDescription() {
156 if(cdm == null) {
157 return StaticStrings.EMPTY_STR;
158 }
159 CollectionMeta collection_extra_collectionmeta = cdm.collectionmeta_manager.getMetadatum(StaticStrings.COLLECTIONMETADATA_COLLECTIONEXTRA_STR);
160 return collection_extra_collectionmeta.getValue(CollectionMeta.TEXT);
161 }
162
163 /** Retrieve the authors email for this collection.
164 * @return The email as a <strong>String</strong>.
165 */
166 public String getEmail() {
167 if(cdm == null) {
168 return StaticStrings.EMPTY_STR;
169 }
170 CollectionMeta creator_collectionmeta = new CollectionMeta(CollectionDesignManager.collect_config.getCreator());
171 return creator_collectionmeta.getValue(CollectionMeta.TEXT);
172 }
173
174 /** Retrieve the short name for this collection.
175 * @return The name as a <strong>String</strong>.
176 */
177 public String getName() {
178 return file.getParentFile().getName();
179 }
180
181 /** Determine if this collection has been saved since the last major change.
182 * @return <i>true</i> if it has been saved recently, <i>false</i> otherwise.
183 */
184 public boolean getSaved() {
185 return saved;
186 }
187
188 /** Determine if this collection has had its collection files changed since the last build.
189 * @return <i>true</i> if they have been saved, <i>false</i> otherwise.
190 */
191 public boolean getFilesChanged() {
192 return filesChanged;
193 }
194
195 /** Set the flag that marks weather or not the files has been changed since the last build.
196 @param changed if files has been added/removed, as a <strong>boolean</strong>
197 */
198 public void setFilesChanged(boolean changed) {
199 filesChanged = changed;
200 }
201
202 /** Determine if this collection has had metadata changed since the last build.
203 * @return <i>true</i> if it has been changed, <i>false</i> otherwise.
204 */
205 public boolean getMetadataChanged() {
206 return metadataChanged;
207 }
208
209 /** Set the flag that marks weather or not the metadata has been changed since the last build.
210 @param changed if metadata has been added/changed/removed, as a <strong>boolean</strong>
211 */
212 public void setMetadataChanged(boolean changed) {
213 metadataChanged = changed;
214 }
215
216 /** Retrieve the title of this collection.
217 * @return The title as a <strong>String</strong>.
218 */
219 public String getTitle() {
220 if(cdm == null) {
221 return StaticStrings.EMPTY_STR;
222 }
223 CollectionMeta collection_name_collectionmeta = cdm.collectionmeta_manager.getMetadatum(StaticStrings.COLLECTIONMETADATA_COLLECTIONNAME_STR);
224 return collection_name_collectionmeta.getValue(CollectionMeta.TEXT);
225 }
226
227 /** Save this xml document to the given file. */
228 public void save() {
229 XMLTools.writeXMLFile(file, document);
230 saved = true;
231 }
232
233 public void setBaseCollection(String base_collection) {
234 set(BASE_COLLECTION, base_collection);
235 }
236
237 /** Set the value of imported to the given value.
238 * @param value The new value for imported, <i>true</i> if the collection has been imported successfully, <i>false</i> otherwise.
239 */
240 public void setImported(boolean value) {
241 set(IMPORTED, value);
242 saved = false;
243 }
244
245 /** Set the value of saved to the given value.
246 * @param value The new value for saved, <i>true</i> if the collection has been saved recently, <i>false</i> otherwise.
247 */
248 public void setSaved(boolean value) {
249 saved = value;
250 }
251
252 /** Set the value of title to the given value.
253 * @param title The new <strong>String</strong> title.
254 */
255 public void setTitle(String title) {
256 if(cdm != null) {
257 CollectionMeta collection_name_collectionmeta = cdm.collectionmeta_manager.getMetadatum(StaticStrings.COLLECTIONMETADATA_COLLECTIONNAME_STR);
258 collection_name_collectionmeta.setValue(title);
259 }
260 }
261
262 /** Method called to return a textual representation of a class, which in this case is the collections title.
263 * @return A <strong>String</strong> containing the collections title.
264 */
265 public String toString() {
266 return getTitle();
267 }
268
269 public boolean toSkimFile(){
270 Element document_element = document.getDocumentElement();
271 NodeList arguments = document_element.getElementsByTagName(ARGUMENT);
272 for(int i = 0; i < arguments.getLength(); i++) {
273 Element argument_element = (Element) arguments.item(i);
274 if(argument_element.getAttribute(StaticStrings.NAME_ATTRIBUTE).equalsIgnoreCase(SKIM_FILE)) {
275 String skimfile = XMLTools.getValue(argument_element);
276 if (skimfile !=null && skimfile.trim().toLowerCase().equals("false")){
277 return false;
278 }
279 }
280
281 }
282
283 return true;
284 }
285
286
287 /** Get the value of a collection argument. */
288 private boolean get(String name) {
289 boolean result = false;
290 try {
291 Element document_element = document.getDocumentElement();
292 NodeList arguments = document_element.getElementsByTagName(ARGUMENT);
293 boolean found = false;
294 for(int i = 0; !found && i < arguments.getLength(); i++) {
295 Element argument_element = (Element) arguments.item(i);
296 if(argument_element.getParentNode() == document_element) {
297 if(argument_element.getAttribute(StaticStrings.NAME_ATTRIBUTE).equalsIgnoreCase(name)) {
298 String value = XMLTools.getValue(argument_element);
299 if(value.equalsIgnoreCase(StaticStrings.TRUE_STR)) {
300 result = true;
301 }
302 found = true;
303 value = null;
304 }
305 }
306 argument_element = null;
307 }
308 arguments = null;
309 document_element = null;
310 }
311 catch (Exception error) {
312 DebugStream.printStackTrace(error);
313 }
314 return result;
315 }
316
317 /** Get the value of a collection argument. */
318 private String getString(String name) {
319 String result = "";
320 try {
321 Element document_element = document.getDocumentElement();
322 NodeList arguments = document_element.getElementsByTagName(ARGUMENT);
323 boolean found = false;
324 for(int i = 0; !found && i < arguments.getLength(); i++) {
325 Element argument_element = (Element) arguments.item(i);
326 if(argument_element.getParentNode() == document_element) {
327 if(argument_element.getAttribute(StaticStrings.NAME_ATTRIBUTE).equalsIgnoreCase(name)) {
328 result = XMLTools.getValue(argument_element);
329 found = true;
330 }
331 }
332 argument_element = null;
333 }
334 arguments = null;
335 document_element = null;
336 }
337 catch (Exception error) {
338 DebugStream.printStackTrace(error);
339 }
340 return result;
341 }
342
343
344
345 /** Method to retrieve the current import/build/export/whatever options associated with this Collection. */
346 private Element getValues(String val_type) {
347 Element values_element = null;
348 try {
349 Element document_element = document.getDocumentElement();
350 Element config_element = (Element) XMLTools.getNodeFromNamed(document_element, BUILD_CONFIG);
351 values_element = (Element) XMLTools.getNodeFromNamed(config_element, val_type);
352 config_element = null;
353 document_element = null;
354 }
355 catch (Exception error) {
356 DebugStream.printStackTrace(error);
357 }
358 return values_element;
359 }
360
361
362 /** Method to retrieve the current build options associated with this Collection. */
363 private Element getBuildValues() {
364 Element build_values_element = null;
365 try {
366 Element document_element = document.getDocumentElement();
367 Element build_config_element = (Element) XMLTools.getNodeFromNamed(document_element, BUILD_CONFIG);
368 build_values_element = (Element) XMLTools.getNodeFromNamed(build_config_element, BUILD);
369 build_config_element = null;
370 document_element = null;
371 }
372 catch (Exception error) {
373 DebugStream.printStackTrace(error);
374 }
375 return build_values_element;
376 }
377
378
379 /** Count either documents or folders, depending on the state of the given boolean. */
380 private int getCount(TreeNode node, boolean count_folders, boolean count_files) {
381 int count = 0;
382 File file = ((CollectionTreeNode)node).getFile();
383 if(file.isFile() && count_files) {
384 count++;
385 }
386 else if(file.isDirectory() && count_folders) {
387 count++;
388 }
389 for(int i = 0; !file.getName().equals("CVS") && i < node.getChildCount(); i++) {
390 count = count + getCount(node.getChildAt(i), count_folders, count_files);
391 }
392 return count;
393 }
394
395 /** Method to retrieve the current import options associated with this Collection. */
396 public Element getImportValues() {
397 Element import_values_element = null;
398 try {
399 Element document_element = document.getDocumentElement();
400 Element build_config_element = (Element) XMLTools.getNodeFromNamed(document_element, BUILD_CONFIG);
401 import_values_element = (Element) XMLTools.getNodeFromNamed(build_config_element, IMPORT);
402 build_config_element = null;
403 document_element = null;
404 }
405 catch (Exception error) {
406 DebugStream.printStackTrace(error);
407 }
408 return import_values_element;
409 }
410
411 /** Set the value of a collection argument. */
412 private void set(String name, boolean value) {
413 set(name, (value ? StaticStrings.TRUE_STR : StaticStrings.FALSE_STR));
414 }
415
416 private void set(String name, String value) {
417 try {
418 Element document_element = document.getDocumentElement();
419 NodeList arguments = document_element.getElementsByTagName(ARGUMENT);
420 boolean found = false;
421 for(int i = 0; !found && i < arguments.getLength(); i++) {
422 Element argument_element = (Element) arguments.item(i);
423 if(argument_element.getParentNode() == document_element) {
424 if(argument_element.getAttribute(StaticStrings.NAME_ATTRIBUTE).equalsIgnoreCase(name)) {
425 // Strip any current value nodes.
426 while(argument_element.hasChildNodes()) {
427 argument_element.removeChild(argument_element.getFirstChild());
428 }
429 // Append new value
430 argument_element.appendChild(document.createTextNode(value));
431 found = true;
432 }
433 }
434 argument_element = null;
435 }
436 // Append it
437 if(!found) {
438 Element argument_element = document.createElement(ARGUMENT);
439 argument_element.setAttribute(StaticStrings.NAME_ATTRIBUTE, name);
440 argument_element.appendChild(document.createTextNode(value));
441 document_element.appendChild(argument_element);
442 argument_element = null;
443 }
444 arguments = null;
445 document_element = null;
446 }
447 catch (Exception error) {
448 DebugStream.printStackTrace(error);
449 }
450 }
451}
Note: See TracBrowser for help on using the repository browser.