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

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

Replaced all "Gatherer.config" with "Configuration".

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