Changeset 8313 for trunk/gli/src/org/greenstone/gatherer/collection
- Timestamp:
- 2004-10-13T14:48:20+13:00 (20 years ago)
- Location:
- trunk/gli/src/org/greenstone/gatherer/collection
- Files:
-
- 1 added
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/gli/src/org/greenstone/gatherer/collection/Collection.java
r8253 r8313 48 48 import org.greenstone.gatherer.cdm.CollectionMetaManager; 49 49 import org.greenstone.gatherer.file.FileNode; 50 import org.greenstone.gatherer.msm.MetadataXMLFileManager;51 import org.greenstone.gatherer.msm.MetadataSetManager;52 50 import org.greenstone.gatherer.util.StaticStrings; 53 51 import org.greenstone.gatherer.util.Utility; … … 64 62 /** A reference to the Collection Design Manager. */ 65 63 public CollectionDesignManager cdm; 66 /** A reference to the Greenstone Directory Metadata Manager. */67 public MetadataXMLFileManager gdm;68 /** A reference to the Metadata Set Manager. */69 public MetadataSetManager msm;70 64 /** true if an error has occurred during construction */ 71 65 public boolean error = false; … … 119 113 public void destroy() { 120 114 cdm.destroy(); 121 gdm.destroy();122 msm.destroy();123 115 Configuration.setCollectionConfiguration(null); 124 116 if (Gatherer.g_man != null) { … … 127 119 cdm = null; 128 120 document = null; 129 gdm = null;130 msm = null;131 121 } 132 122 … … 170 160 } 171 161 172 public MetadataXMLFileManager getGDM() {173 return gdm;174 }175 176 162 /** Retrieve the short name for this collection. 177 163 * @return The name as a <strong>String</strong>. -
trunk/gli/src/org/greenstone/gatherer/collection/CollectionManager.java
r8253 r8313 62 62 import org.greenstone.gatherer.gui.WarningDialog; 63 63 import org.greenstone.gatherer.gui.tree.WorkspaceTree; 64 import org.greenstone.gatherer.msm.ElementWrapper; 65 import org.greenstone.gatherer.msm.MetadataXMLFileManager; 66 import org.greenstone.gatherer.msm.GreenstoneArchiveParser; 67 import org.greenstone.gatherer.msm.LegacyCollectionImporter; 68 import org.greenstone.gatherer.msm.MetadataSet; 69 import org.greenstone.gatherer.msm.MetadataSetManager; 70 import org.greenstone.gatherer.msm.MSMEvent; 71 import org.greenstone.gatherer.msm.MSMListener; 72 import org.greenstone.gatherer.msm.MSMProfiler; 64 import org.greenstone.gatherer.metadata.DocXMLFileManager; 65 import org.greenstone.gatherer.metadata.MetadataSet; 66 import org.greenstone.gatherer.metadata.MetadataSetManager; 67 import org.greenstone.gatherer.metadata.MetadataXMLFileManager; 68 import org.greenstone.gatherer.metadata.ProfileXMLFileManager; 73 69 import org.greenstone.gatherer.shell.GShell; 74 70 import org.greenstone.gatherer.shell.GShellEvent; … … 89 85 */ 90 86 public class CollectionManager 91 implements GShellListener, MSMListener { 92 /** A reference to the metadata set manager. */ 93 public MetadataSetManager msm; 87 implements GShellListener { 88 94 89 /** Are we currently in the process of building? */ 95 90 private boolean building = false; … … 100 95 /** The collection_model. */ 101 96 private FileSystemModel collection_model = null; 102 /** The workspace model. This becomes invalid on a collection change. */103 // private FileSystemModel workspace_model = null;104 97 /** An inner class listener responsible for noting tree changes and resetting saved when they occur. */ 105 98 private FMTreeModelListener fm_tree_model_listener = null; … … 127 120 this.collection = null; 128 121 } 122 123 129 124 /** Add a special directory mapping. 130 125 * @param name The name for this directory mapping as a <strong>String</strong>. 131 126 * @param file The directory this mapping maps to as a <strong>File</strong>. 132 127 */ 133 public void addDirectoryMapping(String name, File file) { 128 public void addDirectoryMapping(String name, File file) 129 { 134 130 // Update the information stored in the Gatherer config 135 131 Configuration.addDirectoryMapping(name, file); … … 137 133 Gatherer.g_man.refreshWorkspaceTree(WorkspaceTree.FOLDER_SHORTCUTS_CHANGED); 138 134 } 135 136 139 137 /** This method calls the builcol.pl scripts via a GShell so as to not lock up the processor. 140 138 * @see org.greenstone.gatherer.Configuration … … 147 145 * @see org.greenstone.gatherer.util.Utility 148 146 */ 149 p ublicvoid buildCollection() {147 private void buildCollection() { 150 148 DebugStream.println("CollectionManager.buildCollection()"); 151 149 building = true; … … 227 225 lock_file.delete(); 228 226 if(lock_file.exists()) { 229 System.err.println("Lockfile was not successfully deleted."); 230 } 231 collection.msm.destroy(); 227 System.err.println("Lockfile was not successfully deleted."); 228 } 229 230 MetadataSetManager.clearMetadataSets(); 231 MetadataXMLFileManager.clearMetadataXMLFiles(); 232 DocXMLFileManager.clearDocXMLFiles(); 233 ProfileXMLFileManager.clearProfileXMLFile(); 234 232 235 collection = null; 233 236 collection_model = null; … … 324 327 * @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 325 328 */ 326 public void createCollection(String description, String email, String name, String title, File base_collection_directory, ArrayList metadata_sets) { 327 boolean cancelled = false; 328 329 public void createCollection(String description, String email, String name, String title, File base_collection_directory, ArrayList metadata_sets) 330 { 329 331 try { 330 332 // first make sure that the collect directory exists … … 340 342 // ******************* 341 343 //check that this creation has worked - simply by checking for the existence of the collect.cfg file 342 String a_dir;344 String collection_dir; 343 345 if (Gatherer.GS3) { 344 a_dir = Utility.getCollectionDir(Configuration.gsdl3_path, Configuration.site_name, name);346 collection_dir = Utility.getCollectionDir(Configuration.gsdl3_path, Configuration.site_name, name); 345 347 } else { 346 a_dir = Utility.getCollectionDir(Configuration.gsdl_path, name);347 } 348 File config_file = new File(Utility.getConfigFile( a_dir));348 collection_dir = Utility.getCollectionDir(Configuration.gsdl_path, name); 349 } 350 File config_file = new File(Utility.getConfigFile(collection_dir)); 349 351 if (!config_file.exists()) { 350 352 // no point continuing … … 356 358 357 359 // ACTIVE_DIR/log/ 358 File log_dir_temp = new File(Utility.getLogDir( a_dir)+"temp.dat");360 File log_dir_temp = new File(Utility.getLogDir(collection_dir)+"temp.dat"); 359 361 File log_dir = log_dir_temp.getParentFile(); 360 362 log_dir.mkdirs(); … … 364 366 365 367 // Make sure an import folder exists 366 File import_directory = new File(Utility.getImportDir( a_dir));368 File import_directory = new File(Utility.getImportDir(collection_dir)); 367 369 if (!import_directory.exists()) { 368 370 import_directory.mkdirs(); … … 372 374 373 375 // Now create the collection object around the directory. 374 collection = new Collection(new File( a_dir, name + ".col"));375 collection.msm = new MetadataSetManager(); 376 msm = collection.msm; // Legacy377 collection.msm.load();378 379 // Import default metadata sets if any. 380 for(int i = 0; metadata_sets != null && i < metadata_sets.size(); i++) {381 MetadataSet metadata_set = (MetadataSet) metadata_sets.get(i);382 collection.msm.importMDS(metadata_set.getFile(), false);383 } 384 385 boolean skip_import_phase = false;376 collection = new Collection(new File(collection_dir, name + ".col")); 377 378 MetadataSetManager.clearMetadataSets(); 379 MetadataXMLFileManager.clearMetadataXMLFiles(); 380 DocXMLFileManager.clearDocXMLFiles(); 381 382 // Import default metadata sets, if any 383 for (int i = 0; metadata_sets != null && i < metadata_sets.size(); i++) { 384 importMetadataSet((MetadataSet) metadata_sets.get(i)); 385 } 386 387 ProfileXMLFileManager.loadProfileXMLFile(new File(collection_dir, Utility.META_DIR)); 386 388 387 389 // Before we create the CollectionDesignManager we have to check if we are basing it upon some other collection. 388 if(base_collection_directory != null) { 389 DebugStream.println("Basing new collection on existing one: " + base_collection_directory); 390 collection.setBaseCollection(base_collection_directory.getAbsolutePath()); 391 // copy over other needed directories 392 copyExtraBaseCollStuff(new File(a_dir), base_collection_directory); 393 // Try to import any existing metadata sets for this collection. Look in base_collection_directory/metadata and import any metadata sets found. 394 File base_metadata = new File(base_collection_directory, Utility.META_DIR); 395 if(base_metadata.exists()) { 396 DebugStream.println("Found the metadata directory."); 397 File[] possible_metadata_sets = base_metadata.listFiles(); 398 for(int i = 0; possible_metadata_sets != null && i < possible_metadata_sets.length; i++) { 399 String filename = possible_metadata_sets[i].getName(); 400 if(filename.endsWith(".mds")) { 401 DebugStream.println("+ Found a metadata set. Importing: " + possible_metadata_sets[i].getAbsolutePath()); 402 collection.msm.importMDS(possible_metadata_sets[i], false); 403 skip_import_phase = true; 404 } 390 if (base_collection_directory != null) { 391 DebugStream.println("Basing new collection on existing one: " + base_collection_directory); 392 collection.setBaseCollection(base_collection_directory.getAbsolutePath()); 393 // copy over other needed directories 394 copyExtraBaseCollStuff(new File(collection_dir), base_collection_directory); 395 // Try to import any existing metadata sets for this collection 396 // Look in base_collection_directory/metadata and import any metadata sets found. 397 File base_metadata_directory = new File(base_collection_directory, Utility.META_DIR); 398 ArrayList base_metadata_sets = MetadataSetManager.listMetadataSets(base_metadata_directory); 399 if (base_metadata_sets != null) { 400 for (int i = 0; i < base_metadata_sets.size(); i++) { 401 importMetadataSet((MetadataSet) base_metadata_sets.get(i)); 405 402 } 406 403 } … … 408 405 DebugStream.println("This base collection has no metadata directory."); 409 406 } 410 // If no sets were imported, then create a new metadata with this new collections name. 411 if(collection.msm.getSets().size() == 0) { 407 408 // If no sets were imported... 409 if (MetadataSetManager.getMetadataSets().size() == 0) { 412 410 // Prompt the user so that they can choose at least one initial metadata set. We're sneaky here and just create a ncm_prompt 413 411 DebugStream.println("This collection has no metadata sets. Present the user with the metadata set selection prompt."); 414 412 NewCollectionMetadataPrompt ncm_prompt = new NewCollectionMetadataPrompt(); 415 413 // If cancelled then they really do mean to start a collection with no metadata sets. 416 if (!ncm_prompt.isCancelled()) {414 if (!ncm_prompt.isCancelled()) { 417 415 ArrayList initial_sets = ncm_prompt.getSets(); 418 for(int i = 0; initial_sets != null && i < initial_sets.size(); i++) { 419 MetadataSet metadata_set = (MetadataSet) initial_sets.get(i); 420 collection.msm.importMDS(metadata_set.getFile(), false); 421 metadata_set = null; 416 for (int i = 0; initial_sets != null && i < initial_sets.size(); i++) { 417 importMetadataSet((MetadataSet) initial_sets.get(i)); 422 418 } 423 initial_sets = null;424 419 } 425 420 ncm_prompt.dispose(); 426 421 ncm_prompt = null; 427 422 } 428 // Do a dry metadata import run over the entire base collection, recording profile mappings. We do this by finding the archive files, and then iterating over them using the GreenstoneArchiveParser to retrieve metadata from them. We then process the importing of new metadata elements using the selectElement prompt used in a file action metadata import. However the big change is that we don't actually import any metadata, just create importing profiles.429 if(!skip_import_phase) {430 File base_archive = new File(base_collection_directory, Utility.ARCHIVES_DIR);431 if(base_archive.exists()) {432 DebugStream.println("+ Archive directory found. Inspecting archives for metadata information.");433 ArrayList metadata_elements = GreenstoneArchiveParser.extractMetadataElements(base_archive);434 for(int i = 0; !cancelled && i < metadata_elements.size(); i++) {435 String metadata_name = (String) metadata_elements.get(i);436 ElementWrapper target = collection.msm.prompt.selectElement(metadata_name);437 cancelled = Gatherer.c_man.getCollection().msm.prompt.wasDialogCancelled();438 if(!cancelled) {439 if(target != null) {440 collection.msm.profiler.addAction(base_collection_directory.getAbsolutePath(), metadata_name, target.getName());441 }442 else {443 collection.msm.profiler.addAction(base_collection_directory.getAbsolutePath(), metadata_name, null);444 }445 }446 }447 // Hopefully mappings should now be in place for metadata extracted from this collection.448 }449 else {450 DebugStream.println("+ Searching files for metadata.xml information.");451 // Find the import directory452 File base_import = new File(base_collection_directory, Utility.IMPORT_DIR);453 if(base_import.exists()) {454 searchForMetadata(base_import);455 }456 }457 }458 // And if that fails then we must have been asked by Satan himself to build the very digital collections of hell, because they don't match any goodly greenstone collection I have ever seen, so you can't blame me if I can't import them.459 423 460 424 // Now we update our collect.cfg 461 425 DebugStream.println("Copy and update collect.cfg from base collection."); 462 updateCollectionCFG(new File(base_collection_directory, Utility.CONFIG_FILE), new File( a_dir, Utility.CONFIG_FILE), description, email, title);426 updateCollectionCFG(new File(base_collection_directory, Utility.CONFIG_FILE), new File(collection_dir, Utility.CONFIG_FILE), description, email, title); 463 427 } 464 428 465 429 // Always import the extracted metadata set if we didn't already 466 if(collection.msm.getSet(Utility.EXTRACTED_METADATA_NAMESPACE) == null) { 467 collection.msm.importMDS(new File(Utility.METADATA_DIR + Utility.EXTRACTED_METADATA_NAMESPACE + StaticStrings.METADATA_SET_EXTENSION), false); 430 if (MetadataSetManager.getMetadataSet(MetadataSetManager.EXTRACTED_METADATA_NAMESPACE) == null) { 431 File extracted_metadata_set_file = new File(Utility.METADATA_DIR + Utility.EXTRACTED_METADATA_NAMESPACE + StaticStrings.METADATA_SET_EXTENSION); 432 importMetadataSet(new MetadataSet(extracted_metadata_set_file)); 468 433 } 469 434 470 435 collection.cdm = new CollectionDesignManager(new File(getCollectionConfig())); 471 436 472 437 // 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 473 if (base_collection_directory != null) {438 if (base_collection_directory != null) { 474 439 // Update the creator and maintainer 475 440 CollectionMeta creator_collectionmeta = new CollectionMeta(collection.cdm.collect_config.getCreator()); … … 479 444 maintainer_collectionmeta.setValue(email); 480 445 maintainer_collectionmeta = null; 446 481 447 // Update the collection title 482 448 CollectionMeta collection_name_collectionmeta = collection.cdm.collectionmeta_manager.getMetadatum(StaticStrings.COLLECTIONMETADATA_COLLECTIONNAME_STR); 483 449 collection_name_collectionmeta.setValue(title); 484 450 collection_name_collectionmeta = null; 451 485 452 // And now the description 486 453 CollectionMeta collection_extra_collectionmeta = collection.cdm.collectionmeta_manager.getMetadatum(StaticStrings.COLLECTIONMETADATA_COLLECTIONEXTRA_STR); 487 454 collection_extra_collectionmeta.setValue(description); 488 455 collection_extra_collectionmeta = null; 456 489 457 // All collections based on others are automatically public 490 458 CollectionMeta public_collectionmeta = new CollectionMeta(collection.cdm.collect_config.getPublic()); 491 459 public_collectionmeta.setValue(StaticStrings.TRUE_STR); 492 460 public_collectionmeta = null; 461 493 462 // Finally reset the icons 494 463 CollectionMeta icon_collection_collectionmeta = collection.cdm.collectionmeta_manager.getMetadatum(StaticStrings.COLLECTIONMETADATA_ICONCOLLECTION_STR); … … 499 468 icon_collection_small_collectionmeta = null; 500 469 } 501 502 collection.gdm = new MetadataXMLFileManager();503 470 504 471 progress.setProgress(3); 505 472 506 // Has to be done after creating metadata set manager.507 File gmeta_dir_temp = new File(getCollectionMetadata()+"temp.dat");508 File gmeta_dir = gmeta_dir_temp.getParentFile();509 gmeta_dir.mkdirs();510 if(progress != null) {511 progress.setNote("GMeta created");512 }513 progress.setProgress(4);514 515 progress.setProgress(6);516 // Register ourselves as being interested in what the msm has to say.517 collection.msm.addMSMListener(this);518 473 // Create a lock file. 519 File lock_file = new File( a_dir, LOCK_FILE);474 File lock_file = new File(collection_dir, LOCK_FILE); 520 475 FileOutputStream out = new FileOutputStream(lock_file); 521 476 out.write(LOCK_FILE.getBytes()); 522 477 out.close(); 523 478 out = null; 479 524 480 progress.setProgress(7); 525 String args[] = new String[1]; 526 args[0] = name; 527 progress.setNote(Dictionary.get("CollectionManager.Session_Ready", args)); 481 progress.setNote(Dictionary.get("CollectionManager.Session_Ready", name)); 528 482 progress.close(); 529 483 } … … 543 497 } 544 498 } 499 545 500 546 501 public void createLockFile(File destination) { … … 569 524 } 570 525 571 /** Method that is called whenever an element within a set is changed or modified. We want to mark the collection so that it needs saving again.572 * @param event A <strong>MSMEvent</strong> containing details of the event that caused this message to be fired.573 * @see org.greenstone.gatherer.collection.Collection574 */575 public void elementChanged(MSMEvent event) {576 // This means the state of the collections has changed, so we should set saved to false.577 collection.setSaved(false);578 }579 /** Used to retrieve the build options associated with the currently loaded collection. If none yet exist, default ones are created.580 * @return A <strong>BuildOptions</strong> object containing the build options for the current collection.581 * @see org.greenstone.gatherer.collection.Collection582 */583 /* private BuildOptions getBuildOptions() {584 return collection.build_options;585 } */586 526 587 527 /** Retrieve the current collection. … … 591 531 return collection; 592 532 } 533 534 593 535 /** Constructs the absolute filename of the collection archive directory, which should resemble "$GSDLHOME/collect/<col_name>/archive/" 594 536 * @return A <strong>String</strong> containing the filename. … … 630 572 return Utility.getConfigFile(Utility.getCollectionDir(Configuration.gsdl3_path, Configuration.site_name, collection.getName())); 631 573 } else { 632 return Utility.getConfigFile(Utility.getCollectionDir(Configuration.gsdl_path, collection.getName()));574 return Utility.getConfigFile(Utility.getCollectionDir(Configuration.gsdl_path, collection.getName())); 633 575 } 634 576 } … … 660 602 return Utility.getEtcDir(Utility.getCollectionDir(Configuration.gsdl3_path, Configuration.site_name, collection.getName())); 661 603 } else { 662 return Utility.getEtcDir(Utility.getCollectionDir(Configuration.gsdl_path, collection.getName()));604 return Utility.getEtcDir(Utility.getCollectionDir(Configuration.gsdl_path, collection.getName())); 663 605 } 664 606 } … … 674 616 return Utility.getCollectionDir(Configuration.gsdl3_path, Configuration.site_name, collection.getName()) + collection.getName() + ".col"; 675 617 } else { 676 return Utility.getCollectionDir(Configuration.gsdl_path, collection.getName()) + collection.getName() + ".col";618 return Utility.getCollectionDir(Configuration.gsdl_path, collection.getName()) + collection.getName() + ".col"; 677 619 } 678 620 } … … 688 630 return Utility.getImagesDir(Utility.getCollectionDir(Configuration.gsdl3_path, Configuration.site_name, collection.getName())); 689 631 } else { 690 return Utility.getImagesDir(Utility.getCollectionDir(Configuration.gsdl_path, collection.getName()));632 return Utility.getImagesDir(Utility.getCollectionDir(Configuration.gsdl_path, collection.getName())); 691 633 } 692 634 } … … 748 690 } 749 691 750 /** This method either returns the title of the current collection, or a placeholder string of 'No Collection'. 751 * @return A <strong>String</strong> which represents what we wish to display for a collection title.752 * @see org.greenstone.gatherer.collection.Collection753 */ 754 /* private String getCollectionTitle() { 755 if(collection != null){756 return collection.getTitle();757 } 758 return Dictionary.get("Collection.No_Collection"); 759 } */692 693 static public String getCollectionMetadataDirectory() 694 { 695 if (Gatherer.GS3) { 696 return Utility.getMetadataDir(Utility.getCollectionDir(Configuration.gsdl3_path, Configuration.site_name, collection.getName())); 697 } else { 698 return Utility.getMetadataDir(Utility.getCollectionDir(Configuration.gsdl_path, collection.getName())); 699 } 700 } 701 760 702 761 703 /** Retrieve the record set (tree model) associated with the current collection. */ … … 827 769 index_dir = index_dir.getParentFile(); 828 770 if(index_dir.exists()) { 829 DebugStream.println("Old Index = " + index_dir.getAbsolutePath()+", testing for deletability"); 830 if (!canDelete(index_dir)) { 831 // tell the user 832 JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("CollectionManager.Cannot_Delete_Index"), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE); 833 // tell the gui manager 834 // a message for the building log 835 GShellEvent event = new GShellEvent(this, 0, GShell.IMPORT, Dictionary.get("CollectionManager.Cannot_Delete_Index_Log"), GShell.ERROR); 836 Gatherer.g_man.create_pane.message(event); 837 event = new GShellEvent(this, 0, GShell.IMPORT, "", GShell.ERROR); 838 Gatherer.g_man.create_pane.processComplete(event); 839 importing = false; 840 return; 841 } 842 771 DebugStream.println("Old Index = " + index_dir.getAbsolutePath()+", testing for deletability"); 772 if (!canDelete(index_dir)) { 773 // tell the user 774 JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("CollectionManager.Cannot_Delete_Index"), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE); 775 // tell the gui manager 776 // a message for the building log 777 GShellEvent event = new GShellEvent(this, 0, GShell.IMPORT, Dictionary.get("CollectionManager.Cannot_Delete_Index_Log"), GShell.ERROR); 778 Gatherer.g_man.create_pane.message(event); 779 event = new GShellEvent(this, 0, GShell.IMPORT, "", GShell.ERROR); 780 Gatherer.g_man.create_pane.processComplete(event); 781 importing = false; 782 return; 783 } 843 784 } 844 785 … … 879 820 } 880 821 822 823 public void importMetadataSet(MetadataSet external_metadata_set) 824 { 825 // Copy the .mds file into the collection's "metadata" folder... 826 File external_metadata_set_file = external_metadata_set.getMetadataSetFile(); 827 828 // ...but not if it is the redundant "hidden.mds" file 829 if (external_metadata_set_file.getName().equals("hidden.mds")) { 830 return; 831 } 832 833 // ...and only if it doesn't already exist 834 File metadata_set_file = new File(getCollectionMetadata(), external_metadata_set_file.getName()); 835 if (!metadata_set_file.exists()) { 836 try { 837 Gatherer.f_man.getQueue().copyFile(external_metadata_set_file, metadata_set_file, null); 838 } 839 catch (Exception ex) { 840 ex.printStackTrace(); 841 } 842 843 // Load it into the MetadataSetManager 844 MetadataSetManager.loadMetadataSet(metadata_set_file); 845 } 846 } 847 848 881 849 /** 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. 882 850 * @return true if the gli is currently importing … … 891 859 * @see org.greenstone.gatherer.Gatherer 892 860 * @see org.greenstone.gatherer.collection.Collection 893 * @see org.greenstone.gatherer.msm.MetadataSetManager894 * @see org.greenstone.gatherer.msm.MSMListener895 861 * @see org.greenstone.gatherer.util.Utility 896 862 */ 897 public boolean loadCollection(String location) { 898 DebugStream.println("Load Collection '" + location + "'"); 899 String[] args2 = new String[1]; 900 args2[0] = location; 901 boolean result = false; 863 public boolean loadCollection(String location) 864 { 865 DebugStream.println("Loading collection " + location + "..."); 902 866 boolean non_gatherer_collection = false; 867 903 868 // Check we have actually been given a .col file. 904 if (!location.endsWith(".col")) {905 JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("CollectionManager.Not_Col_File", args2), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);869 if (!location.endsWith(".col")) { 870 JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("CollectionManager.Not_Col_File", location), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE); 906 871 DebugStream.println("CollectionManager.loadCollection: Haven't been given a .col file."); 907 872 return false; 908 873 } 874 909 875 // Check that there is the collection configuration file available 910 911 876 File collection_file = new File(location); 912 877 // Ensure that the directory exists. 913 878 File collection_directory = collection_file.getParentFile(); 914 if (!collection_directory.exists()) {879 if (!collection_directory.exists()) { 915 880 // we cant open this 916 881 collection_directory = null; … … 926 891 927 892 // Special case of a user trying to open an old greenstone collection. 928 File metadata_directory = new File(collection_directory, Utility.META_DIR); 929 if(!metadata_directory.exists()) { 930 931 DebugStream.println("CollectionManager.loadCollection: trying to load up a non-gatherer collection"); 893 File collection_metadata_directory = new File(collection_directory, Utility.META_DIR); 894 if (!collection_metadata_directory.exists()) { 895 DebugStream.println("Loading non-gatherer collection..."); 932 896 non_gatherer_collection = true; 933 897 } 934 898 899 // Now determine if a lock already exists on this collection. 935 900 String name = collection_directory.getName(); 936 901 File lock_file = new File(collection_file.getParentFile(), LOCK_FILE); 937 // Now determine if a lock already exists on this collection. 938 int choice = LockFileDialog.YES_OPTION; 939 if(lock_file.exists()) { 902 if (lock_file.exists()) { 940 903 LockFileDialog dialog = new LockFileDialog(Gatherer.g_man, name, lock_file); 941 choice = dialog.getChoice();904 int choice = dialog.getChoice(); 942 905 dialog.dispose(); 943 906 dialog = null; 944 } 945 946 if(choice != LockFileDialog.YES_OPTION) { 947 // user has cancelled 948 lock_file = null; 949 collection_directory = null; 950 collection_config_file = null; 951 return false; 952 } 953 954 907 908 if (choice != LockFileDialog.YES_OPTION) { 909 // user has cancelled 910 lock_file = null; 911 collection_directory = null; 912 collection_config_file = null; 913 return false; 914 } 915 916 lock_file.delete(); 917 } 918 919 boolean result = false; 955 920 try { 956 if(lock_file.exists()) {957 lock_file.delete();958 }959 921 // Create a lock file. 960 922 createLockFile(lock_file); … … 964 926 System.err.println("Cannot write lock file!"); 965 927 String args[] = new String[2]; 966 args[0] = args2[0];928 args[0] = location; 967 929 args[1] = Dictionary.get("FileActions.Write_Not_Permitted_Title"); 968 930 JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("CollectionManager.Cannot_Open_With_Reason", args), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE); … … 970 932 return false; 971 933 } 934 972 935 // Open the collection file 973 936 this.collection = new Collection(collection_file); … … 975 938 collection = null; 976 939 // Remove lock file 977 if (lock_file.exists()) {940 if (lock_file.exists()) { 978 941 lock_file.delete(); 979 942 } … … 981 944 } 982 945 983 collection.msm = new MetadataSetManager(); 984 msm = collection.msm; // Legacy 985 collection.msm.load(); 986 // if non-gatherer collection, need to add some metadata sets 946 MetadataSetManager.clearMetadataSets(); 947 MetadataSetManager.loadMetadataSets(collection_metadata_directory); 948 949 ProfileXMLFileManager.loadProfileXMLFile(collection_metadata_directory); 950 951 // If this is a non-GLI (legacy) collection, ask the user to choose some metadata sets 987 952 if (non_gatherer_collection) { 988 953 if (!addSomeMetadataSets(collection_directory)) { 989 // for now - return of false means its been cancelled. Any error messages should be sent from the function itself990 954 lock_file = null; 991 955 collection_directory = null; … … 993 957 return false; 994 958 } 995 } 959 960 // Recurse the import folder tree, backing up the metadata.xml files before they are edited 961 LegacyCollectionImporter.backupMetadataXMLFiles(collection_directory); 962 } 963 964 // Read through the metadata.xml files in the import directory, building up the metadata value trees 965 File collection_import_directory = new File(collection_directory, Utility.IMPORT_DIR); 966 MetadataXMLFileManager.clearMetadataXMLFiles(); 967 MetadataXMLFileManager.loadMetadataXMLFiles(collection_import_directory); 968 969 // Read through the doc.xml files in the archives directory 970 File collection_archives_directory = new File(collection_directory, Utility.ARCHIVES_DIR); 971 DocXMLFileManager.clearDocXMLFiles(); 972 DocXMLFileManager.loadDocXMLFiles(collection_archives_directory); 996 973 997 974 collection.cdm = new CollectionDesignManager(collection_config_file); 998 975 if (non_gatherer_collection) { 999 // We first recurse the Import folder tree, reading in any metadata.xml files, and then altering the non-namespaced element names to be valid GLI names 1000 LegacyCollectionImporter lci = new LegacyCollectionImporter(collection_directory, collection.cdm); 1001 lci.backupMetadataXMLFiles(collection_directory); 1002 lci.importMetadata(); 1003 lci.updateClassifiers(); 1004 lci = null; 1005 } 1006 // Whether the collection is legacy or not, we should now be able to prepare the MetadataXMLFileManager 1007 collection.gdm = new MetadataXMLFileManager(); 976 // Change the classifiers to use the namespaced element names 977 LegacyCollectionImporter.updateClassifiers(collection.cdm); 978 } 1008 979 1009 980 // Tell everyone that it worked. 1010 String[] args = new String[1]; 1011 args[0] = name; 1012 DebugStream.println(Dictionary.get("CollectionManager.Loading_Successful", args)); 1013 // Now we need to hook up classes that depend on messages from the metadata set manager to keep their content fresh. 1014 collection.msm.addMSMListener(this); 981 DebugStream.println(Dictionary.get("CollectionManager.Loading_Successful", name)); 982 1015 983 // We're done. Let everyone know. 1016 if(Gatherer.g_man != null) { 1017 // workspace_model = null; 984 if (Gatherer.g_man != null) { 1018 985 Gatherer.g_man.collectionChanged(ready()); 1019 986 } 1020 987 result = true; 1021 } catch (Exception error) { 988 } 989 catch (Exception error) { 1022 990 // There is obviously no existing collection present. 1023 991 DebugStream.printStackTrace(error); 1024 992 if(error.getMessage() != null) { 1025 993 String[] args = new String[2]; 1026 args[0] = args2[0];994 args[0] = location; 1027 995 args[1] = error.getMessage(); 1028 996 JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("CollectionManager.Cannot_Open_With_Reason", args), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE); 1029 997 } 1030 998 else { 1031 JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("CollectionManager.Cannot_Open", args2), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);999 JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("CollectionManager.Cannot_Open", location), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE); 1032 1000 } 1033 1001 } … … 1037 1005 collection_config_file = null; 1038 1006 1039 args2 = null;1040 1007 return result; 1041 1008 } … … 1147 1114 public synchronized void message(GShellEvent event) { 1148 1115 } 1149 /** Called whenever the metadata value changes in some way, such as the addition of a new value. We want to mark the collection so that it needs saving again. 1150 * @param event A <strong>MSMEvent</strong> containing details of the event that caused this message to be fired. 1151 * @see org.greenstone.gatherer.collection.Collection 1152 */ 1153 public void metadataChanged(MSMEvent event) { 1154 // Again this change means we need to save the collection again. 1155 collection.setSaved(false); 1156 } 1116 1157 1117 /** This call is fired whenever a process within a GShell created by this class begins. 1158 1118 * @param event A <strong>GShellEvent</strong> containing information about the GShell process. … … 1282 1242 return file; 1283 1243 } 1244 1245 1246 public void removeMetadataSet(MetadataSet metadata_set) 1247 { 1248 System.err.println("Removing metadata set..."); 1249 1250 // Delete the .mds file from the collection's "metadata" folder... 1251 File metadata_set_file = metadata_set.getMetadataSetFile(); 1252 1253 // ...but not if it is the "ex.mds" file 1254 if (metadata_set_file.getName().equals("ex.mds")) { 1255 return; 1256 } 1257 1258 // ...and only if it exists 1259 if (metadata_set_file.exists()) { 1260 metadata_set_file.delete(); 1261 1262 // Unload it from the MetadataSetManager 1263 MetadataSetManager.unloadMetadataSet(metadata_set); 1264 } 1265 } 1266 1267 1284 1268 /** Used to check whether all open collections have a 'saved' state. 1285 1269 * @return A <i>boolean</i> which is <i>true</i> if the collection has been saved. … … 1313 1297 } 1314 1298 1315 /** Method that is called whenever the metadata set collection changes in some way, such as the addition of a new set or the merging of two sets. We want to mark the collection so that it needs saving again.1316 * @param event A <strong>MSMEvent</strong> containing details of the event that caused this message to be fired.1317 * @see org.greenstone.gatherer.collection.Collection1318 */1319 public void setChanged(MSMEvent event) {1320 // Invalidate saved1321 collection.setSaved(false);1322 }1323 1324 1299 public void setClosingThread(boolean set) { 1325 1300 if(set) { … … 1331 1306 } 1332 1307 1333 /** Updates the given workspace tree model to reference the private cache of the currently loaded collection. */1334 /* private void updatePrivateWorkspace(DefaultTreeModel model) {1335 // Add Private workspace if a collection has been loaded.1336 if(ready() && !Configuration.get("workflow.mirror", true)) {1337 FileNode root = (FileNode)model.getRoot();1338 // Remove old private workspace1339 FileNode old = (FileNode)model.getChild(root, 2);1340 model.removeNodeFromParent(old);1341 // Create and insert new.1342 FileNode private_workspace = new FileNode(new File(getCollectionCache()), Dictionary.get("Tree.Private"));1343 model.insertNodeInto(private_workspace, root, 2);1344 }1345 } */1346 /** Called whenever the value tree of an metadata element changes in some way, such as the addition of a new value. We want to mark the collection so that it needs saving again.1347 * @param event A <strong>MSMEvent</strong> containing details of the event that caused this message to be fired.1348 * @see org.greenstone.gatherer.collection.Collection1349 */1350 public void valueChanged(MSMEvent event) {1351 collection.setSaved(false);1352 }1353 1308 1354 1309 /** 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] */ … … 1364 1319 1365 1320 /*if (meta_choice == ExternalCollectionPrompt.NEW_META_SET) { 1366 1367 1368 1369 }1370 1371 1372 1373 } else if (meta_choice == ExternalCollectionPrompt.EXISTING_META_SET) {1321 NewMetaSetPrompt nmsp = new NewMetaSetPrompt(); 1322 if (nmsp.isCancelled()) { 1323 return false; 1324 } 1325 String namespace_str = nmsp.getNamespace(); 1326 String name_str = nmsp.getName(); 1327 // MetadataSet set = Gatherer.c_man.msm.addSet(namespace_str, name_str); 1328 } else if (meta_choice == ExternalCollectionPrompt.EXISTING_META_SET) { 1374 1329 */ 1375 1330 // now we reuse the newcoll metadata prompt for the user to select metadata sets … … 1381 1336 // Import default metadata sets if any. 1382 1337 for(int i = 0; metadata_sets != null && i < metadata_sets.size(); i++) { 1383 MetadataSet metadata_set = (MetadataSet) metadata_sets.get(i); 1384 collection.msm.importMDS(metadata_set.getFile(), false); 1385 } 1386 /*} else { 1387 return false; 1388 }*/ 1338 importMetadataSet((MetadataSet) metadata_sets.get(i)); 1339 } 1340 1389 1341 // Always import the extracted metadata set 1390 collection.msm.importMDS(new File(Utility.METADATA_DIR + Utility.EXTRACTED_METADATA_NAMESPACE + StaticStrings.METADATA_SET_EXTENSION), false); 1342 File extracted_metadata_set_file = new File(Utility.METADATA_DIR + Utility.EXTRACTED_METADATA_NAMESPACE + StaticStrings.METADATA_SET_EXTENSION); 1343 importMetadataSet(new MetadataSet(extracted_metadata_set_file)); 1391 1344 1392 1345 return true; 1393 1346 } 1394 1347 1395 1396 // // now try to load in existing metadata, delete the existing metadata.xml files, then save the new ones.1397 // private boolean findAndLoadExistingMetadata(File collection_dir)1398 // {1399 // TreeModel collection_tree = getRecordSet();1400 // FileNode root_node = (FileNode) collection_tree.getRoot();1401 // if (searchForMetadata(collection_tree, root_node) == false) {1402 // return false;1403 // }1404 // collection.gdm.save(root_node);1405 // return true;1406 // }1407 1348 1408 1349 // used as arg in the perl scripts … … 1423 1364 } 1424 1365 1366 1425 1367 /** Install collection by moving its files from building to index after a successful build. 1426 1368 * @see org.greenstone.gatherer.Gatherer 1427 1369 * @see org.greenstone.gatherer.util.Utility 1428 1370 */ 1429 private boolean installCollection() { 1371 private boolean installCollection() 1372 { 1430 1373 DebugStream.println("Build complete. Moving files."); 1431 1374 … … 1506 1449 1507 1450 1508 private boolean searchArchivesForMetadata(File archive_directory) { 1509 /** @todo - ensure GreenstoneArchiveParser works as expected. */ 1510 return true; 1511 } 1512 1513 /** now this returns true if successful, false if unsuccessful or cancelled */ 1514 private boolean searchForMetadata(File current_file) { 1515 boolean success = true; 1516 if(current_file.isFile() && current_file.getName().equals(Utility.METADATA_XML)) { 1517 success = collection.msm.searchForMetadata(null, new FileNode(current_file), false, true); // A dummy run only. 1518 } 1519 else { 1520 File[] children_files = current_file.listFiles(); 1521 for(int i = 0; success && children_files != null && i < children_files.length; i++) { 1522 success = searchForMetadata(children_files[i]); 1523 } 1524 } 1525 return success; 1526 } 1527 1528 private boolean searchForMetadata(TreeModel collection_tree, FileNode current_node) { 1529 File source_file = current_node.getFile(); 1530 String source_file_name = source_file.getName(); 1531 if (source_file_name.equals(Utility.METADATA_XML) || source_file_name.equals("CVS")) { 1532 return true; 1533 } 1534 if (collection.msm.searchForMetadata(current_node, current_node, false)== false) { 1535 return false; 1536 } 1537 int num_children = collection_tree.getChildCount(current_node); 1538 for (int i=0; i<num_children; i++) { 1539 FileNode child = (FileNode)collection_tree.getChild(current_node, i); 1540 if (searchForMetadata(collection_tree, child)==false) { 1541 return false; 1542 } 1543 } 1544 return true; 1545 } 1546 1547 private void updateCollectionCFG(File base_cfg, File new_cfg, String description, String email, String title) { 1451 private void updateCollectionCFG(File base_cfg, File new_cfg, String description, String email, String title) 1452 { 1548 1453 boolean first_name = true; 1549 1454 boolean first_extra = true; 1550 1455 String collection_path = (base_cfg.getParentFile().getParentFile()).getAbsolutePath(); 1551 1552 HashMap mappings = collection.msm.profiler.getActions(collection_path); 1553 if(mappings == null) { 1554 DebugStream.println("Mappings is null, which is odd. Leaving all configuration commands which use metadata as they are."); 1555 } 1556 1456 1557 1457 // 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. 1558 1458 try { … … 1620 1520 text.append(token); 1621 1521 String temp_metadata = tokenizer.nextToken(); 1622 String replacement = null; 1623 if(mappings != null) { 1624 replacement = (String) mappings.get(temp_metadata); 1625 } 1626 if(replacement != null) { 1522 String replacement = ProfileXMLFileManager.getMetadataElementFor(temp_metadata); 1523 if (replacement != null && !replacement.equals("")) { 1627 1524 token = replacement; 1628 1525 old_metadata = temp_metadata; … … 1641 1538 text.append(token); 1642 1539 String temp_metadata = tokenizer.nextToken(); 1643 String replacement = null; 1644 if(mappings != null) { 1645 replacement = (String) mappings.get(temp_metadata); 1646 } 1647 if(replacement != null) { 1540 String replacement = ProfileXMLFileManager.getMetadataElementFor(temp_metadata); 1541 if (replacement != null && !replacement.equals("")) { 1648 1542 token = replacement; 1649 1543 } … … 1681 1575 old_metadata = null; 1682 1576 write(out, command); 1683 1684 1577 } else { 1578 // the rest of the commands just want a string - we read in all the tokens from the tokeniser and get rid of it. 1685 1579 StringBuffer new_command = new StringBuffer(command_type_str); 1686 1580 while (tokenizer.hasMoreTokens()) { … … 1694 1588 // we really want to build up the whole command here 1695 1589 boolean format_command = command_type_str.equals(Utility.CFG_FORMAT); 1696 // Replace mapping strings 1697 if(mappings != null) { 1698 for(Iterator keys = mappings.keySet().iterator(); keys.hasNext(); ) { 1590 HashMap metadata_mapping = ProfileXMLFileManager.getMetadataMapping(); 1591 if (metadata_mapping != null) { 1592 Iterator keys = metadata_mapping.keySet().iterator(); 1593 while (keys.hasNext()) { 1699 1594 String target = (String) keys.next(); 1700 String replacement = (String) mappings.get(target); 1701 if(format_command) { 1702 target = "\\[" + target + "\\]"; 1703 replacement = "{Or}{[" + replacement + "]," + target + "}"; 1595 String replacement = (String) metadata_mapping.get(target); 1596 if (replacement != null && !replacement.equals("")) { 1597 if (format_command) { 1598 target = "\\[" + target + "\\]"; 1599 replacement = "{Or}{[" + replacement + "]," + target + "}"; 1600 } 1601 command = command.replaceAll(target, replacement); 1704 1602 } 1705 command = command.replaceAll(target, replacement);1706 1603 } 1707 1604 } 1605 1708 1606 write(out, command); 1709 1607 } 1710 1608 tokenizer = null; 1711 1609 } … … 1727 1625 out.newLine(); 1728 1626 } 1627 1729 1628 1730 1629 /** The CollectionManager class is getting too confusing by half so I'll implement this TreeModelListener in a private class to make responsibility clear. */ -
trunk/gli/src/org/greenstone/gatherer/collection/SaveCollectionTask.java
r8243 r8313 43 43 import org.greenstone.gatherer.DebugStream; 44 44 import org.greenstone.gatherer.Gatherer; 45 import org.greenstone.gatherer.metadata.MetadataSetManager; 45 46 import org.greenstone.gatherer.util.Utility; 46 47 … … 66 67 static final private int COPY_COLLECTION = 4; 67 68 static final private int MAKE_COLLECTION = 5; 68 static final private int METADATA_SAVED = 6;69 static final private int METADATA_XML_SAVED = 7;70 69 static final private int OPEN_COLLECTION = 8; 71 70 static final private int RESTORE_COLLECTION = 9; … … 112 111 // spd.setMillisToDecideToPopup(100); 113 112 // spd.setMillisToPopup(100); 114 // 0. Force all remaining metadata.xml files to load. 113 115 114 // 1. Perform a regular collection save on what we will later refer to as the origin collection. 116 115 ///ystem.err.println("1. Save origin."); … … 119 118 args[0] = collection.getName() + ".col"; 120 119 try { 121 // Block until all of the metadata files have been read in.122 collection.gdm.waitUntilComplete();123 // Write out the metadata xml files. The destroy below is meant to do this, but never does.124 collection.gdm.save();125 // spd.setProgress(getValue(METADATA_XML_SAVED));126 127 120 File file = new File(tmp_loc); 128 121 // Create backup … … 131 124 backup.deleteOnExit(); 132 125 if(!file.renameTo(backup)) { 133 DebugStream.println("Error in CollectionManager.load(): FileRenamedException");126 DebugStream.println("Error in SaveCollectionTask.run(): FileRenamedException"); 134 127 } 135 128 } … … 143 136 // spd.setProgress(getValue(COLLECTION_CFG_SAVED)); 144 137 145 // Write out the metadata files. 146 Gatherer.c_man.msm.save(); 147 // spd.setProgress(getValue(METADATA_SAVED)); 138 // Write hfiles for the loaded metadata elements into the collection "etc" directory 139 MetadataSetManager.writeHierarchyFiles(new File(Gatherer.c_man.getCollectionEtc())); 148 140 149 141 collection.setSaved(true); … … 307 299 return (int)((double)70 * multiplier); 308 300 case COLLECTION_CFG_SAVED: 309 case METADATA_SAVED:310 case METADATA_XML_SAVED:311 301 return (int)((double)10 * multiplier); 312 302 // Save As … … 325 315 } 326 316 } 327 328 329 330 331
Note:
See TracChangeset
for help on using the changeset viewer.