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

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

Moved some stuff out of Configuration.updateUI into GUIManager because it depends on the Gatherer.class. More to come.

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