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

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

Renamed GDM* classes to MetadataXMLFile*, for our sanity.

  • Property svn:keywords set to Author Date Id Revision
File size: 13.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.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.MetadataXMLFileManager;
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 MetadataXMLFileManager 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 import element. */
88 static final private String IMPORT = "Import";
89 /** The name of the imported attribute. */
90 static final private String IMPORTED = "imported";
91
92 /** Constructor. */
93 public Collection(File collection_xml) {
94 this.file = collection_xml;
95 // Try to load this collections details.
96 document = Utility.parse(collection_xml, false);
97 // If that fails load the default settings for a collection.
98 if(document == null) {
99 document = Utility.parse(COLLECTION_XML_TEMPLATE, true);
100 }
101 // Point the Configuration class at our gatherer config arguments.
102 Gatherer.config.setCollectionConfiguration(document);
103 // Finally create all of the child managers that are directly dependant on a collection
104 build_options = new BuildOptions(getBuildValues(), getImportValues());
105 }
106
107 /** Destructor.
108 * @see org.greenstone.gatherer.Configuration
109 * @see org.greenstone.gatherer.Gatherer
110 * @see org.greenstone.gatherer.cdm.CollectionDesignManager
111 * @see org.greenstone.gatherer.msm.MetadataXMLFileManager
112 * @see org.greenstone.gatherer.msm.MetadataSetManager
113 */
114 public void destroy() {
115 cdm.destroy();
116 gdm.destroy();
117 msm.destroy();
118 Gatherer.config.setCollectionConfiguration(null);
119 cdm = null;
120 document = null;
121 gdm = null;
122 msm = 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.getRecordSet().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.getRecordSet().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 public MetadataXMLFileManager getGDM() {
165 return gdm;
166 }
167
168 /** Retrieve the short name for this collection.
169 * @return The name as a <strong>String</strong>.
170 */
171 public String getName() {
172 return file.getParentFile().getName();
173 }
174
175 /** Determine if this collection has been saved since the last major change.
176 * @return <i>true</i> if it has been saved recently, <i>false</i> otherwise.
177 */
178 public boolean getSaved() {
179 return saved;
180 }
181
182 /** Retrieve the title of this collection.
183 * @return The title as a <strong>String</strong>.
184 */
185 public String getTitle() {
186 if(cdm == null) {
187 return StaticStrings.EMPTY_STR;
188 }
189 CollectionMeta collection_name_collectionmeta = cdm.collectionmeta_manager.getMetadatum(StaticStrings.COLLECTIONMETADATA_COLLECTIONNAME_STR);
190 return collection_name_collectionmeta.getValue(CollectionMeta.TEXT);
191 }
192
193 /** Save this xml document to the given file. */
194 public void save() {
195 Utility.export(document, file);
196 }
197
198 public void setBaseCollection(String base_collection) {
199 set(BASE_COLLECTION, base_collection);
200 }
201
202 /** Set the value of imported to the given value.
203 * @param value The new value for imported, <i>true</i> if the collection has been imported successfully, <i>false</i> otherwise.
204 */
205 public void setImported(boolean value) {
206 set(IMPORTED, value);
207 saved = false;
208 }
209
210 /** Set the value of saved to the given value.
211 * @param value The new value for saved, <i>true</i> if the collection has been saved recently, <i>false</i> otherwise.
212 */
213 public void setSaved(boolean value) {
214 saved = value;
215 }
216
217 /** Set the value of title to the given value.
218 * @param title The new <strong>String</strong> title.
219 */
220 public void setTitle(String title) {
221 if(cdm != null) {
222 CollectionMeta collection_name_collectionmeta = cdm.collectionmeta_manager.getMetadatum(StaticStrings.COLLECTIONMETADATA_COLLECTIONNAME_STR);
223 collection_name_collectionmeta.setValue(title);
224 }
225 }
226
227 /** Method called to return a textual representation of a class, which in this case is the collections title.
228 * @return A <strong>String</strong> containing the collections title.
229 */
230 public String toString() {
231 return getTitle();
232 }
233
234 /** Get the value of a collection argument. */
235 private boolean get(String name) {
236 boolean result = false;
237 try {
238 Element document_element = document.getDocumentElement();
239 NodeList arguments = document_element.getElementsByTagName(ARGUMENT);
240 boolean found = false;
241 for(int i = 0; !found && i < arguments.getLength(); i++) {
242 Element argument_element = (Element) arguments.item(i);
243 if(argument_element.getParentNode() == document_element) {
244 if(argument_element.getAttribute(StaticStrings.NAME_ATTRIBUTE).equalsIgnoreCase(name)) {
245 String value = MSMUtils.getValue(argument_element);
246 if(value.equalsIgnoreCase(StaticStrings.TRUE_STR)) {
247 result = true;
248 }
249 found = true;
250 value = null;
251 }
252 }
253 argument_element = null;
254 }
255 arguments = null;
256 document_element = null;
257 }
258 catch (Exception error) {
259 Gatherer.printStackTrace(error);
260 }
261 return result;
262 }
263
264 /** Get the value of a collection argument. */
265 private String getString(String name) {
266 String result = "";
267 try {
268 Element document_element = document.getDocumentElement();
269 NodeList arguments = document_element.getElementsByTagName(ARGUMENT);
270 boolean found = false;
271 for(int i = 0; !found && i < arguments.getLength(); i++) {
272 Element argument_element = (Element) arguments.item(i);
273 if(argument_element.getParentNode() == document_element) {
274 if(argument_element.getAttribute(StaticStrings.NAME_ATTRIBUTE).equalsIgnoreCase(name)) {
275 result = MSMUtils.getValue(argument_element);
276 found = true;
277 }
278 }
279 argument_element = null;
280 }
281 arguments = null;
282 document_element = null;
283 }
284 catch (Exception error) {
285 Gatherer.printStackTrace(error);
286 }
287 return result;
288 }
289
290 /** Method to retrieve the current build options associated with this Collection. */
291 private Element getBuildValues() {
292 Element build_values_element = null;
293 try {
294 Element document_element = document.getDocumentElement();
295 Element build_config_element = (Element) MSMUtils.getNodeFromNamed(document_element, BUILD_CONFIG);
296 build_values_element = (Element) MSMUtils.getNodeFromNamed(build_config_element, BUILD);
297 build_config_element = null;
298 document_element = null;
299 }
300 catch (Exception error) {
301 Gatherer.printStackTrace(error);
302 }
303 return build_values_element;
304 }
305
306 /** Count either documents or folders, depending on the state of the given boolean. */
307 private int getCount(TreeNode node, boolean count_folders, boolean count_files) {
308 int count = 0;
309 File file = ((FileNode)node).getFile();
310 if(file.isFile() && count_files) {
311 count++;
312 }
313 else if(file.isDirectory() && count_folders) {
314 count++;
315 }
316 for(int i = 0; !file.getName().equals("CVS") && i < node.getChildCount(); i++) {
317 count = count + getCount(node.getChildAt(i), count_folders, count_files);
318 }
319 return count;
320 }
321
322 /** Method to retrieve the current import options associated with this Collection. */
323 public Element getImportValues() {
324 Element import_values_element = null;
325 try {
326 Element document_element = document.getDocumentElement();
327 Element build_config_element = (Element) MSMUtils.getNodeFromNamed(document_element, BUILD_CONFIG);
328 import_values_element = (Element) MSMUtils.getNodeFromNamed(build_config_element, IMPORT);
329 build_config_element = null;
330 document_element = null;
331 }
332 catch (Exception error) {
333 Gatherer.printStackTrace(error);
334 }
335 return import_values_element;
336 }
337
338 /** Set the value of a collection argument. */
339 private void set(String name, boolean value) {
340 set(name, (value ? StaticStrings.TRUE_STR : StaticStrings.FALSE_STR));
341 }
342
343 private void set(String name, String value) {
344 try {
345 Element document_element = document.getDocumentElement();
346 NodeList arguments = document_element.getElementsByTagName(ARGUMENT);
347 boolean found = false;
348 for(int i = 0; !found && i < arguments.getLength(); i++) {
349 Element argument_element = (Element) arguments.item(i);
350 if(argument_element.getParentNode() == document_element) {
351 if(argument_element.getAttribute(StaticStrings.NAME_ATTRIBUTE).equalsIgnoreCase(name)) {
352 // Strip any current value nodes.
353 while(argument_element.hasChildNodes()) {
354 argument_element.removeChild(argument_element.getFirstChild());
355 }
356 // Append new value
357 argument_element.appendChild(document.createTextNode(value));
358 found = true;
359 }
360 }
361 argument_element = null;
362 }
363 // Append it
364 if(!found) {
365 Element argument_element = document.createElement(ARGUMENT);
366 argument_element.setAttribute(StaticStrings.NAME_ATTRIBUTE, name);
367 argument_element.appendChild(document.createTextNode(value));
368 document_element.appendChild(argument_element);
369 argument_element = null;
370 }
371 arguments = null;
372 document_element = null;
373 }
374 catch (Exception error) {
375 Gatherer.printStackTrace(error);
376 }
377 }
378}
Note: See TracBrowser for help on using the repository browser.