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

Last change on this file since 6086 was 6086, checked in by jmt12, 20 years ago

Moved the special folder 'shortcuts' from collection based to shared for the installation of gli. This caused some added fun when it turned out one of the workspace tree refreshes was blocking the AWTEvent thread, despite me putting tests in place to stop that from happening. It had never reared its ugly head before, as there were never any shortcut collections being mapped when a collection had been closed, or when GLI was exiting before.

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