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

Last change on this file since 5581 was 5581, checked in by mdewsnip, 21 years ago

Many formatting, structural and code improvements.

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