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

Last change on this file since 5237 was 5237, checked in by jmt12, 21 years ago

Fix 203B142. Collections that are built outside of GLI will now be recognized as previewable

  • Property svn:keywords set to Author Date Id Revision
File size: 16.3 KB
Line 
1package org.greenstone.gatherer.collection;
2/**
3 *#########################################################################
4 *
5 * A component of the Gatherer application, part of the Greenstone digital
6 * library suite from the New Zealand Digital Library Project at the
7 * University of Waikato, New Zealand.
8 *
9 * <BR><BR>
10 *
11 * Author: John Thompson, Greenstone Digital Library, University of Waikato
12 *
13 * <BR><BR>
14 *
15 * Copyright (C) 1999 New Zealand Digital Library Project
16 *
17 * <BR><BR>
18 *
19 * This program is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation; either version 2 of the License, or
22 * (at your option) any later version.
23 *
24 * <BR><BR>
25 *
26 * This program is distributed in the hope that it will be useful,
27 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 * GNU General Public License for more details.
30 *
31 * <BR><BR>
32 *
33 * You should have received a copy of the GNU General Public License
34 * along with this program; if not, write to the Free Software
35 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
36 *########################################################################
37 */
38import java.io.*;
39import java.util.*;
40import javax.swing.*;
41import javax.swing.tree.*;
42import org.greenstone.gatherer.Gatherer;
43import org.greenstone.gatherer.cdm.CollectionDesignManager;
44import org.greenstone.gatherer.collection.BuildOptions;
45import org.greenstone.gatherer.collection.CollectionManager;
46import org.greenstone.gatherer.file.FileNode;
47import org.greenstone.gatherer.msm.ElementWrapper;
48import org.greenstone.gatherer.msm.GDMManager;
49import org.greenstone.gatherer.msm.MetadataSetManager;
50import org.greenstone.gatherer.msm.MSMUtils;
51import org.greenstone.gatherer.util.Utility;
52import org.w3c.dom.*;
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 BuildOptions. */
59 public BuildOptions build_options;
60 /** A reference to the Collection Design Manager. */
61 public CollectionDesignManager cdm;
62 /** A reference to the Greenstone Directory Metadata Manager. */
63 public GDMManager gdm;
64 /** A reference to the Metadata Set Manager. */
65 public MetadataSetManager msm;
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 collectio configuration file for this collection. */
69 private CollectionConfiguration collect_cfg;
70 /** The document around which this collection class is based. */
71 private Document document;
72 /** The file the collection is in (the file may not actually exist, such in the case of a legacy collection)! */
73 private File file;
74 /** The name of the argument element. */
75 static final private String ARGUMENT = "Argument";
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 directory mappings element. */
83 static final private String DIRECTORY_MAPPINGS = "DirectoryMappings";
84 /** The name of the file attribute. */
85 static final private String FILE = "file";
86 /** The name of the import element. */
87 static final private String IMPORT = "Import";
88 /** The name of the imported attribute. */
89 static final private String IMPORTED = "imported";
90 /** The name of the mapping element. */
91 static final private String MAPPING = "Mapping";
92 /** The name of the name attribute. */
93 static final private String NAME = "name";
94 /** The name of the true value. */
95 static final private String TRUE = "true";
96 /** Constructor. */
97 public Collection(File collection_xml) {
98 this.file = collection_xml;
99 // Try to load this collections details.
100 document = Utility.parse(collection_xml, false);
101 // If that fails load the default settings for a collection.
102 if(document == null) {
103 document = Utility.parse(COLLECTION_XML_TEMPLATE, true);
104 }
105 // Point the Configuration class at our gatherer config arguments.
106 Gatherer.config.setCollectionConfiguration(document);
107 // We also attempt to parse the collection configuration file.
108 collect_cfg = new CollectionConfiguration(new File(collection_xml.getParentFile(), Utility.CONFIG_DIR));
109 // Finally create all of the child managers that are directly dependant on a collection
110 build_options = new BuildOptions(getBuildValues(), getImportValues());
111 }
112 /** Add a special directory mapping. */
113 public boolean addDirectoryMapping(String name, File file) {
114 boolean result = false;
115 try {
116 Element document_element = document.getDocumentElement();
117 Element directory_mappings_element = (Element) MSMUtils.getNodeFromNamed(document_element, DIRECTORY_MAPPINGS);
118 // Ensure the name isn't already in use.
119 boolean found = false;
120 NodeList mappings = directory_mappings_element.getElementsByTagName(MAPPING);
121 for(int i = 0; !found && i < mappings.getLength(); i++) {
122 Element mapping_element = (Element) mappings.item(i);
123 if(mapping_element.getAttribute(NAME).equalsIgnoreCase(name)) {
124 found = true;
125 }
126 mapping_element = null;
127 }
128 // Otherwise add the mapping.
129 if(!found) {
130 Element mapping_element = document.createElement(MAPPING);
131 mapping_element.setAttribute(NAME, name);
132 mapping_element.setAttribute(FILE, file.toString());
133 directory_mappings_element.appendChild(mapping_element);
134 result = true;
135 mapping_element = null;
136 }
137 mappings = null;
138 directory_mappings_element = null;
139 document_element = null;
140 saved = false;
141 }
142 catch (Exception error) {
143 Gatherer.printStackTrace(error);
144 }
145 return result;
146 }
147
148 /** Destructor.
149 * @see org.greenstone.gatherer.collection.CollectionModel */
150 public void destroy() {
151 cdm.destroy();
152 gdm.destroy();
153 msm.destroy();
154 Gatherer.config.setCollectionConfiguration(null);
155 cdm = null;
156 document = null;
157 gdm = null;
158 msm = null;
159 }
160
161 /** Determine the number of documents and folders in this collection. */
162 public int getCount() {
163 return getCount((TreeNode)Gatherer.c_man.getRecordSet().getRoot(), true, true);
164 }
165
166 /** Calculates the number of documents in this collection. */
167 public int getDocumentCount() {
168 return getCount((TreeNode)Gatherer.c_man.getRecordSet().getRoot(), false, true);
169 }
170 /** Retrieve the description of this collection.
171 * @return The description as a <strong>String</strong>.
172 */
173 public String getDescription() {
174 return collect_cfg.getDescription();
175 }
176 /** Retrieve a specific directory mapping associated with this collection.
177 * @param name The name of the mapping to retrieve as a <strong>String</strong>.
178 * @return The <strong>File</strong> this name maps to, or <i>null</i> if no such mapping.
179 */
180 public File getDirectoryMapping(String name) {
181 File result = null;
182 try {
183 Element document_element = document.getDocumentElement();
184 Element directory_mappings_element = (Element) MSMUtils.getNodeFromNamed(document_element, DIRECTORY_MAPPINGS);
185 // Ensure the name isn't already in use.
186 boolean found = false;
187 NodeList mappings = directory_mappings_element.getElementsByTagName(MAPPING);
188 for(int i = 0; !found && i < mappings.getLength(); i++) {
189 Element mapping_element = (Element) mappings.item(i);
190 if(mapping_element.getAttribute(NAME).equalsIgnoreCase(name)) {
191 result = new File(MSMUtils.getValue(mapping_element));
192 found = true;
193 }
194 mapping_element = null;
195 }
196 mappings = null;
197 directory_mappings_element = null;
198 document_element = null;
199 }
200 catch(Exception error) {
201 Gatherer.printStackTrace(error);
202 }
203 return result;
204 }
205 /** Retrieve the special directory mappings associated with this collection.
206 * @return A <strong>HashMap</strong> containing mappings from names to directories.
207 */
208 public HashMap getDirectoryMappings() {
209 HashMap special_directories = new HashMap();
210 try {
211 Element document_element = document.getDocumentElement();
212 Element directory_mappings_element = (Element) MSMUtils.getNodeFromNamed(document_element, DIRECTORY_MAPPINGS);
213 // Ensure the name isn't already in use.
214 boolean found = false;
215 NodeList mappings = directory_mappings_element.getElementsByTagName(MAPPING);
216 for(int i = 0; !found && i < mappings.getLength(); i++) {
217 Element mapping_element = (Element) mappings.item(i);
218 String name = mapping_element.getAttribute(NAME);
219 File file = new File(mapping_element.getAttribute(FILE));
220 special_directories.put(name, file);
221 file = null;
222 name = null;
223 mapping_element = null;
224 }
225 mappings = null;
226 directory_mappings_element = null;
227 document_element = null;
228 }
229 catch(Exception error) {
230 Gatherer.printStackTrace(error);
231 }
232 return special_directories;
233 }
234 /** Retrieve the authors email for this collection.
235 * @return The email as a <strong>String</strong>.
236 */
237 public String getEmail() {
238 return collect_cfg.getCreator();
239 }
240 /** Counts the number of folders used in the current record set. */
241 public int getFolderCount() {
242 return getCount((TreeNode)Gatherer.c_man.getRecordSet().getRoot(), true, false);
243 }
244 /** Determine if this collection has had an import action run upon it since the last major change.
245 * @return <i>true</i> if an import has occured, <i>false</i> otherwise.
246 */
247 public boolean getImported() {
248 return get(IMPORTED);
249 }
250 /** Retrieve the short name for this collection.
251 * @return The name as a <strong>String</strong>.
252 */
253 public String getName() {
254 return file.getParentFile().getName();
255 }
256 /** Determine if this collection has been saved since the last major change.
257 * @return <i>true</i> if it has been saved recently, <i>false</i> otherwise.
258 */
259 public boolean getSaved() {
260 return saved;
261 }
262 /** Retrieve the title of this collection.
263 * @return The title as a <strong>String</strong>.
264 */
265 public String getTitle() {
266 return collect_cfg.getName();
267 }
268 /** Remove a previously defined special directory mapping.
269 * @param name The name of the mapping to remove as a <strong>String</strong>.
270 * @return The <strong>File</strong> of the mapping removed.
271 */
272 public File removeDirectoryMapping(String name) {
273 File file = null;
274 try {
275 Element document_element = document.getDocumentElement();
276 Element directory_mappings_element = (Element) MSMUtils.getNodeFromNamed(document_element, DIRECTORY_MAPPINGS);
277 // Ensure the name isn't already in use.
278 boolean found = false;
279 NodeList mappings = directory_mappings_element.getElementsByTagName(MAPPING);
280 for(int i = 0; !found && i < mappings.getLength(); i++) {
281 Element mapping_element = (Element) mappings.item(i);
282 if(mapping_element.getAttribute(NAME).equalsIgnoreCase(name)) {
283 file = new File(MSMUtils.getValue(mapping_element));
284 directory_mappings_element.removeChild(mapping_element);
285 found = true;
286 }
287 mapping_element = null;
288 }
289 mappings = null;
290 directory_mappings_element = null;
291 document_element = null;
292 saved = false;
293 }
294 catch(Exception error) {
295 Gatherer.printStackTrace(error);
296 }
297 return file;
298 }
299 /** Save this xml document to the given file. */
300 public void save() {
301 Utility.export(document, file);
302 }
303
304 /** Set the value of imported to the given value.
305 * @param value The new value for imported, <i>true</i> if the collection has been imported successfully, <i>false</i> otherwise.
306 */
307 public void setImported(boolean value) {
308 set(IMPORTED, value);
309 saved = false;
310 }
311 /** Set the value of saved to the given value.
312 * @param value The new value for saved, <i>true</i> if the collection has been saved recently, <i>false</i> otherwise.
313 */
314 public void setSaved(boolean value) {
315 saved = value;
316 }
317 /** Set the value of title to the given value.
318 * @param title The new <strong>String</strong> title.
319 */
320 public void setTitle(String title) {
321 collect_cfg.setName(title);
322 }
323 /** Method called to return a textual representation of a class, which in this case is the collections title.
324 * @return A <strong>String</strong> containing the collections title.
325 */
326 public String toString() {
327 return collect_cfg.getName();
328 }
329 /** Get the value of a collection argument. */
330 private boolean get(String name) {
331 boolean result = false;
332 try {
333 Element document_element = document.getDocumentElement();
334 NodeList arguments = document_element.getElementsByTagName(ARGUMENT);
335 boolean found = false;
336 for(int i = 0; !found && i < arguments.getLength(); i++) {
337 Element argument_element = (Element) arguments.item(i);
338 if(argument_element.getParentNode() == document_element) {
339 if(argument_element.getAttribute(NAME).equalsIgnoreCase(name)) {
340 String value = MSMUtils.getValue(argument_element);
341 if(value.equalsIgnoreCase(TRUE)) {
342 result = true;
343 }
344 found = true;
345 value = null;
346 }
347 }
348 argument_element = null;
349 }
350 arguments = null;
351 document_element = null;
352 }
353 catch (Exception error) {
354 Gatherer.printStackTrace(error);
355 }
356 return result;
357 }
358
359 /** Method to retrieve the current build options associated with this Collection. */
360 private Element getBuildValues() {
361 Element build_values_element = null;
362 try {
363 Element document_element = document.getDocumentElement();
364 Element build_config_element = (Element) MSMUtils.getNodeFromNamed(document_element, BUILD_CONFIG);
365 build_values_element = (Element) MSMUtils.getNodeFromNamed(build_config_element, BUILD);
366 build_config_element = null;
367 document_element = null;
368 }
369 catch (Exception error) {
370 Gatherer.printStackTrace(error);
371 }
372 return build_values_element;
373 }
374
375 /** Count either documents or folders, depending on the state of the given boolean. */
376 private int getCount(TreeNode node, boolean count_folders, boolean count_files) {
377 int count = 0;
378 File file = ((FileNode)node).getFile();
379 if(file.isFile() && count_files) {
380 count++;
381 }
382 else if(file.isDirectory() && count_folders) {
383 count++;
384 }
385 for(int i = 0; i < node.getChildCount(); i++) {
386 count = count + getCount(node.getChildAt(i), count_folders, count_files);
387 }
388 return count;
389 }
390
391 /** Method to retrieve the current import options associated with this Collection. */
392 public Element getImportValues() {
393 Element import_values_element = null;
394 try {
395 Element document_element = document.getDocumentElement();
396 Element build_config_element = (Element) MSMUtils.getNodeFromNamed(document_element, BUILD_CONFIG);
397 import_values_element = (Element) MSMUtils.getNodeFromNamed(build_config_element, IMPORT);
398 build_config_element = null;
399 document_element = null;
400 }
401 catch (Exception error) {
402 Gatherer.printStackTrace(error);
403 }
404 return import_values_element;
405 }
406
407 /** Set the value of a collection argument. */
408 private void set(String name, boolean value) {
409 try {
410 Element document_element = document.getDocumentElement();
411 NodeList arguments = document_element.getElementsByTagName(ARGUMENT);
412 boolean found = false;
413 for(int i = 0; !found && i < arguments.getLength(); i++) {
414 Element argument_element = (Element) arguments.item(i);
415 if(argument_element.getParentNode() == document_element) {
416 if(argument_element.getAttribute(NAME).equalsIgnoreCase(name)) {
417 // Strip any current value nodes.
418 while(argument_element.hasChildNodes()) {
419 argument_element.removeChild(argument_element.getFirstChild());
420 }
421 // Append new value
422 argument_element.appendChild(document.createTextNode(value ? "true" : "false"));
423 }
424 }
425 argument_element = null;
426 }
427 arguments = null;
428 document_element = null;
429 }
430 catch (Exception error) {
431 Gatherer.printStackTrace(error);
432 }
433 }
434}
Note: See TracBrowser for help on using the repository browser.