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