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

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

Here is the result of sixteen hours work over the weekend. I'm too tired to comment them all separately, but here are some of the highlights:
Rewrote how the 'base on collection' method actually retrieves and updates the collection configuration - ensuring the CDM.CollectionConfiguration class is used instead of the retarded Collection.CollectionConfiguration (which coincidently has had a name change to BasicCollectionConfiguration). Went through code search for places where the two versions had been confused. Rewrote large swathes of GDMDocument so as to differentiate between normal and extracted metadata - an attempt to prevent the snowballing extracted metadata problem. Fixed problem where GLI was correctly recieving the last few lines of an external process. The collection shortname is no longer visible, nor is the confusing double name for metadata elements. Also coloured folders in the trees are kaput. The users email is now saved as part of the GLI configuration and is used as appropriate to fill out collection fields. There are new options on the right click menus over trees to allow the expansion and collapsing of folders. 'Show Files' now shows all types (or at least 6 types) of image properly (arg, the plagues of copy and paste). 'Based On' collections are public, plugin list automatically moves to next entry if plugin removed (I guess we should do the same in every other screen?) and metadata arguments in plugins/classifiers are no longer editable. There are about a dozen other small things, but I can't remember them. Hope I remembered to set all of the files to UNIX line-endings.

  • Property svn:keywords set to Author Date Id Revision
