Changeset 6093
- Timestamp:
- 2003-12-02T17:24:30+13:00 (20 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/gli/src/org/greenstone/gatherer/msm/LegacyCollectionImporter.java
r6045 r6093 127 127 try { 128 128 Document document = Utility.parse(file.getAbsolutePath(), false); 129 // For each metadata DOM Element 130 NodeList metadata_DOM_elements = document.getDocumentElement().getElementsByTagName(StaticStrings.METADATA_ELEMENT); 131 for(int i = 0; i < metadata_DOM_elements.getLength(); i++) { 132 Element metadata_DOM_element = (Element) metadata_DOM_elements.item(i); 133 // Extract the metadata element name, then check profiles to see if we already know how to handle this element. Once done try to retrieve the element from msm. If that fails, prompt the user for how to import this element. 134 String raw_element_name_str = metadata_DOM_element.getAttribute(StaticStrings.NAME_ATTRIBUTE); 135 // Check profiles 136 if(Gatherer.c_man.getCollection().msm.profiler.containsAction(collection_folder.getAbsolutePath(), raw_element_name_str)) { 137 String element_name_str = Gatherer.c_man.getCollection().msm.profiler.getAction(collection_folder.getAbsolutePath(), raw_element_name_str); 138 // Update the DOM 139 if(element_name_str != null) { 140 metadata_DOM_element.setAttribute(StaticStrings.NAME_ATTRIBUTE, element_name_str); 141 // Now to ensure GLI correctly shows this element as assigned we'll increment its occurances 142 ElementWrapper element_ew = Gatherer.c_man.getCollection().msm.getElement(element_name_str, true); 143 element_ew.inc(); 144 // We now check if this element has a hierarchy, and if so we update the value 145 checkForHierarchy(element_ew, metadata_DOM_element); 146 element_ew = null; 147 } 148 // If the raw name is null now, we have been instructed to ignore this metadata, so we can delete it from the metadata.xml (GDMDocument would do this anyway when it tried to save) 149 else { 150 // Retrieve the parent node 151 Node parent_DOM_node = metadata_DOM_element.getParentNode(); 152 parent_DOM_node.removeChild(metadata_DOM_element); 153 parent_DOM_node = null; 154 } 155 changed = true; 156 element_name_str = null; 157 } 158 // No profile, lets hope it either matches straight away, or the user can decide what to do with it 159 else { 160 // Try to retrieve an element with this name from the msm. Only a perfect match is acceptable 161 ElementWrapper element_ew = Gatherer.c_man.getCollection().msm.getElement(raw_element_name_str, true); 162 // If an element was found, we increment the element count but thats it. Welcome to the only case where the DOM isn't changed 163 if(element_ew != null) { 164 element_ew.inc(); 165 // We now check if this element has a hierarchy, and if so we update the value 166 checkForHierarchy(element_ew, metadata_DOM_element); 167 } 168 // If no match was found prompt the user as to how to proceed 169 else { 170 boolean confirmed = false; 171 boolean dialog_cancelled = false; 172 while(!confirmed) { 173 element_ew = Gatherer.c_man.getCollection().msm.prompt.selectElement(raw_element_name_str); 174 dialog_cancelled = Gatherer.c_man.getCollection().msm.prompt.wasDialogCancelled(); 175 // If the user chooses something then add to profile, and process the DOM as appropriate 176 if(!dialog_cancelled) { 177 // Choosen an element to append or merge 178 if(element_ew != null) { 179 String element_name_str = element_ew.getName(); 180 Gatherer.c_man.getCollection().msm.profiler.addAction(collection_folder.getAbsolutePath(), raw_element_name_str, element_name_str); 181 metadata_DOM_element.setAttribute(StaticStrings.NAME_ATTRIBUTE, element_name_str); 182 element_name_str = null; 183 element_ew.inc(); 184 185 // We can now build the hfile properly is necessary 186 File hfile_file = (File) get(raw_element_name_str); 187 if(hfile_file != null) { 188 ///ystem.err.println("Processing the mapping for: " + raw_element_name_str); 189 // Remove the current mapping 190 remove(raw_element_name_str); 191 // Create the HFile 192 try { 193 HFile hfile = new HFile(element_ew, hfile_file); 194 // Store the mapping 195 ///ystem.err.println("Adding a hfile mapping for: " + element_ew); 196 put(element_ew, hfile); 197 // Now update the value 198 String raw_value_str = MSMUtils.getValue(metadata_DOM_element); 199 String new_value_str = hfile.getFullValue(raw_value_str); 200 MSMUtils.setValue(metadata_DOM_element, new_value_str); 201 raw_value_str = null; 202 new_value_str = null; 203 hfile = null; 204 } 205 catch (Exception exception) { 206 Gatherer.println("***** Exception in org.greenstone.gatherer.msm.LegacyCollectionImporter *****"); 207 Gatherer.printStackTrace(exception); 208 } 209 } 210 element_ew = null; 211 } 212 // Choosen to ignore the element 213 else { 214 Gatherer.c_man.getCollection().msm.profiler.addAction(collection_folder.getAbsolutePath(), raw_element_name_str, null); 215 // Retrieve the parent node 216 Node parent_DOM_node = metadata_DOM_element.getParentNode(); 217 parent_DOM_node.removeChild(metadata_DOM_element); 218 parent_DOM_node = null; 219 } 220 confirmed = true; 221 changed = true; 222 } 223 // Cancel only skips the current metadata.xml file - actually I'll ignore the file on a cancel, as otherwise it will bugger up GDMManager when it starts up 224 else { 225 // Confirm the cancel, informing the user that the metadata.xml will be completely ignored if they continue 226 confirmed = (JOptionPane.showConfirmDialog(Gatherer.g_man, Dictionary.get("MSM.Legacy.Delete_Metadata_XML", file.getAbsolutePath()), Dictionary.get("General.Warning"), JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION); 227 } 228 } 229 } 230 } 231 raw_element_name_str = null; 232 metadata_DOM_element = null; 233 } 234 // If the DOM has saved, write it back out again 235 if(changed) { 129 // Now, if we haven't currently got any metadata sets available to the collection, then this is as far as we need go. Rename the metadata.xml file and then move on the to next one. Remember we always have one set, the extracted metadata set. 130 if(Gatherer.c_man.getCollection().msm.getSets().size() <= 1) { 236 131 // First of all we create a backup of the current metadata.xml 237 132 // I'll start by trying to rename it - but we all know how successful thats been in the past … … 240 135 File old_file = new File(file.getAbsolutePath()); 241 136 File new_file = new File(file.getParentFile(), StaticStrings.METADATA_BAK); 242 137 // Remove any existing backup 243 138 if(new_file.exists()) { 244 139 new_file.delete(); … … 252 147 } 253 148 catch(Exception exception) { 254 255 } 256 149 ///ystem.err.println("Rename Baaaaaad"); 150 } 151 257 152 // Failing that I'll copy it. 258 153 if(!done) { … … 266 161 } 267 162 } 268 Utility.export(document, file);269 163 } 270 document = null; 164 // Otherwise me have some metadata sets, and so can carry on importing 165 else { 166 // For each metadata DOM Element 167 NodeList metadata_DOM_elements = document.getDocumentElement().getElementsByTagName(StaticStrings.METADATA_ELEMENT); 168 for(int i = 0; i < metadata_DOM_elements.getLength(); i++) { 169 Element metadata_DOM_element = (Element) metadata_DOM_elements.item(i); 170 // Extract the metadata element name, then check profiles to see if we already know how to handle this element. Once done try to retrieve the element from msm. If that fails, prompt the user for how to import this element. 171 String raw_element_name_str = metadata_DOM_element.getAttribute(StaticStrings.NAME_ATTRIBUTE); 172 // Check profiles 173 if(Gatherer.c_man.getCollection().msm.profiler.containsAction(collection_folder.getAbsolutePath(), raw_element_name_str)) { 174 String element_name_str = Gatherer.c_man.getCollection().msm.profiler.getAction(collection_folder.getAbsolutePath(), raw_element_name_str); 175 // Update the DOM 176 if(element_name_str != null) { 177 metadata_DOM_element.setAttribute(StaticStrings.NAME_ATTRIBUTE, element_name_str); 178 // Now to ensure GLI correctly shows this element as assigned we'll increment its occurances 179 ElementWrapper element_ew = Gatherer.c_man.getCollection().msm.getElement(element_name_str, true); 180 element_ew.inc(); 181 // We now check if this element has a hierarchy, and if so we update the value 182 checkForHierarchy(element_ew, metadata_DOM_element); 183 element_ew = null; 184 } 185 // If the raw name is null now, we have been instructed to ignore this metadata, so we can delete it from the metadata.xml (GDMDocument would do this anyway when it tried to save) 186 else { 187 // Retrieve the parent node 188 Node parent_DOM_node = metadata_DOM_element.getParentNode(); 189 parent_DOM_node.removeChild(metadata_DOM_element); 190 parent_DOM_node = null; 191 } 192 changed = true; 193 element_name_str = null; 194 } 195 // No profile, lets hope it either matches straight away, or the user can decide what to do with it 196 else { 197 // Try to retrieve an element with this name from the msm. Only a perfect match is acceptable 198 ElementWrapper element_ew = Gatherer.c_man.getCollection().msm.getElement(raw_element_name_str, true); 199 // If an element was found, we increment the element count but thats it. Welcome to the only case where the DOM isn't changed 200 if(element_ew != null) { 201 element_ew.inc(); 202 // We now check if this element has a hierarchy, and if so we update the value 203 checkForHierarchy(element_ew, metadata_DOM_element); 204 } 205 // If no match was found prompt the user as to how to proceed 206 else { 207 boolean confirmed = false; 208 boolean dialog_cancelled = false; 209 while(!confirmed) { 210 element_ew = Gatherer.c_man.getCollection().msm.prompt.selectElement(raw_element_name_str); 211 dialog_cancelled = Gatherer.c_man.getCollection().msm.prompt.wasDialogCancelled(); 212 // If the user chooses something then add to profile, and process the DOM as appropriate 213 if(!dialog_cancelled) { 214 // Choosen an element to append or merge 215 if(element_ew != null) { 216 String element_name_str = element_ew.getName(); 217 Gatherer.c_man.getCollection().msm.profiler.addAction(collection_folder.getAbsolutePath(), raw_element_name_str, element_name_str); 218 metadata_DOM_element.setAttribute(StaticStrings.NAME_ATTRIBUTE, element_name_str); 219 element_name_str = null; 220 element_ew.inc(); 221 222 // We can now build the hfile properly is necessary 223 File hfile_file = (File) get(raw_element_name_str); 224 if(hfile_file != null) { 225 ///ystem.err.println("Processing the mapping for: " + raw_element_name_str); 226 // Remove the current mapping 227 remove(raw_element_name_str); 228 // Create the HFile 229 try { 230 HFile hfile = new HFile(element_ew, hfile_file); 231 // Store the mapping 232 ///ystem.err.println("Adding a hfile mapping for: " + element_ew); 233 put(element_ew, hfile); 234 // Now update the value 235 String raw_value_str = MSMUtils.getValue(metadata_DOM_element); 236 String new_value_str = hfile.getFullValue(raw_value_str); 237 MSMUtils.setValue(metadata_DOM_element, new_value_str); 238 raw_value_str = null; 239 new_value_str = null; 240 hfile = null; 241 } 242 catch (Exception exception) { 243 Gatherer.println("***** Exception in org.greenstone.gatherer.msm.LegacyCollectionImporter *****"); 244 Gatherer.printStackTrace(exception); 245 } 246 } 247 element_ew = null; 248 } 249 // Choosen to ignore the element 250 else { 251 Gatherer.c_man.getCollection().msm.profiler.addAction(collection_folder.getAbsolutePath(), raw_element_name_str, null); 252 // Retrieve the parent node 253 Node parent_DOM_node = metadata_DOM_element.getParentNode(); 254 parent_DOM_node.removeChild(metadata_DOM_element); 255 parent_DOM_node = null; 256 } 257 confirmed = true; 258 changed = true; 259 } 260 // Cancel only skips the current metadata.xml file - actually I'll ignore the file on a cancel, as otherwise it will bugger up GDMManager when it starts up 261 else { 262 // Confirm the cancel, informing the user that the metadata.xml will be completely ignored if they continue 263 confirmed = (JOptionPane.showConfirmDialog(Gatherer.g_man, Dictionary.get("MSM.Legacy.Delete_Metadata_XML", file.getAbsolutePath()), Dictionary.get("General.Warning"), JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION); 264 } 265 } 266 } 267 } 268 raw_element_name_str = null; 269 metadata_DOM_element = null; 270 } 271 // If the DOM has saved, write it back out again 272 if(changed) { 273 // First of all we create a backup of the current metadata.xml 274 // I'll start by trying to rename it - but we all know how successful thats been in the past 275 boolean done = false; 276 try { 277 File old_file = new File(file.getAbsolutePath()); 278 File new_file = new File(file.getParentFile(), StaticStrings.METADATA_BAK); 279 // Remove any existing backup 280 if(new_file.exists()) { 281 new_file.delete(); 282 } 283 old_file.renameTo(new_file); 284 // Then test if the file has been renamed 285 if(new_file.exists()) { 286 ///ystem.err.println("Rename Gooooood"); 287 done = true; 288 } 289 } 290 catch(Exception exception) { 291 ///ystem.err.println("Rename Baaaaaad"); 292 } 293 294 // Failing that I'll copy it. 295 if(!done) { 296 File new_file = new File(file.getParentFile(), StaticStrings.METADATA_BAK); 297 Gatherer.f_man.getQueue().copyFile(file, new_file, null); 298 if(new_file.exists()) { 299 ///ystem.err.println("Copy Goooooood"); 300 } 301 else { 302 ///ystem.err.println("Copy Baaaaaaad"); 303 } 304 } 305 Utility.export(document, file); 306 } 307 document = null; 308 } 271 309 } 272 310 catch(Exception exception) {
Note:
See TracChangeset
for help on using the changeset viewer.