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

Last change on this file since 11625 was 11625, checked in by mdewsnip, 18 years ago

Removed some more stuff from Utility.

  • Property svn:keywords set to Author Date Id Revision
File size: 5.4 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 /** Definition of an important directory name, in this case the backup import directory for the collection. */
30 static final public String IMPORT_BAK_DIR = "import.bak" + File.separator;
31
32
33 // This copies all the existing metadata.xml files into a backup directory
34 static public void backupMetadataXMLFiles(File collection_dir)
35 {
36 File import_dir = new File(collection_dir, "import");
37 File import_bak_dir = new File(collection_dir, IMPORT_BAK_DIR);
38 import_bak_dir.mkdir();
39 copyMetadataXMLFiles(import_dir, import_bak_dir);
40 }
41
42
43 static private void copyMetadataXMLFiles(File source_dir, File backup_dir)
44 {
45 if (source_dir == null || !source_dir.exists()) {
46 return;
47 }
48
49 // Find the metadata file in this dir
50 File metadata_xml_file_file = new File(source_dir, "metadata.xml");
51 if (metadata_xml_file_file.exists()) {
52 File new_metadata_xml_file_file = new File(backup_dir, "metadata.xml");
53 try {
54 backup_dir.mkdirs();
55 Gatherer.f_man.getQueue().copyFile(metadata_xml_file_file, new_metadata_xml_file_file, false);
56 if (!new_metadata_xml_file_file.exists()) {
57 throw new Exception("");
58 }
59 }
60 catch (Exception e) {
61 DebugStream.println("Exception: couldn't copy the file " + metadata_xml_file_file.getPath() + e.getMessage());
62 }
63 }
64
65 // Now go through child directories
66 File [] children = source_dir.listFiles();
67 for (int i = 0; i < children.length; i++) {
68 File child = children[i];
69 if (child.isDirectory()) {
70 copyMetadataXMLFiles(child, new File(backup_dir, child.getName()));
71 }
72 }
73 }
74
75
76 static public void updateClassifiers(CollectionDesignManager cdm)
77 {
78 // Update the metadata elements in each of the classifiers
79 for (int i = 0; i < cdm.classifier_manager.getSize(); i++) {
80 Classifier classifier = cdm.classifier_manager.getClassifier(i);
81 // System.err.println("Classifier: " + classifier);
82
83 // Update the "-metadata" value
84 Argument metadata_argument = classifier.getArgument("-metadata");
85 String metadata_argument_value = metadata_argument.getValue();
86 String metadata_element_name_full = mapLegacyMetadataElementName(metadata_argument_value);
87 if (metadata_element_name_full != null && !metadata_element_name_full.equals("")) {
88 String metadata_element_display_name_full = MetadataTools.getDisplayNameForMetadataElementWithName(metadata_element_name_full);
89 if (metadata_element_display_name_full != null) {
90 metadata_argument_value = metadata_element_display_name_full;
91 metadata_argument.setValue(metadata_argument_value);
92 }
93 }
94
95 // Update the "-sort" value
96 Argument sort_argument = classifier.getArgument("-sort");
97 if (sort_argument != null) {
98 String sort_element_name_full = mapLegacyMetadataElementName(sort_argument.getValue());
99 if (sort_element_name_full != null && !sort_element_name_full.equals("")) {
100 String sort_element_display_name_full = MetadataTools.getDisplayNameForMetadataElementWithName(sort_element_name_full);
101 if (sort_element_display_name_full != null) {
102 sort_argument.setValue(sort_element_display_name_full);
103 }
104 }
105 }
106
107 // Set the "-buttonname" value if it isn't already set
108 Argument buttonname_argument = classifier.getArgument("-buttonname");
109 if (buttonname_argument != null && buttonname_argument.getValue().equals("")) {
110 int namespace_index = metadata_argument_value.indexOf(".");
111 if (namespace_index != -1) {
112 buttonname_argument.setValue(metadata_argument_value.substring(namespace_index + 1));
113 }
114 else {
115 buttonname_argument.setValue(metadata_argument_value);
116 }
117 buttonname_argument.setAssigned(true);
118 }
119
120 // With Hierarchy classifiers, update the hfile arguments
121 if (classifier.getName().equalsIgnoreCase(StaticStrings.HIERARCHY_CLASSIFIER)) {
122 // Update the "-hfile" value
123 Argument hfile_argument = classifier.getArgument(StaticStrings.HFILE_ARGUMENT);
124 hfile_argument.setValue(metadata_element_name_full + ".txt");
125 }
126
127 // System.err.println("Classifier (after): " + classifier);
128 }
129 }
130
131
132 static private String mapLegacyMetadataElementName(String metadata_element_name)
133 {
134 // Remove the extracted namespace if it has been added
135 if (metadata_element_name.startsWith(StaticStrings.EXTRACTED_NAMESPACE)) {
136 metadata_element_name = metadata_element_name.substring(StaticStrings.EXTRACTED_NAMESPACE.length());
137 }
138
139 // Update the metadata value to the new (namespaced) one
140 return ProfileXMLFileManager.getMetadataElementFor(metadata_element_name);
141 }
142}
Note: See TracBrowser for help on using the repository browser.