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

Last change on this file since 5912 was 5815, checked in by kjdon, 21 years ago

added support for build types and search types in the Collection and CollectionConfiguration classes so that the preview pane can set the right url based on the collection'sbuild type

  • Property svn:keywords set to Author Date Id Revision
File size: 18.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.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
188 public String getCollectionType() {
189 return collect_cfg.getBuildType();
190 }
191
192 public String getSearchTypes() {
193 return collect_cfg.getSearchTypes();
194 }
195 /** Retrieve a specific directory mapping associated with this collection.
196 * @param name The name of the mapping to retrieve as a <strong>String</strong>.
197 * @return The <strong>File</strong> this name maps to, or <i>null</i> if no such mapping.
198 */
199 /* private File getDirectoryMapping(String name) {
200 File result = null;
201 try {
202 Element document_element = document.getDocumentElement();
203 Element directory_mappings_element = (Element) MSMUtils.getNodeFromNamed(document_element, DIRECTORY_MAPPINGS);
204 // Ensure the name isn't already in use.
205 boolean found = false;
206 NodeList mappings = directory_mappings_element.getElementsByTagName(MAPPING);
207 for(int i = 0; !found && i < mappings.getLength(); i++) {
208 Element mapping_element = (Element) mappings.item(i);
209 if(mapping_element.getAttribute(NAME).equalsIgnoreCase(name)) {
210 result = new File(MSMUtils.getValue(mapping_element));
211 found = true;
212 }
213 mapping_element = null;
214 }
215 mappings = null;
216 directory_mappings_element = null;
217 document_element = null;
218 }
219 catch(Exception error) {
220 Gatherer.printStackTrace(error);
221 }
222 return result;
223 } */
224 /** Retrieve the special directory mappings associated with this collection.
225 * @return A <strong>HashMap</strong> containing mappings from names to directories.
226 */
227 public HashMap getDirectoryMappings() {
228 HashMap special_directories = new HashMap();
229 try {
230 Element document_element = document.getDocumentElement();
231 Element directory_mappings_element = (Element) MSMUtils.getNodeFromNamed(document_element, DIRECTORY_MAPPINGS);
232 // Ensure the name isn't already in use.
233 boolean found = false;
234 NodeList mappings = directory_mappings_element.getElementsByTagName(MAPPING);
235 for(int i = 0; !found && i < mappings.getLength(); i++) {
236 Element mapping_element = (Element) mappings.item(i);
237 String name = mapping_element.getAttribute(NAME);
238 File file = new File(mapping_element.getAttribute(FILE));
239 special_directories.put(name, file);
240 file = null;
241 name = null;
242 mapping_element = null;
243 }
244 mappings = null;
245 directory_mappings_element = null;
246 document_element = null;
247 }
248 catch(Exception error) {
249 Gatherer.printStackTrace(error);
250 }
251 return special_directories;
252 }
253 /** Retrieve the authors email for this collection.
254 * @return The email as a <strong>String</strong>.
255 */
256 public String getEmail() {
257 return collect_cfg.getCreator();
258 }
259 /** Counts the number of folders used in the current record set. */
260 /* private int getFolderCount() {
261 return getCount((TreeNode)Gatherer.c_man.getRecordSet().getRoot(), true, false);
262 } */
263 /** Determine if this collection has had an import action run upon it since the last major change.
264 * @return <i>true</i> if an import has occured, <i>false</i> otherwise.
265 */
266 /* private boolean getImported() {
267 return get(IMPORTED);
268 } */
269 /** Retrieve the short name for this collection.
270 * @return The name as a <strong>String</strong>.
271 */
272 public String getName() {
273 return file.getParentFile().getName();
274 }
275 /** Determine if this collection has been saved since the last major change.
276 * @return <i>true</i> if it has been saved recently, <i>false</i> otherwise.
277 */
278 public boolean getSaved() {
279 return saved;
280 }
281 /** Retrieve the title of this collection.
282 * @return The title as a <strong>String</strong>.
283 */
284 public String getTitle() {
285 return collect_cfg.getName();
286 }
287 /** Remove a previously defined special directory mapping.
288 * @param name The name of the mapping to remove as a <strong>String</strong>.
289 * @return The <strong>File</strong> of the mapping removed.
290 */
291 public File removeDirectoryMapping(String name) {
292 File file = null;
293 try {
294 Element document_element = document.getDocumentElement();
295 Element directory_mappings_element = (Element) MSMUtils.getNodeFromNamed(document_element, DIRECTORY_MAPPINGS);
296 // Ensure the name isn't already in use.
297 boolean found = false;
298 NodeList mappings = directory_mappings_element.getElementsByTagName(MAPPING);
299 for(int i = 0; !found && i < mappings.getLength(); i++) {
300 Element mapping_element = (Element) mappings.item(i);
301 if(mapping_element.getAttribute(NAME).equalsIgnoreCase(name)) {
302 file = new File(MSMUtils.getValue(mapping_element));
303 directory_mappings_element.removeChild(mapping_element);
304 found = true;
305 }
306 mapping_element = null;
307 }
308 mappings = null;
309 directory_mappings_element = null;
310 document_element = null;
311 saved = false;
312 }
313 catch(Exception error) {
314 Gatherer.printStackTrace(error);
315 }
316 return file;
317 }
318 /** Save this xml document to the given file. */
319 public void save() {
320 Utility.export(document, file);
321 }
322
323 public void setBaseCollection(String base_collection) {
324 set(BASE_COLLECTION, base_collection);
325 }
326
327 /** Set the value of imported to the given value.
328 * @param value The new value for imported, <i>true</i> if the collection has been imported successfully, <i>false</i> otherwise.
329 */
330 public void setImported(boolean value) {
331 set(IMPORTED, value);
332 saved = false;
333 }
334 /** Set the value of saved to the given value.
335 * @param value The new value for saved, <i>true</i> if the collection has been saved recently, <i>false</i> otherwise.
336 */
337 public void setSaved(boolean value) {
338 saved = value;
339 }
340 /** Set the value of title to the given value.
341 * @param title The new <strong>String</strong> title.
342 */
343 public void setTitle(String title) {
344 collect_cfg.setName(title);
345 }
346 /** Method called to return a textual representation of a class, which in this case is the collections title.
347 * @return A <strong>String</strong> containing the collections title.
348 */
349 public String toString() {
350 return collect_cfg.getName();
351 }
352 /** Get the value of a collection argument. */
353 private boolean get(String name) {
354 boolean result = false;
355 try {
356 Element document_element = document.getDocumentElement();
357 NodeList arguments = document_element.getElementsByTagName(ARGUMENT);
358 boolean found = false;
359 for(int i = 0; !found && i < arguments.getLength(); i++) {
360 Element argument_element = (Element) arguments.item(i);
361 if(argument_element.getParentNode() == document_element) {
362 if(argument_element.getAttribute(NAME).equalsIgnoreCase(name)) {
363 String value = MSMUtils.getValue(argument_element);
364 if(value.equalsIgnoreCase(TRUE)) {
365 result = true;
366 }
367 found = true;
368 value = null;
369 }
370 }
371 argument_element = null;
372 }
373 arguments = null;
374 document_element = null;
375 }
376 catch (Exception error) {
377 Gatherer.printStackTrace(error);
378 }
379 return result;
380 }
381
382 /** Get the value of a collection argument. */
383 private String getString(String name) {
384 String result = "";
385 try {
386 Element document_element = document.getDocumentElement();
387 NodeList arguments = document_element.getElementsByTagName(ARGUMENT);
388 boolean found = false;
389 for(int i = 0; !found && i < arguments.getLength(); i++) {
390 Element argument_element = (Element) arguments.item(i);
391 if(argument_element.getParentNode() == document_element) {
392 if(argument_element.getAttribute(NAME).equalsIgnoreCase(name)) {
393 result = MSMUtils.getValue(argument_element);
394 found = true;
395 }
396 }
397 argument_element = null;
398 }
399 arguments = null;
400 document_element = null;
401 }
402 catch (Exception error) {
403 Gatherer.printStackTrace(error);
404 }
405 return result;
406 }
407
408 /** Method to retrieve the current build options associated with this Collection. */
409 private Element getBuildValues() {
410 Element build_values_element = null;
411 try {
412 Element document_element = document.getDocumentElement();
413 Element build_config_element = (Element) MSMUtils.getNodeFromNamed(document_element, BUILD_CONFIG);
414 build_values_element = (Element) MSMUtils.getNodeFromNamed(build_config_element, BUILD);
415 build_config_element = null;
416 document_element = null;
417 }
418 catch (Exception error) {
419 Gatherer.printStackTrace(error);
420 }
421 return build_values_element;
422 }
423
424 /** Count either documents or folders, depending on the state of the given boolean. */
425 private int getCount(TreeNode node, boolean count_folders, boolean count_files) {
426 int count = 0;
427 File file = ((FileNode)node).getFile();
428 if(file.isFile() && count_files) {
429 count++;
430 }
431 else if(file.isDirectory() && count_folders) {
432 count++;
433 }
434 for(int i = 0; i < node.getChildCount(); i++) {
435 count = count + getCount(node.getChildAt(i), count_folders, count_files);
436 }
437 return count;
438 }
439
440 /** Method to retrieve the current import options associated with this Collection. */
441 public Element getImportValues() {
442 Element import_values_element = null;
443 try {
444 Element document_element = document.getDocumentElement();
445 Element build_config_element = (Element) MSMUtils.getNodeFromNamed(document_element, BUILD_CONFIG);
446 import_values_element = (Element) MSMUtils.getNodeFromNamed(build_config_element, IMPORT);
447 build_config_element = null;
448 document_element = null;
449 }
450 catch (Exception error) {
451 Gatherer.printStackTrace(error);
452 }
453 return import_values_element;
454 }
455
456 /** Set the value of a collection argument. */
457 private void set(String name, boolean value) {
458 set(name, (value ? StaticStrings.TRUE_STR : StaticStrings.FALSE_STR));
459 }
460
461 private void set(String name, String value) {
462 try {
463 Element document_element = document.getDocumentElement();
464 NodeList arguments = document_element.getElementsByTagName(ARGUMENT);
465 boolean found = false;
466 for(int i = 0; !found && i < arguments.getLength(); i++) {
467 Element argument_element = (Element) arguments.item(i);
468 if(argument_element.getParentNode() == document_element) {
469 if(argument_element.getAttribute(NAME).equalsIgnoreCase(name)) {
470 // Strip any current value nodes.
471 while(argument_element.hasChildNodes()) {
472 argument_element.removeChild(argument_element.getFirstChild());
473 }
474 // Append new value
475 argument_element.appendChild(document.createTextNode(value));
476 found = true;
477 }
478 }
479 argument_element = null;
480 }
481 // Append it
482 if(!found) {
483 Element argument_element = document.createElement(ARGUMENT);
484 argument_element.setAttribute(NAME, name);
485 argument_element.appendChild(document.createTextNode(value));
486 document_element.appendChild(argument_element);
487 argument_element = null;
488 }
489 arguments = null;
490 document_element = null;
491 }
492 catch (Exception error) {
493 Gatherer.printStackTrace(error);
494 }
495 }
496}
Note: See TracBrowser for help on using the repository browser.