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

Last change on this file since 8236 was 8236, checked in by mdewsnip, 20 years ago

Replaced all Gatherer.print* with DebugStream.print*.

  • Property svn:keywords set to Author Date Id Revision
File size: 13.6 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.Configuration;
44import org.greenstone.gatherer.DebugStream;
45import org.greenstone.gatherer.Gatherer;
46import org.greenstone.gatherer.cdm.CollectionDesignManager;
47import org.greenstone.gatherer.cdm.CollectionMeta;
48import org.greenstone.gatherer.cdm.CollectionMetaManager;
49import org.greenstone.gatherer.collection.BuildOptions;
50import org.greenstone.gatherer.collection.BasicCollectionConfiguration;
51import org.greenstone.gatherer.collection.CollectionManager;
52import org.greenstone.gatherer.file.FileNode;
53import org.greenstone.gatherer.msm.MetadataXMLFileManager;
54import org.greenstone.gatherer.msm.MetadataSetManager;
55import org.greenstone.gatherer.util.StaticStrings;
56import org.greenstone.gatherer.util.Utility;
57import org.greenstone.gatherer.util.XMLTools;
58import org.w3c.dom.*;
59
60/** 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.
61 * @author John Thompson, Greenstone Digital Library, University of Waikato
62 * @version 2.3c
63 */
64public class Collection {
65 /** A reference to the BuildOptions. */
66 public BuildOptions build_options;
67 /** A reference to the Collection Design Manager. */
68 public CollectionDesignManager cdm;
69 /** A reference to the Greenstone Directory Metadata Manager. */
70 public MetadataXMLFileManager gdm;
71 /** A reference to the Metadata Set Manager. */
72 public MetadataSetManager msm;
73 /** true if an error has occurred during construction */
74 public boolean error = false;
75 /** <i>true</i> if the currently loaded collection has been saved since the last significant change, <i>false</i> otherwise. */
76 private boolean saved = false;
77 /** The document around which this collection class is based. */
78 private Document document;
79 /** The file the collection is in (the file may not actually exist, such in the case of a legacy collection)! */
80 private File file;
81 /** The name of the argument element. */
82 static final private String ARGUMENT = "Argument";
83 static final private String BASE_COLLECTION = "base_collection";
84 /** The name of the build element. */
85 static final private String BUILD = "Build";
86 /** The name of the build config element. */
87 static final private String BUILD_CONFIG = "BuildConfig";
88 /** The name of the collection xml template. */
89 static final private String COLLECTION_XML_TEMPLATE = "xml/template.col";
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
95 /** Constructor. */
96 public Collection(File collection_xml) {
97 this.file = collection_xml;
98 // Try to load this collections details.
99 document = Utility.parse(collection_xml, false);
100 // If that fails load the default settings for a collection.
101 if(document == null) {
102 document = Utility.parse(COLLECTION_XML_TEMPLATE, true);
103 }
104 if (document == null) {
105 error = true;
106 return;
107 }
108 // Point the Configuration class at our gatherer config arguments.
109 Configuration.setCollectionConfiguration(document);
110 // Finally create all of the child managers that are directly dependant on a collection
111 build_options = new BuildOptions(getBuildValues(), getImportValues());
112 }
113
114 /** Destructor.
115 * @see org.greenstone.gatherer.Configuration
116 * @see org.greenstone.gatherer.Gatherer
117 * @see org.greenstone.gatherer.cdm.CollectionDesignManager
118 */
119 public void destroy() {
120 cdm.destroy();
121 gdm.destroy();
122 msm.destroy();
123 Configuration.setCollectionConfiguration(null);
124 cdm = null;
125 document = null;
126 gdm = null;
127 msm = null;
128 }
129
130 /** Determine the path to the base collection.
131 * @return the path as a String
132 */
133 public String getBaseCollection() {
134 return getString(BASE_COLLECTION);
135 }
136
137 /** Determine the number of documents and folders in this collection. */
138 public int getCount() {
139 return getCount((TreeNode)Gatherer.c_man.getRecordSet().getRoot(), true, true);
140 }
141
142 /** Calculates the number of documents in this collection. */
143 public int getDocumentCount() {
144 return getCount((TreeNode)Gatherer.c_man.getRecordSet().getRoot(), false, true);
145 }
146
147 /** Retrieve the description of this collection.
148 * @return a String
149 */
150 public String getDescription() {
151 if(cdm == null) {
152 return StaticStrings.EMPTY_STR;
153 }
154 CollectionMeta collection_extra_collectionmeta = cdm.collectionmeta_manager.getMetadatum(StaticStrings.COLLECTIONMETADATA_COLLECTIONEXTRA_STR);
155 return collection_extra_collectionmeta.getValue(CollectionMeta.TEXT);
156 }
157
158 /** Retrieve the authors email for this collection.
159 * @return The email as a <strong>String</strong>.
160 */
161 public String getEmail() {
162 if(cdm == null) {
163 return StaticStrings.EMPTY_STR;
164 }
165 CollectionMeta creator_collectionmeta = new CollectionMeta(CollectionDesignManager.collect_config.getCreator());
166 return creator_collectionmeta.getValue(CollectionMeta.TEXT);
167 }
168
169 public MetadataXMLFileManager getGDM() {
170 return gdm;
171 }
172
173 /** Retrieve the short name for this collection.
174 * @return The name as a <strong>String</strong>.
175 */
176 public String getName() {
177 return file.getParentFile().getName();
178 }
179
180 /** Determine if this collection has been saved since the last major change.
181 * @return <i>true</i> if it has been saved recently, <i>false</i> otherwise.
182 */
183 public boolean getSaved() {
184 return saved;
185 }
186
187 /** Retrieve the title of this collection.
188 * @return The title as a <strong>String</strong>.
189 */
190 public String getTitle() {
191 if(cdm == null) {
192 return StaticStrings.EMPTY_STR;
193 }
194 CollectionMeta collection_name_collectionmeta = cdm.collectionmeta_manager.getMetadatum(StaticStrings.COLLECTIONMETADATA_COLLECTIONNAME_STR);
195 return collection_name_collectionmeta.getValue(CollectionMeta.TEXT);
196 }
197
198 /** Save this xml document to the given file. */
199 public void save() {
200 Utility.export(document, file);
201 }
202
203 public void setBaseCollection(String base_collection) {
204 set(BASE_COLLECTION, base_collection);
205 }
206
207 /** Set the value of imported to the given value.
208 * @param value The new value for imported, <i>true</i> if the collection has been imported successfully, <i>false</i> otherwise.
209 */
210 public void setImported(boolean value) {
211 set(IMPORTED, value);
212 saved = false;
213 }
214
215 /** Set the value of saved to the given value.
216 * @param value The new value for saved, <i>true</i> if the collection has been saved recently, <i>false</i> otherwise.
217 */
218 public void setSaved(boolean value) {
219 saved = value;
220 }
221
222 /** Set the value of title to the given value.
223 * @param title The new <strong>String</strong> title.
224 */
225 public void setTitle(String title) {
226 if(cdm != null) {
227 CollectionMeta collection_name_collectionmeta = cdm.collectionmeta_manager.getMetadatum(StaticStrings.COLLECTIONMETADATA_COLLECTIONNAME_STR);
228 collection_name_collectionmeta.setValue(title);
229 }
230 }
231
232 /** Method called to return a textual representation of a class, which in this case is the collections title.
233 * @return A <strong>String</strong> containing the collections title.
234 */
235 public String toString() {
236 return getTitle();
237 }
238
239 /** Get the value of a collection argument. */
240 private boolean get(String name) {
241 boolean result = false;
242 try {
243 Element document_element = document.getDocumentElement();
244 NodeList arguments = document_element.getElementsByTagName(ARGUMENT);
245 boolean found = false;
246 for(int i = 0; !found && i < arguments.getLength(); i++) {
247 Element argument_element = (Element) arguments.item(i);
248 if(argument_element.getParentNode() == document_element) {
249 if(argument_element.getAttribute(StaticStrings.NAME_ATTRIBUTE).equalsIgnoreCase(name)) {
250 String value = XMLTools.getValue(argument_element);
251 if(value.equalsIgnoreCase(StaticStrings.TRUE_STR)) {
252 result = true;
253 }
254 found = true;
255 value = null;
256 }
257 }
258 argument_element = null;
259 }
260 arguments = null;
261 document_element = null;
262 }
263 catch (Exception error) {
264 DebugStream.printStackTrace(error);
265 }
266 return result;
267 }
268
269 /** Get the value of a collection argument. */
270 private String getString(String name) {
271 String result = "";
272 try {
273 Element document_element = document.getDocumentElement();
274 NodeList arguments = document_element.getElementsByTagName(ARGUMENT);
275 boolean found = false;
276 for(int i = 0; !found && i < arguments.getLength(); i++) {
277 Element argument_element = (Element) arguments.item(i);
278 if(argument_element.getParentNode() == document_element) {
279 if(argument_element.getAttribute(StaticStrings.NAME_ATTRIBUTE).equalsIgnoreCase(name)) {
280 result = XMLTools.getValue(argument_element);
281 found = true;
282 }
283 }
284 argument_element = null;
285 }
286 arguments = null;
287 document_element = null;
288 }
289 catch (Exception error) {
290 DebugStream.printStackTrace(error);
291 }
292 return result;
293 }
294
295 /** Method to retrieve the current build options associated with this Collection. */
296 private Element getBuildValues() {
297 Element build_values_element = null;
298 try {
299 Element document_element = document.getDocumentElement();
300 Element build_config_element = (Element) XMLTools.getNodeFromNamed(document_element, BUILD_CONFIG);
301 build_values_element = (Element) XMLTools.getNodeFromNamed(build_config_element, BUILD);
302 build_config_element = null;
303 document_element = null;
304 }
305 catch (Exception error) {
306 DebugStream.printStackTrace(error);
307 }
308 return build_values_element;
309 }
310
311 /** Count either documents or folders, depending on the state of the given boolean. */
312 private int getCount(TreeNode node, boolean count_folders, boolean count_files) {
313 int count = 0;
314 File file = ((FileNode)node).getFile();
315 if(file.isFile() && count_files) {
316 count++;
317 }
318 else if(file.isDirectory() && count_folders) {
319 count++;
320 }
321 for(int i = 0; !file.getName().equals("CVS") && i < node.getChildCount(); i++) {
322 count = count + getCount(node.getChildAt(i), count_folders, count_files);
323 }
324 return count;
325 }
326
327 /** Method to retrieve the current import options associated with this Collection. */
328 public Element getImportValues() {
329 Element import_values_element = null;
330 try {
331 Element document_element = document.getDocumentElement();
332 Element build_config_element = (Element) XMLTools.getNodeFromNamed(document_element, BUILD_CONFIG);
333 import_values_element = (Element) XMLTools.getNodeFromNamed(build_config_element, IMPORT);
334 build_config_element = null;
335 document_element = null;
336 }
337 catch (Exception error) {
338 DebugStream.printStackTrace(error);
339 }
340 return import_values_element;
341 }
342
343 /** Set the value of a collection argument. */
344 private void set(String name, boolean value) {
345 set(name, (value ? StaticStrings.TRUE_STR : StaticStrings.FALSE_STR));
346 }
347
348 private void set(String name, String value) {
349 try {
350 Element document_element = document.getDocumentElement();
351 NodeList arguments = document_element.getElementsByTagName(ARGUMENT);
352 boolean found = false;
353 for(int i = 0; !found && i < arguments.getLength(); i++) {
354 Element argument_element = (Element) arguments.item(i);
355 if(argument_element.getParentNode() == document_element) {
356 if(argument_element.getAttribute(StaticStrings.NAME_ATTRIBUTE).equalsIgnoreCase(name)) {
357 // Strip any current value nodes.
358 while(argument_element.hasChildNodes()) {
359 argument_element.removeChild(argument_element.getFirstChild());
360 }
361 // Append new value
362 argument_element.appendChild(document.createTextNode(value));
363 found = true;
364 }
365 }
366 argument_element = null;
367 }
368 // Append it
369 if(!found) {
370 Element argument_element = document.createElement(ARGUMENT);
371 argument_element.setAttribute(StaticStrings.NAME_ATTRIBUTE, name);
372 argument_element.appendChild(document.createTextNode(value));
373 document_element.appendChild(argument_element);
374 argument_element = null;
375 }
376 arguments = null;
377 document_element = null;
378 }
379 catch (Exception error) {
380 DebugStream.printStackTrace(error);
381 }
382 }
383}
Note: See TracBrowser for help on using the repository browser.