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

Last change on this file since 5721 was 5721, checked in by jmt12, 21 years ago

Added a new attribute - base collection. Thus when importing files from a base collection - given we for sure have the same metadatasets - we can assume any non-namespaced metadata is extracted

  • Property svn:keywords set to Author Date Id Revision
File size: 17.9 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.Gatherer;
44import org.greenstone.gatherer.cdm.CollectionDesignManager;
45import org.greenstone.gatherer.collection.BuildOptions;
46import org.greenstone.gatherer.collection.CollectionManager;
47import org.greenstone.gatherer.file.FileNode;
48import org.greenstone.gatherer.msm.ElementWrapper;
49import org.greenstone.gatherer.msm.GDMManager;
50import org.greenstone.gatherer.msm.MetadataSetManager;
51import org.greenstone.gatherer.msm.MSMUtils;
52import org.greenstone.gatherer.util.StaticStrings;
53import org.greenstone.gatherer.util.Utility;
54import org.w3c.dom.*;
55
56/** 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.
57 * @author John Thompson, Greenstone Digital Library, University of Waikato
58 * @version 2.3c
59 */
60public class Collection {
61 /** A reference to the BuildOptions. */
62 public BuildOptions build_options;
63 /** A reference to the Collection Design Manager. */
64 public CollectionDesignManager cdm;
65 /** A reference to the Greenstone Directory Metadata Manager. */
66 public GDMManager gdm;
67 /** A reference to the Metadata Set Manager. */
68 public MetadataSetManager msm;
69 /** <i>true</i> if the currently loaded collection has been saved since the last significant change, <i>false</i> otherwise. */
70 private boolean saved = false;
71 /** The collectio configuration file for this collection. */
72 private CollectionConfiguration collect_cfg;
73 /** The document around which this collection class is based. */
74 private Document document;
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 directory mappings element. */
87 static final private String DIRECTORY_MAPPINGS = "DirectoryMappings";
88 /** The name of the file attribute. */
89 static final private String FILE = "file";
90 /** The name of the import element. */
91 static final private String IMPORT = "Import";
92 /** The name of the imported attribute. */
93 static final private String IMPORTED = "imported";
94 /** The name of the mapping element. */
95 static final private String MAPPING = "Mapping";
96 /** The name of the name attribute. */
97 static final private String NAME = "name";
98 /** The name of the true value. */
99 static final private String TRUE = "true";
100 /** Constructor. */
101 public Collection(File collection_xml) {
102 this.file = collection_xml;
103 // Try to load this collections details.
104 document = Utility.parse(collection_xml, false);
105 // If that fails load the default settings for a collection.
106 if(document == null) {
107 document = Utility.parse(COLLECTION_XML_TEMPLATE, true);
108 }
109 // Point the Configuration class at our gatherer config arguments.
110 Gatherer.config.setCollectionConfiguration(document);
111 // We also attempt to parse the collection configuration file.
112 collect_cfg = new CollectionConfiguration(new File(collection_xml.getParentFile(), Utility.CONFIG_DIR));
113 // Finally create all of the child managers that are directly dependant on a collection
114 build_options = new BuildOptions(getBuildValues(), getImportValues());
115 }
116 /** Add a special directory mapping. */
117 public boolean addDirectoryMapping(String name, File file) {
118 boolean result = false;
119 try {
120 Element document_element = document.getDocumentElement();
121 Element directory_mappings_element = (Element) MSMUtils.getNodeFromNamed(document_element, DIRECTORY_MAPPINGS);
122 // Ensure the name isn't already in use.
123 boolean found = false;
124 NodeList mappings = directory_mappings_element.getElementsByTagName(MAPPING);
125 for(int i = 0; !found && i < mappings.getLength(); i++) {
126 Element mapping_element = (Element) mappings.item(i);
127 if(mapping_element.getAttribute(NAME).equalsIgnoreCase(name)) {
128 found = true;
129 }
130 mapping_element = null;
131 }
132 // Otherwise add the mapping.
133 if(!found) {
134 Element mapping_element = document.createElement(MAPPING);
135 mapping_element.setAttribute(NAME, name);
136 mapping_element.setAttribute(FILE, file.toString());
137 directory_mappings_element.appendChild(mapping_element);
138 result = true;
139 mapping_element = null;
140 }
141 mappings = null;
142 directory_mappings_element = null;
143 document_element = null;
144 saved = false;
145 }
146 catch (Exception error) {
147 Gatherer.printStackTrace(error);
148 }
149 return result;
150 }
151
152 /** Destructor.
153 * @see org.greenstone.gatherer.collection.CollectionModel */
154 public void destroy() {
155 cdm.destroy();
156 gdm.destroy();
157 msm.destroy();
158 Gatherer.config.setCollectionConfiguration(null);
159 cdm = null;
160 document = null;
161 gdm = null;
162 msm = null;
163 }
164
165 /** Determine the path to the base collection.
166 * @return the path as a String
167 */
168 public String getBaseCollection() {
169 return getString(BASE_COLLECTION);
170 }
171
172 /** Determine the number of documents and folders in this collection. */
173 public int getCount() {
174 return getCount((TreeNode)Gatherer.c_man.getRecordSet().getRoot(), true, true);
175 }
176
177 /** Calculates the number of documents in this collection. */
178 public int getDocumentCount() {
179 return getCount((TreeNode)Gatherer.c_man.getRecordSet().getRoot(), false, true);
180 }
181 /** Retrieve the description of this collection.
182 * @return The description as a <strong>String</strong>.
183 */
184 public String getDescription() {
185 return collect_cfg.getDescription();
186 }
187 /** Retrieve a specific directory mapping associated with this collection.
188 * @param name The name of the mapping to retrieve as a <strong>String</strong>.
189 * @return The <strong>File</strong> this name maps to, or <i>null</i> if no such mapping.
190 */
191 public File getDirectoryMapping(String name) {
192 File result = null;
193 try {
194 Element document_element = document.getDocumentElement();
195 Element directory_mappings_element = (Element) MSMUtils.getNodeFromNamed(document_element, DIRECTORY_MAPPINGS);
196 // Ensure the name isn't already in use.
197 boolean found = false;
198 NodeList mappings = directory_mappings_element.getElementsByTagName(MAPPING);
199 for(int i = 0; !found && i < mappings.getLength(); i++) {
200 Element mapping_element = (Element) mappings.item(i);
201 if(mapping_element.getAttribute(NAME).equalsIgnoreCase(name)) {
202 result = new File(MSMUtils.getValue(mapping_element));
203 found = true;
204 }
205 mapping_element = null;
206 }
207 mappings = null;
208 directory_mappings_element = null;
209 document_element = null;
210 }
211 catch(Exception error) {
212 Gatherer.printStackTrace(error);
213 }
214 return result;
215 }
216 /** Retrieve the special directory mappings associated with this collection.
217 * @return A <strong>HashMap</strong> containing mappings from names to directories.
218 */
219 public HashMap getDirectoryMappings() {
220 HashMap special_directories = new HashMap();
221 try {
222 Element document_element = document.getDocumentElement();
223 Element directory_mappings_element = (Element) MSMUtils.getNodeFromNamed(document_element, DIRECTORY_MAPPINGS);
224 // Ensure the name isn't already in use.
225 boolean found = false;
226 NodeList mappings = directory_mappings_element.getElementsByTagName(MAPPING);
227 for(int i = 0; !found && i < mappings.getLength(); i++) {
228 Element mapping_element = (Element) mappings.item(i);
229 String name = mapping_element.getAttribute(NAME);
230 File file = new File(mapping_element.getAttribute(FILE));
231 special_directories.put(name, file);
232 file = null;
233 name = null;
234 mapping_element = null;
235 }
236 mappings = null;
237 directory_mappings_element = null;
238 document_element = null;
239 }
240 catch(Exception error) {
241 Gatherer.printStackTrace(error);
242 }
243 return special_directories;
244 }
245 /** Retrieve the authors email for this collection.
246 * @return The email as a <strong>String</strong>.
247 */
248 public String getEmail() {
249 return collect_cfg.getCreator();
250 }
251 /** Counts the number of folders used in the current record set. */
252 public int getFolderCount() {
253 return getCount((TreeNode)Gatherer.c_man.getRecordSet().getRoot(), true, false);
254 }
255 /** Determine if this collection has had an import action run upon it since the last major change.
256 * @return <i>true</i> if an import has occured, <i>false</i> otherwise.
257 */
258 public boolean getImported() {
259 return get(IMPORTED);
260 }
261 /** Retrieve the short name for this collection.
262 * @return The name as a <strong>String</strong>.
263 */
264 public String getName() {
265 return file.getParentFile().getName();
266 }
267 /** Determine if this collection has been saved since the last major change.
268 * @return <i>true</i> if it has been saved recently, <i>false</i> otherwise.
269 */
270 public boolean getSaved() {
271 return saved;
272 }
273 /** Retrieve the title of this collection.
274 * @return The title as a <strong>String</strong>.
275 */
276 public String getTitle() {
277 return collect_cfg.getName();
278 }
279 /** Remove a previously defined special directory mapping.
280 * @param name The name of the mapping to remove as a <strong>String</strong>.
281 * @return The <strong>File</strong> of the mapping removed.
282 */
283 public File removeDirectoryMapping(String name) {
284 File file = null;
285 try {
286 Element document_element = document.getDocumentElement();
287 Element directory_mappings_element = (Element) MSMUtils.getNodeFromNamed(document_element, DIRECTORY_MAPPINGS);
288 // Ensure the name isn't already in use.
289 boolean found = false;
290 NodeList mappings = directory_mappings_element.getElementsByTagName(MAPPING);
291 for(int i = 0; !found && i < mappings.getLength(); i++) {
292 Element mapping_element = (Element) mappings.item(i);
293 if(mapping_element.getAttribute(NAME).equalsIgnoreCase(name)) {
294 file = new File(MSMUtils.getValue(mapping_element));
295 directory_mappings_element.removeChild(mapping_element);
296 found = true;
297 }
298 mapping_element = null;
299 }
300 mappings = null;
301 directory_mappings_element = null;
302 document_element = null;
303 saved = false;
304 }
305 catch(Exception error) {
306 Gatherer.printStackTrace(error);
307 }
308 return file;
309 }
310 /** Save this xml document to the given file. */
311 public void save() {
312 Utility.export(document, file);
313 }
314
315 public void setBaseCollection(String base_collection) {
316 set(BASE_COLLECTION, base_collection);
317 }
318
319 /** Set the value of imported to the given value.
320 * @param value The new value for imported, <i>true</i> if the collection has been imported successfully, <i>false</i> otherwise.
321 */
322 public void setImported(boolean value) {
323 set(IMPORTED, value);
324 saved = false;
325 }
326 /** Set the value of saved to the given value.
327 * @param value The new value for saved, <i>true</i> if the collection has been saved recently, <i>false</i> otherwise.
328 */
329 public void setSaved(boolean value) {
330 saved = value;
331 }
332 /** Set the value of title to the given value.
333 * @param title The new <strong>String</strong> title.
334 */
335 public void setTitle(String title) {
336 collect_cfg.setName(title);
337 }
338 /** Method called to return a textual representation of a class, which in this case is the collections title.
339 * @return A <strong>String</strong> containing the collections title.
340 */
341 public String toString() {
342 return collect_cfg.getName();
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(NAME).equalsIgnoreCase(name)) {
355 String value = MSMUtils.getValue(argument_element);
356 if(value.equalsIgnoreCase(TRUE)) {
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 Gatherer.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(NAME).equalsIgnoreCase(name)) {
385 result = MSMUtils.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 Gatherer.printStackTrace(error);
396 }
397 return result;
398 }
399
400 /** Method to retrieve the current build options associated with this Collection. */
401 private Element getBuildValues() {
402 Element build_values_element = null;
403 try {
404 Element document_element = document.getDocumentElement();
405 Element build_config_element = (Element) MSMUtils.getNodeFromNamed(document_element, BUILD_CONFIG);
406 build_values_element = (Element) MSMUtils.getNodeFromNamed(build_config_element, BUILD);
407 build_config_element = null;
408 document_element = null;
409 }
410 catch (Exception error) {
411 Gatherer.printStackTrace(error);
412 }
413 return build_values_element;
414 }
415
416 /** Count either documents or folders, depending on the state of the given boolean. */
417 private int getCount(TreeNode node, boolean count_folders, boolean count_files) {
418 int count = 0;
419 File file = ((FileNode)node).getFile();
420 if(file.isFile() && count_files) {
421 count++;
422 }
423 else if(file.isDirectory() && count_folders) {
424 count++;
425 }
426 for(int i = 0; i < node.getChildCount(); i++) {
427 count = count + getCount(node.getChildAt(i), count_folders, count_files);
428 }
429 return count;
430 }
431
432 /** Method to retrieve the current import options associated with this Collection. */
433 public Element getImportValues() {
434 Element import_values_element = null;
435 try {
436 Element document_element = document.getDocumentElement();
437 Element build_config_element = (Element) MSMUtils.getNodeFromNamed(document_element, BUILD_CONFIG);
438 import_values_element = (Element) MSMUtils.getNodeFromNamed(build_config_element, IMPORT);
439 build_config_element = null;
440 document_element = null;
441 }
442 catch (Exception error) {
443 Gatherer.printStackTrace(error);
444 }
445 return import_values_element;
446 }
447
448 /** Set the value of a collection argument. */
449 private void set(String name, boolean value) {
450 set(name, (value ? StaticStrings.TRUE_STR : StaticStrings.FALSE_STR));
451 }
452
453 private void set(String name, String value) {
454 try {
455 Element document_element = document.getDocumentElement();
456 NodeList arguments = document_element.getElementsByTagName(ARGUMENT);
457 boolean found = false;
458 for(int i = 0; !found && i < arguments.getLength(); i++) {
459 Element argument_element = (Element) arguments.item(i);
460 if(argument_element.getParentNode() == document_element) {
461 if(argument_element.getAttribute(NAME).equalsIgnoreCase(name)) {
462 // Strip any current value nodes.
463 while(argument_element.hasChildNodes()) {
464 argument_element.removeChild(argument_element.getFirstChild());
465 }
466 // Append new value
467 argument_element.appendChild(document.createTextNode(value));
468 found = true;
469 }
470 }
471 argument_element = null;
472 }
473 // Append it
474 if(!found) {
475 Element argument_element = document.createElement(ARGUMENT);
476 argument_element.setAttribute(NAME, name);
477 argument_element.appendChild(document.createTextNode(value));
478 document_element.appendChild(argument_element);
479 argument_element = null;
480 }
481 arguments = null;
482 document_element = null;
483 }
484 catch (Exception error) {
485 Gatherer.printStackTrace(error);
486 }
487 }
488}
Note: See TracBrowser for help on using the repository browser.