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

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

I can't remember what has changed, but I bet it was for the better

  • Property svn:keywords set to Author Date Id Revision
File size: 16.9 KB
Line 
1package org.greenstone.gatherer.collection;
2/**
3 *#########################################################################
4 *
5 * A component of the Gatherer application, part of the Greenstone digital
6 * library suite from the New Zealand Digital Library Project at the
7 * University of Waikato, New Zealand.
8 *
9 * <BR><BR>
10 *
11 * Author: John Thompson, Greenstone Digital Library, University of Waikato
12 *
13 * <BR><BR>
14 *
15 * Copyright (C) 1999 New Zealand Digital Library Project
16 *
17 * <BR><BR>
18 *
19 * This program is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation; either version 2 of the License, or
22 * (at your option) any later version.
23 *
24 * <BR><BR>
25 *
26 * This program is distributed in the hope that it will be useful,
27 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 * GNU General Public License for more details.
30 *
31 * <BR><BR>
32 *
33 * You should have received a copy of the GNU General Public License
34 * along with this program; if not, write to the Free Software
35 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
36 *########################################################################
37 */
38import java.io.*;
39import java.util.*;
40import javax.swing.*;
41import javax.swing.tree.*;
42import org.greenstone.gatherer.Gatherer;
43import org.greenstone.gatherer.cdm.CollectionDesignManager;
44import org.greenstone.gatherer.collection.BuildOptions;
45import org.greenstone.gatherer.collection.CollectionManager;
46import org.greenstone.gatherer.file.FileNode;
47import org.greenstone.gatherer.msm.ElementWrapper;
48import org.greenstone.gatherer.msm.GDMManager;
49import org.greenstone.gatherer.msm.MetadataSetManager;
50import org.greenstone.gatherer.msm.MSMUtils;
51import org.greenstone.gatherer.util.Utility;
52import org.w3c.dom.*;
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 BuildOptions. */
59 public BuildOptions build_options;
60 /** A reference to the Collection Design Manager. */
61 public CollectionDesignManager cdm;
62 /** A reference to the Greenstone Directory Metadata Manager. */
63 public GDMManager gdm;
64 /** A reference to the Metadata Set Manager. */
65 public MetadataSetManager msm;
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 /** The collectio configuration file for this collection. */
69 private CollectionConfiguration collect_cfg;
70 /** The document around which this collection class is based. */
71 private Document document;
72 /** The file the collection is in (the file may not actually exist, such in the case of a legacy collection)! */
73 private File file;
74 /** The name of the argument element. */
75 static final private String ARGUMENT = "Argument";
76 /** The name of the build element. */
77 static final private String BUILD = "Build";
78 /** The name of the built attribute. */
79 static final private String BUILT = "built";
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 /** Determine whether this collection has been successfully built in the past.
163 * @return <i>true</i> if the collection has been built, <i>false</i> otherwise.
164 */
165 public boolean getBuilt() {
166 return get(BUILT);
167 }
168
169 /** Determine the number of documents and folders in this collection. */
170 public int getCount() {
171 return getCount((TreeNode)Gatherer.c_man.getRecordSet().getRoot(), true, true);
172 }
173
174 /** Calculates the number of documents in this collection. */
175 public int getDocumentCount() {
176 return getCount((TreeNode)Gatherer.c_man.getRecordSet().getRoot(), false, true);
177 }
178 /** Retrieve the description of this collection.
179 * @return The description as a <strong>String</strong>.
180 */
181 public String getDescription() {
182 return collect_cfg.getDescription();
183 }
184 /** Retrieve a specific directory mapping associated with this collection.
185 * @param name The name of the mapping to retrieve as a <strong>String</strong>.
186 * @return The <strong>File</strong> this name maps to, or <i>null</i> if no such mapping.
187 */
188 public File getDirectoryMapping(String name) {
189 File result = null;
190 try {
191 Element document_element = document.getDocumentElement();
192 Element directory_mappings_element = (Element) MSMUtils.getNodeFromNamed(document_element, DIRECTORY_MAPPINGS);
193 // Ensure the name isn't already in use.
194 boolean found = false;
195 NodeList mappings = directory_mappings_element.getElementsByTagName(MAPPING);
196 for(int i = 0; !found && i < mappings.getLength(); i++) {
197 Element mapping_element = (Element) mappings.item(i);
198 if(mapping_element.getAttribute(NAME).equalsIgnoreCase(name)) {
199 result = new File(MSMUtils.getValue(mapping_element));
200 found = true;
201 }
202 mapping_element = null;
203 }
204 mappings = null;
205 directory_mappings_element = null;
206 document_element = null;
207 }
208 catch(Exception error) {
209 Gatherer.printStackTrace(error);
210 }
211 return result;
212 }
213 /** Retrieve the special directory mappings associated with this collection.
214 * @return A <strong>HashMap</strong> containing mappings from names to directories.
215 */
216 public HashMap getDirectoryMappings() {
217 HashMap special_directories = new HashMap();
218 try {
219 Element document_element = document.getDocumentElement();
220 Element directory_mappings_element = (Element) MSMUtils.getNodeFromNamed(document_element, DIRECTORY_MAPPINGS);
221 // Ensure the name isn't already in use.
222 boolean found = false;
223 NodeList mappings = directory_mappings_element.getElementsByTagName(MAPPING);
224 for(int i = 0; !found && i < mappings.getLength(); i++) {
225 Element mapping_element = (Element) mappings.item(i);
226 String name = mapping_element.getAttribute(NAME);
227 File file = new File(mapping_element.getAttribute(FILE));
228 special_directories.put(name, file);
229 file = null;
230 name = null;
231 mapping_element = null;
232 }
233 mappings = null;
234 directory_mappings_element = null;
235 document_element = null;
236 }
237 catch(Exception error) {
238 Gatherer.printStackTrace(error);
239 }
240 return special_directories;
241 }
242 /** Retrieve the authors email for this collection.
243 * @return The email as a <strong>String</strong>.
244 */
245 public String getEmail() {
246 return collect_cfg.getCreator();
247 }
248 /** Counts the number of folders used in the current record set. */
249 public int getFolderCount() {
250 return getCount((TreeNode)Gatherer.c_man.getRecordSet().getRoot(), true, false);
251 }
252 /** Determine if this collection has had an import action run upon it since the last major change.
253 * @return <i>true</i> if an import has occured, <i>false</i> otherwise.
254 */
255 public boolean getImported() {
256 return get(IMPORTED);
257 }
258 /** Retrieve the short name for this collection.
259 * @return The name as a <strong>String</strong>.
260 */
261 public String getName() {
262 return file.getParentFile().getName();
263 }
264 /** Determine if this collection has been saved since the last major change.
265 * @return <i>true</i> if it has been saved recently, <i>false</i> otherwise.
266 */
267 public boolean getSaved() {
268 return saved;
269 }
270 /** Retrieve the title of this collection.
271 * @return The title as a <strong>String</strong>.
272 */
273 public String getTitle() {
274 return collect_cfg.getName();
275 }
276 /** Remove a previously defined special directory mapping.
277 * @param name The name of the mapping to remove as a <strong>String</strong>.
278 * @return The <strong>File</strong> of the mapping removed.
279 */
280 public File removeDirectoryMapping(String name) {
281 File file = null;
282 try {
283 Element document_element = document.getDocumentElement();
284 Element directory_mappings_element = (Element) MSMUtils.getNodeFromNamed(document_element, DIRECTORY_MAPPINGS);
285 // Ensure the name isn't already in use.
286 boolean found = false;
287 NodeList mappings = directory_mappings_element.getElementsByTagName(MAPPING);
288 for(int i = 0; !found && i < mappings.getLength(); i++) {
289 Element mapping_element = (Element) mappings.item(i);
290 if(mapping_element.getAttribute(NAME).equalsIgnoreCase(name)) {
291 file = new File(MSMUtils.getValue(mapping_element));
292 directory_mappings_element.removeChild(mapping_element);
293 found = true;
294 }
295 mapping_element = null;
296 }
297 mappings = null;
298 directory_mappings_element = null;
299 document_element = null;
300 saved = false;
301 }
302 catch(Exception error) {
303 Gatherer.printStackTrace(error);
304 }
305 return file;
306 }
307 /** Save this xml document to the given file. */
308 public void save() {
309 Utility.export(document, file);
310 }
311 /** Set the value of built to the given value.
312 * @param value The new value for built, <i>true</i> if the collection has been built successfully, <i>false</i> otherwise.
313 */
314 public void setBuilt(boolean value) {
315 set(BUILT, value);
316 saved = false;
317 }
318 /** Set the value of imported to the given value.
319 * @param value The new value for imported, <i>true</i> if the collection has been imported successfully, <i>false</i> otherwise.
320 */
321 public void setImported(boolean value) {
322 set(IMPORTED, value);
323 saved = false;
324 }
325 /** Set the value of saved to the given value.
326 * @param value The new value for saved, <i>true</i> if the collection has been saved recently, <i>false</i> otherwise.
327 */
328 public void setSaved(boolean value) {
329 saved = value;
330 }
331 /** Set the value of title to the given value.
332 * @param title The new <strong>String</strong> title.
333 */
334 public void setTitle(String title) {
335 collect_cfg.setName(title);
336 }
337 /** Method called to return a textual representation of a class, which in this case is the collections title.
338 * @return A <strong>String</strong> containing the collections title.
339 */
340 public String toString() {
341 return collect_cfg.getName();
342 }
343 /** Get the value of a collection argument. */
344 private boolean get(String name) {
345 boolean result = false;
346 try {
347 Element document_element = document.getDocumentElement();
348 NodeList arguments = document_element.getElementsByTagName(ARGUMENT);
349 boolean found = false;
350 for(int i = 0; !found && i < arguments.getLength(); i++) {
351 Element argument_element = (Element) arguments.item(i);
352 if(argument_element.getParentNode() == document_element) {
353 if(argument_element.getAttribute(NAME).equalsIgnoreCase(BUILT)) {
354 String value = MSMUtils.getValue(argument_element);
355 if(value.equalsIgnoreCase(TRUE)) {
356 result = true;
357 }
358 found = true;
359 value = null;
360 }
361 }
362 argument_element = null;
363 }
364 arguments = null;
365 document_element = null;
366 }
367 catch (Exception error) {
368 Gatherer.printStackTrace(error);
369 }
370 return result;
371 }
372
373 /** Method to retrieve the current build options associated with this Collection. */
374 private Element getBuildValues() {
375 Element build_values_element = null;
376 try {
377 Element document_element = document.getDocumentElement();
378 Element build_config_element = (Element) MSMUtils.getNodeFromNamed(document_element, BUILD_CONFIG);
379 build_values_element = (Element) MSMUtils.getNodeFromNamed(build_config_element, BUILD);
380 build_config_element = null;
381 document_element = null;
382 }
383 catch (Exception error) {
384 Gatherer.printStackTrace(error);
385 }
386 return build_values_element;
387 }
388
389 /** Count either documents or folders, depending on the state of the given boolean. */
390 private int getCount(TreeNode node, boolean count_folders, boolean count_files) {
391 int count = 0;
392 File file = ((FileNode)node).getFile();
393 if(file.isFile() && count_files) {
394 count++;
395 }
396 else if(file.isDirectory() && count_folders) {
397 count++;
398 }
399 for(int i = 0; i < node.getChildCount(); i++) {
400 count = count + getCount(node.getChildAt(i), count_folders, count_files);
401 }
402 return count;
403 }
404
405 /** Method to retrieve the current import options associated with this Collection. */
406 public Element getImportValues() {
407 Element import_values_element = null;
408 try {
409 Element document_element = document.getDocumentElement();
410 Element build_config_element = (Element) MSMUtils.getNodeFromNamed(document_element, BUILD_CONFIG);
411 import_values_element = (Element) MSMUtils.getNodeFromNamed(build_config_element, IMPORT);
412 build_config_element = null;
413 document_element = null;
414 }
415 catch (Exception error) {
416 Gatherer.printStackTrace(error);
417 }
418 return import_values_element;
419 }
420
421 /** Set the value of a collection argument. */
422 private void set(String name, boolean value) {
423 try {
424 Element document_element = document.getDocumentElement();
425 NodeList arguments = document_element.getElementsByTagName(ARGUMENT);
426 boolean found = false;
427 for(int i = 0; !found && i < arguments.getLength(); i++) {
428 Element argument_element = (Element) arguments.item(i);
429 if(argument_element.getParentNode() == document_element) {
430 if(argument_element.getAttribute(NAME).equalsIgnoreCase(BUILT)) {
431 // Strip any current value nodes.
432 while(argument_element.hasChildNodes()) {
433 argument_element.removeChild(argument_element.getFirstChild());
434 }
435 // Append new value
436 argument_element.appendChild(document.createTextNode(value ? "true" : "false"));
437 }
438 }
439 argument_element = null;
440 }
441 arguments = null;
442 document_element = null;
443 }
444 catch (Exception error) {
445 Gatherer.printStackTrace(error);
446 }
447 }
448}
Note: See TracBrowser for help on using the repository browser.