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

Last change on this file since 13819 was 13819, checked in by shaoqun, 17 years ago

added a skimfile switch in collection's .col file

  • Property svn:keywords set to Author Date Id Revision
File size: 15.2 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;
[8236]44import org.greenstone.gatherer.DebugStream;
[5164]45import org.greenstone.gatherer.Gatherer;
46import org.greenstone.gatherer.cdm.CollectionDesignManager;
[6051]47import org.greenstone.gatherer.cdm.CollectionMeta;
48import org.greenstone.gatherer.cdm.CollectionMetaManager;
[5721]49import org.greenstone.gatherer.util.StaticStrings;
[8015]50import org.greenstone.gatherer.util.XMLTools;
[5164]51import org.w3c.dom.*;
[5581]52
[5164]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 {
[6086]58 /** A reference to the Collection Design Manager. */
59 public CollectionDesignManager cdm;
[9136]60 /** A reference to the build ScriptOptions. */
61 public ScriptOptions build_options;
62 /** A reference to the import ScriptOptions. */
63 public ScriptOptions import_options;
[7281]64 /** true if an error has occurred during construction */
65 public boolean error = false;
[6086]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;
[9166]68 /*<i>true</i> if the currently loaded collection has had files added since its last build */
[10237]69 private boolean filesChanged = false;
[9166]70 /*<i>true</i> if the currently loaded collection has had metadata added since its last build */
[10237]71 private boolean metadataChanged = false;
[6086]72 /** The document around which this collection class is based. */
73 private Document document;
[13819]74
75 /** The file the collection is in (the file may not actually exist, such in the case of a legacy collection)! */
76 private File file;
[6086]77 /** The name of the argument element. */
78 static final private String ARGUMENT = "Argument";
79 static final private String BASE_COLLECTION = "base_collection";
80 /** The name of the build element. */
81 static final private String BUILD = "Build";
82 /** The name of the build config element. */
83 static final private String BUILD_CONFIG = "BuildConfig";
84 /** The name of the collection xml template. */
85 static final private String COLLECTION_XML_TEMPLATE = "xml/template.col";
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";
[13819]90 /** The name of the imported attribute. */
91 static final private String SKIM_FILE = "skimfile";
92
[6051]93
[6086]94 /** Constructor. */
95 public Collection(File collection_xml) {
96 this.file = collection_xml;
97 // Try to load this collections details.
[13819]98 document = XMLTools.parseXMLFile(collection_xml);
[6086]99 // If that fails load the default settings for a collection.
100 if(document == null) {
[10006]101 document = XMLTools.parseXMLFile(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);
[8253]109 if (Gatherer.g_man != null) {
110 Gatherer.g_man.updateUI();
111 }
[6086]112 // Finally create all of the child managers that are directly dependant on a collection
[13748]113 build_options = new ScriptOptions(getBuildValues(), "buildcol.pl");
114 import_options = new ScriptOptions(getImportValues(), "import.pl");
[6086]115 }
116
117 /** Destructor.
[6387]118 * @see org.greenstone.gatherer.Configuration
119 * @see org.greenstone.gatherer.Gatherer
120 * @see org.greenstone.gatherer.cdm.CollectionDesignManager
121 */
[6086]122 public void destroy() {
[5164]123 cdm.destroy();
[8231]124 Configuration.setCollectionConfiguration(null);
[12589]125 // this is called after calling collection.destroy, so we shouldn't need it here too.
126// if (Gatherer.g_man != null) {
127// Gatherer.g_man.updateUI();
128// }
[5164]129 cdm = null;
130 document = null;
[6086]131 }
132
133 /** Determine the number of documents and folders in this collection. */
134 public int getCount() {
[8846]135 return getCount((TreeNode)Gatherer.c_man.getCollectionTreeModel().getRoot(), true, true);
[6086]136 }
137
138 /** Calculates the number of documents in this collection. */
139 public int getDocumentCount() {
[8846]140 return getCount((TreeNode)Gatherer.c_man.getCollectionTreeModel().getRoot(), false, true);
[6086]141 }
142
143 /** Retrieve the description of this collection.
144 * @return a String
145 */
146 public String getDescription() {
147 if(cdm == null) {
148 return StaticStrings.EMPTY_STR;
[6051]149 }
[6086]150 CollectionMeta collection_extra_collectionmeta = cdm.collectionmeta_manager.getMetadatum(StaticStrings.COLLECTIONMETADATA_COLLECTIONEXTRA_STR);
151 return collection_extra_collectionmeta.getValue(CollectionMeta.TEXT);
152 }
153
154 /** Retrieve the authors email for this collection.
155 * @return The email as a <strong>String</strong>.
156 */
157 public String getEmail() {
158 if(cdm == null) {
159 return StaticStrings.EMPTY_STR;
[6051]160 }
[6086]161 CollectionMeta creator_collectionmeta = new CollectionMeta(CollectionDesignManager.collect_config.getCreator());
162 return creator_collectionmeta.getValue(CollectionMeta.TEXT);
163 }
[6204]164
[6086]165 /** Retrieve the short name for this collection.
166 * @return The name as a <strong>String</strong>.
167 */
168 public String getName() {
169 return file.getParentFile().getName();
170 }
171
172 /** Determine if this collection has been saved since the last major change.
173 * @return <i>true</i> if it has been saved recently, <i>false</i> otherwise.
174 */
175 public boolean getSaved() {
176 return saved;
177 }
[9166]178
179 /** Determine if this collection has had its collection files changed since the last build.
180 * @return <i>true</i> if they have been saved, <i>false</i> otherwise.
181 */
182 public boolean getFilesChanged() {
183 return filesChanged;
184 }
[6086]185
[9166]186 /** Set the flag that marks weather or not the files has been changed since the last build.
187 @param changed if files has been added/removed, as a <strong>boolean</strong>
188 */
189 public void setFilesChanged(boolean changed) {
190 filesChanged = changed;
191 }
192
193 /** Determine if this collection has had metadata changed since the last build.
194 * @return <i>true</i> if it has been changed, <i>false</i> otherwise.
195 */
196 public boolean getMetadataChanged() {
197 return metadataChanged;
198 }
199
200 /** Set the flag that marks weather or not the metadata has been changed since the last build.
201 @param changed if metadata has been added/changed/removed, as a <strong>boolean</strong>
202 */
203 public void setMetadataChanged(boolean changed) {
204 metadataChanged = changed;
205 }
206
[6086]207 /** Retrieve the title of this collection.
208 * @return The title as a <strong>String</strong>.
209 */
210 public String getTitle() {
211 if(cdm == null) {
212 return StaticStrings.EMPTY_STR;
[5164]213 }
[6086]214 CollectionMeta collection_name_collectionmeta = cdm.collectionmeta_manager.getMetadatum(StaticStrings.COLLECTIONMETADATA_COLLECTIONNAME_STR);
215 return collection_name_collectionmeta.getValue(CollectionMeta.TEXT);
216 }
[6051]217
[6086]218 /** Save this xml document to the given file. */
219 public void save() {
[9036]220 XMLTools.writeXMLFile(file, document);
[8586]221 saved = true;
[6086]222 }
[5237]223
[6086]224 public void setBaseCollection(String base_collection) {
225 set(BASE_COLLECTION, base_collection);
226 }
227
228 /** Set the value of imported to the given value.
229 * @param value The new value for imported, <i>true</i> if the collection has been imported successfully, <i>false</i> otherwise.
230 */
231 public void setImported(boolean value) {
[5164]232 set(IMPORTED, value);
233 saved = false;
[6086]234 }
235
236 /** Set the value of saved to the given value.
237 * @param value The new value for saved, <i>true</i> if the collection has been saved recently, <i>false</i> otherwise.
238 */
239 public void setSaved(boolean value) {
[5164]240 saved = value;
[6086]241 }
242
243 /** Set the value of title to the given value.
244 * @param title The new <strong>String</strong> title.
245 */
246 public void setTitle(String title) {
247 if(cdm != null) {
248 CollectionMeta collection_name_collectionmeta = cdm.collectionmeta_manager.getMetadatum(StaticStrings.COLLECTIONMETADATA_COLLECTIONNAME_STR);
249 collection_name_collectionmeta.setValue(title);
[6051]250 }
[6086]251 }
252
253 /** Method called to return a textual representation of a class, which in this case is the collections title.
254 * @return A <strong>String</strong> containing the collections title.
255 */
256 public String toString() {
257 return getTitle();
258 }
[13819]259
260 public boolean toSkimFile(){
261 Element document_element = document.getDocumentElement();
262 NodeList arguments = document_element.getElementsByTagName(ARGUMENT);
263 for(int i = 0; i < arguments.getLength(); i++) {
264 Element argument_element = (Element) arguments.item(i);
265 if(argument_element.getAttribute(StaticStrings.NAME_ATTRIBUTE).equalsIgnoreCase(SKIM_FILE)) {
266 String skimfile = XMLTools.getValue(argument_element);
267 if (skimfile !=null && skimfile.trim().toLowerCase().equals("false")){
268 return false;
269 }
270 }
271
272 }
273
274 return true;
275 }
276
[6086]277
278 /** Get the value of a collection argument. */
279 private boolean get(String name) {
[5164]280 boolean result = false;
281 try {
[6086]282 Element document_element = document.getDocumentElement();
283 NodeList arguments = document_element.getElementsByTagName(ARGUMENT);
284 boolean found = false;
285 for(int i = 0; !found && i < arguments.getLength(); i++) {
[5164]286 Element argument_element = (Element) arguments.item(i);
287 if(argument_element.getParentNode() == document_element) {
[6086]288 if(argument_element.getAttribute(StaticStrings.NAME_ATTRIBUTE).equalsIgnoreCase(name)) {
[8015]289 String value = XMLTools.getValue(argument_element);
[6086]290 if(value.equalsIgnoreCase(StaticStrings.TRUE_STR)) {
291 result = true;
[5164]292 }
293 found = true;
294 value = null;
[6086]295 }
[5164]296 }
297 argument_element = null;
[6086]298 }
299 arguments = null;
300 document_element = null;
[5164]301 }
302 catch (Exception error) {
[8236]303 DebugStream.printStackTrace(error);
[5164]304 }
305 return result;
[6086]306 }
307
308 /** Get the value of a collection argument. */
309 private String getString(String name) {
[5721]310 String result = "";
311 try {
[6086]312 Element document_element = document.getDocumentElement();
313 NodeList arguments = document_element.getElementsByTagName(ARGUMENT);
314 boolean found = false;
315 for(int i = 0; !found && i < arguments.getLength(); i++) {
[5721]316 Element argument_element = (Element) arguments.item(i);
317 if(argument_element.getParentNode() == document_element) {
[6086]318 if(argument_element.getAttribute(StaticStrings.NAME_ATTRIBUTE).equalsIgnoreCase(name)) {
[8015]319 result = XMLTools.getValue(argument_element);
[5721]320 found = true;
[6086]321 }
[5721]322 }
323 argument_element = null;
[6086]324 }
325 arguments = null;
326 document_element = null;
[5721]327 }
328 catch (Exception error) {
[8236]329 DebugStream.printStackTrace(error);
[5721]330 }
331 return result;
[6086]332 }
[13819]333
[6086]334
[13819]335
336
[6086]337 /** Method to retrieve the current build options associated with this Collection. */
338 private Element getBuildValues() {
[5164]339 Element build_values_element = null;
340 try {
[6086]341 Element document_element = document.getDocumentElement();
[8015]342 Element build_config_element = (Element) XMLTools.getNodeFromNamed(document_element, BUILD_CONFIG);
343 build_values_element = (Element) XMLTools.getNodeFromNamed(build_config_element, BUILD);
[6086]344 build_config_element = null;
345 document_element = null;
[5164]346 }
347 catch (Exception error) {
[8236]348 DebugStream.printStackTrace(error);
[5164]349 }
350 return build_values_element;
[6086]351 }
352
353 /** Count either documents or folders, depending on the state of the given boolean. */
354 private int getCount(TreeNode node, boolean count_folders, boolean count_files) {
[5164]355 int count = 0;
[8783]356 File file = ((CollectionTreeNode)node).getFile();
[5164]357 if(file.isFile() && count_files) {
[6086]358 count++;
[5164]359 }
360 else if(file.isDirectory() && count_folders) {
[6086]361 count++;
[5164]362 }
[6325]363 for(int i = 0; !file.getName().equals("CVS") && i < node.getChildCount(); i++) {
[6086]364 count = count + getCount(node.getChildAt(i), count_folders, count_files);
[5164]365 }
366 return count;
[6086]367 }
368
369 /** Method to retrieve the current import options associated with this Collection. */
370 public Element getImportValues() {
[5164]371 Element import_values_element = null;
372 try {
[6086]373 Element document_element = document.getDocumentElement();
[8015]374 Element build_config_element = (Element) XMLTools.getNodeFromNamed(document_element, BUILD_CONFIG);
375 import_values_element = (Element) XMLTools.getNodeFromNamed(build_config_element, IMPORT);
[6086]376 build_config_element = null;
377 document_element = null;
[5164]378 }
379 catch (Exception error) {
[8236]380 DebugStream.printStackTrace(error);
[5164]381 }
382 return import_values_element;
[6086]383 }
384
385 /** Set the value of a collection argument. */
386 private void set(String name, boolean value) {
387 set(name, (value ? StaticStrings.TRUE_STR : StaticStrings.FALSE_STR));
388 }
389
390 private void set(String name, String value) {
391 try {
392 Element document_element = document.getDocumentElement();
393 NodeList arguments = document_element.getElementsByTagName(ARGUMENT);
394 boolean found = false;
395 for(int i = 0; !found && i < arguments.getLength(); i++) {
[6051]396 Element argument_element = (Element) arguments.item(i);
397 if(argument_element.getParentNode() == document_element) {
[6086]398 if(argument_element.getAttribute(StaticStrings.NAME_ATTRIBUTE).equalsIgnoreCase(name)) {
399 // Strip any current value nodes.
400 while(argument_element.hasChildNodes()) {
401 argument_element.removeChild(argument_element.getFirstChild());
402 }
403 // Append new value
404 argument_element.appendChild(document.createTextNode(value));
405 found = true;
406 }
[6051]407 }
408 argument_element = null;
[6086]409 }
410 // Append it
411 if(!found) {
[6051]412 Element argument_element = document.createElement(ARGUMENT);
[6086]413 argument_element.setAttribute(StaticStrings.NAME_ATTRIBUTE, name);
[6051]414 argument_element.appendChild(document.createTextNode(value));
415 document_element.appendChild(argument_element);
416 argument_element = null;
[6086]417 }
418 arguments = null;
419 document_element = null;
420 }
421 catch (Exception error) {
[8236]422 DebugStream.printStackTrace(error);
[6086]423 }
424 }
[5164]425}
Note: See TracBrowser for help on using the repository browser.