source: main/trunk/gli/src/org/greenstone/gatherer/collection/CollectionManager.java@ 22467

Last change on this file since 22467 was 22467, checked in by ak19, 14 years ago

The trailing slash returned by the updated CollectionManager.getCollectDirectory() was causing problems when running import.pl and such scripts

  • Property svn:keywords set to Author Date Id Revision
File size: 86.9 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.event.*;
43import javax.swing.filechooser.FileSystemView;
44import javax.swing.tree.*;
45import org.greenstone.gatherer.Configuration;
46import org.greenstone.gatherer.DebugStream;
47import org.greenstone.gatherer.Dictionary;
48import org.greenstone.gatherer.Gatherer;
49import org.greenstone.gatherer.cdm.CollectionDesignManager;
50import org.greenstone.gatherer.cdm.CollectionMeta;
51import org.greenstone.gatherer.cdm.CollectionMetaManager;
52import org.greenstone.gatherer.cdm.CommandTokenizer;
53import org.greenstone.gatherer.cdm.BuildTypeManager;
54import org.greenstone.gatherer.cdm.CollectionConfiguration;
55import org.greenstone.gatherer.greenstone.Classifiers;
56import org.greenstone.gatherer.greenstone.LocalGreenstone;
57import org.greenstone.gatherer.greenstone.LocalLibraryServer;
58import org.greenstone.gatherer.greenstone.Plugins;
59import org.greenstone.gatherer.greenstone3.ServletConfiguration;
60import org.greenstone.gatherer.gui.LockFileDialog;
61import org.greenstone.gatherer.gui.ModalProgressPopup;
62import org.greenstone.gatherer.gui.WarningDialog;
63import org.greenstone.gatherer.metadata.DocXMLFileManager;
64import org.greenstone.gatherer.metadata.MetadataChangedListener;
65import org.greenstone.gatherer.metadata.MetadataSet;
66import org.greenstone.gatherer.metadata.MetadataSetManager;
67import org.greenstone.gatherer.metadata.MetadataXMLFileManager;
68import org.greenstone.gatherer.metadata.ProfileXMLFileManager;
69import org.greenstone.gatherer.remote.RemoteGreenstoneServer;
70import org.greenstone.gatherer.shell.GShell;
71import org.greenstone.gatherer.shell.GShellEvent;
72import org.greenstone.gatherer.shell.GShellListener;
73import org.greenstone.gatherer.shell.GShellProgressMonitor;
74import org.greenstone.gatherer.util.Codec;
75import org.greenstone.gatherer.util.StaticStrings;
76import org.greenstone.gatherer.util.Utility;
77import org.greenstone.gatherer.util.XMLTools;
78import org.w3c.dom.*;
79
80/** This class manages many aspects of the collection, from its creation via scripts, data access via methods and its importing and building into the final collection. It is also responsible for firing appropriate event when significant changes have occured within the collection, and for creating a new metadata set manager as necessary.
81 * @author John Thompson
82 * @version 2.3
83 */
84public class CollectionManager
85 implements GShellListener, MetadataChangedListener
86{
87 /** Are we currently in the process of building? */
88 static private boolean building = false;
89 /** Are we currently in the process of importing? */
90 static private boolean importing = false;
91 /** Are we currently in the process of scheduling? */
92 static private boolean scheduling = false;
93 /** The objects listening for CollectionContentsChanged events. */
94 static private ArrayList collection_contents_changed_listeners = new ArrayList();
95 /** The collection this manager is managing! */
96 static private Collection collection = null;
97 /** The collection tree (used in both Gather and Enrich panes). */
98 static private CollectionTree collection_tree = null;
99 /** The collection tree model. */
100 static private CollectionTreeModel collection_tree_model = null;
101 /** An inner class listener responsible for noting tree changes and resetting saved when they occur. */
102 static private FMTreeModelListener fm_tree_model_listener = null;
103 /** The monitor responsible for parsing the build process. */
104 static private GShellProgressMonitor build_monitor = null;
105 /** The monitor responsible for parsing the import process. */
106 static private GShellProgressMonitor import_monitor = null;
107 /** The monitor responsible for parsing the scheduler process. */
108 static private GShellProgressMonitor schedule_monitor = null;
109
110 /** The name of the standard lock file. */
111 static final public String LOCK_FILE = "gli.lck";
112
113 /** Used to indicate the source of the message is the file collection methods. */
114 static final public int COLLECT = 3;
115 /** Used to indicate the source of the message is the building methods. */
116 static final public int BUILDING = 5;
117 /** Used to indicate the source of the message is in the scheduling methods...? */
118 static final public int SCHEDULING = 7;
119
120 /** To store the path to the perl scripts. In the case of local Greenstone servers,
121 * this will be the local bin/script folder. */
122 static private String scriptPath = "";
123
124 /** Constructor. */
125 public CollectionManager() {
126 // Initialisation.
127 this.building = false;
128 this.importing = false;
129 this.scheduling = false;
130 this.collection = null;
131
132 MetadataXMLFileManager.addMetadataChangedListener(this);
133
134 // If using a remote Greenstone server, delete the local collect directory because it will be out of date
135 if (Gatherer.isGsdlRemote) {
136 System.err.println("Deleting user's local collect directory...");
137 Utility.delete(new File(Gatherer.getCollectDirectoryPath()));
138 System.err.println("Done.");
139 new File(Gatherer.getCollectDirectoryPath()).mkdirs();
140
141 scriptPath = ""; // remote greenstone: scriptPath will be determined on remote server side
142 }
143 else { // local greenstone case: scripts are inside bin/script
144 scriptPath = LocalGreenstone.getBinScriptDirectoryPath();
145 }
146 }
147
148
149 static public void addCollectionContentsChangedListener(CollectionContentsChangedListener listener)
150 {
151 collection_contents_changed_listeners.add(listener);
152 }
153
154
155 /** This method calls the builcol.pl scripts via a GShell so as to not lock up the processor.
156 * @see org.greenstone.gatherer.Configuration
157 * @see org.greenstone.gatherer.Gatherer
158 * @see org.greenstone.gatherer.collection.Collection
159 * @see org.greenstone.gatherer.gui.BuildOptions
160 * @see org.greenstone.gatherer.shell.GShell
161 * @see org.greenstone.gatherer.shell.GShellListener
162 * @see org.greenstone.gatherer.shell.GShellProgressMonitor
163 * @see org.greenstone.gatherer.util.Utility
164 */
165 public void buildCollection()
166 {
167
168 DebugStream.println("In CollectionManager.buildCollection(), CollectionDesignManager.isCompleteBuild(): " + CollectionDesignManager.isCompleteBuild());
169 DebugStream.println("Is event dispatch thread: " + SwingUtilities.isEventDispatchThread());
170 building = true;
171
172 // Generate the buildcol.pl command
173 ArrayList command_parts_list = new ArrayList();
174 if (!Gatherer.isGsdlRemote) {
175 command_parts_list.add(Configuration.perl_path);
176 command_parts_list.add("-S");
177 }
178
179 if (Configuration.fedora_info.isActive()) {
180 command_parts_list.add(scriptPath + "g2f-buildcol.pl");
181
182 command_parts_list.add("-hostname");
183 command_parts_list.add(Configuration.fedora_info.getHostname());
184
185 command_parts_list.add("-port");
186 command_parts_list.add(Configuration.fedora_info.getPort());
187
188 command_parts_list.add("-username");
189 command_parts_list.add(Configuration.fedora_info.getUsername());
190
191 command_parts_list.add("-password");
192 command_parts_list.add(Configuration.fedora_info.getPassword());
193
194 command_parts_list.add("-protocol");
195 command_parts_list.add(Configuration.fedora_info.getProtocol());
196
197 }
198 else {
199
200 if ( !CollectionDesignManager.isCompleteBuild() && CollectionDesignManager.index_manager.isLucene() ) {
201 command_parts_list.add(scriptPath + "incremental-buildcol.pl");
202 CollectionDesignManager.setBuildcolWasFull(false);
203 } else {
204 command_parts_list.add(scriptPath + "full-buildcol.pl");
205 CollectionDesignManager.setBuildcolWasFull(true);
206 }
207 }
208
209 command_parts_list.add("-gli");
210 command_parts_list.add("-language");
211 command_parts_list.add(Configuration.getLanguage());
212 if(!Gatherer.isGsdlRemote) {
213 command_parts_list.add("-collectdir");
214 command_parts_list.add(getCollectDirectory()); // <../collect/>
215 }
216
217 String[] build_options = collection.build_options.getValues();
218 for (int i = 0; i < build_options.length; i++) {
219 command_parts_list.add(build_options[i]);
220 }
221
222 command_parts_list.add(collection.getGroupQualifiedName(false)); // (colgroup/)colname
223
224 // Run the buildcol.pl and
225 String[] command_parts = (String[]) command_parts_list.toArray(new String[0]);
226 GShell shell = new GShell(command_parts, GShell.BUILD, BUILDING, this, build_monitor, GShell.GSHELL_BUILD);
227 shell.addGShellListener(Gatherer.g_man.create_pane);
228 shell.addGShellListener(Gatherer.g_man.format_pane);
229 shell.start();
230
231 }
232
233 /*probably repeating alot of work, but I want to keep this separate... wendy*/
234 public void scheduleBuild()
235 {
236 DebugStream.println("In CollectionManager.scheduleBuild(), CollectionDesignManager.isCompleteBuild(): " + CollectionDesignManager.isCompleteBuild());
237 DebugStream.println("Is event dispatch threa: " + SwingUtilities.isEventDispatchThread());
238
239 ArrayList sched_list = new ArrayList();
240 if (!Gatherer.isGsdlRemote) {
241 sched_list.add(Configuration.perl_path);
242 sched_list.add("-S");
243 }
244 sched_list.add(scriptPath + "schedule.pl");
245 sched_list.add("-colname");
246 sched_list.add(collection.getName());
247 sched_list.add("-gli");
248
249 // First, generate the import.pl command, also converting to a string
250 // Generate the import.pl command
251 ArrayList import_list = new ArrayList();
252 if (!Gatherer.isGsdlRemote) {
253 import_list.add(Configuration.perl_path);
254 import_list.add("-S");
255 }
256
257 String cmdPrefix = CollectionDesignManager.isCompleteBuild() ? "full-" : "incremental-";
258 import_list.add(scriptPath + cmdPrefix + "import.pl");
259 import_list.add("-language");
260 import_list.add(Configuration.getLanguage());
261 if(!Gatherer.isGsdlRemote) {
262 import_list.add("-collectdir");
263 import_list.add(getCollectDirectory());
264 }
265
266 String[] import_options = collection.import_options.getValues();
267 int i = 0;
268 for (i = 0; i < import_options.length; i++) {
269 import_list.add(import_options[i]);
270 }
271
272 import_list.add(collection.getGroupQualifiedName(false)); // (colgroup/)colname
273
274 String[] import_parts = (String[]) import_list.toArray(new String[0]);
275 String command = "";
276 i = 0;
277 for (i = 0; i < import_parts.length-1; i++) {
278 command = command + import_parts[i] + " ";
279 }
280 command = command + import_parts[i];
281
282 sched_list.add("-import");
283 sched_list.add("\"" + command + "\"");
284
285 // Generate the buildcol.pl command, also converting to a string
286 ArrayList build_list = new ArrayList();
287
288 // i'm not doing this in schedule.pl right now - should i be?
289 if (!Gatherer.isGsdlRemote) {
290 build_list.add(Configuration.perl_path);
291 build_list.add("-S");
292 }
293
294 String buildType = (new CollectionMeta( CollectionDesignManager.collect_config.getBuildType() )).getValue(CollectionMeta.TEXT);
295 if ( !CollectionDesignManager.isCompleteBuild() && buildType.equals( BuildTypeManager.BUILD_TYPE_LUCENE ) ) {
296 build_list.add(scriptPath + "incremental-buildcol.pl");
297 } else {
298 build_list.add(scriptPath + "full-buildcol.pl");
299 }
300
301 build_list.add("-language");
302 build_list.add(Configuration.getLanguage());
303 if(!Gatherer.isGsdlRemote) {
304 build_list.add("-collectdir");
305 build_list.add(getCollectDirectory());
306 }
307
308 String[] build_options = collection.build_options.getValues();
309 for (i = 0; i < build_options.length; i++) {
310 build_list.add(build_options[i]);
311 }
312
313 build_list.add(collection.getGroupQualifiedName(false)); // (colgroup/)colname
314
315 //build actual string
316 String[] build_parts = (String[]) build_list.toArray(new String[0]);
317 String command2 = "";
318 for(i = 0; i < build_parts.length-1; i++) {
319 command2 = command2 + build_parts[i] + " ";
320 }
321 command2 = command2 + build_parts[i];
322
323 sched_list.add("-build");
324 sched_list.add("\"" + command2 + "\"");
325
326 //next, the scheduling frequency goes here
327 String[] schedule_options = collection.schedule_options.getValues();
328 for(i = 0; i < schedule_options.length; i++) {
329 sched_list.add(schedule_options[i]);
330 }
331
332 //now, hope it will run. ;)
333 String[] sched_parts = (String[]) sched_list.toArray(new String[0]);
334
335 GShell shell = new GShell(sched_parts, GShell.SCHEDULE, SCHEDULING, this, schedule_monitor, GShell.GSHELL_SCHEDULE);
336 shell.addGShellListener(Gatherer.g_man.create_pane);
337 shell.addGShellListener(Gatherer.g_man.format_pane);
338 shell.start();
339 }
340
341 /** Used to determine whether the currently active collection has been built.
342 * @return A boolean indicating the built status of the collection.
343 */
344 public boolean built() {
345 if(collection != null) {
346 // Determine if the collection has been built by looking for the build.cfg (gs2)
347 // buildConfig.xml (gs3) or export.inf (fedora) file
348 String file_name = "";
349
350 if (Configuration.fedora_info != null && Configuration.fedora_info.isActive()) { // FLI case
351 // Fedora build
352 //file_name = getLoadedCollectionArchivesDirectoryPath() + "import.inf";
353 //file_name = getLoadedCollectionExportDirectoryPath() + "export.inf"; // export.pl no longer generates this
354 file_name = getLoadedCollectionExportDirectoryPath() + "archiveinf-doc.gdb";
355 } else {
356 // GLI is running, check if it's greenstone 3 or greenstone 2
357 if (Gatherer.GS3) { // GS3 GLI
358 file_name = getLoadedCollectionIndexDirectoryPath() + Utility.BUILD_CONFIG_XML;
359 }
360 else { // greenstone 2 GLI
361 file_name = getLoadedCollectionIndexDirectoryPath() + Utility.BUILD_CFG;
362 }
363 }
364 File test_file = new File(file_name);
365 return test_file.exists();
366 }
367 return false;
368 }
369
370 /** Used to determine whether the currently active collection has been imported.
371 * @return A boolean indicating the imported status of the collection.
372 */
373 public boolean imported() {
374 if ( collection != null ) {
375 String file_name = getLoadedCollectionDirectoryPath() + "archives";
376 File test_file = new File(file_name);
377 return test_file.exists();
378 }
379 return false;
380 }
381
382 /** a test method to see if we can delete a directory/file - returns false is the file or any of the contents of a directory cannot be deleted */
383 static private boolean canDelete(File file)
384 {
385 if (!file.isDirectory()) {
386 return file.canWrite();
387 }
388 File [] file_list = file.listFiles();
389 for (int i=0; i<file_list.length; i++) {
390 if (!canDelete(file_list[i])) {
391 return false;
392 }
393 }
394 return true;
395 }
396
397
398 /** Called to close the current collection and remove its lock file.
399 * @see org.greenstone.gatherer.Gatherer
400 * @see org.greenstone.gatherer.collection.Collection
401 * @see org.greenstone.gatherer.util.Utility
402 */
403 public void closeCollection() {
404 DebugStream.println("Close collection: " + collection.getName());
405
406 // Remove the lock on this file, then remove the collection.
407 File lock_file = new File(getLoadedCollectionDirectoryPath() + LOCK_FILE);
408 lock_file.delete();
409 if (lock_file.exists()) {
410 System.err.println("Warning: Lockfile was not successfully deleted.");
411 }
412
413 // Remove the lock file on the server
414 if (Gatherer.isGsdlRemote) {
415 Gatherer.remoteGreenstoneServer.deleteCollectionFile(collection.getGroupQualifiedName(false), lock_file);
416 }
417
418 MetadataSetManager.clearMetadataSets();
419 MetadataXMLFileManager.clearMetadataXMLFiles();
420 DocXMLFileManager.clearDocXMLFiles();
421 ProfileXMLFileManager.clearProfileXMLFile();
422
423 collection.destroy();
424 collection = null;
425 collection_tree_model = null;
426 //Configuration.setCollectionConfiguration(null);
427 Gatherer.refresh(Gatherer.COLLECTION_CLOSED);
428 if (Gatherer.g_man != null) {
429 Gatherer.g_man.updateUI(); // !!! Necessary?
430 }
431 }
432
433//This method is no longer used in gs3 since the modification of CollectionConfiguration.java
434// public void convertToGS3Collection() {
435// // Generate the convert_coll_from_gs2.pl command
436// ArrayList command_parts_list = new ArrayList();
437// if ((Utility.isWindows()) && (!Gatherer.isGsdlRemote)) {
438// command_parts_list.add(Configuration.perl_path);
439// command_parts_list.add("-S");
440// }
441// command_parts_list.add(Configuration.getGS3ScriptPath() + "convert_coll_from_gs2.pl");
442// command_parts_list.add("-collectdir");
443// command_parts_list.add(getCollectDirectory());
444// command_parts_list.add(collection.getGroupQualifiedName(false)); // (colgroup/)colname
445//
446// // Run the convert_coll_from_gs2.pl command
447// String[] command_parts = (String[]) command_parts_list.toArray(new String[0]);
448// GShell process = new GShell(command_parts, GShell.CONVERT, COLLECT, this, null, GShell.GSHELL_CONVERT);
449// process.addGShellListener(this);
450// process.run(); // Don't bother threading this... yet
451//
452// }
453
454 /** When basing a new collection on an existing one, we need to copy
455 * over some extra directories: all except import, archives, building, index
456 * really we just want images, macros, perllib, but there may also be eg style, or other dirs.
457 */
458 private boolean copyExtraBaseCollStuff(File new_coll_dir, File base_coll_dir) {
459 if (!new_coll_dir.isDirectory() || !base_coll_dir.isDirectory()) {
460 return false;
461 }
462 DebugStream.println("Copying extra dirs from the base collection");
463
464
465 File subdirs[] = base_coll_dir.listFiles();
466 for (int i = 0; subdirs != null && i < subdirs.length; i++) {
467 File subdir = subdirs[i];
468 if (subdir.isDirectory()) {
469 String dir_name = subdir.getName();
470 // ignore those we don't need, (archives, buildng, index) and
471 // those we are handling in another place (import, etc, metadata)
472 if (dir_name.startsWith ("import") || dir_name.startsWith("archives") || dir_name.startsWith("building") || dir_name.startsWith("index") || dir_name.startsWith("etc") || dir_name.startsWith("metadata") || dir_name.startsWith("log") || dir_name.startsWith("tmp")) {
473 continue;
474 }
475 try {
476 // copy the directory
477 File new_coll_subdir = new File(new_coll_dir, dir_name);
478 new_coll_subdir.mkdirs();
479 Gatherer.f_man.getQueue().copyDirectoryContents(subdir, new_coll_subdir);
480 }
481 catch (Exception e) {
482 DebugStream.println("Couldn't copy over the" + subdir+" dir from the base collection: "+e.toString());
483 }
484 }
485 }
486
487 return true;
488 }
489
490 /** Used to set the current collection to the given collection. Note that this call should -always- be proceeded by a ready call, and if the collection is ready and the saved flag is unset then the user should be prompted to save. Also note that this method creates yet another GShell to run buildcol.pl.
491 * @param description a description of the collection as a String
492 * @param email the email address of the author/maintainer as a String
493 * @param name the short name of the collection, which will subsequently be used to refer to this particular collection, as a String
494 * @param title the longer title of the collection as a String
495 * @param base_collection_directory if the user has chosen to base their new collection on an existing one, this is the directory where this base collection can be found, as a File, otherwise its null
496 * @param metadata_sets if the user has decided to select several metadata sets with which to initially populate the GLI then this is an ArrayList of metadata set file names, otherwise its null
497 */
498 public void createCollection(String description, String email, String name, String title, File base_collection_directory, ArrayList metadata_sets)
499 {
500 // Display a modal progress popup to indicate that the collection is being loaded
501 ModalProgressPopup create_collection_progress_popup = new ModalProgressPopup(Dictionary.get("CollectionManager.Creating_Collection"), Dictionary.get("CollectionManager.Creating_Collection_Please_Wait"));
502 create_collection_progress_popup.display();
503
504 // Create the collection on a separate thread so the progress bar updates correctly
505 (new CreateCollectionTask(description, email, name, title, base_collection_directory, metadata_sets, create_collection_progress_popup)).start();
506 }
507
508
509 private class CreateCollectionTask
510 extends Thread
511 {
512 private String description = null;
513 private String email = null;
514 private String name = null;
515 private String title = null;
516 private File base_collection_directory = null;
517 private ArrayList metadata_sets = null;
518 private ModalProgressPopup create_collection_progress_popup = null;
519
520 public CreateCollectionTask(String description, String email, String name, String title, File base_collection_directory, ArrayList metadata_sets, ModalProgressPopup create_collection_progress_popup)
521 {
522 this.description = description;
523 this.email = email;
524 this.name = name;
525 this.title = title;
526 this.base_collection_directory = base_collection_directory;
527 this.metadata_sets = metadata_sets;
528 this.create_collection_progress_popup = create_collection_progress_popup;
529 }
530
531 public void run()
532 {
533 createCollectionInternal(description, email, name, title, base_collection_directory, metadata_sets);
534 create_collection_progress_popup.close();
535 }
536 }
537
538
539 private void createCollectionInternal(String description, String email, String name, String title, File base_collection_directory, ArrayList metadata_sets)
540 {
541 try {
542 // first make sure that the collect directory exists
543 File collect_dir = new File(getDefaultCollectDirectory());
544 if (!collect_dir.exists()) {
545 collect_dir.mkdirs();
546 }
547
548 // Create the new collection
549 makeCollection(name, email);
550
551 // Check that the collection has been created successfully
552 String collection_directory_path = getCollectionDirectoryPath(name);
553 if (!new File(collection_directory_path).exists()) {
554 // If there is no collection directory then the creation was unsuccessful, or cancelled
555
556 return;
557 }
558
559 // Check for the existence of the collection configuration file
560 String file_name = ((Gatherer.GS3 == true)? Utility.COLLECTION_CONFIG_XML : Utility.COLLECT_CFG);
561 File collect_cfg_file = new File(collection_directory_path + "etc" + File.separator + file_name);
562
563 if (!collect_cfg_file.exists()) {
564 System.err.println("Error: no " + file_name + " file has been created!");
565 JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("CollectionManager.Cannot_Create_Collection_With_Reason", Dictionary.get("CollectionManager.No_Config_File")), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);
566 return;
567 }
568
569 // ACTIVE_DIR/log/
570 File log_dir = new File(collection_directory_path + "log");
571 log_dir.mkdirs();
572
573 // Make sure an import folder exists
574 File collection_import_directory = new File(collection_directory_path + "import");
575 if (!collection_import_directory.exists()) {
576 collection_import_directory.mkdirs();
577 if (Gatherer.isGsdlRemote) {
578 Gatherer.remoteGreenstoneServer.newCollectionDirectory(name, collection_import_directory);
579 }
580 }
581
582 // Now create the collection object around the directory.
583 collection = new Collection(new File(collection_directory_path, "gli.col"));
584
585 // for remote case, scheduling causes an Exception on creating a new collection that
586 // can't be recovered from. For GS3, it doesn't work since it it trying to access etc/main.cfg
587 if (canDoScheduling()) {
588 scheduling();
589 }
590
591 MetadataSetManager.clearMetadataSets();
592 MetadataXMLFileManager.clearMetadataXMLFiles();
593 DocXMLFileManager.clearDocXMLFiles();
594
595 // Import default metadata sets, if any
596 // for (int i = 0; metadata_sets != null && i < metadata_sets.size(); i++) {
597 // importMetadataSet((MetadataSet) metadata_sets.get(i));
598 // }
599
600 ProfileXMLFileManager.loadProfileXMLFile(new File(collection_directory_path + "metadata"));
601
602 // Before creating the CollectionDesignManager check if we are basing it upon some other collection
603 if (base_collection_directory != null) {
604 DebugStream.println("Basing new collection on existing one: " + base_collection_directory);
605
606 // If we're using a remote Greenstone server, download the collection shell to get the files needed
607 if (Gatherer.isGsdlRemote) {
608 String base_collection_name = base_collection_directory.getName();
609 Gatherer.remoteGreenstoneServer.downloadCollection(base_collection_name);
610 }
611
612 collection.setBaseCollection(base_collection_directory.getAbsolutePath());
613 // copy over other needed directories
614 copyExtraBaseCollStuff(new File(collection_directory_path), base_collection_directory);
615
616 // Try to import any existing metadata sets for this collection
617 // Look in base_collection_directory/metadata and import any metadata sets found.
618 File base_metadata_directory = new File(base_collection_directory, "metadata");
619 ArrayList base_metadata_sets = MetadataSetManager.listMetadataSets(base_metadata_directory);
620 if (base_metadata_sets != null) {
621 for (int i = 0; i < base_metadata_sets.size(); i++) {
622 importMetadataSet((MetadataSet) base_metadata_sets.get(i));
623 }
624 }
625 else {
626 DebugStream.println("This base collection has no metadata directory.");
627 }
628
629 // Now we update our collect.cfg
630 DebugStream.println("Copy and update " + file_name + " from base collection.");
631
632 if (Gatherer.GS3 == true) {
633 updateCollectionConfigXML(new File(base_collection_directory, Utility.CONFIG_GS3_FILE),
634 new File(collection_directory_path, Utility.CONFIG_GS3_FILE));
635 } else {
636 updateCollectionCFG(new File(base_collection_directory, Utility.CONFIG_FILE),
637 new File(collection_directory_path, Utility.CONFIG_FILE),
638 description, email, title);
639 }
640 }
641 else {
642 // only load metadata sets here if we have not based the collection on any other.
643 // Load the default metadata sets
644 addDefaultMetadataSets();
645
646 // Make sure we always have the extracted metadata set
647 addRequiredMetadataSets();
648 }
649
650 collection.cdm = new CollectionDesignManager(new File(getLoadedCollectionCfgFilePath()));
651
652 // We always set title and description here rather than calling mkcol.pl with Unicode arguments
653 CollectionMeta collection_name_collectionmeta = collection.cdm.collectionmeta_manager.getMetadatum(StaticStrings.COLLECTIONMETADATA_COLLECTIONNAME_STR);
654 collection_name_collectionmeta.setValue(title);
655 CollectionMeta collection_extra_collectionmeta = collection.cdm.collectionmeta_manager.getMetadatum(StaticStrings.COLLECTIONMETADATA_COLLECTIONEXTRA_STR);
656 collection_extra_collectionmeta.setValue(description);
657
658 // Now that we have a CDM, update several settings, such as if we created this collection by basing it on another, set it as public automatically. This update is done to the internal xml structure which may be saved into collect.cfg or collectionConfig.xml accordingly.
659 if (base_collection_directory != null) {
660 // Update the creator and maintainer
661 CollectionMeta creator_collectionmeta = new CollectionMeta(collection.cdm.collect_config.getCreator());
662 creator_collectionmeta.setValue(email);
663 creator_collectionmeta = null;
664 CollectionMeta maintainer_collectionmeta = new CollectionMeta(collection.cdm.collect_config.getMaintainer());
665 maintainer_collectionmeta.setValue(email);
666 maintainer_collectionmeta = null;
667
668 // All collections based on others are automatically public
669 CollectionMeta public_collectionmeta = new CollectionMeta(collection.cdm.collect_config.getPublic());
670 public_collectionmeta.setValue(StaticStrings.TRUE_STR);
671 public_collectionmeta = null;
672
673 // Finally reset the icons
674 CollectionMeta icon_collection_collectionmeta = collection.cdm.collectionmeta_manager.getMetadatum(StaticStrings.COLLECTIONMETADATA_ICONCOLLECTION_STR);
675 icon_collection_collectionmeta.setValue(StaticStrings.EMPTY_STR);
676 icon_collection_collectionmeta = null;
677 CollectionMeta icon_collection_small_collectionmeta = collection.cdm.collectionmeta_manager.getMetadatum(StaticStrings.COLLECTIONMETADATA_ICONCOLLECTIONSMALL_STR);
678 icon_collection_small_collectionmeta.setValue(StaticStrings.EMPTY_STR);
679 icon_collection_small_collectionmeta = null;
680 }
681
682 saveCollection();
683
684 // Create a lock file
685 createLockFile(new File(collection_directory_path, LOCK_FILE));
686
687 // We're done. Let everyone know.
688 Gatherer.refresh(Gatherer.COLLECTION_OPENED);
689 }
690 catch (Exception error) {
691 DebugStream.printStackTrace(error);
692 }
693 }
694
695 private void scheduling()
696 throws Exception
697 {
698 //try to obtain email address of collection owner if it exists...
699 String stmp = Configuration.getEmail();
700 if(stmp != null) {
701 collection.schedule_options.setValue("toaddr", false, Configuration.getEmail());
702 }
703
704 //The next few items deal with updating the SMTP server, and the to: and from: addresses
705 //from main.cfg and the collection configuration. if no changes are made, or the
706 //values are result to NULL, any existing values are kept.
707
708 //try to obtain email address of Greenstone installation webmaster for - used to indicate "sender".
709 File mcfg = new File(LocalGreenstone.getDirectoryPath() + File.separator + "etc" + File.separator + "main.cfg");
710 BufferedReader maincfg = new BufferedReader(new FileReader(mcfg));
711 stmp = "";
712 String fromaddr = "";
713 while((stmp = maincfg.readLine()) != null) {
714 if(stmp.startsWith("maintainer")) {
715 fromaddr = stmp.substring(10); //length of MailServer
716 fromaddr = fromaddr.trim();
717 break;
718 }
719 }
720 maincfg.close();
721 if(!fromaddr.equals("NULL") && !fromaddr.equals("null")) {
722 collection.schedule_options.setValue("fromaddr", false, fromaddr);
723 }
724
725 //try to obtain an smtp server address from main.cfg. If that fails,
726 //try mail.server if an email address exists. If that fails,
727 //maybe a message to set attribute in main.cfg?
728 //i'm pretty sure there exists functionality to do this, but
729 //i'll finish this faster if I just wrote it
730
731
732 maincfg = new BufferedReader(new FileReader(mcfg));
733 String smtptmp = "NULL";
734 while((stmp = maincfg.readLine()) != null) {
735 if(stmp.startsWith("MailServer")) {
736 smtptmp = stmp.substring(10); //length of MailServer
737 smtptmp = smtptmp.trim();
738 break;
739 }
740 }
741 maincfg.close();
742
743 //try if lookup fails
744 if(smtptmp.equals("NULL") || smtptmp.equals("null")) {
745 String email2=fromaddr;
746 if(!email2.equals("NULL") && !email2.equals("null")) {
747 int loc = email2.indexOf('@');
748 email2 = email2.substring(loc+1);
749 smtptmp = "mail."+email2;
750 }
751 }
752 if(!smtptmp.equals("NULL") && !smtptmp.equals("null")) {
753 collection.schedule_options.setValue("smtp", false, smtptmp);
754 }
755
756 }
757
758
759 private void createLockFile(File lock_file)
760 {
761 try {
762 Document default_lockfile = XMLTools.parseXMLFile("xml/" + LOCK_FILE, true);
763 String user_name = System.getProperty("user.name");
764 Element person_element = (Element) XMLTools.getNodeFromNamed(default_lockfile.getDocumentElement(), "User");
765 person_element.appendChild(default_lockfile.createTextNode(user_name));
766 person_element = null;
767 user_name = null;
768 String machine_name = Utility.getMachineName();
769 Element machine_element = (Element) XMLTools.getNodeFromNamed(default_lockfile.getDocumentElement(), "Machine");
770 machine_element.appendChild(default_lockfile.createTextNode(machine_name));
771 machine_element = null;
772 machine_name = null;
773 String date_time = Utility.getDateString();
774 Element date_element = (Element) XMLTools.getNodeFromNamed(default_lockfile.getDocumentElement(), "Date");
775 date_element.appendChild(default_lockfile.createTextNode(date_time));
776 date_element = null;
777 date_time = null;
778 XMLTools.writeXMLFile(lock_file, default_lockfile);
779 }
780 catch (Exception exception) {
781 DebugStream.printStackTrace(exception);
782 }
783 }
784
785
786 public boolean deleteCollection(String collection_name)
787 {
788 // First we must release the collection from the local library, if it's running
789 if (LocalLibraryServer.isRunning() == true) {
790 LocalLibraryServer.releaseCollection(collection_name);
791 }
792
793 // Delete the collection on the server if we're using a remote Greenstone
794 if (Gatherer.isGsdlRemote) {
795 Gatherer.remoteGreenstoneServer.deleteCollection(collection_name);
796 }
797
798 // if Greenstone3, need to deactivate the collection on the server
799 if (Gatherer.GS3) {
800 Gatherer.configGS3Server(Configuration.site_name, ServletConfiguration.DEACTIVATE_COMMAND + collection_name);
801 }
802
803 // Now delete the collection directory
804 return Utility.delete(new File(getCollectionDirectoryPath(collection_name)));
805 }
806
807
808 public void fireFileAddedToCollection(File file)
809 {
810 // Send the event off to all the CollectionContentsChangedListeners
811 for (int i = 0; i < collection_contents_changed_listeners.size(); i++) {
812 ((CollectionContentsChangedListener) collection_contents_changed_listeners.get(i)).fileAddedToCollection(file);
813 }
814 }
815
816
817 /** Retrieve the current collection.
818 * @return The <strong>Collection</strong> itself.
819 */
820 public Collection getCollection() {
821 return collection;
822 }
823
824
825 /** Returns the absolute filename of the specified collection's directory.
826 */
827 static public String getCollectionDirectoryPath(String collection_name)
828 {
829 return Gatherer.getCollectDirectoryPath() + collection_name + File.separator;
830 }
831
832
833 /** Returns the absolute filename of the loaded collection's archives directory.
834 */
835 static public String getLoadedCollectionArchivesDirectoryPath()
836 {
837 return getLoadedCollectionDirectoryPath() + "archives" + File.separator;
838 }
839
840 /** Returns the absolute filename of the loaded collection's export directory.
841 */
842 static public String getLoadedCollectionExportDirectoryPath()
843 {
844 return getLoadedCollectionDirectoryPath() + "export" + File.separator;
845 }
846
847
848
849 /** Returns the absolute filename of the loaded collection's building directory.
850 */
851 static public String getLoadedCollectionBuildingDirectoryPath()
852 {
853 return getLoadedCollectionDirectoryPath() + "building" + File.separator;
854 }
855
856
857 /** Returns the absolute filename of the loaded collection's collect.cfg file.
858 */
859 static public String getLoadedCollectionCfgFilePath()
860 {
861 String path = (Gatherer.GS3 == true)? Utility.COLLECTION_CONFIG_XML : Utility.COLLECT_CFG;
862 return getLoadedCollectionEtcDirectoryPath() + path;
863 }
864
865
866 /** Returns the absolute filename of the loaded collection's directory.
867 */
868 static public String getLoadedCollectionDirectoryPath()
869 {
870 return collection.getCollectionDirectory().getPath() + File.separator;
871 }
872
873
874 /** Returns the absolute filename of the loaded collection's etc directory.
875 */
876 static public String getLoadedCollectionEtcDirectoryPath()
877 {
878 return getLoadedCollectionDirectoryPath() + "etc" + File.separator;
879 }
880
881
882 /** Returns the absolute filename of the loaded collection's .col file.
883 */
884 static public String getLoadedCollectionColFilePath()
885 {
886 return getLoadedCollectionDirectoryPath() + "gli.col";
887 }
888
889
890 /** Returns the absolute filename of the loaded collection's images directory.
891 */
892 static public String getLoadedCollectionImagesDirectoryPath()
893 {
894 return getLoadedCollectionDirectoryPath() + "images" + File.separator;
895 }
896
897
898 /** Returns the absolute filename of the loaded collection's import directory.
899 */
900 static public String getLoadedCollectionImportDirectoryPath()
901 {
902 return getLoadedCollectionDirectoryPath() + "import" + File.separator;
903 }
904
905
906 /** Returns the absolute filename of the loaded collection's index directory.
907 */
908 static public String getLoadedCollectionIndexDirectoryPath()
909 {
910 return getLoadedCollectionDirectoryPath() + "index" + File.separator;
911 }
912
913
914 /** Returns the absolute filename of the loaded collection's log directory.
915 */
916 static public String getLoadedCollectionLogDirectoryPath()
917 {
918 return getLoadedCollectionDirectoryPath() + "log" + File.separator;
919 }
920
921
922 /** Returns the absolute filename of the loaded collection's macros directory.
923 */
924 static public String getLoadedCollectionMacrosDirectoryPath()
925 {
926 return getLoadedCollectionDirectoryPath() + "macros" + File.separator;
927 }
928
929
930 /** Returns the absolute filename of the loaded collection's metadata directory.
931 */
932 static public String getLoadedCollectionMetadataDirectoryPath()
933 {
934 return getLoadedCollectionDirectoryPath() + "metadata" + File.separator;
935 }
936
937
938 /** Returns the (group-qualified) name of the loaded collection with
939 * OS-dependent file separator.
940 */
941 static public String getLoadedCollectionName()
942 {
943 return CollectionManager.getLoadedCollectionName(false);
944 }
945
946 /** Returns the (group-qualified) name of the loaded collection with
947 * OS-dependent space separator.
948 * @url true if url-type forward slashes, false if OS-dependent filesystem slashes.
949 */
950 static public String getLoadedCollectionName(boolean url)
951 {
952 if (collection != null) {
953 //return collection.getName();
954 return collection.getGroupQualifiedName(url);
955 }
956
957 return null;
958 }
959
960 /** @return the subname of any collection (stripped of any collection-group). */
961 static public String getLoadedCollectionTailName()
962 {
963 if (collection != null) {
964 return collection.getCollectionTailName();
965 }
966
967 return null;
968 }
969
970 /** Returns the "collectionGroupName/collectionName" or just the collectionName
971 * depending on whether the collection is part of a collection group or not.
972 * If url = true, then returns the sub-path as a URL (containing / only),
973 * and if url = false, then the sub-path is returned in filepath form
974 * (\ or /, depending on the OS).
975 */
976 static public String getLoadedGroupQualifiedCollectionName(boolean url)
977 {
978 if (collection != null) {
979 return collection.getGroupQualifiedName(url);
980 }
981
982 return null;
983 }
984
985 public CollectionTree getCollectionTree()
986 {
987 if (collection_tree == null) {
988 collection_tree = new CollectionTree(collection_tree_model, true);
989 }
990
991 return collection_tree;
992 }
993
994
995 /** Retrieve the tree model associated with the current collection. */
996 public CollectionTreeModel getCollectionTreeModel()
997 {
998 if (collection_tree_model == null && collection != null) {
999 // Use the import directory to generate a new CollectionTreeModel
1000 collection_tree_model = new CollectionTreeModel(new CollectionTreeNode(new File(getLoadedCollectionImportDirectoryPath())));
1001 // Ensure that the manager is a change listener for the tree.
1002 if (fm_tree_model_listener == null) {
1003 fm_tree_model_listener = new FMTreeModelListener();
1004 }
1005 collection_tree_model.addTreeModelListener(fm_tree_model_listener);
1006 }
1007 return collection_tree_model;
1008 }
1009
1010
1011 /** This method when called, creates a new GShell in order to run the import.pl script.
1012 * @see org.greenstone.gatherer.Configuration
1013 * @see org.greenstone.gatherer.Gatherer
1014 * @see org.greenstone.gatherer.gui.BuildOptions
1015 * @see org.greenstone.gatherer.shell.GShell
1016 * @see org.greenstone.gatherer.shell.GShellListener
1017 * @see org.greenstone.gatherer.shell.GShellProgressMonitor
1018 * @see org.greenstone.gatherer.util.Utility
1019 */
1020 public void importCollection() {
1021 importing = true;
1022
1023 if (!saved()) {
1024 DebugStream.println("CollectionManager.importCollection().forcesave");
1025 import_monitor.saving();
1026 saveCollection();
1027 }
1028
1029 DebugStream.println("CollectionManager.importCollection()");
1030 DebugStream.println("Is event dispatch thread: " + SwingUtilities.isEventDispatchThread());
1031 //check that we can remove the old index before starting import
1032 File index_dir = new File(getLoadedCollectionIndexDirectoryPath());
1033 if (index_dir.exists()) {
1034 DebugStream.println("Old Index = " + index_dir.getAbsolutePath()+", testing for deletability");
1035 if (!canDelete(index_dir)) {
1036 // tell the user
1037 JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("CollectionManager.Cannot_Delete_Index"), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);
1038 // tell the gui manager
1039 // a message for the building log
1040 GShellEvent event = new GShellEvent(this, 0, GShell.IMPORT, Dictionary.get("CollectionManager.Cannot_Delete_Index_Log"), GShell.ERROR);
1041 Gatherer.g_man.create_pane.message(event);
1042 event = new GShellEvent(this, 0, GShell.IMPORT, "", GShell.ERROR);
1043 Gatherer.g_man.create_pane.processComplete(event);
1044 importing = false;
1045 return;
1046 }
1047 }
1048
1049 // Generate the import.pl command
1050 ArrayList command_parts_list = new ArrayList();
1051 if (!Gatherer.isGsdlRemote) {
1052 command_parts_list.add(Configuration.perl_path);
1053 command_parts_list.add("-S");
1054 }
1055
1056 if (Configuration.fedora_info.isActive()) {
1057 command_parts_list.add(scriptPath + "g2f-import.pl");
1058
1059 command_parts_list.add("-hostname");
1060 command_parts_list.add(Configuration.fedora_info.getHostname());
1061
1062 command_parts_list.add("-port");
1063 command_parts_list.add(Configuration.fedora_info.getPort());
1064
1065 command_parts_list.add("-username");
1066 command_parts_list.add(Configuration.fedora_info.getUsername());
1067
1068 command_parts_list.add("-password");
1069 command_parts_list.add(Configuration.fedora_info.getPassword());
1070
1071 command_parts_list.add("-protocol");
1072 command_parts_list.add(Configuration.fedora_info.getProtocol());
1073 }
1074 else {
1075 String cmdPrefix = null;
1076 if ( CollectionDesignManager.isCompleteBuild() ) {
1077 cmdPrefix = "full-";
1078 CollectionDesignManager.setImportWasFull( true );
1079 } else {
1080 cmdPrefix = "incremental-";
1081 CollectionDesignManager.setImportWasFull( false );
1082 }
1083 command_parts_list.add(scriptPath + cmdPrefix + "import.pl"); // scriptPath already set according to local or remote case
1084 }
1085
1086 command_parts_list.add("-gli");
1087 command_parts_list.add("-language");
1088 command_parts_list.add(Configuration.getLanguage());
1089 if(!Gatherer.isGsdlRemote) {
1090 command_parts_list.add("-collectdir");
1091 command_parts_list.add(getCollectDirectory());
1092 }
1093
1094 String[] import_options = collection.import_options.getValues();
1095 for (int i = 0; i < import_options.length; i++) {
1096 System.err.println( "Tacking on option: " + import_options[i] );
1097 command_parts_list.add(import_options[i]);
1098 }
1099
1100 command_parts_list.add(collection.getGroupQualifiedName(false)); // (colgroup/)colname
1101
1102 // Run the import.pl command
1103 String[] command_parts = (String[]) command_parts_list.toArray(new String[0]);
1104 GShell shell = new GShell(command_parts, GShell.IMPORT, BUILDING, this, import_monitor, GShell.GSHELL_IMPORT);
1105 //shell.setEventProperty("is_incremental", Boolean.toString(is_incremental));
1106 shell.addGShellListener(Gatherer.g_man.create_pane);
1107 shell.addGShellListener(Gatherer.g_man.format_pane);
1108 shell.start();
1109 DebugStream.println("CollectionManager.importCollection().return");
1110
1111 importing = false;
1112 }
1113
1114
1115 public void importMetadataSet(MetadataSet external_metadata_set)
1116 {
1117 // Copy the .mds file into the collection's "metadata" folder...
1118 File external_metadata_set_file = external_metadata_set.getMetadataSetFile();
1119
1120 // ...but not if it is the redundant "hidden.mds" file
1121 if (external_metadata_set_file.getName().equals("hidden.mds")) {
1122 return;
1123 }
1124
1125 // ...and only if it doesn't already exist
1126 File metadata_set_file = new File(getLoadedCollectionMetadataDirectoryPath(), external_metadata_set_file.getName());
1127 if (!metadata_set_file.exists()) {
1128 try {
1129 Gatherer.f_man.getQueue().copyFile(external_metadata_set_file, metadata_set_file, false);
1130
1131 // If we're using a remote Greenstone server, upload the metadata file
1132 if (Gatherer.isGsdlRemote) {
1133 Gatherer.remoteGreenstoneServer.uploadCollectionFile(collection.getGroupQualifiedName(false), metadata_set_file);
1134 }
1135 }
1136 catch (Exception exception) {
1137 DebugStream.printStackTrace(exception);
1138 }
1139
1140 // Load it into the MetadataSetManager
1141 MetadataSetManager.loadMetadataSet(metadata_set_file);
1142 }
1143 }
1144
1145
1146 /** Determine if we are currently in the middle of importing (and thus, in this case, we can't allow the log writer to exit). Boy was this a mission to track down. The cascade of crap rolls out something like this: Joe Schmo clicks 'Build Collection', which calls the importCollection() method above, which in turn saves the collection with a saveTask, which fires a collectionChanged message once its finished, which drives the list of logs shown on the create pane to update, which fires a itemChanged() event to the OptionsPane who dutifully tells the current log writer thread to finish up writing (all zero lines its been asked to write) and then die. Wereapon Joe Schmo gets a pretty log to look at, but it isn't actually being written to file so the next time he tries to view it faeces hits the air motion cooling device. Joy.
1147 * @return true if the gli is currently importing
1148 */
1149 public boolean isImporting() {
1150 return importing;
1151 }
1152
1153
1154 public void loadCollection(String collection_file_path)
1155 {
1156 // Display a modal progress popup to indicate that the collection is being loaded
1157 ModalProgressPopup load_collection_progress_popup = new ModalProgressPopup(Dictionary.get("CollectionManager.Loading_Collection"), Dictionary.get("CollectionManager.Loading_Collection_Please_Wait"));
1158 load_collection_progress_popup.display();
1159
1160 // Load the collection on a separate thread so the progress bar updates correctly
1161 (new LoadCollectionTask(collection_file_path, load_collection_progress_popup)).start();
1162 }
1163
1164
1165 private class LoadCollectionTask
1166 extends Thread
1167 {
1168 private String collection_file_path = null;
1169 private ModalProgressPopup load_collection_progress_popup = null;
1170
1171 public LoadCollectionTask(String collection_file_path, ModalProgressPopup load_collection_progress_popup)
1172 {
1173 this.collection_file_path = collection_file_path;
1174 this.load_collection_progress_popup = load_collection_progress_popup;
1175 }
1176
1177 public void run()
1178 {
1179 loadCollectionInternal(collection_file_path);
1180 load_collection_progress_popup.close();
1181 Gatherer.setMenuBarEnabled(true);
1182 }
1183 }
1184
1185
1186 /** Attempts to load the given collection. Currently uses simple serialization of the collection class.
1187 * @param location The path to the collection as a <strong>String</strong>.
1188 * @see org.greenstone.gatherer.Configuration
1189 * @see org.greenstone.gatherer.Gatherer
1190 * @see org.greenstone.gatherer.collection.Collection
1191 * @see org.greenstone.gatherer.util.Utility
1192 */
1193 private void loadCollectionInternal(String location)
1194 {
1195 DebugStream.println("Loading collection " + location + "...");
1196
1197
1198 // Check we have actually been given a .col file.
1199 if (!location.endsWith(".col")) {
1200 JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("CollectionManager.Not_Col_File", location), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);
1201 DebugStream.println("CollectionManager.loadCollection: Haven't been given a .col file.");
1202 return;
1203 }
1204
1205 // Check that the collection configuration file is available
1206 File collection_file = new File(location);
1207
1208 //String collection_name = collection_directory.getName();
1209 String collection_name = "";
1210 File collection_directory = collection_file.getParentFile();
1211
1212 // To get colname = (colgroup/)coltailname, subtract Gatherer.getCollectDirectoryPath() from collection_directory:
1213 int index = collection_directory.getAbsolutePath().indexOf(Gatherer.getCollectDirectoryPath());
1214 if(index == -1) {
1215 System.err.println("*** ERROR: collection directory " + collection_directory + " is not located in collect folder: " + Gatherer.getCollectDirectoryPath());
1216 } else {
1217 index += Gatherer.getCollectDirectoryPath().length();
1218 collection_name = collection_directory.getAbsolutePath().substring(index);
1219 }
1220
1221 if (Gatherer.isGsdlRemote) {
1222 if (Gatherer.remoteGreenstoneServer.downloadCollection(collection_name).equals("")) {
1223 return;
1224 }
1225 }
1226
1227 // Ensure that the collection directory exists
1228 if (collection_directory == null || !collection_directory.exists()) {
1229 // We can't open this
1230 System.err.println("CollectionManager.loadCollection: No collection directory.");
1231 return;
1232 }
1233
1234 String file_str = (Gatherer.GS3)? Utility.CONFIG_GS3_FILE : Utility.CONFIG_FILE;
1235 File collection_config_file = new File(collection_directory, file_str);
1236 if (!collection_config_file.exists()) {
1237 System.err.println("CollectionManager.loadCollection: No config file.");
1238 collection_directory = null;
1239 collection_config_file = null;
1240 return;
1241 }
1242
1243 // Ensure that an import directory exists for this collection
1244 File collection_import_directory = new File(collection_directory, "import");
1245 if (!collection_import_directory.exists()) {
1246 collection_import_directory.mkdir();
1247 }
1248
1249 // Special case of a user trying to open an old greenstone collection.
1250 boolean non_gli_collection = false;
1251 File collection_metadata_directory = new File(collection_directory, "metadata");
1252 if (!collection_metadata_directory.exists()) {
1253 DebugStream.println("Loading non-gatherer collection...");
1254 // Show a warning message in case user wants to quit now
1255 non_gli_collection = true;
1256 WarningDialog legacy_dialog = new WarningDialog("warning.LegacyCollection", Dictionary.get("LegacyCollection.Title"), Dictionary.get("LegacyCollection.Message"), null, true);
1257 if (legacy_dialog.display()==JOptionPane.CANCEL_OPTION) {
1258 legacy_dialog.dispose();
1259 collection_directory = null;
1260 collection_config_file = null;
1261 return;
1262 }
1263 legacy_dialog.dispose();
1264
1265 }
1266
1267 // Now determine if a lock already exists on this collection.
1268 File lock_file = new File(collection_file.getParentFile(), LOCK_FILE);
1269 if (lock_file.exists()) {
1270 LockFileDialog dialog = new LockFileDialog(Gatherer.g_man, collection_name, lock_file);
1271 int choice = dialog.getChoice();
1272 dialog.dispose();
1273 dialog = null;
1274
1275 if (choice != LockFileDialog.YES_OPTION) {
1276 // user has cancelled
1277 lock_file = null;
1278 collection_directory = null;
1279 collection_config_file = null;
1280 return;
1281 }
1282
1283 lock_file.delete();
1284 }
1285
1286 // now we are using gli.col - old colls may have used the collection name
1287 if (!collection_file.exists()) {
1288 File old_coll_file = new File(collection_directory, collection_name+".col");
1289 if (old_coll_file.exists()) {
1290 try {
1291 old_coll_file.renameTo(collection_file);
1292 } catch (Exception e) {
1293 DebugStream.println("Couldn't rename "+old_coll_file.getName()+" to gli.col. Will just carry on with default gli.col");
1294 // but just carry on.
1295 }
1296 }
1297 }
1298
1299 try {
1300 // Create a lock file.
1301 createLockFile(lock_file);
1302 // This lock file may not have been created so check
1303 if(!lock_file.canWrite()) {
1304 // The lock file cannot be written to. Most likely cause incorrect file permissions.
1305 System.err.println("Cannot write lock file!");
1306 String args[] = new String[2];
1307 args[0] = location;
1308 args[1] = Dictionary.get("FileActions.Write_Not_Permitted_Message", new String[]{lock_file.getAbsolutePath()});
1309 if(Gatherer.client_operating_system.toUpperCase().indexOf("WINDOWS")!=-1){
1310 //if(Gatherer.client_operating_system.toUpperCase().indexOf("VISTA")!=-1){
1311 args[1] += Dictionary.get("FileActions.File_Permission_Detail", new String[]{Configuration.gsdl_path, System.getProperty("user.name")});
1312 //}
1313 }
1314 JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("CollectionManager.Cannot_Open_With_Reason", args), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);
1315 args = null;
1316 return;
1317 }
1318
1319 // need to fix this up as currently it craps out if the .col file is not there, which is may not always be.
1320 if (canDoScheduling() && collection_file.exists()) {
1321 //THIS LOOKS LIKE THE BEST PLACE TO TRY AND UPDATE .col FILES FOR EXISTING COLLECTIONS...Wendy
1322 // Don't need to update anything if collection_file doesn't exist yet.
1323 //First, see if "Schedule" exists in the XMl File...
1324 BufferedReader bir = new BufferedReader(new FileReader(collection_file));
1325 boolean flag = false;
1326 try {
1327 String stmp = new String();
1328
1329 while((stmp = bir.readLine()) != null) {
1330 stmp = stmp.trim();
1331 if(stmp.equals("<Schedule>") || stmp.equals("<Schedule/>")) {
1332 flag = true;
1333 break;
1334 }
1335 }
1336 bir.close();
1337
1338 } catch (IOException ioe) {
1339 DebugStream.printStackTrace(ioe);
1340 }
1341
1342 //modify if old .col (i.e. no Schedule exists in XML file)
1343 if(!flag) {
1344 File new_collection_file = new File(collection_directory.getAbsolutePath() + "/tmp.col");
1345
1346
1347 BufferedWriter bor = new BufferedWriter(new FileWriter(new_collection_file));
1348 bir = new BufferedReader(new FileReader(collection_file));
1349
1350 try {
1351 String stmp = new String();
1352 while((stmp = bir.readLine()) != null) {
1353 String stmp2 = stmp.trim();
1354 if(stmp2.startsWith("<!ELEMENT Argument")) {
1355 bor.write(" <!ELEMENT Schedule (Arguments*)>\n");
1356 }
1357 else if(stmp2.equals("</BuildConfig>")) {
1358 bor.write(" <Schedule/>\n");
1359 }
1360
1361 bor.write(stmp + "\n");
1362
1363 }
1364 bir.close();
1365 bor.close();
1366 } catch (IOException ioe) {
1367 DebugStream.printStackTrace(ioe);
1368 }
1369
1370 //copy over tmp.col to replace
1371 try {
1372 collection_file.delete();
1373 new_collection_file.renameTo(collection_file);
1374 } catch (Exception e) {
1375 DebugStream.printStackTrace(e);
1376 }
1377 }
1378 }
1379
1380 // Open the collection file
1381 this.collection = new Collection(collection_file);
1382 if (collection.error) {
1383 collection = null;
1384 // Remove lock file
1385 if (lock_file.exists()) {
1386 lock_file.delete();
1387 }
1388 throw(new Exception(Dictionary.get("CollectionManager.Missing_Config"))); // this error message does not agree with the error
1389 }
1390
1391 if (canDoScheduling()) {
1392 scheduling();
1393 }
1394
1395 // These may have been set in the past, but are no longer used
1396 // by GLI
1397 collection.import_options.removeValue("removeold");
1398 collection.import_options.removeValue("keepold");
1399
1400 MetadataSetManager.clearMetadataSets();
1401 MetadataSetManager.loadMetadataSets(collection_metadata_directory);
1402
1403 // Make sure we always have the extracted metadata set
1404 addRequiredMetadataSets();
1405
1406 ProfileXMLFileManager.loadProfileXMLFile(collection_metadata_directory);
1407
1408 // If this is a non-GLI (legacy) collection, load the default metadata sets
1409 if (non_gli_collection) {
1410 addDefaultMetadataSets();
1411
1412 // Recurse the import folder tree, backing up the metadata.xml files before they are edited
1413 LegacyCollectionImporter.backupMetadataXMLFiles(collection_directory);
1414 }
1415
1416 // Read through the metadata.xml files in the import directory, building up the metadata value trees
1417 MetadataXMLFileManager.clearMetadataXMLFiles();
1418 MetadataXMLFileManager.loadMetadataXMLFiles(collection_import_directory,collection.toSkimFile());
1419
1420
1421 // get rid of the previous scan through docxml files
1422 DocXMLFileManager.clearDocXMLFiles();
1423
1424 if (Configuration.fedora_info.isActive()) { // FLI case
1425 // Read through the docmets.xml files in the export directory
1426 File collection_export_directory = new File(getLoadedCollectionExportDirectoryPath());
1427 DocXMLFileManager.loadDocXMLFiles(collection_export_directory,"docmets.xml");
1428 }
1429 else {
1430 // Read through the doc.xml files in the archives directory
1431 File collection_archives_directory = new File(getLoadedCollectionArchivesDirectoryPath());
1432 DocXMLFileManager.loadDocXMLFiles(collection_archives_directory,"doc.xml");
1433 }
1434
1435
1436 // Get a list of the collection specific classifiers and plugins
1437 Classifiers.loadClassifiersList(collection_name);
1438 Plugins.loadPluginsList(collection_name);
1439
1440 collection.cdm = new CollectionDesignManager(collection_config_file);
1441 if (non_gli_collection) {
1442 // Change the classifiers to use the namespaced element names
1443 LegacyCollectionImporter.updateClassifiers(collection.cdm);
1444 }
1445
1446 // We're done. Let everyone know.
1447 DebugStream.println(Dictionary.get("CollectionManager.Loading_Successful", collection_name));
1448 Gatherer.refresh(Gatherer.COLLECTION_OPENED);
1449 }
1450 catch (Exception error) {
1451 // There is obviously no existing collection present.
1452 DebugStream.printStackTrace(error);
1453 error.printStackTrace();
1454 if(error.getMessage() != null) {
1455 String[] args = new String[2];
1456 args[0] = location;
1457 args[1] = error.getMessage();
1458 //args[1] = "The Librarian Interface does not have permission to write to... Please check file permissions.";
1459 JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("CollectionManager.Cannot_Open_With_Reason", args), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);
1460 }
1461 else {
1462 JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("CollectionManager.Cannot_Open", location), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);
1463 }
1464 }
1465
1466 lock_file = null;
1467 collection_directory = null;
1468 collection_config_file = null;
1469 }
1470
1471 /** At present, scheduling only works for GS2, only when GS2 is local and only when GLI runs from
1472 * within a GS2 installation. This method can be adjusted as scheduling becomes available for more
1473 * more situations. */
1474 public static boolean canDoScheduling() {
1475 // Would be nice to support more of these, rather than returning false
1476 if(Gatherer.isGsdlRemote) {
1477 return false;
1478 }
1479 if(Gatherer.GS3) {
1480 return false;
1481 }
1482 if (Configuration.fedora_info.isActive()) {
1483 return false;
1484 }
1485
1486 // GS2's etc/main.cfg is necessary for scheduling, but scheduling looks for it locally:
1487 // it assumes GLI is inside a GS2 installation
1488 File mcfg = new File(LocalGreenstone.getDirectoryPath() + File.separator + "etc" + File.separator + "main.cfg");
1489 if(!mcfg.exists()) {
1490 System.out.println("Cannot do scheduling, since there is no file: " + mcfg.getAbsolutePath()
1491 + ".\nScheduling presently depends on GLI running from inside a GS2.");
1492 return false;
1493 }
1494
1495 return true;
1496 }
1497
1498 private void makeCollection(String name, String email)
1499 {
1500 // Generate the mkcol.pl command
1501 ArrayList command_parts_list = new ArrayList();
1502 if (!Gatherer.isGsdlRemote) {
1503 command_parts_list.add(Configuration.perl_path);
1504 command_parts_list.add("-S");
1505 }
1506 command_parts_list.add(scriptPath + "mkcol.pl");
1507 if(Gatherer.GS3) {
1508 command_parts_list.add(Utility.GS3MODE_ARGUMENT); // add '-gs3mode'
1509 }
1510 if(!Gatherer.isGsdlRemote) {
1511 command_parts_list.add("-collectdir");
1512 command_parts_list.add(getDefaultCollectDirectory());
1513 }
1514 command_parts_list.add("-win31compat");
1515 command_parts_list.add((Gatherer.isGsdlRemote) ? "false" : "true");
1516
1517 if (email != null && !email.equals("")) {
1518 command_parts_list.add("-creator");
1519 command_parts_list.add(email);
1520 }
1521
1522 command_parts_list.add(name);
1523
1524 // Run the mkcol.pl command
1525 String[] command_parts = (String[]) command_parts_list.toArray(new String[0]);
1526 //for(int i = 0; i < command_parts.length; i++) {
1527 ///ystem.err.println("\""+command_parts[i]+"\"");
1528 //}
1529
1530 GShell process = new GShell(command_parts, GShell.NEW, COLLECT, this, null, GShell.GSHELL_NEW);
1531 process.run(); // Don't bother threading this... yet
1532 }
1533
1534
1535 /** Any implementation of GShellListener must include this method to allow the GShell to send messages to listeners. However in this case the CollectionManager is in no way interested in what the messages are, just the import events which have a specific type and are handled elsewhere. Thus we can safely ignore this event.
1536 * @param event A <strong>GShellEvent</strong> which contains a the message.
1537 */
1538 public synchronized void message(GShellEvent event) {
1539
1540 }
1541
1542
1543 public void metadataChanged(CollectionTreeNode[] file_nodes)
1544 {
1545 if (collection != null) {
1546 collection.setMetadataChanged(true);
1547 }
1548 }
1549
1550
1551 public void openCollectionFromLastTime() {
1552 // If there was an open collection last session, reopen it
1553 if (Gatherer.open_collection_file_path != null) {
1554 // Load the collection now
1555 loadCollection(Gatherer.open_collection_file_path);
1556 }
1557
1558 }
1559
1560
1561 /** This call is fired whenever a process within a GShell created by this class begins.
1562 * @param event A <strong>GShellEvent</strong> containing information about the GShell process.
1563 * @see org.greenstone.gatherer.Gatherer
1564 * @see org.greenstone.gatherer.gui.GUIManager
1565 * @see org.greenstone.gatherer.shell.GShell
1566 */
1567 public synchronized void processBegun(GShellEvent event) {
1568 DebugStream.println("CollectionManager.processBegun(" + event.getType() + ")");
1569 ///ystem.err.println("ProcessBegun " + event.getType());
1570 // If this is one of the types where we wish to lock user control
1571 Gatherer.g_man.lockCollection((event.getType() == GShell.IMPORT), true);
1572 }
1573 /** This call is fired whenever a process within a GShell created by this class ends.
1574 * @param event A <strong>GShellEvent</strong> containing information about the GShell process.
1575 * @see org.greenstone.gatherer.Gatherer
1576 * @see org.greenstone.gatherer.gui.GUIManager
1577 * @see org.greenstone.gatherer.shell.GShell
1578 */
1579 public synchronized void processComplete(GShellEvent event) {
1580 //ystem.err.println("CollectionManager.processComplete(" + event.getType() + ")");
1581 Gatherer.g_man.lockCollection((event.getType() == GShell.IMPORT), false);
1582 ///ystem.err.println("Received process complete event - " + event);
1583 // If we were running an import, now run a build.
1584 if(event.getType() == GShell.IMPORT && event.getStatus() == GShell.OK) {
1585 // Finish import.
1586 collection.setImported(true);
1587 collection.setFilesChanged(false);
1588 collection.setMetadataChanged(false);
1589 buildCollection();
1590 }
1591 else if(event.getType() == GShell.SCHEDULE && event.getStatus() == GShell.OK ) {
1592
1593 WarningDialog collection_built_warning_dialog = new WarningDialog("warning.ScheduleBuilt", Dictionary.get("ScheduleBuilt.Title"), Dictionary.get("ScheduleBuilt.Message"), null, false);
1594 collection_built_warning_dialog.setMessageOnly(true); // Not a warning
1595 collection_built_warning_dialog.display();
1596 collection_built_warning_dialog.dispose();
1597 collection_built_warning_dialog = null;
1598 }
1599 // If we were running a build, now is when we move files across.
1600 else if(event.getType() == GShell.BUILD && event.getStatus() == GShell.OK) {
1601
1602 if ( CollectionDesignManager.buildcolWasFull() ) {
1603 if(installCollection()) {
1604 // If we have a local library running then ask it to add our newly create collection
1605 if (LocalLibraryServer.isRunning() == true) {
1606 LocalLibraryServer.addCollection(collection.getName());
1607 }
1608 else if (Gatherer.GS3) {
1609 //xiao comment out this: convertToGS3Collection();
1610 Gatherer.configGS3Server(Configuration.site_name, ServletConfiguration.ADD_COMMAND + collection.getName());
1611 }
1612
1613 // Fire a collection changed first to update the preview etc buttons
1614 Gatherer.refresh(Gatherer.COLLECTION_REBUILT);
1615
1616 // Now display a message dialog saying its all built
1617 WarningDialog collection_built_warning_dialog = new WarningDialog("warning.CollectionBuilt", Dictionary.get("CollectionBuilt.Title"), Dictionary.get("CollectionBuilt.Message"), null, false);
1618 collection_built_warning_dialog.setMessageOnly(true); // Not a warning
1619 collection_built_warning_dialog.display();
1620 collection_built_warning_dialog.dispose();
1621 collection_built_warning_dialog = null;
1622
1623 //Set nothing as needing rebuilding, as a build has just finished :-)
1624 CollectionDesignManager.resetRebuildTypeRequired();
1625 }
1626 else {
1627 JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("CollectionManager.Preview_Ready_Failed"), Dictionary.get("CollectionManager.Preview_Ready_Title"), JOptionPane.ERROR_MESSAGE);
1628 Gatherer.refresh(Gatherer.COLLECTION_REBUILT);
1629 DebugStream.println("Status is ok but !installCollection()");
1630 }
1631 }
1632 }
1633 else if (event.getStatus() == GShell.CANCELLED) {
1634 JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("CollectionManager.Build_Cancelled"), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);
1635 Gatherer.g_man.repaint();
1636 }
1637 else if (event.getStatus() == GShell.ERROR) {
1638 if (event.getType() == GShell.NEW) {
1639 String name = event.getMessage();
1640 String collectDir = getCollectionDirectoryPath(name);
1641 String errMsg = "";
1642 if (!new File(getCollectionDirectoryPath(name)).exists() || !new File(getCollectionDirectoryPath(name)).canWrite()) {
1643 String reason = Dictionary.get("FileActions.Write_Not_Permitted_Message", new String[]{collectDir});
1644 errMsg = Dictionary.get("CollectionManager.Cannot_Create_Collection_With_Reason", new String[]{reason});
1645 if(Gatherer.client_operating_system.toUpperCase().indexOf("WINDOWS") != -1){
1646 //if(Gatherer.client_operating_system.toUpperCase().indexOf("VISTA")!=-1){
1647 errMsg += Dictionary.get("FileActions.File_Permission_Detail", new String[]{Configuration.gsdl_path, System.getProperty("user.name")});
1648 //}
1649 }
1650 } else {
1651 errMsg = Dictionary.get("CollectionManager.Cannot_Create_Collection");
1652 }
1653 JOptionPane.showMessageDialog(Gatherer.g_man, errMsg, Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);
1654 }
1655 else if(event.getType() == GShell.SCHEDULE) {
1656 JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("CollectionManager.Schedule_Failed"), Dictionary.get("CollectionManager.Schedule_Ready_Title"), JOptionPane.ERROR_MESSAGE);
1657 }
1658 else {
1659 JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("CollectionManager.Preview_Ready_Failed"), Dictionary.get("CollectionManager.Preview_Ready_Title"), JOptionPane.ERROR_MESSAGE);
1660 Gatherer.refresh(Gatherer.COLLECTION_REBUILT);
1661 }
1662
1663 Gatherer.g_man.repaint(); // It appears Java's own dialogs have the same not always painting correct area bug that I suffer from. Well I don't suffer from it personally, but my ModalDialog components do.
1664 }
1665 }
1666
1667
1668 /** Determine if the manager is ready for actions apon its collection.
1669 * @return A <i>boolean</i> which is <i>true</i> to indicate a collection has been loaded and thus the collection is ready for editing, <i>false</i> otherwise.
1670 */
1671 static public synchronized boolean ready() {
1672 if(collection != null) {
1673 return true;
1674 }
1675 else {
1676 return false;
1677 }
1678 }
1679
1680
1681 /** This method associates the collection build monitor with the build monitor created in CreatePane.
1682 * @param monitor A <strong>GShellProgressMonitor</strong> which we will use as the build monitor.
1683 */
1684 public void registerBuildMonitor(GShellProgressMonitor monitor) {
1685 build_monitor = monitor;
1686 }
1687 /** This method associates the collection import monitor with the import monitor created in CreatePane.
1688 * @param monitor A <strong>GShellProgressMonitor</strong> which we will use as the import monitor.
1689 */
1690 public void registerImportMonitor(GShellProgressMonitor monitor) {
1691 import_monitor = monitor;
1692 }
1693
1694 public void registerScheduleMonitor(GShellProgressMonitor monitor) {
1695 schedule_monitor = monitor;
1696 }
1697
1698
1699 static public void removeCollectionContentsChangedListener(CollectionContentsChangedListener listener)
1700 {
1701 collection_contents_changed_listeners.remove(listener);
1702 }
1703
1704
1705 public void removeMetadataSet(MetadataSet metadata_set)
1706 {
1707 DebugStream.println("Removing metadata set...");
1708
1709 // Delete the .mds file from the collection's "metadata" folder...
1710 File metadata_set_file = metadata_set.getMetadataSetFile();
1711
1712 // ...but not if it is the "ex.mds" file
1713 if (metadata_set_file.getName().equals("ex.mds")) {
1714 return;
1715 }
1716
1717 // ...and only if it exists
1718 if (metadata_set_file.exists()) {
1719 metadata_set_file.delete();
1720
1721 // Unload it from the MetadataSetManager
1722 MetadataSetManager.unloadMetadataSet(metadata_set);
1723
1724 // If we're using a remote Greenstone server, delete the metadata file on the server
1725 if (Gatherer.isGsdlRemote) {
1726 Gatherer.remoteGreenstoneServer.deleteCollectionFile(collection.getGroupQualifiedName(false), metadata_set_file);
1727 }
1728 }
1729 }
1730
1731
1732 /** Used to check whether all open collections have a 'saved' state.
1733 * @return A <i>boolean</i> which is <i>true</i> if the collection has been saved.
1734 * @see org.greenstone.gatherer.collection.Collection
1735 */
1736 public boolean saved() {
1737 boolean result = true;
1738 if(collection != null) {
1739 result = collection.getSaved();
1740 }
1741 return result;
1742 }
1743
1744
1745 /** Saves the currently loaded collection. */
1746 public void saveCollection()
1747 {
1748
1749 if (collection == null) return;
1750
1751 DebugStream.println("Saving collection " + collection.getName() + "...");
1752
1753 // Change cursor to hourglass
1754 Gatherer.g_man.wait(true);
1755
1756 // Create a backup of the collection file, just in case anything goes wrong
1757 File collection_file = new File(getLoadedCollectionColFilePath());
1758 if (collection_file.exists()) {
1759 File collection_file_backup = new File(collection_file.getAbsolutePath() + "~");
1760 if (!collection_file.renameTo(collection_file_backup)) {
1761 DebugStream.println("Error in CollectionManager.saveCollection(): could not create backup file.");
1762 }
1763 collection_file_backup.deleteOnExit();
1764 }
1765
1766 // Write out the collection file
1767 collection.save();
1768
1769 // Write out the collection configuration file
1770 collection.cdm.save();
1771
1772 // Change cursor back to normal
1773 Gatherer.g_man.wait(false);
1774 }
1775
1776
1777 /** I started giving the user the choice of using an existing meta set or creating a new one. The second option being so that they didn't have to add/merge/ignore each element, they could all be added automatically. However, I am not sure where the merge prompt gets called from, and it is not essential, so I am leaving it for now - it should be added back in and finished. [kjdon] */
1778 // now add in greenstone metadata set too.
1779 private void addDefaultMetadataSets()
1780 {
1781 // Add dublin core which is the default metadata set. The user
1782 // can change this later
1783 File dc_file = new File(Gatherer.getGLIMetadataDirectoryPath()+"dublin.mds");
1784 if (dc_file.exists()) {
1785 importMetadataSet(new MetadataSet(dc_file));
1786 }
1787 File gs_file = new File(Gatherer.getGLIMetadataDirectoryPath()+"greenstone.mds");
1788 if (gs_file.exists()) {
1789 importMetadataSet(new MetadataSet(gs_file));
1790 }
1791 }
1792
1793
1794 private void addRequiredMetadataSets()
1795 {
1796 // Always import the extracted metadata set
1797 File extracted_metadata_set_file = new File(Gatherer.getGLIMetadataDirectoryPath() + MetadataSetManager.EXTRACTED_METADATA_NAMESPACE + StaticStrings.METADATA_SET_EXTENSION);
1798 importMetadataSet(new MetadataSet(extracted_metadata_set_file));
1799 }
1800
1801 private String getDefaultCollectDirectory() {
1802 String collect_dir = Gatherer.getCollectDirectoryPath();
1803 // Remove erroneous file windows file separator as it causes problems when running import.pl
1804 if(collect_dir.length() > 2 && collect_dir.endsWith("\\")) {
1805 collect_dir = collect_dir.substring(0, collect_dir.length() - 1);
1806 }
1807 return collect_dir;
1808 }
1809
1810 // used as arg in the perl scripts
1811 private String getCollectDirectory() {
1812 String collect_dir = Gatherer.getCollectDirectoryPath();
1813 return collect_dir.substring(0, collect_dir.length()-1); // remove trailing slash
1814
1815 // the following will stick any colgroup at the end of the collect directory, making it no longer
1816 // possible to get the real collect dir in a general manner if this were located outside greenstone
1817 //String collect_dir = collection.getCollectionDirectory().getParentFile().getPath();
1818 //return collect_dir;
1819 }
1820
1821
1822 /** Install collection by moving its files from building to index after a successful build.
1823 * @see org.greenstone.gatherer.Gatherer
1824 * @see org.greenstone.gatherer.util.Utility
1825 */
1826 private boolean installCollection()
1827 {
1828 if (Configuration.fedora_info.isActive()) {
1829 DebugStream.println("Fedora build complete. No need to move files.");
1830 return true;
1831 }
1832
1833
1834 DebugStream.println("Build complete. Moving files.");
1835
1836 try {
1837 // Ensure that the local library has released this collection so we can delete the index directory
1838 if (LocalLibraryServer.isRunning() == true) {
1839 LocalLibraryServer.releaseCollection(collection.getName());
1840 }
1841 // deactivate it in tomcat so that windows will release the index files
1842 if (Gatherer.GS3 && !Gatherer.isGsdlRemote) {
1843 Gatherer.configGS3Server(Configuration.site_name, ServletConfiguration.DEACTIVATE_COMMAND + collection.getName());
1844 }
1845 File index_dir = new File(getLoadedCollectionIndexDirectoryPath());
1846 DebugStream.println("Index = " + index_dir.getAbsolutePath());
1847
1848 File building_dir = new File(getLoadedCollectionBuildingDirectoryPath());
1849 DebugStream.println("Building = " + building_dir.getAbsolutePath());
1850
1851 // Get the build mode from the build options
1852 String build_mode = collection.build_options.getValue("mode");
1853
1854 // Special case for build mode "all": replace index dir with building dir
1855 if (build_mode == null || build_mode.equals(Dictionary.get("CreatePane.Mode_All"))) {
1856 // Remove the old index directory
1857 if (index_dir.exists()) {
1858 Utility.delete(index_dir);
1859
1860 // Wait for a couple of seconds, just for luck
1861 wait(2000);
1862
1863 // Check the delete worked
1864 if (index_dir.exists()) {
1865 throw new Exception(Dictionary.get("CollectionManager.Index_Not_Deleted"));
1866 }
1867 }
1868
1869 if (Gatherer.isGsdlRemote) {
1870 Gatherer.remoteGreenstoneServer.deleteCollectionFile(
1871 collection.getGroupQualifiedName(false), new File(getLoadedCollectionIndexDirectoryPath()));
1872 Gatherer.remoteGreenstoneServer.moveCollectionFile(collection.getGroupQualifiedName(false),
1873 new File(getLoadedCollectionBuildingDirectoryPath()), new File(getLoadedCollectionIndexDirectoryPath()));
1874 }
1875
1876 // Move the building directory to become the new index directory
1877 if (building_dir.renameTo(index_dir) == false) {
1878 throw new Exception(Dictionary.get("CollectionManager.Build_Not_Moved"));
1879 }
1880 }
1881
1882 // Otherwise copy everything in the building dir into the index dir
1883 else {
1884 moveContentsInto(building_dir, index_dir);
1885 }
1886 }
1887 catch (Exception exception) {
1888 JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("CollectionManager.Install_Exception", exception.getMessage()), "Error", JOptionPane.ERROR_MESSAGE);
1889 return false;
1890 }
1891 return true;
1892 }
1893
1894
1895 /** Moves all the files in one directory into another, overwriting existing files */
1896 private void moveContentsInto(File source_directory, File target_directory)
1897 {
1898 File[] source_files = source_directory.listFiles();
1899 for (int i = 0; i < source_files.length; i++) {
1900 File source_file = source_files[i];
1901 File target_file = new File(target_directory, source_file.getName());
1902
1903 if (source_file.isDirectory()) {
1904 moveContentsInto(source_file, target_file);
1905 source_file.delete();
1906 }
1907 else {
1908 if (target_file.exists()) {
1909 target_file.delete();
1910 }
1911
1912 source_file.renameTo(target_file);
1913 }
1914 }
1915 }
1916
1917 private void updateCollectionConfigXML(File base_cfg, File new_cfg) {
1918 //In this method, the files base_cfg and new_cfg are all xml files.
1919
1920 Document base_cfg_doc = XMLTools.parseXMLFile(base_cfg);
1921 XMLTools.writeXMLFile(new_cfg, base_cfg_doc);
1922 Document new_cfg_doc = XMLTools.parseXMLFile(new_cfg);
1923 Element collection_config = new_cfg_doc.getDocumentElement();
1924
1925 Node browseNode = XMLTools.getChildByTagNameIndexed(collection_config, StaticStrings.BROWSE_STR, 0);
1926 NodeList classifier_children = ((Element)browseNode).getElementsByTagName(StaticStrings.CLASSIFIER_STR);
1927 int num_nodes = classifier_children.getLength();
1928
1929 if (num_nodes < 1) {
1930 return;
1931 }
1932
1933 // Read in the classifier command watching for hfile, metadata and sort arguments.
1934 String buttonname = null;
1935 String hfile = null;
1936 String metadata = null;
1937 String sort = null;
1938
1939 for (int i=0; i<num_nodes; i++) {
1940 Element classifier_element = (Element)classifier_children.item(i);
1941 NodeList option_children = classifier_element.getElementsByTagName(StaticStrings.OPTION_STR);
1942 for (int j=0; j<option_children.getLength(); j++) {
1943 Element option_element = (Element)option_children.item(j);
1944 String name_str = option_element.getAttribute(StaticStrings.NAME_ATTRIBUTE);
1945 String value_str = option_element.getAttribute(StaticStrings.VALUE_ATTRIBUTE);
1946
1947 if (name_str == null || name_str.equals("")) {
1948 continue;
1949 }
1950 if (name_str != null && value_str == null ) {
1951 value_str = "";
1952 }
1953 if (name_str.equals("hfile")) {
1954 hfile = value_str;
1955 }
1956 else if (name_str.equals("metadata") && value_str != null) {
1957 String replacement = ProfileXMLFileManager.getMetadataElementFor(value_str);
1958 if (replacement != null && !replacement.equals("")) {
1959 metadata = replacement;
1960 }
1961 }
1962 else if (name_str.equals("sort") && value_str != null) {
1963 String replacement = ProfileXMLFileManager.getMetadataElementFor(value_str);
1964 if (replacement != null && !replacement.equals("")) {
1965 sort = replacement;
1966 }
1967 }
1968 else if(name_str.equals("buttonname") && value_str != null) {
1969 buttonname = value_str;
1970 }
1971 }
1972 }
1973 for (int i=0; i<num_nodes; i++) {
1974 Element classifier_element = (Element)classifier_children.item(i);
1975 NodeList option_children = classifier_element.getElementsByTagName(StaticStrings.OPTION_STR);
1976 for (int j=0; j<option_children.getLength(); j++) {
1977 Element option_element = (Element)option_children.item(j);
1978 String name_str = option_element.getAttribute(StaticStrings.NAME_ATTRIBUTE);
1979
1980 if (name_str.equals("metadata") && metadata != null) {
1981 option_element.setAttribute(StaticStrings.VALUE_ATTRIBUTE, metadata);
1982 }
1983 else if (name_str.equals("hfile") && hfile != null) {
1984 option_element.setAttribute(StaticStrings.VALUE_ATTRIBUTE, metadata + ".txt");
1985 }
1986 else if (name_str.equals("sort") && sort != null) {
1987 option_element.setAttribute(StaticStrings.VALUE_ATTRIBUTE, sort);
1988 }
1989 else if(name_str.equals("buttonname") && (buttonname == "" || buttonname == null)) {
1990 // No buttonname has been specified. Lets create one using the metadata as its value
1991 Element option = new_cfg_doc.createElement(StaticStrings.OPTION_STR);
1992 option.setAttribute(StaticStrings.NAME_ATTRIBUTE, "buttonname");
1993 option_element.setAttribute(StaticStrings.VALUE_ATTRIBUTE, metadata);
1994 classifier_element.appendChild(option);
1995 }
1996 }
1997 }
1998 }
1999
2000 private void updateCollectionCFG(File base_cfg, File new_cfg, String description, String email, String title)
2001 {
2002 boolean first_name = true;
2003 boolean first_extra = true;
2004
2005 // Now read in base_cfg line by line, parsing important onces and/or replacing them with information pertinent to our collection. Each line is then written back out to the new collect.cfg file.
2006 try {
2007 BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(base_cfg), "UTF-8"));
2008 BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new_cfg), "UTF-8"));
2009 String command = null;
2010 while((command = in.readLine()) != null) {
2011 if (command.length()==0) {
2012 // output a new line
2013 out.newLine();
2014 continue;
2015 }
2016 // We have to test the end of command for the special character '\'. If found, remove it and append the next line, then repeat.
2017 while(command.trim().endsWith("\\")) {
2018 command = command.substring(0, command.lastIndexOf("\\"));
2019 String next_line = in.readLine();
2020 if(next_line != null) {
2021 command = command + next_line;
2022 }
2023 }
2024 // commands can extend over more than one line so use the CommandTokenizer which takes care of that
2025 CommandTokenizer tokenizer = new CommandTokenizer(command, in, false);
2026 String command_type_str = tokenizer.nextToken().toLowerCase();
2027
2028 if (command_type_str.equals(StaticStrings.COLLECTIONMETADATA_STR)) {
2029 // read the whole thing in, but for collectionname, collectionextra, iconcollection, iconcollectionsmall we will ignore them
2030 StringBuffer new_command = new StringBuffer(command_type_str);
2031 String meta_name = tokenizer.nextToken();
2032 new_command.append(' ');
2033 new_command.append(meta_name);
2034 while (tokenizer.hasMoreTokens()) {
2035 new_command.append(' ');
2036 new_command.append(tokenizer.nextToken());
2037 }
2038 if (meta_name.equals(StaticStrings.COLLECTIONMETADATA_COLLECTIONNAME_STR) || meta_name.equals(StaticStrings.COLLECTIONMETADATA_COLLECTIONEXTRA_STR) || meta_name.equals(StaticStrings.COLLECTIONMETADATA_ICONCOLLECTION_STR) || meta_name.equals(StaticStrings.COLLECTIONMETADATA_ICONCOLLECTIONSMALL_STR)) {
2039 // dont save
2040 } else {
2041 write(out, new_command.toString());
2042 }
2043 new_command = null;
2044 continue;
2045 } // if collectionmeta
2046
2047 if (command_type_str.equals("classify")) {
2048 StringBuffer text = new StringBuffer(command_type_str);
2049 // Read in the classifier command watching for hfile, metadata and sort arguments.
2050 String buttonname = null;
2051 String hfile = null;
2052 String new_metadata = null;
2053 String old_metadata = null;
2054
2055 while(tokenizer.hasMoreTokens()) {
2056 String token = tokenizer.nextToken();
2057 if (token.equals("-hfile")) {
2058 if(tokenizer.hasMoreTokens()) {
2059 text.append(" ");
2060 text.append(token);
2061 token = tokenizer.nextToken();
2062 hfile = token;
2063 }
2064 }
2065 else if (token.equals("-metadata")) {
2066 if(tokenizer.hasMoreTokens()) {
2067 text.append(" ");
2068 text.append(token);
2069 String temp_metadata = tokenizer.nextToken();
2070 String replacement = ProfileXMLFileManager.getMetadataElementFor(temp_metadata);
2071 if (replacement != null && !replacement.equals("")) {
2072 token = replacement;
2073 old_metadata = temp_metadata;
2074 new_metadata = replacement;
2075 }
2076 else {
2077 token = temp_metadata;
2078 }
2079 temp_metadata = null;
2080 replacement = null;
2081 }
2082 }
2083 else if (token.equals("-sort")) {
2084 if(tokenizer.hasMoreTokens()) {
2085 text.append(" ");
2086 text.append(token);
2087 String temp_metadata = tokenizer.nextToken();
2088 String replacement = ProfileXMLFileManager.getMetadataElementFor(temp_metadata);
2089 if (replacement != null && !replacement.equals("")) {
2090 token = replacement;
2091 }
2092 else {
2093 token = temp_metadata;
2094 }
2095 temp_metadata = null;
2096 replacement = null;
2097 }
2098 }
2099 else if(token.equals("-buttonname")) {
2100 buttonname = token;
2101 }
2102 text.append(' ');
2103 text.append(token);
2104 token = null;
2105 }
2106
2107 // If we replaced the metadata argument and didn't encounter a buttonname, then add one now pointing back to the old metadata name in order to accomodate macro files which required such names (buttonname is metadata name by default)!
2108 if(old_metadata != null && new_metadata != null && buttonname == null) {
2109 text.append(' ');
2110 text.append("-buttonname");
2111 text.append(' ');
2112 text.append(old_metadata);
2113 }
2114 command = text.toString();
2115 // Replace the hfile if we found it
2116 if(hfile != null && new_metadata != null) {
2117 command = command.replaceAll(hfile, new_metadata + ".txt");
2118 }
2119
2120 buttonname = null;
2121 hfile = null;
2122 new_metadata = null;
2123 old_metadata = null;
2124 write(out, command);
2125 } else {
2126 // the rest of the commands just want a string - we read in all the tokens from the tokeniser and get rid of it.
2127 StringBuffer new_command = new StringBuffer(command_type_str);
2128 while (tokenizer.hasMoreTokens()) {
2129 new_command.append(' ');
2130 new_command.append(tokenizer.nextToken());
2131 }
2132
2133 command = new_command.toString();
2134
2135 // There is still one special case, that of the format command. In such a command we have to search for [<target>] to ensure we don't change parts of the format which have nothing to do with the metadata elements.
2136 // we really want to build up the whole command here
2137 boolean format_command = command_type_str.equals("format");
2138 HashMap metadata_mapping = ProfileXMLFileManager.getMetadataMapping();
2139 if (metadata_mapping != null) {
2140 Iterator keys = metadata_mapping.keySet().iterator();
2141 while (keys.hasNext()) {
2142 String target = (String) keys.next();
2143 String replacement = (String) metadata_mapping.get(target);
2144 if (replacement != null && !replacement.equals("")) {
2145 if (format_command) {
2146 target = "\\[" + target + "\\]";
2147 replacement = "{Or}{[" + replacement + "]," + target + "}";
2148 }
2149 command = command.replaceAll(target, replacement);
2150 }
2151 }
2152 }
2153
2154 write(out, command);
2155 }
2156 tokenizer = null;
2157 }
2158 in.close();
2159 in = null;
2160 out.flush();
2161 out.close();
2162 out = null;
2163 }
2164 catch(Exception error) {
2165 DebugStream.printStackTrace(error);
2166 }
2167 // All done, I hope.
2168 }
2169
2170 private void write(BufferedWriter out, String message)
2171 throws Exception {
2172 out.write(message, 0, message.length());
2173 out.newLine();
2174 }
2175
2176
2177 /** The CollectionManager class is getting too confusing by half so I'll implement this TreeModelListener in a private class to make responsibility clear. */
2178 private class FMTreeModelListener
2179 implements TreeModelListener {
2180 /** Any action that changes one of the tree models within a collection, which are the only models we listen to, mean the collections contents have changed and so saved should be set to false.
2181 * @param event A <strong>TreeModelEvent</strong> encompassing all the information about the event which has changed the tree.
2182 */
2183 public void treeNodesChanged(TreeModelEvent event) {
2184 if(collection != null) {
2185 collection.setSaved(false);
2186 collection.setFilesChanged(true);
2187 }
2188 }
2189 /** Any action that changes one of the tree models within a collection, which are the only models we listen to, mean the collections contents have changed and so saved should be set to false.
2190 * @param event A <strong>TreeModelEvent</strong> encompassing all the information about the event which has changed the tree.
2191 */
2192 public void treeNodesInserted(TreeModelEvent event) {
2193 if(collection != null) {
2194 collection.setSaved(false);
2195 collection.setFilesChanged(true);
2196 }
2197 }
2198 /** Any action that changes one of the tree models within a collection, which are the only models we listen to, mean the collections contents have changed and so saved should be set to false.
2199 * @param event A <strong>TreeModelEvent</strong> encompassing all the information about the event which has changed the tree.
2200 */
2201 public void treeNodesRemoved(TreeModelEvent event) {
2202 if(collection != null) {
2203 collection.setSaved(false);
2204 collection.setFilesChanged(true);
2205
2206 }
2207 }
2208 /** Any action that changes one of the tree models within a collection, which are the only models we listen to, mean the collections contents have changed and so saved should be set to false.
2209 * @param event A <strong>TreeModelEvent</strong> encompassing all the information about the event which has changed the tree.
2210 */
2211 public void treeStructureChanged(TreeModelEvent event) {
2212 if(collection != null) {
2213 collection.setSaved(false);
2214 }
2215 }
2216 }
2217}
Note: See TracBrowser for help on using the repository browser.