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

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

Replaced all "Gatherer.config" with "Configuration".

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