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

Last change on this file since 13748 was 13748, checked in by mdewsnip, 17 years ago

No longer saves the import.pl and buildcol.pl arguments in the GLI config.xml file. This was dodgy with different languages, and the saved options were often out of date. Now the options are simply read when required (similar to the plugin and classifier options).

  • Property svn:keywords set to Author Date Id Revision
File size: 14.5 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.XMLTools;
51import org.w3c.dom.*;
52
53/** 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.
54 * @author John Thompson, Greenstone Digital Library, University of Waikato
55 * @version 2.3c
56 */
57public class Collection {
58 /** A reference to the Collection Design Manager. */
59 public CollectionDesignManager cdm;
60 /** A reference to the build ScriptOptions. */
61 public ScriptOptions build_options;
62 /** A reference to the import ScriptOptions. */
63 public ScriptOptions import_options;
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 /*<i>true</i> if the currently loaded collection has had files added since its last build */
69 private boolean filesChanged = false;
70 /*<i>true</i> if the currently loaded collection has had metadata added since its last build */
71 private boolean metadataChanged = false;
72 /** The document around which this collection class is based. */
73 private Document document;
74 /** The file the collection is in (the file may not actually exist, such in the case of a legacy collection)! */
75 private File file;
76 /** The name of the argument element. */
77 static final private String ARGUMENT = "Argument";
78 static final private String BASE_COLLECTION = "base_collection";
79 /** The name of the build element. */
80 static final private String BUILD = "Build";
81 /** The name of the build config element. */
82 static final private String BUILD_CONFIG = "BuildConfig";
83 /** The name of the collection xml template. */
84 static final private String COLLECTION_XML_TEMPLATE = "xml/template.col";
85 /** The name of the import element. */
86 static final private String IMPORT = "Import";
87 /** The name of the imported attribute. */
88 static final private String IMPORTED = "imported";
89
90 /** Constructor. */
91 public Collection(File collection_xml) {
92 this.file = collection_xml;
93 // Try to load this collections details.
94 document = XMLTools.parseXMLFile(collection_xml);
95 // If that fails load the default settings for a collection.
96 if(document == null) {
97 document = XMLTools.parseXMLFile(COLLECTION_XML_TEMPLATE, true);
98 }
99 if (document == null) {
100 error = true;
101 return;
102 }
103 // Point the Configuration class at our gatherer config arguments.
104 Configuration.setCollectionConfiguration(document);
105 if (Gatherer.g_man != null) {
106 Gatherer.g_man.updateUI();
107 }
108 // Finally create all of the child managers that are directly dependant on a collection
109 build_options = new ScriptOptions(getBuildValues(), "buildcol.pl");
110 import_options = new ScriptOptions(getImportValues(), "import.pl");
111 }
112
113 /** Destructor.
114 * @see org.greenstone.gatherer.Configuration
115 * @see org.greenstone.gatherer.Gatherer
116 * @see org.greenstone.gatherer.cdm.CollectionDesignManager
117 */
118 public void destroy() {
119 cdm.destroy();
120 Configuration.setCollectionConfiguration(null);
121 // this is called after calling collection.destroy, so we shouldn't need it here too.
122// if (Gatherer.g_man != null) {
123// Gatherer.g_man.updateUI();
124// }
125 cdm = null;
126 document = null;
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 /** Determine if this collection has had its collection files changed since the last build.
176 * @return <i>true</i> if they have been saved, <i>false</i> otherwise.
177 */
178 public boolean getFilesChanged() {
179 return filesChanged;
180 }
181
182 /** Set the flag that marks weather or not the files has been changed since the last build.
183 @param changed if files has been added/removed, as a <strong>boolean</strong>
184 */
185 public void setFilesChanged(boolean changed) {
186 filesChanged = changed;
187 }
188
189 /** Determine if this collection has had metadata changed since the last build.
190 * @return <i>true</i> if it has been changed, <i>false</i> otherwise.
191 */
192 public boolean getMetadataChanged() {
193 return metadataChanged;
194 }
195
196 /** Set the flag that marks weather or not the metadata has been changed since the last build.
197 @param changed if metadata has been added/changed/removed, as a <strong>boolean</strong>
198 */
199 public void setMetadataChanged(boolean changed) {
200 metadataChanged = changed;
201 }
202
203 /** Retrieve the title of this collection.
204 * @return The title as a <strong>String</strong>.
205 */
206 public String getTitle() {
207 if(cdm == null) {
208 return StaticStrings.EMPTY_STR;
209 }
210 CollectionMeta collection_name_collectionmeta = cdm.collectionmeta_manager.getMetadatum(StaticStrings.COLLECTIONMETADATA_COLLECTIONNAME_STR);
211 return collection_name_collectionmeta.getValue(CollectionMeta.TEXT);
212 }
213
214 /** Save this xml document to the given file. */
215 public void save() {
216 XMLTools.writeXMLFile(file, document);
217 saved = true;
218 }
219
220 public void setBaseCollection(String base_collection) {
221 set(BASE_COLLECTION, base_collection);
222 }
223
224 /** Set the value of imported to the given value.
225 * @param value The new value for imported, <i>true</i> if the collection has been imported successfully, <i>false</i> otherwise.
226 */
227 public void setImported(boolean value) {
228 set(IMPORTED, value);
229 saved = false;
230 }
231
232 /** Set the value of saved to the given value.
233 * @param value The new value for saved, <i>true</i> if the collection has been saved recently, <i>false</i> otherwise.
234 */
235 public void setSaved(boolean value) {
236 saved = value;
237 }
238
239 /** Set the value of title to the given value.
240 * @param title The new <strong>String</strong> title.
241 */
242 public void setTitle(String title) {
243 if(cdm != null) {
244 CollectionMeta collection_name_collectionmeta = cdm.collectionmeta_manager.getMetadatum(StaticStrings.COLLECTIONMETADATA_COLLECTIONNAME_STR);
245 collection_name_collectionmeta.setValue(title);
246 }
247 }
248
249 /** Method called to return a textual representation of a class, which in this case is the collections title.
250 * @return A <strong>String</strong> containing the collections title.
251 */
252 public String toString() {
253 return getTitle();
254 }
255
256 /** Get the value of a collection argument. */
257 private boolean get(String name) {
258 boolean result = false;
259 try {
260 Element document_element = document.getDocumentElement();
261 NodeList arguments = document_element.getElementsByTagName(ARGUMENT);
262 boolean found = false;
263 for(int i = 0; !found && i < arguments.getLength(); i++) {
264 Element argument_element = (Element) arguments.item(i);
265 if(argument_element.getParentNode() == document_element) {
266 if(argument_element.getAttribute(StaticStrings.NAME_ATTRIBUTE).equalsIgnoreCase(name)) {
267 String value = XMLTools.getValue(argument_element);
268 if(value.equalsIgnoreCase(StaticStrings.TRUE_STR)) {
269 result = true;
270 }
271 found = true;
272 value = null;
273 }
274 }
275 argument_element = null;
276 }
277 arguments = null;
278 document_element = null;
279 }
280 catch (Exception error) {
281 DebugStream.printStackTrace(error);
282 }
283 return result;
284 }
285
286 /** Get the value of a collection argument. */
287 private String getString(String name) {
288 String result = "";
289 try {
290 Element document_element = document.getDocumentElement();
291 NodeList arguments = document_element.getElementsByTagName(ARGUMENT);
292 boolean found = false;
293 for(int i = 0; !found && i < arguments.getLength(); i++) {
294 Element argument_element = (Element) arguments.item(i);
295 if(argument_element.getParentNode() == document_element) {
296 if(argument_element.getAttribute(StaticStrings.NAME_ATTRIBUTE).equalsIgnoreCase(name)) {
297 result = XMLTools.getValue(argument_element);
298 found = true;
299 }
300 }
301 argument_element = null;
302 }
303 arguments = null;
304 document_element = null;
305 }
306 catch (Exception error) {
307 DebugStream.printStackTrace(error);
308 }
309 return result;
310 }
311
312 /** Method to retrieve the current build options associated with this Collection. */
313 private Element getBuildValues() {
314 Element build_values_element = null;
315 try {
316 Element document_element = document.getDocumentElement();
317 Element build_config_element = (Element) XMLTools.getNodeFromNamed(document_element, BUILD_CONFIG);
318 build_values_element = (Element) XMLTools.getNodeFromNamed(build_config_element, BUILD);
319 build_config_element = null;
320 document_element = null;
321 }
322 catch (Exception error) {
323 DebugStream.printStackTrace(error);
324 }
325 return build_values_element;
326 }
327
328 /** Count either documents or folders, depending on the state of the given boolean. */
329 private int getCount(TreeNode node, boolean count_folders, boolean count_files) {
330 int count = 0;
331 File file = ((CollectionTreeNode)node).getFile();
332 if(file.isFile() && count_files) {
333 count++;
334 }
335 else if(file.isDirectory() && count_folders) {
336 count++;
337 }
338 for(int i = 0; !file.getName().equals("CVS") && i < node.getChildCount(); i++) {
339 count = count + getCount(node.getChildAt(i), count_folders, count_files);
340 }
341 return count;
342 }
343
344 /** Method to retrieve the current import options associated with this Collection. */
345 public Element getImportValues() {
346 Element import_values_element = null;
347 try {
348 Element document_element = document.getDocumentElement();
349 Element build_config_element = (Element) XMLTools.getNodeFromNamed(document_element, BUILD_CONFIG);
350 import_values_element = (Element) XMLTools.getNodeFromNamed(build_config_element, IMPORT);
351 build_config_element = null;
352 document_element = null;
353 }
354 catch (Exception error) {
355 DebugStream.printStackTrace(error);
356 }
357 return import_values_element;
358 }
359
360 /** Set the value of a collection argument. */
361 private void set(String name, boolean value) {
362 set(name, (value ? StaticStrings.TRUE_STR : StaticStrings.FALSE_STR));
363 }
364
365 private void set(String name, String value) {
366 try {
367 Element document_element = document.getDocumentElement();
368 NodeList arguments = document_element.getElementsByTagName(ARGUMENT);
369 boolean found = false;
370 for(int i = 0; !found && i < arguments.getLength(); i++) {
371 Element argument_element = (Element) arguments.item(i);
372 if(argument_element.getParentNode() == document_element) {
373 if(argument_element.getAttribute(StaticStrings.NAME_ATTRIBUTE).equalsIgnoreCase(name)) {
374 // Strip any current value nodes.
375 while(argument_element.hasChildNodes()) {
376 argument_element.removeChild(argument_element.getFirstChild());
377 }
378 // Append new value
379 argument_element.appendChild(document.createTextNode(value));
380 found = true;
381 }
382 }
383 argument_element = null;
384 }
385 // Append it
386 if(!found) {
387 Element argument_element = document.createElement(ARGUMENT);
388 argument_element.setAttribute(StaticStrings.NAME_ATTRIBUTE, name);
389 argument_element.appendChild(document.createTextNode(value));
390 document_element.appendChild(argument_element);
391 argument_element = null;
392 }
393 arguments = null;
394 document_element = null;
395 }
396 catch (Exception error) {
397 DebugStream.printStackTrace(error);
398 }
399 }
400}
Note: See TracBrowser for help on using the repository browser.