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

Last change on this file since 9136 was 9136, checked in by kjdon, 19 years ago

changed to use SCriptOptions instead of BuildOptions

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