File size: 16.4 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 directory mappings element. */
88 static final private String DIRECTORY_MAPPINGS = "DirectoryMappings";
89 /** The name of the file attribute. */
90 static final private String FILE = "file";
91 /** The name of the import element. */
92 static final private String IMPORT = "Import";
93 /** The name of the imported attribute. */
94 static final private String IMPORTED = "imported";
95 /** The name of the mapping element. */
96 static final private String MAPPING = "Mapping";
97 /** The name of the name attribute. */
98 static final private String NAME = "name";
99 /** The name of the true value. */
100 static final private String TRUE = "true";
101 /** Constructor. */
102 public Collection(File collection_xml) {
103 this.file = collection_xml;
104 // Try to load this collections details.
105 document = Utility.parse(collection_xml, false);
106 // If that fails load the default settings for a collection.
107 if(document == null) {
108 document = Utility.parse(COLLECTION_XML_TEMPLATE, true);
109 }
110 // Point the Configuration class at our gatherer config arguments.
111 Gatherer.config.setCollectionConfiguration(document);
112 // Finally create all of the child managers that are directly dependant on a collection
113 build_options = new BuildOptions(getBuildValues(), getImportValues());
114 }
115
116 /** Add a special directory mapping. */
117 public boolean addDirectoryMapping(String name, File file) {
118 boolean result = false;
119 try {
120 Element document_element = document.getDocumentElement();
121 Element directory_mappings_element = (Element) MSMUtils.getNodeFromNamed(document_element, DIRECTORY_MAPPINGS);
122 // Ensure the name isn't already in use.
123 boolean found = false;
124 NodeList mappings = directory_mappings_element.getElementsByTagName(MAPPING);
125 for(int i = 0; !found && i < mappings.getLength(); i++) {
126 Element mapping_element = (Element) mappings.item(i);
127 if(mapping_element.getAttribute(NAME).equalsIgnoreCase(name)) {
128 found = true;
129 }
130 mapping_element = null;
131 }
132 // Otherwise add the mapping.
133 if(!found) {
134 Element mapping_element = document.createElement(MAPPING);
135 mapping_element.setAttribute(NAME, name);
136 mapping_element.setAttribute(FILE, file.toString());
137 directory_mappings_element.appendChild(mapping_element);
138 result = true;
139 mapping_element = null;
140 }
141 mappings = null;
142 directory_mappings_element = null;
143 document_element = null;
144 saved = false;
145 }
146 catch (Exception error) {
147 Gatherer.printStackTrace(error);
148 }
149 return result;
150 }
151
152 /** Destructor.
153 * @see org.greenstone.gatherer.collection.CollectionModel */
154 public void destroy() {
155 cdm.destroy();
156 gdm.destroy();
157 msm.destroy();
158 Gatherer.config.setCollectionConfiguration(null);
159 cdm = null;
160 document = null;
161 gdm = null;
162 msm = null;
163 }
164
165 /** Determine the path to the base collection.
166 * @return the path as a String
167 */
168 public String getBaseCollection() {
169 return getString(BASE_COLLECTION);
170 }
171
172 /** Determine the number of documents and folders in this collection. */
173 public int getCount() {
174 return getCount((TreeNode)Gatherer.c_man.getRecordSet().getRoot(), true, true);
175 }
176
177 /** Calculates the number of documents in this collection. */
178 public int getDocumentCount() {
179 return getCount((TreeNode)Gatherer.c_man.getRecordSet().getRoot(), false, true);
180 }
181
182 /** Retrieve the description of this collection.
183 * @return a String
184 */
185 public String getDescription() {
186 if(cdm == null) {
187 return StaticStrings.EMPTY_STR;
188 }
189 CollectionMeta collection_extra_collectionmeta = cdm.collectionmeta_manager.getMetadatum(StaticStrings.COLLECTIONMETADATA_COLLECTIONEXTRA_STR);
190 return collection_extra_collectionmeta.getValue(CollectionMeta.TEXT);
191 }
192
193 /** Retrieve the special directory mappings associated with this collection.
194 * @return A <strong>HashMap</strong> containing mappings from names to directories.
195 */
196 public HashMap getDirectoryMappings() {
197 HashMap special_directories = new HashMap();
198 try {
199 Element document_element = document.getDocumentElement();
200 Element directory_mappings_element = (Element) MSMUtils.getNodeFromNamed(document_element, DIRECTORY_MAPPINGS);
201 // Ensure the name isn't already in use.
202 boolean found = false;
203 NodeList mappings = directory_mappings_element.getElementsByTagName(MAPPING);
204 for(int i = 0; !found && i < mappings.getLength(); i++) {
205 Element mapping_element = (Element) mappings.item(i);
206 String name = mapping_element.getAttribute(NAME);
207 File file = new File(mapping_element.getAttribute(FILE));
208 special_directories.put(name, file);
209 file = null;
210 name = null;
211 mapping_element = null;
212 }
213 mappings = null;
214 directory_mappings_element = null;
215 document_element = null;
216 }
217 catch(Exception error) {
218 Gatherer.printStackTrace(error);
219 }
220 return special_directories;
221 }
222 /** Retrieve the authors email for this collection.
223 * @return The email as a <strong>String</strong>.
224 */
225 public String getEmail() {
226 if(cdm == null) {
227 return StaticStrings.EMPTY_STR;
228 }
229 CollectionMeta creator_collectionmeta = new CollectionMeta(CollectionDesignManager.collect_config.getCreator());
230 return creator_collectionmeta.getValue(CollectionMeta.TEXT);
231 }
232
233 /** Retrieve the short name for this collection.
234 * @return The name as a <strong>String</strong>.
235 */
236 public String getName() {
237 return file.getParentFile().getName();
238 }
239
240 /** Determine if this collection has been saved since the last major change.
241 * @return <i>true</i> if it has been saved recently, <i>false</i> otherwise.
242 */
243 public boolean getSaved() {
244 return saved;
245 }
246
247 /** Retrieve the title of this collection.
248 * @return The title as a <strong>String</strong>.
249 */
250 public String getTitle() {
251 if(cdm == null) {
252 return StaticStrings.EMPTY_STR;
253 }
254 CollectionMeta collection_name_collectionmeta = cdm.collectionmeta_manager.getMetadatum(StaticStrings.COLLECTIONMETADATA_COLLECTIONNAME_STR);
255 return collection_name_collectionmeta.getValue(CollectionMeta.TEXT);
256 }
257 /** Remove a previously defined special directory mapping.
258 * @param name The name of the mapping to remove as a <strong>String</strong>.
259 * @return The <strong>File</strong> of the mapping removed.
260 */
261 public File removeDirectoryMapping(String name) {
262 File file = null;
263 try {
264 Element document_element = document.getDocumentElement();
265 Element directory_mappings_element = (Element) MSMUtils.getNodeFromNamed(document_element, DIRECTORY_MAPPINGS);
266 // Ensure the name isn't already in use.
267 boolean found = false;
268 NodeList mappings = directory_mappings_element.getElementsByTagName(MAPPING);
269 for(int i = 0; !found && i < mappings.getLength(); i++) {
270 Element mapping_element = (Element) mappings.item(i);
271 if(mapping_element.getAttribute(NAME).equalsIgnoreCase(name)) {
272 file = new File(MSMUtils.getValue(mapping_element));
273 directory_mappings_element.removeChild(mapping_element);
274 found = true;
275 }
276 mapping_element = null;
277 }
278 mappings = null;
279 directory_mappings_element = null;
280 document_element = null;
281 saved = false;
282 }
283 catch(Exception error) {
284 Gatherer.printStackTrace(error);
285 }
286 return file;
287 }
288 /** Save this xml document to the given file. */
289 public void save() {
290 Utility.export(document, file);
291 }
292
293 public void setBaseCollection(String base_collection) {
294 set(BASE_COLLECTION, base_collection);
295 }
296
297 /** Set the value of imported to the given value.
298 * @param value The new value for imported, <i>true</i> if the collection has been imported successfully, <i>false</i> otherwise.
299 */
300 public void setImported(boolean value) {
301 set(IMPORTED, value);
302 saved = false;
303 }
304 /** Set the value of saved to the given value.
305 * @param value The new value for saved, <i>true</i> if the collection has been saved recently, <i>false</i> otherwise.
306 */
307 public void setSaved(boolean value) {
308 saved = value;
309 }
310 /** Set the value of title to the given value.
311 * @param title The new <strong>String</strong> title.
312 */
313 public void setTitle(String title) {
314 if(cdm != null) {
315 CollectionMeta collection_name_collectionmeta = cdm.collectionmeta_manager.getMetadatum(StaticStrings.COLLECTIONMETADATA_COLLECTIONNAME_STR);
316 collection_name_collectionmeta.setValue(title);
317 }
318 }
319 /** Method called to return a textual representation of a class, which in this case is the collections title.
320 * @return A <strong>String</strong> containing the collections title.
321 */
322 public String toString() {
323 return getTitle();
324 }
325 /** Get the value of a collection argument. */
326 private boolean get(String name) {
327 boolean result = false;
328 try {
329 Element document_element = document.getDocumentElement();
330 NodeList arguments = document_element.getElementsByTagName(ARGUMENT);
331 boolean found = false;
332 for(int i = 0; !found && i < arguments.getLength(); i++) {
333 Element argument_element = (Element) arguments.item(i);
334 if(argument_element.getParentNode() == document_element) {
335 if(argument_element.getAttribute(NAME).equalsIgnoreCase(name)) {
336 String value = MSMUtils.getValue(argument_element);
337 if(value.equalsIgnoreCase(TRUE)) {
338 result = true;
339 }
340 found = true;
341 value = null;
342 }
343 }
344 argument_element = null;
345 }
346 arguments = null;
347 document_element = null;
348 }
349 catch (Exception error) {
350 Gatherer.printStackTrace(error);
351 }
352 return result;
353 }
354
355 /** Get the value of a collection argument. */
356 private String getString(String name) {
357 String result = "";
358 try {
359 Element document_element = document.getDocumentElement();
360 NodeList arguments = document_element.getElementsByTagName(ARGUMENT);
361 boolean found = false;
362 for(int i = 0; !found && i < arguments.getLength(); i++) {
363 Element argument_element = (Element) arguments.item(i);
364 if(argument_element.getParentNode() == document_element) {
365 if(argument_element.getAttribute(NAME).equalsIgnoreCase(name)) {
366 result = MSMUtils.getValue(argument_element);
367 found = true;
368 }
369 }
370 argument_element = null;
371 }
372 arguments = null;
373 document_element = null;
374 }
375 catch (Exception error) {
376 Gatherer.printStackTrace(error);
377 }
378 return result;
379 }
380
381 /** Method to retrieve the current build options associated with this Collection. */
382 private Element getBuildValues() {
383 Element build_values_element = null;
384 try {
385 Element document_element = document.getDocumentElement();
386 Element build_config_element = (Element) MSMUtils.getNodeFromNamed(document_element, BUILD_CONFIG);
387 build_values_element = (Element) MSMUtils.getNodeFromNamed(build_config_element, BUILD);
388 build_config_element = null;
389 document_element = null;
390 }
391 catch (Exception error) {
392 Gatherer.printStackTrace(error);
393 }
394 return build_values_element;
395 }
396
397 /** Count either documents or folders, depending on the state of the given boolean. */
398 private int getCount(TreeNode node, boolean count_folders, boolean count_files) {
399 int count = 0;
400 File file = ((FileNode)node).getFile();
401 if(file.isFile() && count_files) {
402 count++;
403 }
404 else if(file.isDirectory() && count_folders) {
405 count++;
406 }
407 for(int i = 0; i < node.getChildCount(); i++) {
408 count = count + getCount(node.getChildAt(i), count_folders, count_files);
409 }
410 return count;
411 }
412
413 /** Method to retrieve the current import options associated with this Collection. */
414 public Element getImportValues() {
415 Element import_values_element = null;
416 try {
417 Element document_element = document.getDocumentElement();
418 Element build_config_element = (Element) MSMUtils.getNodeFromNamed(document_element, BUILD_CONFIG);
419 import_values_element = (Element) MSMUtils.getNodeFromNamed(build_config_element, IMPORT);
420 build_config_element = null;
421 document_element = null;
422 }
423 catch (Exception error) {
424 Gatherer.printStackTrace(error);
425 }
426 return import_values_element;
427 }
428
429 /** Set the value of a collection argument. */
430 private void set(String name, boolean value) {
431 set(name, (value ? StaticStrings.TRUE_STR : StaticStrings.FALSE_STR));
432 }
433
434 private void set(String name, String value) {
435 try {
436 Element document_element = document.getDocumentElement();
437 NodeList arguments = document_element.getElementsByTagName(ARGUMENT);
438 boolean found = false;
439 for(int i = 0; !found && i < arguments.getLength(); i++) {
440 Element argument_element = (Element) arguments.item(i);
441 if(argument_element.getParentNode() == document_element) {
442 if(argument_element.getAttribute(NAME).equalsIgnoreCase(name)) {
443 // Strip any current value nodes.
444 while(argument_element.hasChildNodes()) {
445 argument_element.removeChild(argument_element.getFirstChild());
446 }
447 // Append new value
448 argument_element.appendChild(document.createTextNode(value));
449 found = true;
450 }
451 }
452 argument_element = null;
453 }
454 // Append it
455 if(!found) {
456 Element argument_element = document.createElement(ARGUMENT);
457 argument_element.setAttribute(NAME, name);
458 argument_element.appendChild(document.createTextNode(value));
459 document_element.appendChild(argument_element);
460 argument_element = null;
461 }
462 arguments = null;
463 document_element = null;
464 }
465 catch (Exception error) {
466 Gatherer.printStackTrace(error);
467 }
468 }
469}
Note: See TracBrowser for help on using the repository browser.