Changeset 5796
- Timestamp:
- 2003-11-07T11:50:28+13:00 (20 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/gli/src/org/greenstone/gatherer/collection/CollectionManager.java
r5785 r5796 53 53 import org.greenstone.gatherer.gui.LockFileDialog; 54 54 import org.greenstone.gatherer.gui.NewCollectionMetadataPrompt; 55 import org.greenstone.gatherer.gui.ExternalCollectionPrompt; 56 import org.greenstone.gatherer.gui.NewMetaSetPrompt; 55 57 import org.greenstone.gatherer.msm.ElementWrapper; 56 58 import org.greenstone.gatherer.msm.GDMManager; … … 764 766 args2[0] = location; 765 767 boolean result = false; 768 boolean non_gatherer_collection = false; 766 769 // Check we have actually been given a .col file. 767 if(location.endsWith(".col")) { 768 File collection_file = new File(location); 769 // Ensure that the directory exists. 770 File collection_directory = collection_file.getParentFile(); 771 if(collection_directory.exists()) { 772 // Special case of a user trying to open an old greenstone collection. One day this will be supported, but for now complain, and explain how to actually re-build this collection 773 File metadata_directory = new File(collection_directory, Utility.META_DIR); 774 if(!metadata_directory.exists()) { 775 JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("CollectionManager.Old_Collection", args2), Dictionary.get("General.Warning"), JOptionPane.INFORMATION_MESSAGE); 776 } 777 // Normal GLI collection 778 else { 779 String name = collection_directory.getName(); 780 File lock_file = new File(collection_file.getParentFile(), LOCK_FILE); 781 // Now determine if a lock already exists on this collection. 782 int choice = LockFileDialog.YES_OPTION; 783 if(lock_file.exists()) { 784 LockFileDialog dialog = new LockFileDialog(Gatherer.g_man, name, lock_file); 785 choice = dialog.getChoice(); 786 dialog.dispose(); 787 dialog = null; 788 } 789 if(choice == LockFileDialog.YES_OPTION) { 790 try { 791 if(lock_file.exists()) { 792 lock_file.delete(); 793 } 794 // Create a lock file. 795 createLockFile(lock_file); 796 // Open the collection file 797 collection = new Collection(collection_file); 798 ///ystem.err.println("In CollectionManager::loadCollection(), collection: " + collection); 799 collection.msm = new MetadataSetManager(); 800 msm = collection.msm; // Legacy 801 collection.msm.load(); 802 collection.cdm = new CollectionDesignManager(new File(collection_file.getParent(), Utility.CONFIG_DIR)); 803 collection.gdm = new GDMManager(); 804 // Tell everyone that it worked. 805 String[] args = new String[1]; 806 args[0] = name; 807 Gatherer.println(Dictionary.get("CollectionManager.Loading_Successful", args)); 808 // Now we need to hook up classes that depend on messages from the metadata set manager to keep their content fresh. 809 collection.msm.addMSMListener(this); 810 // We're done. Let everyone know. 811 if(Gatherer.g_man != null) { 812 workspace_model = null; 813 Gatherer.g_man.collectionChanged(ready()); 814 } 815 result = true; 816 ///ystem.err.println("Done loadCollection()."); 817 } catch (Exception error) { 818 // There is obviously no existing collection present. 819 Gatherer.printStackTrace(error); 820 JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("CollectionManager.Cannot_Open", args2), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE); 821 } 822 } 770 if(!location.endsWith(".col")) { 771 JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("CollectionManager.Not_Col_File", args2), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE); 772 Gatherer.println("CollectionManager.loadCollection: Haven't been given a .col file."); 773 return false; 774 } 775 File collection_file = new File(location); 776 // Ensure that the directory exists. 777 File collection_directory = collection_file.getParentFile(); 778 if(!collection_directory.exists()) { 779 // we cant open this 780 collection_directory = null; 781 return false; 782 } 783 784 // Special case of a user trying to open an old greenstone collection. 785 File metadata_directory = new File(collection_directory, Utility.META_DIR); 786 if(!metadata_directory.exists()) { 787 788 Gatherer.println("CollectionManager.loadCollection: trying to load up a non-gatherer collection"); 789 non_gatherer_collection = true; 790 } 791 792 String name = collection_directory.getName(); 793 File lock_file = new File(collection_file.getParentFile(), LOCK_FILE); 794 // Now determine if a lock already exists on this collection. 795 int choice = LockFileDialog.YES_OPTION; 796 if(lock_file.exists()) { 797 LockFileDialog dialog = new LockFileDialog(Gatherer.g_man, name, lock_file); 798 choice = dialog.getChoice(); 799 dialog.dispose(); 800 dialog = null; 801 } 802 803 if(choice != LockFileDialog.YES_OPTION) { 804 // user has cancelled 805 lock_file = null; 806 collection_directory = null; 807 return false; 808 } 809 810 811 try { 812 if(lock_file.exists()) { 813 lock_file.delete(); 814 } 815 // Create a lock file. 816 createLockFile(lock_file); 817 // Open the collection file 818 collection = new Collection(collection_file); 819 collection.msm = new MetadataSetManager(); 820 msm = collection.msm; // Legacy 821 collection.msm.load(); 822 823 // if non-gatherer collection, need to add some metadata sets 824 if (non_gatherer_collection) { 825 if (!addSomeMetadataSets(collection_directory)) { 826 // for now - return of false means its been cancelled. Any error messages should be sent from the function itself 823 827 lock_file = null; 824 } 825 } 826 else { 827 JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("CollectionManager.File_Not_Found", args2), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE); 828 } 829 collection_directory = null; 830 } 831 else { 832 JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("CollectionManager.Not_Col_File", args2), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE); 833 Gatherer.println("Not a Gatherer Collection."); 834 } 828 collection_directory = null; 829 closeCollection(); 830 return false; 831 } 832 } 833 834 collection.cdm = new CollectionDesignManager(new File(collection_file.getParent(), Utility.CONFIG_DIR)); 835 if (non_gatherer_collection) { 836 // do a dummy load of GDMManager - so we don't load in the old 837 // metadata.xml files, we just create new empty ones. 838 collection.gdm = new GDMManager(true); 839 if (findAndLoadExistingMetadata(collection_directory)==false) { 840 lock_file = null; 841 collection_directory = null; 842 closeCollection(); 843 return false; 844 } 845 } else { 846 collection.gdm = new GDMManager(); 847 } 848 849 // Tell everyone that it worked. 850 String[] args = new String[1]; 851 args[0] = name; 852 Gatherer.println(Dictionary.get("CollectionManager.Loading_Successful", args)); 853 // Now we need to hook up classes that depend on messages from the metadata set manager to keep their content fresh. 854 collection.msm.addMSMListener(this); 855 // We're done. Let everyone know. 856 if(Gatherer.g_man != null) { 857 workspace_model = null; 858 Gatherer.g_man.collectionChanged(ready()); 859 } 860 result = true; 861 ///ystem.err.println("Done loadCollection()."); 862 } catch (Exception error) { 863 // There is obviously no existing collection present. 864 Gatherer.printStackTrace(error); 865 JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("CollectionManager.Cannot_Open", args2), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE); 866 } 867 868 lock_file = null; 869 collection_directory = null; 870 835 871 args2 = null; 836 872 return result; 873 } 874 875 /** 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 shoudl be added back in and finished. [kjdon] */ 876 private boolean addSomeMetadataSets(File collection_dir) { 877 878 879 ExternalCollectionPrompt external_prompt = new ExternalCollectionPrompt(); 880 int meta_choice = external_prompt.getMetadataChoice(); 881 boolean cancelled = external_prompt.isCancelled(); 882 if (cancelled) { 883 return false; 884 } 885 886 /*if (meta_choice == ExternalCollectionPrompt.NEW_META_SET) { 887 NewMetaSetPrompt nmsp = new NewMetaSetPrompt(); 888 if (nmsp.isCancelled()) { 889 return false; 890 } 891 String namespace_str = nmsp.getNamespace(); 892 String name_str = nmsp.getName(); 893 MetadataSet set = Gatherer.c_man.msm.addSet(namespace_str, name_str); 894 } else if (meta_choice == ExternalCollectionPrompt.EXISTING_META_SET) { 895 */ 896 // now we reuse the newcoll metadata prompt for the user to select metadata sets 897 NewCollectionMetadataPrompt ncm_prompt = new NewCollectionMetadataPrompt(true); 898 if (ncm_prompt.isCancelled()) { 899 return false; 900 } 901 ArrayList metadata_sets = ncm_prompt.getSets(); 902 // Import default metadata sets if any. 903 for(int i = 0; metadata_sets != null && i < metadata_sets.size(); i++) { 904 MetadataSet metadata_set = (MetadataSet) metadata_sets.get(i); 905 collection.msm.importMDS(metadata_set.getFile(), false); 906 } 907 /*} else { 908 return false; 909 }*/ 910 // Always import the extracted metadata set 911 collection.msm.importMDS(new File(Utility.METADATA_DIR + Utility.EXTRACTED_METADATA_NAMESPACE + StaticStrings.METADATA_SET_EXTENSION), false); 912 913 return true; 914 } 915 916 917 // now try to load in existing metadata, delete the existing metadata.xml files, then save the new ones. 918 private boolean findAndLoadExistingMetadata(File collection_dir) { 919 920 TreeModel collection_tree = getRecordSet(); 921 FileNode root_node = (FileNode)collection_tree.getRoot(); 922 if (searchForMetadata(collection_tree, root_node)==false) { 923 return false; 924 } 925 removeOldMetadataFiles(collection_dir); 926 collection.gdm.save(root_node); 927 return true; 928 } 929 930 // this moves all the existing metadata files into a back up directory 931 public void removeOldMetadataFiles(File collection_dir) { 932 File import_dir = new File(collection_dir, Utility.IMPORT_DIR); 933 File import_bak_dir = new File(collection_dir, Utility.IMPORT_BAK_DIR); 934 import_bak_dir.mkdir(); 935 936 moveMetadataFiles(import_dir, import_bak_dir); 937 938 } 939 940 private boolean moveMetadataFiles(File source_dir, File dest_dir) { 941 942 // find the metadata file in this dir 943 File meta_file = new File(source_dir, "metadata.xml"); 944 if (meta_file.exists()) { 945 File new_meta_file = new File(dest_dir, "metadata.xml"); 946 try { 947 dest_dir.mkdirs(); 948 if (meta_file.renameTo(new_meta_file)== false) { 949 throw new Exception(""); 950 } 951 } catch (Exception e) { 952 Gatherer.println("Exception: couldn't move the file "+meta_file.getPath() + e.getMessage()); 953 } 954 } 955 956 // now go through child directories 957 File [] children = source_dir.listFiles(); 958 for (int i=0; i<children.length; i++) { 959 File child = children[i]; 960 if (child.isDirectory()) { 961 moveMetadataFiles(child, new File(dest_dir, child.getName())); 962 } 963 } 964 return true; 965 } 966 967 968 private boolean searchForMetadata(TreeModel collection_tree, FileNode current_node) { 969 File source_file = current_node.getFile(); 970 String source_file_name = source_file.getName(); 971 if (source_file_name.equals(Utility.METADATA_XML) || source_file_name.equals("CVS")) { 972 return true; 973 } 974 if (collection.msm.searchForMetadata(current_node, current_node, false)== false) { 975 return false; 976 } 977 int num_children = collection_tree.getChildCount(current_node); 978 for (int i=0; i<num_children; i++) { 979 FileNode child = (FileNode)collection_tree.getChild(current_node, i); 980 if (searchForMetadata(collection_tree, child)==false) { 981 return false; 982 } 983 } 984 return true; 837 985 } 838 986 … … 1026 1174 public void registerBuildMonitor(GShellProgressMonitor monitor) { 1027 1175 build_monitor = monitor; 1028 } 1176 } 1029 1177 /** This method associates the collection copy monitor with the copy monitor created in CreatePane. 1030 1178 * @param monitor A <strong>GShellProgressMonitor</strong> which we will use as the copy monitor. … … 1212 1360 } 1213 1361 1214 private boolean searchForMetadata(File current_file) {1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 }1362 private boolean searchForMetadata(File current_file) { 1363 boolean cancelled = false; 1364 if(current_file.isFile() && current_file.getName().equals(Utility.METADATA_XML)) { 1365 cancelled = collection.msm.searchForMetadata(null, new FileNode(current_file), false, true); // A dummy run only. 1366 } 1367 else { 1368 File[] children_files = current_file.listFiles(); 1369 for(int i = 0; !cancelled && children_files != null && i < children_files.length; i++) { 1370 cancelled = searchForMetadata(children_files[i]); 1371 } 1372 } 1373 return cancelled; 1374 } 1227 1375 1228 1376 private void updateCollectionCFG(File base_cfg, File new_cfg, String description, String email, String title) {
Note:
See TracChangeset
for help on using the changeset viewer.