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

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

Added a pointless method getGDM() in a fit of object-orientedness. Why couldn't I just leave gdm public?

  • Property svn:keywords set to Author Date Id Revision
File size: 13.2 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 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.collection.CollectionModel */
109 public void destroy() {
110 cdm.destroy();
111 gdm.destroy();
112 msm.destroy();
113 Gatherer.config.setCollectionConfiguration(null);
114 cdm = null;
115 document = null;
116 gdm = null;
117 msm = null;
118 }
119
120 /** Determine the path to the base collection.
121 * @return the path as a String
122 */
123 public String getBaseCollection() {
124 return getString(BASE_COLLECTION);
125 }
126
127 /** Determine the number of documents and folders in this collection. */
128 public int getCount() {
129 return getCount((TreeNode)Gatherer.c_man.getRecordSet().getRoot(), true, true);
130 }
131
132 /** Calculates the number of documents in this collection. */
133 public int getDocumentCount() {
134 return getCount((TreeNode)Gatherer.c_man.getRecordSet().getRoot(), false, true);
135 }
136
137 /** Retrieve the description of this collection.
138 * @return a String
139 */
140 public String getDescription() {
141 if(cdm == null) {
142 return StaticStrings.EMPTY_STR;
143 }
144 CollectionMeta collection_extra_collectionmeta = cdm.collectionmeta_manager.getMetadatum(StaticStrings.COLLECTIONMETADATA_COLLECTIONEXTRA_STR);
145 return collection_extra_collectionmeta.getValue(CollectionMeta.TEXT);
146 }
147
148 /** Retrieve the authors email for this collection.
149 * @return The email as a <strong>String</strong>.
150 */
151 public String getEmail() {
152 if(cdm == null) {
153 return StaticStrings.EMPTY_STR;
154 }
155 CollectionMeta creator_collectionmeta = new CollectionMeta(CollectionDesignManager.collect_config.getCreator());
156 return creator_collectionmeta.getValue(CollectionMeta.TEXT);
157 }
158
159 public GDMManager getGDM() {
160 return gdm;
161 }
162
163 /** Retrieve the short name for this collection.
164 * @return The name as a <strong>String</strong>.
165 */
166 public String getName() {
167 return file.getParentFile().getName();
168 }
169
170 /** Determine if this collection has been saved since the last major change.
171 * @return <i>true</i> if it has been saved recently, <i>false</i> otherwise.
172 */
173 public boolean getSaved() {
174 return saved;
175 }
176
177 /** Retrieve the title of this collection.
178 * @return The title as a <strong>String</strong>.
179 */
180 public String getTitle() {
181 if(cdm == null) {
182 return StaticStrings.EMPTY_STR;
183 }
184 CollectionMeta collection_name_collectionmeta = cdm.collectionmeta_manager.getMetadatum(StaticStrings.COLLECTIONMETADATA_COLLECTIONNAME_STR);
185 return collection_name_collectionmeta.getValue(CollectionMeta.TEXT);
186 }
187
188 /** Save this xml document to the given file. */
189 public void save() {
190 Utility.export(document, file);
191 }
192
193 public void setBaseCollection(String base_collection) {
194 set(BASE_COLLECTION, base_collection);
195 }
196
197 /** Set the value of imported to the given value.
198 * @param value The new value for imported, <i>true</i> if the collection has been imported successfully, <i>false</i> otherwise.
199 */
200 public void setImported(boolean value) {
201 set(IMPORTED, value);
202 saved = false;
203 }
204
205 /** Set the value of saved to the given value.
206 * @param value The new value for saved, <i>true</i> if the collection has been saved recently, <i>false</i> otherwise.
207 */
208 public void setSaved(boolean value) {
209 saved = value;
210 }
211
212 /** Set the value of title to the given value.
213 * @param title The new <strong>String</strong> title.
214 */
215 public void setTitle(String title) {
216 if(cdm != null) {
217 CollectionMeta collection_name_collectionmeta = cdm.collectionmeta_manager.getMetadatum(StaticStrings.COLLECTIONMETADATA_COLLECTIONNAME_STR);
218 collection_name_collectionmeta.setValue(title);
219 }
220 }
221
222 /** Method called to return a textual representation of a class, which in this case is the collections title.
223 * @return A <strong>String</strong> containing the collections title.
224 */
225 public String toString() {
226 return getTitle();
227 }
228
229 /** Get the value of a collection argument. */
230 private boolean get(String name) {
231 boolean result = false;
232 try {
233 Element document_element = document.getDocumentElement();
234 NodeList arguments = document_element.getElementsByTagName(ARGUMENT);
235 boolean found = false;
236 for(int i = 0; !found && i < arguments.getLength(); i++) {
237 Element argument_element = (Element) arguments.item(i);
238 if(argument_element.getParentNode() == document_element) {
239 if(argument_element.getAttribute(StaticStrings.NAME_ATTRIBUTE).equalsIgnoreCase(name)) {
240 String value = MSMUtils.getValue(argument_element);
241 if(value.equalsIgnoreCase(StaticStrings.TRUE_STR)) {
242 result = true;
243 }
244 found = true;
245 value = null;
246 }
247 }
248 argument_element = null;
249 }
250 arguments = null;
251 document_element = null;
252 }
253 catch (Exception error) {
254 Gatherer.printStackTrace(error);
255 }
256 return result;
257 }
258
259 /** Get the value of a collection argument. */
260 private String getString(String name) {
261 String result = "";
262 try {
263 Element document_element = document.getDocumentElement();
264 NodeList arguments = document_element.getElementsByTagName(ARGUMENT);
265 boolean found = false;
266 for(int i = 0; !found && i < arguments.getLength(); i++) {
267 Element argument_element = (Element) arguments.item(i);
268 if(argument_element.getParentNode() == document_element) {
269 if(argument_element.getAttribute(StaticStrings.NAME_ATTRIBUTE).equalsIgnoreCase(name)) {
270 result = MSMUtils.getValue(argument_element);
271 found = true;
272 }
273 }
274 argument_element = null;
275 }
276 arguments = null;
277 document_element = null;
278 }
279 catch (Exception error) {
280 Gatherer.printStackTrace(error);
281 }
282 return result;
283 }
284
285 /** Method to retrieve the current build options associated with this Collection. */
286 private Element getBuildValues() {
287 Element build_values_element = null;
288 try {
289 Element document_element = document.getDocumentElement();
290 Element build_config_element = (Element) MSMUtils.getNodeFromNamed(document_element, BUILD_CONFIG);
291 build_values_element = (Element) MSMUtils.getNodeFromNamed(build_config_element, BUILD);
292 build_config_element = null;
293 document_element = null;
294 }
295 catch (Exception error) {
296 Gatherer.printStackTrace(error);
297 }
298 return build_values_element;
299 }
300
301 /** Count either documents or folders, depending on the state of the given boolean. */
302 private int getCount(TreeNode node, boolean count_folders, boolean count_files) {
303 int count = 0;
304 File file = ((FileNode)node).getFile();
305 if(file.isFile() && count_files) {
306 count++;
307 }
308 else if(file.isDirectory() && count_folders) {
309 count++;
310 }
311 for(int i = 0; i < node.getChildCount(); i++) {
312 count = count + getCount(node.getChildAt(i), count_folders, count_files);
313 }
314 return count;
315 }
316
317 /** Method to retrieve the current import options associated with this Collection. */
318 public Element getImportValues() {
319 Element import_values_element = null;
320 try {
321 Element document_element = document.getDocumentElement();
322 Element build_config_element = (Element) MSMUtils.getNodeFromNamed(document_element, BUILD_CONFIG);
323 import_values_element = (Element) MSMUtils.getNodeFromNamed(build_config_element, IMPORT);
324 build_config_element = null;
325 document_element = null;
326 }
327 catch (Exception error) {
328 Gatherer.printStackTrace(error);
329 }
330 return import_values_element;
331 }
332
333 /** Set the value of a collection argument. */
334 private void set(String name, boolean value) {
335 set(name, (value ? StaticStrings.TRUE_STR : StaticStrings.FALSE_STR));
336 }
337
338 private void set(String name, String value) {
339 try {
340 Element document_element = document.getDocumentElement();
341 NodeList arguments = document_element.getElementsByTagName(ARGUMENT);
342 boolean found = false;
343 for(int i = 0; !found && i < arguments.getLength(); i++) {
344 Element argument_element = (Element) arguments.item(i);
345 if(argument_element.getParentNode() == document_element) {
346 if(argument_element.getAttribute(StaticStrings.NAME_ATTRIBUTE).equalsIgnoreCase(name)) {
347 // Strip any current value nodes.
348 while(argument_element.hasChildNodes()) {
349 argument_element.removeChild(argument_element.getFirstChild());
350 }
351 // Append new value
352 argument_element.appendChild(document.createTextNode(value));
353 found = true;
354 }
355 }
356 argument_element = null;
357 }
358 // Append it
359 if(!found) {
360 Element argument_element = document.createElement(ARGUMENT);
361 argument_element.setAttribute(StaticStrings.NAME_ATTRIBUTE, name);
362 argument_element.appendChild(document.createTextNode(value));
363 document_element.appendChild(argument_element);
364 argument_element = null;
365 }
366 arguments = null;
367 document_element = null;
368 }
369 catch (Exception error) {
370 Gatherer.printStackTrace(error);
371 }
372 }
373}
Note: See TracBrowser for help on using the repository browser.