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

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

Initial revision

  • Property svn:keywords set to Author Date Id Revision
File size: 16.7 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" + File.separator + "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 /** Calculates the number of documents in this collection. */
169 public int getDocumentCount() {
170 return getCount((TreeNode)Gatherer.c_man.getRecordSet().getRoot(), false);
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);
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 /** Set the value of built to the given value.
306 * @param value The new value for built, <i>true</i> if the collection has been built successfully, <i>false</i> otherwise.
307 */
308 public void setBuilt(boolean value) {
309 set(BUILT, value);
310 saved = false;
311 }
312 /** Set the value of imported to the given value.
313 * @param value The new value for imported, <i>true</i> if the collection has been imported successfully, <i>false</i> otherwise.
314 */
315 public void setImported(boolean value) {
316 set(IMPORTED, value);
317 saved = false;
318 }
319 /** Set the value of saved to the given value.
320 * @param value The new value for saved, <i>true</i> if the collection has been saved recently, <i>false</i> otherwise.
321 */
322 public void setSaved(boolean value) {
323 saved = value;
324 }
325 /** Set the value of title to the given value.
326 * @param title The new <strong>String</strong> title.
327 */
328 public void setTitle(String title) {
329 collect_cfg.setName(title);
330 }
331 /** Method called to return a textual representation of a class, which in this case is the collections title.
332 * @return A <strong>String</strong> containing the collections title.
333 */
334 public String toString() {
335 return collect_cfg.getName();
336 }
337 /** Get the value of a collection argument. */
338 private boolean get(String name) {
339 boolean result = false;
340 try {
341 Element document_element = document.getDocumentElement();
342 NodeList arguments = document_element.getElementsByTagName(ARGUMENT);
343 boolean found = false;
344 for(int i = 0; !found && i < arguments.getLength(); i++) {
345 Element argument_element = (Element) arguments.item(i);
346 if(argument_element.getParentNode() == document_element) {
347 if(argument_element.getAttribute(NAME).equalsIgnoreCase(BUILT)) {
348 String value = MSMUtils.getValue(argument_element);
349 if(value.equalsIgnoreCase(TRUE)) {
350 result = true;
351 }
352 found = true;
353 value = null;
354 }
355 }
356 argument_element = null;
357 }
358 arguments = null;
359 document_element = null;
360 }
361 catch (Exception error) {
362 Gatherer.printStackTrace(error);
363 }
364 return result;
365 }
366
367 /** Method to retrieve the current build options associated with this Collection. */
368 private Element getBuildValues() {
369 Element build_values_element = null;
370 try {
371 Element document_element = document.getDocumentElement();
372 Element build_config_element = (Element) MSMUtils.getNodeFromNamed(document_element, BUILD_CONFIG);
373 build_values_element = (Element) MSMUtils.getNodeFromNamed(build_config_element, BUILD);
374 build_config_element = null;
375 document_element = null;
376 }
377 catch (Exception error) {
378 Gatherer.printStackTrace(error);
379 }
380 return build_values_element;
381 }
382
383 /** Count either documents or folders, depending on the state of the given boolean. */
384 private int getCount(TreeNode node, boolean folders) {
385 int count = 0;
386 File file = ((FileNode)node).getFile();
387 if(file.isFile() && !folders) {
388 count++;
389 }
390 else if(file.isDirectory() && folders) {
391 count++;
392 }
393 for(int i = 0; i < node.getChildCount(); i++) {
394 count = count + getCount(node.getChildAt(i), folders);
395 }
396 return count;
397 }
398
399 /** Method to retrieve the current import options associated with this Collection. */
400 public Element getImportValues() {
401 Element import_values_element = null;
402 try {
403 Element document_element = document.getDocumentElement();
404 Element build_config_element = (Element) MSMUtils.getNodeFromNamed(document_element, BUILD_CONFIG);
405 import_values_element = (Element) MSMUtils.getNodeFromNamed(build_config_element, IMPORT);
406 build_config_element = null;
407 document_element = null;
408 }
409 catch (Exception error) {
410 Gatherer.printStackTrace(error);
411 }
412 return import_values_element;
413 }
414
415 /** Set the value of a collection argument. */
416 private void set(String name, boolean 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(NAME).equalsIgnoreCase(BUILT)) {
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 ? "true" : "false"));
431 }
432 }
433 argument_element = null;
434 }
435 arguments = null;
436 document_element = null;
437 }
438 catch (Exception error) {
439 Gatherer.printStackTrace(error);
440 }
441 }
442}
Note: See TracBrowser for help on using the repository browser.