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

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

Made saving collections unthreaded -- this makes a lot of code, and at least one race condition, so away. Saving collections is quick enough that it doesn't matter if it is done on the main thread.

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