source: trunk/gli/src/org/greenstone/gatherer/collection/LegacyCollectionImporter.java@ 10009

Last change on this file since 10009 was 9670, checked in by mdewsnip, 19 years ago

Some classifiers (eg. DateList) don't have a "-buttonname" argument -- added a null-pointer check to account for these cases.

  • Property svn:keywords set to Author Date Id Revision
File size: 5.2 KB
Line 
1package org.greenstone.gatherer.collection;
2
3
4import java.io.*;
5import org.greenstone.gatherer.DebugStream;
6import org.greenstone.gatherer.Gatherer;
7import org.greenstone.gatherer.cdm.Argument;
8import org.greenstone.gatherer.cdm.Classifier;
9import org.greenstone.gatherer.cdm.CollectionDesignManager;
10import org.greenstone.gatherer.metadata.MetadataElement;
11import org.greenstone.gatherer.metadata.MetadataTools;
12import org.greenstone.gatherer.metadata.ProfileXMLFileManager;
13import org.greenstone.gatherer.util.StaticStrings;
14import org.greenstone.gatherer.util.Utility;
15
16
17/**
18 * Importing legacy collections involves two main steps:
19 *
20 * 1. Read the existing metadata.xml files and namespace them, usually by prompting the user
21 * to specify the mapping between old metadata elements and new elements.
22 *
23 * 2. Fix up the classify commands in the collect.cfg file to specify the new element names.
24 *
25 * @author Michael Dewsnip
26 */
27public class LegacyCollectionImporter
28{
29 // This copies all the existing metadata.xml files into a backup directory
30 static public void backupMetadataXMLFiles(File collection_dir)
31 {
32 File import_dir = new File(collection_dir, Utility.IMPORT_DIR);
33 File import_bak_dir = new File(collection_dir, Utility.IMPORT_BAK_DIR);
34 import_bak_dir.mkdir();
35 copyMetadataXMLFiles(import_dir, import_bak_dir);
36 }
37
38
39 static private void copyMetadataXMLFiles(File source_dir, File backup_dir)
40 {
41 if (source_dir == null || !source_dir.exists()) {
42 return;
43 }
44
45 // Find the metadata file in this dir
46 File metadata_xml_file_file = new File(source_dir, "metadata.xml");
47 if (metadata_xml_file_file.exists()) {
48 File new_metadata_xml_file_file = new File(backup_dir, "metadata.xml");
49 try {
50 backup_dir.mkdirs();
51 Gatherer.f_man.getQueue().copyFile(metadata_xml_file_file, new_metadata_xml_file_file, null);
52 if (!new_metadata_xml_file_file.exists()) {
53 throw new Exception("");
54 }
55 }
56 catch (Exception e) {
57 DebugStream.println("Exception: couldn't copy the file " + metadata_xml_file_file.getPath() + e.getMessage());
58 }
59 }
60
61 // Now go through child directories
62 File [] children = source_dir.listFiles();
63 for (int i = 0; i < children.length; i++) {
64 File child = children[i];
65 if (child.isDirectory()) {
66 copyMetadataXMLFiles(child, new File(backup_dir, child.getName()));
67 }
68 }
69 }
70
71
72 static public void updateClassifiers(CollectionDesignManager cdm)
73 {
74 // Update the metadata elements in each of the classifiers
75 for (int i = 0; i < cdm.classifier_manager.getSize(); i++) {
76 Classifier classifier = cdm.classifier_manager.getClassifier(i);
77 // System.err.println("Classifier: " + classifier);
78
79 // Update the "-metadata" value
80 Argument metadata_argument = classifier.getArgument("-metadata");
81 String metadata_argument_value = metadata_argument.getValue();
82 String metadata_element_name_full = mapLegacyMetadataElementName(metadata_argument_value);
83 if (metadata_element_name_full != null && !metadata_element_name_full.equals("")) {
84 String metadata_element_display_name_full = MetadataTools.getDisplayNameForMetadataElementWithName(metadata_element_name_full);
85 if (metadata_element_display_name_full != null) {
86 metadata_argument_value = metadata_element_display_name_full;
87 metadata_argument.setValue(metadata_argument_value);
88 }
89 }
90
91 // Update the "-sort" value
92 Argument sort_argument = classifier.getArgument("-sort");
93 if (sort_argument != null) {
94 String sort_element_name_full = mapLegacyMetadataElementName(sort_argument.getValue());
95 if (sort_element_name_full != null && !sort_element_name_full.equals("")) {
96 String sort_element_display_name_full = MetadataTools.getDisplayNameForMetadataElementWithName(sort_element_name_full);
97 if (sort_element_display_name_full != null) {
98 sort_argument.setValue(sort_element_display_name_full);
99 }
100 }
101 }
102
103 // Set the "-buttonname" value if it isn't already set
104 Argument buttonname_argument = classifier.getArgument("-buttonname");
105 if (buttonname_argument != null && buttonname_argument.getValue().equals("")) {
106 int namespace_index = metadata_argument_value.indexOf(".");
107 if (namespace_index != -1) {
108 buttonname_argument.setValue(metadata_argument_value.substring(namespace_index + 1));
109 }
110 else {
111 buttonname_argument.setValue(metadata_argument_value);
112 }
113 buttonname_argument.setAssigned(true);
114 }
115
116 // With Hierarchy classifiers, update the hfile arguments
117 if (classifier.getName().equalsIgnoreCase(StaticStrings.HIERARCHY_CLASSIFIER)) {
118 // Update the "-hfile" value
119 Argument hfile_argument = classifier.getArgument(StaticStrings.HFILE_ARGUMENT);
120 hfile_argument.setValue(metadata_element_name_full + ".txt");
121 }
122
123 // System.err.println("Classifier (after): " + classifier);
124 }
125 }
126
127
128 static private String mapLegacyMetadataElementName(String metadata_element_name)
129 {
130 // Remove the extracted namespace if it has been added
131 if (metadata_element_name.startsWith(StaticStrings.EXTRACTED_NAMESPACE)) {
132 metadata_element_name = metadata_element_name.substring(StaticStrings.EXTRACTED_NAMESPACE.length());
133 }
134
135 // Update the metadata value to the new (namespaced) one
136 return ProfileXMLFileManager.getMetadataElementFor(metadata_element_name);
137 }
138}
Note: See TracBrowser for help on using the repository browser.