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

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

Finally committing the (many) changes to the GLI to use the new metadata code... I hope this doesn't have too many bugs in it and committing it now doesn't stuff anyone up! (Katherine said I could commit it, so blame her if anything goes wrong).

  • 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 }
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 = ((FileNode)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.