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

Last change on this file since 8846 was 8846, checked in by mdewsnip, 19 years ago

Tidied up the workspace and collection trees a bit more, in preparation for having special icons for some collection files.

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