source: trunk/gli/src/org/greenstone/gatherer/msm/MetadataSetManager.java@ 4673

Last change on this file since 4673 was 4673, checked in by jmt12, 21 years ago

* empty log message *

  • Property svn:keywords set to Author Date Id Revision
File size: 53.3 KB
Line 
1package org.greenstone.gatherer.msm;
2/**
3 *#########################################################################
4 *
5 * A component of the Gatherer application, part of the Greenstone digital
6 * library suite from the New Zealand Digital Library Project at the
7 * University of Waikato, New Zealand.
8 *
9 * <BR><BR>
10 *
11 * Author: John Thompson, Greenstone Digital Library, University of Waikato
12 *
13 * <BR><BR>
14 *
15 * Copyright (C) 1999 New Zealand Digital Library Project
16 *
17 * <BR><BR>
18 *
19 * This program is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation; either version 2 of the License, or
22 * (at your option) any later version.
23 *
24 * <BR><BR>
25 *
26 * This program is distributed in the hope that it will be useful,
27 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 * GNU General Public License for more details.
30 *
31 * <BR><BR>
32 *
33 * You should have received a copy of the GNU General Public License
34 * along with this program; if not, write to the Free Software
35 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
36 *########################################################################
37 */
38import java.io.*;
39import java.util.*;
40import javax.swing.*;
41import javax.swing.filechooser.*;
42import org.greenstone.gatherer.Gatherer;
43import org.greenstone.gatherer.cdm.CommandTokenizer;
44import org.greenstone.gatherer.file.FileNode;
45import org.greenstone.gatherer.gui.MetaEditPrompt;
46import org.greenstone.gatherer.mem.MetadataEditorManager;
47import org.greenstone.gatherer.msm.Declarations;
48import org.greenstone.gatherer.msm.MDSFileFilter;
49import org.greenstone.gatherer.msm.MetadataSet;
50import org.greenstone.gatherer.msm.MSMAction;
51import org.greenstone.gatherer.msm.MSMEvent;
52import org.greenstone.gatherer.msm.MSMPrompt;
53import org.greenstone.gatherer.msm.MSMUtils;
54import org.greenstone.gatherer.valuetree.GValueModel;
55import org.greenstone.gatherer.valuetree.GValueNode;
56import org.greenstone.gatherer.util.MetadataXML;
57import org.greenstone.gatherer.util.Utility;
58import org.apache.xerces.parsers.*;
59import org.apache.xml.serialize.*;
60import org.w3c.dom.*;
61import org.xml.sax.*;
62/** This class is responsible for managing all the metadata sets in the collection and for providing methods for manipulating the aforementioned sets contents.
63 * @author John Thompson, Greenstone Digital Library, University of Waikato
64 * @version 2.3b
65 */
66public class MetadataSetManager {
67
68 /** The name of the hidden, or system, metadata set. */
69 static final public String HIDDEN = "hidden";
70
71 /** A mapping from metadata namespace to metadata set. */
72 static private Hashtable mds_hashtable = new Hashtable();
73
74 /** The class responsible for creating and maintaining all the visual components of the metadata management package. */
75 public MSMPrompt prompt = null;
76 /** The profiler is responsible for remembering what actions a user has requested when importing metadata, so as to prevent the user needlessly re-entering this information for each import. */
77 public MSMProfiler profiler = null;
78
79 /** Records all of the changes made to metadata as part of the current metadata change. Entries map from a particular FileNode to an ArrayList of the modified metadata for that node. Not to be confused with the undo managers idea of undo which records a list of all metadata changes requested. */
80 private HashMap undo_buffer = new HashMap();
81 /** The loader responsible for sequentially loading and attempting to use all registered metadata parsers to extract existing metadata from new files. */
82 private ExistingMetadataLoader loader = null;
83 /** Specialized parser for parsing GreenstoneDirectoryMetadata files, which not only caches entries, but also breaks up massive metadata.xml files into reasonable sizes. */
84 private GDMParser gdm_parser = null;
85 /** A list of classes who are interested in changes to the loaded metadata sets. */
86 private Vector listeners = null;
87
88 /** Constructor. */
89 public MetadataSetManager() {
90 this.gdm_parser = new GDMParser();
91 this.listeners = new Vector();
92 this.loader = new ExistingMetadataLoader();
93 loadProfiler();
94 this.prompt = new MSMPrompt(this);
95 }
96
97 /** Attach a piece of metadata to a record or records, ensuring the value tree is built properly, and correct messaging fired.
98 * @param records A FileNode[] of records, or directories, to add the specified metadata to.
99 * @param element The metadata element, contained within an ElementWrapper to base metadata on.
100 * @param value The value to assign to the metadata as a String.
101 */
102 public void addMetadata(long id, FileNode records[], ElementWrapper element, String value_str) {
103 if (records.length == 1) {
104 addMetadata(id, records, element, value_str, MetaEditPrompt.ACCUMULATE_ALL);
105 }
106 else {
107 addMetadata(id, records, element, value_str, MetaEditPrompt.CONFIRM);
108 }
109 }
110 public void addMetadata(long id, FileNode records[], ElementWrapper element, String value_str, int action) {
111 // Retrieve the appropriate value node from the value tree for this element, creating it if necessary.
112 GValueModel model = getValueTree(element);
113 GValueNode value = null;
114 if(model != null) {
115 value = model.addValue(value_str); // Only adds if not already present, otherwise just returns existing node.
116 }
117 else {
118 value = new GValueNode(element.toString(), value_str);
119 }
120 // Create new metadata.
121 Metadata metadata = new Metadata(element, value);
122 // Reset the undo buffer.
123 undo_buffer.clear();
124 // Assign to records. Note that we must watch for responses from the user prompts, and cease loop if break signalled.
125 // Now add the metadata to each selected file node.
126 for(int i = 0; action != MetaEditPrompt.CANCEL && i < records.length; i++) {
127 action = addMetadata(id, records[i], metadata, action, (records.length > 1));
128 }
129 // If we were cancelled we should undo any changes so far
130 if(action == MetaEditPrompt.CANCEL) {
131 for(Iterator keys = undo_buffer.keySet().iterator(); keys.hasNext(); ) {
132 FileNode record = (FileNode) keys.next();
133 undoAdd(id, record);
134 }
135 }
136 }
137
138 /** Adds a metadata set listener to this set, if it isn't alreay listening.
139 * @param listener The new MSMListener.
140 */
141 public void addMSMListener(MSMListener listener) {
142 if(!listeners.contains(listener)) {
143 listeners.add(listener);
144 }
145 }
146
147 public MetadataSet addSet(String namespace, String name) {
148 MetadataSet mds = new MetadataSet(Utility.METADATA_SET_TEMPLATE);
149 mds.setAttribute("creator","The Gatherer");
150 // Calculate lastchanged to right now on this machine by this user
151 String user_name = System.getProperty("user.name");
152 String machine_name = Utility.getMachineName();
153 mds.setAttribute("lastchanged", Utility.getDateString() + " - " + user_name + " on " + machine_name);
154 // And the remaining attributes.
155 //mds.setAttribute("name", name);
156 mds.setAttribute("namespace", namespace);
157 mds_hashtable.put(namespace, mds);
158 // Add the name element.
159 mds.setName(name);
160 fireSetChanged(mds);
161 return mds;
162 }
163
164 /** Add a value tree to a given metadata element.
165 * @param element The ElementWrapper containing the element you wish to add a value tree for.
166 * @param value_tree The root Element of the value tree.
167 */
168 public void addValueTree(GValueModel model) {
169 ElementWrapper element = model.getElement();
170 String namespace = element.getNamespace();
171 MetadataSet mds = (MetadataSet) mds_hashtable.get(namespace);
172 if(mds != null) {
173 mds.addValueTree(element, model);
174 }
175 }
176 /** Destructor.
177 * @see org.greenstone.gatherer.msm.MSMProfiler
178 */
179 public void destroy() {
180 mds_hashtable.clear();
181 profiler.destroy();
182 profiler = null;
183 }
184 /** Method called to open a metadata set editing window.
185 * @return A boolean indicating if the edit was successful.
186 */
187 public boolean editMDS() {
188 MetadataEditorManager mem = new MetadataEditorManager();
189 mem.dispose();
190 mem = null;
191 return true;
192 }
193
194 /** This method is called to export a metadata set. First a prompt is displayed to gather necessary details such as which metadata set to export, where to export it to and what conditions should be applied when exporting. Once this information is gathered the static method <i>exportMDS()</i> is called with the appropriate output stream.
195 * @return A boolean which is <i>true</i> if the metadata set has been exported successfully, <i>false</i> otherwise.
196 */
197 public boolean exportMDS() {
198 ExportMDSPrompt emdsp = new ExportMDSPrompt(this, true);
199 int result = emdsp.display();
200 MetadataSet set = emdsp.getSelectedSet();
201 if(result == ExportMDSPrompt.EXPORT && set != null) {
202 File file = emdsp.getSelectedFile();
203 MetadataSet set_copy = new MetadataSet(set, emdsp.getSelectedCondition());
204 try {
205 file.getParentFile().mkdirs();
206 Utility.export(set_copy.getDocument(), file);
207 // Now for each element attempt to save its value tree.
208 NodeList elements = set_copy.getElements();
209 for(int i = elements.getLength() - 1; i >= 0; i--) {
210 ElementWrapper value_element = new ElementWrapper((Element)elements.item(i));
211 GValueModel value_tree = set_copy.getValueTree(value_element);
212 if(value_tree != null) {
213 File value_file = new File(file.getParentFile(), value_element.toString() + ".mdv");
214 ///ystem.err.println("Saving value file: " + value_file.toString());
215 Utility.export(value_tree.getDocument(), value_file);
216 }
217 }
218 return true;
219 }
220 catch (Exception error) {
221 error.printStackTrace();
222 }
223 }
224 emdsp.dispose();
225 emdsp = null;
226 return false;
227 }
228
229 /** Fire an element changed message off to all registered listeners.
230 * @param event An MSMEvent detailing the change.
231 */
232 public void fireElementChanged(MSMEvent event) {
233 // Then send it to all the listeners.
234 for(int i = 0; i < listeners.size(); i++) {
235 ((MSMListener)listeners.get(i)).elementChanged(event);
236 }
237 }
238
239 /** Fire a metadata value changed message, whose id is to be generated now, off to all registered listeners. */
240 public void fireMetadataChanged(FileNode node, Metadata old_data, Metadata new_data) {
241 fireMetadataChanged(System.currentTimeMillis(), node, old_data, new_data);
242 }
243
244 /** Fire a metadata value changed message off to all registered listeners. */
245 public void fireMetadataChanged(long id, FileNode node, Metadata old_data, Metadata new_data) {
246 if(old_data != null) {
247 old_data.getElement().dec();
248 }
249 if(new_data != null) {
250 new_data.getElement().inc();
251 }
252 ///ystem.err.println("Metadata changed: " + record + " > '" + old_data + "' -> '" + new_data + "'");
253 // Create a new MSMEvent based on the record.
254 MSMEvent event = new MSMEvent(this, id, node, old_data, new_data);
255 // Then send it to all the listeners.
256 for(int i = 0; i < listeners.size(); i++) {
257 ((MSMListener)listeners.get(i)).metadataChanged(event);
258 }
259 }
260
261 /** Fire a metadata set changed message off to all registered listeners.
262 * @param set The MetadataSet thats changed.
263 */
264 public void fireSetChanged(MetadataSet set) {
265 // Create a new MSMEvent, with a MSMAction containing only the new set.
266 MSMEvent event = new MSMEvent(this, 0L, new MSMAction(set.toString(), null, -1, null));
267 // Then send it to all the listeners.
268 for(int i = 0; i < listeners.size(); i++) {
269 ((MSMListener)listeners.get(i)).setChanged(event);
270 }
271 }
272 /** Called whenever the value tree associated with an element changes significantly.
273 * @param element The metadata element whose value tree has changed, as an ElementWrapper.
274 */
275 public void fireValueChanged(ElementWrapper element, GValueModel old_model, GValueModel new_model) {
276 // Create a new MSMEvent based on the element wrapper.
277 MSMEvent event = new MSMEvent(this, 0L, element, old_model, new_model);
278 // Then send it to all the listeners.
279 for(int i = 0; i < listeners.size(); i++) {
280 ((MSMListener)listeners.get(i)).valueChanged(event);
281 }
282 }
283 /** Builds a list of elements that have been assigned as metadata in this collection. We go through all of the elements, looking for elements whose occurances are greater than 0. A convenience call to the version with one parameter.
284 * @return A Vector of assigned elements.
285 */
286 public Vector getAssignedElements() {
287 return getAssignedElements(false);
288 }
289 /** Builds a list of elements that have been assigned as metadata in this collection. We go through all of the elements, looking for elements whose occurances are greater than 0.
290 * @param hierarchy_only <i>true</i> to only return those elements that are both assigned and have hierarchical value trees, <i>false</i> for just assignments.
291 * @return A Vector of assigned elements.
292 */
293 public Vector getAssignedElements(boolean hierarchy_only) {
294 Vector elements = new Vector();
295 for(Enumeration keys = mds_hashtable.keys(); keys.hasMoreElements(); ) {
296 MetadataSet mds = (MetadataSet)mds_hashtable.get(keys.nextElement());
297 if(!mds.getNamespace().equals(HIDDEN)) {
298 NodeList set_elements = mds.getElements();
299 for(int i = 0; i < set_elements.getLength(); i++) {
300 ElementWrapper element = new ElementWrapper((Element)set_elements.item(i));
301 elements.add(element);
302 }
303 }
304 }
305 Collections.sort(elements);
306 for(int i = elements.size(); i != 0; i--) {
307 ElementWrapper element = (ElementWrapper) elements.get(i - 1);
308 if(element.getOccurances() == 0 && element.getNamespace().length() > 0 ) { //&& !element.toString().equals("Source")) {
309 elements.remove(element);
310 }
311 else if(hierarchy_only) {
312 GValueModel model = getValueTree(element);
313 if(!model.isHierarchy()) {
314 elements.remove(element);
315 }
316 }
317 }
318 return elements;
319 }
320 /** Used to get all the (non-hidden) elements in this manager.
321 * @return A Vector of ElementWrappers.
322 */
323 public Vector getElements() {
324 return getElements(false);
325 }
326 /** Used to get all the elements in this manager.
327 * @param all <i>true</i> if all elements, including hidden, should be returned.
328 * @return A Vector of ElementWrappers.
329 */
330 public Vector getElements(boolean all) {
331 Vector all_elements = new Vector();
332 for(Enumeration keys = mds_hashtable.keys(); keys.hasMoreElements(); ) {
333 MetadataSet mds = (MetadataSet)mds_hashtable.get(keys.nextElement());
334 if(all || !mds.getNamespace().equals(HIDDEN)) {
335 NodeList set_elements = mds.getElements();
336 ///ystem.err.println("The set " + mds + " has " + set_elements.getLength() + " elements.");
337 for(int i = 0; i < set_elements.getLength(); i++) {
338 ElementWrapper element = new ElementWrapper((Element)set_elements.item(i));
339 // if(!element.toString().equals("Source")) {
340 all_elements.add(element);
341 // }
342 }
343 }
344 }
345 Collections.sort(all_elements);
346 return all_elements;
347 }
348 /** Returns all the elements within this set as a combobox model.
349 * @return A MetadataComboBoxModel containing all the metadata elements from all the sets, with namespacing.
350 */
351 public MetadataComboBoxModel getElementModel() {
352 return new MetadataComboBoxModel(this);
353 }
354 /** Retrieve a metadata element by its index.
355 * @param index The specified index as an int.
356 * @return An ElementWrapper containing the specied element, or <i>null</i> is no such element exists.
357 */
358 public ElementWrapper getElement(int index) {
359 Vector elements = getElements(false);
360 ElementWrapper result = null;
361 if(0 <= index && index < elements.size()) {
362 result = (ElementWrapper) elements.get(index);
363 }
364 return result;
365 }
366 /** Retrieve a metadata element by looking at the current metadata element. Note that this 'index' element may now be disconnected from the DOM model, so we have to reload the target element by the string method.
367 * @param element The possibly out-of-data MetadataElement.
368 * @return An ElementWrapper containing the specied element, or <i>null</i> is no such element exists.
369 */
370 public ElementWrapper getElement(ElementWrapper element) {
371 return getElement(element.toString());
372 }
373 /** Retrieve a metadata element by its fully qualified name.
374 * @param name The elements name as a String.
375 * @return An ElementWrapper containing the specied element, or <i>null</i> is no such element exists.
376 */
377 public ElementWrapper getElement(String name) {
378 ///ystem.err.println("Retrieve element " + name);
379 if(name == null) {
380 ///ystem.err.println("No name!");
381 return null;
382 }
383 ElementWrapper result = null;
384 MetadataSet set = null;
385 String element = null;
386 // First we seperate off what set it is in, where we have '<set><namespace_separator><element>'.
387 if(name.indexOf(MSMUtils.NS_SEP) != -1) {
388 String namespace = name.substring(0, name.indexOf(MSMUtils.NS_SEP));
389 // Retrieve the correct set if possible.
390 set = (MetadataSet)mds_hashtable.get(namespace);
391 namespace = null;
392 // Now retrieve the element name.
393 element = name.substring(name.indexOf(MSMUtils.NS_SEP) + 1);
394 }
395 else {
396 // No namespace so assume ns = "".
397 set = (MetadataSet)mds_hashtable.get("");
398 element = name;
399 }
400 if(set != null) {
401 ///ystem.err.print("Trying to match element " + element +"?");
402 Element temp = set.getElement(element);
403 if(temp != null) {
404 ///ystem.err.println("Found!");
405 result = new ElementWrapper(temp);
406 }
407 else {
408 ///ystem.err.println("Not found.");
409 }
410 element = null;
411 temp = null;
412 }
413 else {
414 ///ystem.err.println("No such set");
415 }
416 set = null;
417 if(result == null) {
418 ///ystem.err.println("Shouldn't return null unless this is greenstone archive parsing.");
419 }
420 return result;
421 }
422 /** Retrieve a certain named element from a certain named set.
423 * @param set The metadata set whose element you want.
424 * @param name The name of the element.
425 * @return An ElementWrapper around the requested element, or null if no such set or element.
426 */
427 public ElementWrapper getElement(String set, String name) {
428 if(mds_hashtable.containsKey(set)) {
429 MetadataSet temp = (MetadataSet)mds_hashtable.get(set);
430 return new ElementWrapper(temp.getElement(name));
431 }
432 return null;
433 }
434 /** Get all of the metadata elements as an array of nodelists.
435 * @return A NodeList[] of metadata elements.
436 */
437 public NodeList[] getNodeLists() {
438 NodeList elements[] = null;
439 int index = 0;
440 elements = new NodeList[getSets().size()]; // Remember not to count hidden metadata
441 for(Enumeration keys = mds_hashtable.keys(); keys.hasMoreElements(); ) {
442 MetadataSet mds = (MetadataSet)mds_hashtable.get(keys.nextElement());
443 if(!mds.getNamespace().equals(HIDDEN)) {
444 elements[index] = mds.getElements();
445 index++;
446 }
447 }
448 return elements;
449 }
450
451 /** Retrieve the named metadata set.
452 * @param name The sets name as a String.
453 * @return The MetadataSet as named, or null if no such set.
454 */
455 public MetadataSet getSet(String name) {
456 if(mds_hashtable.containsKey(name)) {
457 return (MetadataSet) mds_hashtable.get(name);
458 }
459 else {
460 ///ystem.err.println("Couldn't find metadata set.");
461 if(name.equals(HIDDEN)) {
462 return createHidden();
463 }
464 }
465 return null;
466 }
467
468 /** Method to retrieve all of the metadata sets loaded in this collection.
469 * @return A Vector of metadata sets.
470 */
471 public Vector getSets() {
472 return getSets(true);
473 }
474 public Vector getSets(boolean include_greenstone) {
475 Vector result = new Vector();
476 for(Enumeration keys = mds_hashtable.keys(); keys.hasMoreElements(); ) {
477 MetadataSet set = (MetadataSet)mds_hashtable.get(keys.nextElement());
478 if(!set.getNamespace().equals(HIDDEN) && (include_greenstone || !set.getNamespace().equals(""))) {
479 result.add(set);
480 }
481 }
482 return result;
483 }
484 /** Find the total number of elements loaded.
485 * @return The element count as an int.
486 */
487 public int getSize() {
488 int count = 0;
489 if(mds_hashtable.size() > 0) {
490 for(Enumeration keys = mds_hashtable.keys(); keys.hasMoreElements();) {
491 MetadataSet mds = (MetadataSet)mds_hashtable.get(keys.nextElement());
492 if(mds.getNamespace().equals(HIDDEN)) {
493 count = count + mds.size();
494 }
495 }
496 }
497 return count;
498 }
499 /** Get the value tree that matches the given element.
500 * @param element The ElementWrapper representing the element.
501 * @return The GValueModel representing the value tree or null.
502 */
503 public GValueModel getValueTree(ElementWrapper element) {
504 GValueModel value_tree = null;
505 if(element != null) {
506 String namespace = element.getNamespace();
507 if(namespace.length() > 0) {
508 MetadataSet mds = (MetadataSet) mds_hashtable.get(namespace);
509 if(mds != null) {
510 value_tree = mds.getValueTree(element);
511 }
512 }
513 }
514 return value_tree;
515 }
516 /** This method is called to import a metadata set. First a prompt is displayed to gather necessary details such as which metadata set to import. Once this information is gathered the method <i>importMDS(File)</i> is called with the appropriate filename.
517 * @return A boolean which is <i>true</i> if the metadata set has been imported successfully, <i>false</i> otherwise.
518 */
519 public boolean importMDS() {
520 JFileChooser chooser = new JFileChooser(new File(Utility.METADATA_DIR));
521 javax.swing.filechooser.FileFilter filter = new MDSFileFilter();
522 chooser.setFileFilter(filter);
523 int returnVal = chooser.showDialog(Gatherer.g_man, Gatherer.dictionary.get("MSMPrompt.File_Import"));
524 if(returnVal == JFileChooser.APPROVE_OPTION) {
525 return importMDS(chooser.getSelectedFile(), true);
526 }
527 return false;
528 }
529
530 public boolean importMDS(File mds_file, boolean user_driven) {
531 // 1. Parse the new file.
532 MetadataSet mds_new = new MetadataSet(mds_file);
533 if(user_driven) {
534 // Display a prompt asking how much of the value structure the user wishes to import.
535 // ...but only if there are some MDV files to read values from
536 FilenameFilter mdv_filter = new MDVFilenameFilter(mds_new.getNamespace());
537 File[] mdv_files = mds_file.getParentFile().listFiles(mdv_filter);
538 if (mdv_files.length > 0) {
539 ExportMDSPrompt imdsp = new ExportMDSPrompt(this, false);
540 int result = imdsp.display();
541 if(result == ExportMDSPrompt.EXPORT) { // Here export means the user didn't cancel.
542 switch(imdsp.getSelectedCondition()) {
543 case MetadataSet.NO_VALUES:
544 mds_new = new MetadataSet(mds_new, MetadataSet.NO_VALUES);
545 break;
546 case MetadataSet.SUBJECTS_ONLY:
547 mds_new = new MetadataSet(mds_new, MetadataSet.SUBJECTS_ONLY);
548 break;
549 default: // ALL_VALUES
550 // Don't do anything.
551 }
552 }
553 else {
554 mds_new = null;
555 }
556 imdsp.dispose();
557 imdsp = null;
558 }
559 }
560 // Carry on importing the new collection
561 if(mds_new != null && mds_new.getDocument() != null) {
562 String family = mds_new.getNamespace();
563 // 2. See if we have another metadata set of the same family
564 // already. If so retrieve it and merge.
565 boolean matched = false;
566 for(Enumeration keys = mds_hashtable.keys();
567 keys.hasMoreElements();) {
568 String key = (String)keys.nextElement();
569 if(key.equals(family)) {
570 matched = true;
571 MetadataSet mds_cur = (MetadataSet)mds_hashtable.get(key);
572 ///ystem.err.println("Merging " + mds_new + " into " + mds_cur);
573 MergeTask task = new MergeTask(mds_cur, mds_new);
574 task.start();
575 }
576 }
577 if(!matched) {
578 ///ystem.err.println("Mapping " + family + " to " + mds_new);
579 mds_hashtable.put(family, mds_new);
580 }
581 fireSetChanged(mds_new);
582 return true;
583 }
584 // else we cancelled for some reason.
585 return false;
586 }
587
588 private class MergeTask
589 extends Thread {
590
591 MetadataSet mds_cur;
592 MetadataSet mds_new;
593
594 MergeTask(MetadataSet mds_cur, MetadataSet mds_new) {
595 this.mds_cur = mds_cur;
596 this.mds_new = mds_new;
597 }
598
599 public void run() {
600 mergeMDS(mds_cur, mds_new);
601 // Fire setChanged() message.
602 fireSetChanged(mds_new);
603 }
604 }
605
606
607 /** Accepts .mdv files for a certain metadata set. */
608 private class MDVFilenameFilter implements FilenameFilter
609 {
610 private String mds_namespace = null;
611
612 public MDVFilenameFilter(String mds_namespace)
613 {
614 this.mds_namespace = mds_namespace.toLowerCase();
615 }
616
617 public boolean accept(File dir, String name)
618 {
619 String copy = name.toLowerCase();
620 if (copy.startsWith(mds_namespace) && copy.endsWith(".mdv")) {
621 return true;
622 }
623
624 return false;
625 }
626 }
627
628 /** This method reloads all of the metadata sets that have been marked as included in this collection by entries in the collection configuration file.
629 */
630 public void load() {
631 File source = new File(Gatherer.c_man.getCollectionMetadata());
632 File files[] = source.listFiles();
633 for(int i = 0; files != null && i < files.length; i++) {
634 if(files[i].getName().endsWith(".mds")) {
635 importMDS(files[i], false);
636 }
637 }
638 // If no current 'hidden' metadata set exists, create one.
639 if(getSet(HIDDEN) == null) {
640 createHidden();
641 }
642 }
643 /** This method takes two metadata sets, the current one and a new one, and merges them. This merge takes place at an element level falling to lower levels as necessary (using <i>mergeMDE()</i> to merge elements and <i>mergeMDV()</i> to merge value trees.
644 * @param mds_current The currently loaded MetadataSet.
645 * @param mds_new A new MetadataSet you wish to merge in.
646 * @return A boolean with value <i>true</i> indicating if the merge was successful, otherwise <i>false</i> if errors were detected.
647 */
648 public boolean mergeMDS(MetadataSet mds_cur, MetadataSet mds_new) {
649 // For a super quick check for equivelent trees, we compare the last changed values.
650 if(mds_cur.getLastChanged().equals(mds_new.getLastChanged())) {
651 // Exactly the same. Nothing to change.
652 return true;
653 }
654 // Show initial progress prompt.
655 prompt.startMerge(mds_new.size());
656 boolean cancel = false;
657 // For each element in the new set
658 for(int i = 0; !cancel && i < mds_new.size(); i++) {
659 boolean cont = false;
660 Element mde_new = mds_new.getElement(i);
661 GValueModel mde_values = mds_new.getValueTree(new ElementWrapper(mde_new));
662 // See if the element already exists in the current set
663 Element mde_cur = mds_cur.getElement(mde_new.getAttribute("name"));
664 int option = Declarations.NO_ACTION;
665 while(!cont && !cancel) {
666 // We may be dealing with a brand new element, or possibly a
667 // renamed one.
668 if(mde_cur == null) {
669 // Provide merge, rename and skip options.
670 option = prompt.mDSPrompt(mds_cur, null, mds_new, mde_new);
671 }
672 else {
673 // If the two elements have equal structure we only have
674 // to worry about merging the values.
675 if(MSMUtils.elementsEqual(mds_cur, mde_cur, mds_new, mde_new, false)) {
676 cancel = !mergeMDV(mds_cur, mde_cur, mds_new, mde_new);
677 cont = true;
678 }
679 else {
680 // Provide add, merge and skip options.
681 option = prompt.mDSPrompt(mds_cur, mde_cur, mds_new, mde_new);
682 }
683 }
684 String reason = null;
685 switch(option) {
686 case Declarations.ADD:
687 // Only available to brand new elements, this options
688 // simply adds the element to the set.
689 reason = mds_cur.addElement(mde_new, mde_values);
690 if(reason == null) {
691 cont = true;
692 }
693 else {
694 prompt.addFailed(mde_new, reason);
695 cont = false;
696 }
697 break;
698 case Declarations.CANCEL:
699 cancel = true;
700 cont = true;
701 break;
702 case Declarations.FORCE_MERGE:
703 // If the mde_cur is null, that means the users has asked
704 // to merge but hasn't choosen any element to merge with.
705 // Make the user select an element.
706 mde_cur = prompt.selectElement(mds_cur);
707 case Declarations.MERGE:
708 // This case in turn calls the mergeMDE method to perform
709 // the actual merging of the Elements.
710 if(mde_cur != null) {
711 cancel = !mergeMDE(mds_cur, mde_cur, mds_new, mde_new);
712 }
713 cont = true;
714 break;
715 case Declarations.RENAME:
716 // This case adds the Element, but requires the user to
717 // enter a unique name.
718 String new_name = prompt.rename(mde_new);
719 if(new_name != null && new_name.length() > 0) {
720 reason = mds_cur.addElement(mde_new, new_name, mde_values);
721 if(reason == null) {
722 mde_cur = mds_cur.getElement(new_name);
723 cont = true;
724 }
725 else {
726 prompt.renameFailed(mde_new, new_name, reason);
727 cont = false;
728 }
729 }
730 else {
731 if(new_name != null) {
732 prompt.renameFailed(mde_new, new_name,
733 "MSMPrompt.Invalid_Name");
734 }
735 cont = false;
736 }
737 break;
738 case Declarations.REPLACE:
739 // Removes the existing Element then adds the new.
740
741 mds_cur.removeElement(mde_cur);
742
743 reason = mds_cur.addElement(mde_new, mde_values);
744 if(reason == null) {
745 cont = true;
746 }
747 else {
748 prompt.removeFailed(mde_cur, reason);
749 cont = false;
750 }
751 break;
752 case Declarations.SKIP:
753 // Does not change the set.
754 cont = true;
755 break;
756 }
757 // Add this action to profile for later reference.
758 if(profiler == null) {
759 ///ystem.err.println("No Profiler");
760 }
761 //profiler.addAction(mds_new.getFile().getAbsolutePath(), MSMUtils.getFullName(mde_new), option, MSMUtils.getFullName(mde_cur));
762 }
763 prompt.incrementMerge();
764 }
765 prompt.endMerge();
766 return true;
767 }
768
769 /** This method allows for two metadata elements to be merged. Essentially merging existing elements give the users such options as keeping or replacing attribute elements as they are merged.
770 * @param mde_cur The Element that already exists in the current metadata sets.
771 * @param mde_new A new Element which has the same name as the current one but different data.
772 * @return A boolean that if <i>true</i> indicats the action was completed. If <i>false</i> then an error or user action has prevented the merge.
773 * TODO Implement
774 */
775 public boolean mergeMDE(MetadataSet mds_cur, Element mde_cur, MetadataSet mds_new, Element mde_new) {
776 for(Node mdn_new = mde_new.getFirstChild(); mdn_new != null; mdn_new = mdn_new.getNextSibling()) {
777 // Only merge the nodes whose name is 'Attribute'
778 if(mdn_new.getNodeName().equals("Attribute")) {
779 Element att_new = (Element) mdn_new;
780 int action = Declarations.NO_ACTION;
781 Element replace_att = null; // replace this att with the new one
782 // Unfortunately some attributes, such as author, can have several occurances, so match each in turn.
783 Element temp[] = MSMUtils.getAttributeNodesNamed(mde_cur, att_new.getAttribute("name"));
784 if (temp==null) {
785 action = Declarations.ADD;
786 } else {
787
788 // look for an exact match
789 for(int i = 0; temp != null && i < temp.length; i++) {
790 Element att_cur = temp[i];
791 if(MSMUtils.attributesEqual(att_cur, att_new)) {
792 action = Declarations.SKIP;
793 break;
794 }
795 att_cur = null;
796 }
797 if (action == Declarations.NO_ACTION) {
798 // we didn't find a match, so we have to prompt teh user for what to do
799 Object result = prompt.mDEPrompt(mde_cur, temp, mde_new, att_new);
800 if (result instanceof Integer) {
801 action = ((Integer)result).intValue();
802 } else {
803 // we have a replace action, and the returned object is the Attribute to replace
804 action = Declarations.REPLACE;
805 replace_att = (Element)result;
806 }
807 }
808 }
809
810 // now do the required action
811 switch (action) {
812 case Declarations.REPLACE:
813 // Out with the old.
814 mde_cur.removeChild(replace_att);
815 case Declarations.ADD:
816 // Simply add the new attribute. No clash is possible as we have already tested for it.
817 MSMUtils.add(mde_cur, att_new);
818 break;
819 case Declarations.SKIP:
820 // Do nothing. Move on to next attribute.
821 break;
822 case Declarations.CANCEL:
823 return false;
824 default:
825 }
826
827 att_new = null;
828 temp = null;
829 replace_att = null;
830 } // if node nmae = attribute
831 } //for each child node
832 return mergeMDV(mds_cur, mde_cur, mds_new, mde_new);
833 }
834
835 /** Merge two metadata value trees.
836 * @param mds_cur The current MetadataSet.
837 * @param mde_cur The current Element which acts as a value tree root.
838 * @param mds_new The MetadataSet we are merging in.
839 * @param mde_new The Element which acts as a value tree that we are merging in.
840 */
841 public boolean mergeMDV(MetadataSet mds_cur, Element mde_cur,
842 MetadataSet mds_new, Element mde_new) {
843 // Remember we may be asked to merge with a current mdv of null.
844 return MSMUtils.updateValueTree(mds_cur, mde_cur, mds_new, mde_new);
845 }
846
847 public void removeElement(ElementWrapper element) {
848 // Retrieve the metadata set this element belongs to.
849 String namespace = element.getNamespace();
850 MetadataSet set = (MetadataSet) mds_hashtable.get(namespace);
851 if(set != null) {
852 // Bugger. Get the old name -before- we remove the element from the set.
853 String old_name = element.toString();
854 // Remove the element.
855 set.removeElement(element.getElement());
856 // Fire event.
857 fireElementChanged(new MSMEvent(this, null, old_name));
858 }
859 else {
860 ///ystem.err.println("no such set " + namespace);
861 }
862 // No such set. No such element.
863 }
864
865 /** Remove a piece of metadata from a record or records[] and fire all the relevant events.
866 * @param records A FileNode[] of records to be changed.
867 * @param metadata The Metadata to remove.
868 */
869 public void removeMetadata(long id, Metadata metadata, FileNode records[]) {
870 // Reset undo buffer
871 undo_buffer.clear();
872 // Simplier than the others. Simply remove the metadata.
873 int action = MetaEditPrompt.CONFIRM;
874 // Now remove metadata from the selected file nodes.
875 for(int i = 0; action != MetaEditPrompt.CANCEL && i < records.length; i++) {
876 action = removeMetadata(id, records[i], metadata, action, (records.length > 1));
877 }
878 // If we were cancelled we should undo any changes so far
879 if(action == MetaEditPrompt.CANCEL) {
880 for(Iterator keys = undo_buffer.keySet().iterator(); keys.hasNext(); ) {
881 FileNode record = (FileNode) keys.next();
882 undoRemove(id, record);
883 }
884 }
885 }
886 /** Remove the specified listener from ourselves.
887 * @param listener The MSMListener in question.
888 */
889 public void removeMSMListener(MSMListener listener) {
890 listeners.remove(listener);
891 }
892
893 public void removeSet(MetadataSet set) {
894 mds_hashtable.remove(set.getNamespace());
895 fireSetChanged(set);
896 }
897
898 /** Rename the identifier of a given element to the name given.
899 * @param element The metadata element effected, as an ElementWrapper.
900 * @param new_name The String to use as the new name.
901 */
902 public void renameElement(ElementWrapper element, String new_name) {
903 Element e = element.getElement();
904 String old_name = element.toString();
905 MSMUtils.setIdentifier(e, new_name);
906 fireElementChanged(new MSMEvent(this, element, old_name));
907 old_name = null;
908 e = null;
909 }
910 /** A method to save the state of this metadata set manager. First we ensure that the names of all included metadata sets have been added to the collection configuration file, then all of the metadata sets contained are exported with full content to the collect/<col_name>/metadata/ directory.
911 */
912 public void save() {
913 // Create the correct file to save these sets to...
914 File file = new File(Gatherer.self.getCollectionMetadata());
915 // And make back ups of all existing metadata set files.
916 File temp[] = file.listFiles();
917 for(int i = 0; temp != null && i < temp.length; i++) {
918 if(temp[i].getName().endsWith(".mds") || temp[i].getName().endsWith(".mdv")) {
919 File backup = new File(temp[i].getAbsolutePath() + "~");
920 backup.deleteOnExit();
921 if(!temp[i].renameTo(backup)) {
922 Gatherer.println("Error in MetadataSetManager.save(): FileRenamedException");
923 }
924 }
925 }
926 // Now save the latest versions of the metadata sets.
927 for(Enumeration keys = mds_hashtable.keys(); keys.hasMoreElements(); ) {
928 String namespace = (String) keys.nextElement();
929 MetadataSet set = (MetadataSet) mds_hashtable.get(namespace);
930 try {
931 File mds_file = null;
932 if(!namespace.equals("")) {
933 mds_file = new File(file, set.getNamespace() + ".mds");
934 }
935 else {
936 mds_file = new File(file, Utility.METADATA_EXTRACTED);
937 }
938 Utility.export(set.getDocument(), mds_file);
939 set.setFile(mds_file);
940 // Now for each element attempt to save its value tree.
941 NodeList elements = set.getElements();
942 for(int i = elements.getLength() - 1; !namespace.equals("") && i >= 0; i--) {
943 ElementWrapper value_element = new ElementWrapper((Element)elements.item(i));
944 GValueModel value_tree = set.getValueTree(value_element);
945 if(value_tree != null) {
946 File value_file = new File(file, value_element.toString() + ".mdv");
947 ///ystem.err.println("Saving value file: " + value_file.toString());
948 Utility.export(value_tree.getDocument(), value_file);
949 // If this is a hierarchy element, write hierarchy file.
950 if(value_element.getNamespace().equals(MetadataSetManager.HIDDEN) || value_tree.isHierarchy()) {
951 MetadataXML.write(value_tree, this, Gatherer.c_man.getCollectionEtc());
952 }
953 }
954 else {
955 ///ystem.err.println("No value tree for " + value_element.toString());
956 }
957 }
958 }
959 catch (Exception error) {
960 error.printStackTrace();
961 }
962 }
963 profiler.save();
964 }
965 /** Given a FileNode of the original file and the new FileNode, search for any metadata, either from a greenstone directory archive xml file, or from one of the registered 'plugin' parsers.
966 * @param source The source FileNode.
967 * @param destination The new FileNode.
968 */
969 public final boolean searchForMetadata(FileNode destination, FileNode source, boolean folder_level) {
970 return searchForMetadata(destination, source, folder_level, false);
971 }
972 public final boolean searchForMetadata(FileNode destination, FileNode source, boolean folder_level, boolean dummy_run) {
973 ///atherer.println("MetadataSetManager.searchForMetadata()");
974 return loader.searchForMetadata(destination, source, folder_level, dummy_run);
975 }
976 /** Build a vector of all the metadata sets that contain an element with the given name.
977 * @param name The name of an element as a String.
978 * @return A Vector of metadata sets.
979 * @see MSMPrompt (org.greenstone.gatherer.msm.MSMPrompt#selectSet)
980 */
981 public Vector setsThatContain(String name) {
982 Vector result = new Vector();
983 for(Enumeration keys = mds_hashtable.keys(); keys.hasMoreElements(); ) {
984 MetadataSet set = (MetadataSet) mds_hashtable.get(keys.nextElement());
985 if(set.getElement(name) != null) {
986 result.add(set);
987 }
988 }
989 return result;
990 }
991
992 public final int size() {
993 return getSets().size();
994 }
995
996 /** Update a piece of metadata connected to a record or records, ensuring the value tree is built properly, and correct messaging fired.
997 * @param records A FileNode[] of records, or directories, to add the specified metadata to.
998 * @param element The metadata element, contained within an ElementWrapper to base metadata on.
999 * @param value The value to assign to the metadata as a String.
1000 * @param action The default action to take in the prompt.
1001 * @param file_level If true then the metadata can be replaced normally, if false then we should actually use an add method instead.
1002 * @return The Metadata we just assigned.
1003 */
1004 public Metadata updateMetadata(long id, Metadata old_metadata, FileNode records[], String value_str, int action, boolean file_level) {
1005 // Retrieve the new value node from the same value tree as the old metadata.
1006 ElementWrapper element = old_metadata.getElement();
1007 GValueModel model = getValueTree(element);
1008 GValueNode value = null;
1009 if(model != null) {
1010 value = model.addValue(value_str);
1011 }
1012 else {
1013 value = new GValueNode(element.toString(), value_str);
1014 }
1015 // Create new metadata.
1016 Metadata new_metadata = new Metadata(value);
1017 // Reset the undo buffer
1018 undo_buffer.clear();
1019 // And update the old with it.
1020 if(action == -1) {
1021 action = MetaEditPrompt.CONFIRM;
1022 }
1023 // And then update each selection file node.
1024 for(int i = 0; action != MetaEditPrompt.CANCEL && i < records.length; i++) {
1025 action = updateMetadata(id, records[i], old_metadata, new_metadata, action, (records.length > 1), file_level);
1026 }
1027 // If we were cancelled we should undo any changes so far
1028 if(action == MetaEditPrompt.CANCEL) {
1029 for(Iterator keys = undo_buffer.keySet().iterator(); keys.hasNext(); ) {
1030 FileNode record = (FileNode) keys.next();
1031 undoUpdate(id, record);
1032 }
1033 }
1034 // All done. Any events would have been fired within the record recursion.
1035 return new_metadata;
1036 }
1037
1038 /** Add a reference to a piece of metadata to the given FileNode. The whole method gets a wee bit messy as we have to allow for several different commands from users such as accumulate / overwrite, skip just this file or cancel the whole batch. Cancelling is especially problematic as we need to rollback any changes (within reason).
1039 * It is also worth mentioning that despite its name, no actual metadata is added directly by this method. Instead a call to fireMetadataChanged() is issued, which is in turn processed by the GDMManager (which, given this method may have been called from GDMManager as well, means the cycle is complete. Um, that doesn't mean theres an infinite loop... I hope).
1040 * @param id a long unique identifier shared by all actions caused by the same gesture.
1041 * @param record the FileNode we are adding the metadata to.
1042 * @param data the new Metadata.
1043 * @param action the default action as an int. May require user interaction.
1044 * @param fire_event <i>true</i> if this action should fire a metadata changed event, <i>false</i> if we are calling this as an affect of a previous event. (Don't want an infinitely recursive loop, do we).
1045 * @param multiple_selection <i>true</i> if more than one file or folder was selected.
1046 * @return an int specifying the current action. Thus changes in lower parts of the tree continue to effect other disjoint subtrees.
1047 */
1048 private int addMetadata(long id, FileNode record, Metadata data, int action, boolean multiple_selection) {
1049 // Super special exception for accumulate all action. We are going to add this metadata anyway, regardless of whats already there, so just add it.
1050 if(action == MetaEditPrompt.ACCUMULATE_ALL || action == MetaEditPrompt.OVERWRITE_ALL) {
1051 fireMetadataChanged(id, record, null, data);
1052 }
1053 else {
1054 // Recover the metadata from this file.
1055 ArrayList metadata = Gatherer.c_man.getCollection().gdm.getMetadata(record.getFile());
1056 // Most important test, we don't have to add the metadata if its already there!
1057 if(!metadata.contains(data)) {
1058 // Record undo information for this file node.
1059 ArrayList undo = new ArrayList();
1060 // Prepare for MEP
1061 int user_action = MetaEditPrompt.ACCUMULATE;
1062 String values = "";
1063 // See if there is any existing metadata with the same name. If so make a string from all the values (bob, jim etc).
1064 int metadata_size = metadata.size();
1065 for(int i = 0; i < metadata_size; i++) {
1066 Metadata current_data = (Metadata)metadata.get(i);
1067 if(current_data.getElement().equals(data.getElement())) {
1068 if(values.length() > 0) {
1069 values = values + ", ";
1070 }
1071 values = values + current_data.getValue();
1072 }
1073 }
1074 // If we are confirming prompt for user_action.
1075 if(values.length() > 0 && action == MetaEditPrompt.CONFIRM) {
1076 MetaEditPrompt mep = new MetaEditPrompt(MetaEditPrompt.ADD_PROMPT, multiple_selection, record.getFile(), data.getElement().toString(), values, data.getValue());
1077 user_action = mep.display();
1078 }
1079 if(user_action == MetaEditPrompt.ACCUMULATE_ALL || user_action == MetaEditPrompt.CANCEL || user_action == MetaEditPrompt.OVERWRITE_ALL) {
1080 action = user_action;
1081 }
1082 // If we are overwriting we first remove all metadata with the same element, unless the metadata is non-file level is which case we leave it, and hope the accumulate vs overwrite will be followed during the determining of metadata assigned.
1083 if(action == MetaEditPrompt.OVERWRITE_ALL || user_action == MetaEditPrompt.OVERWRITE) {
1084 for(int i = metadata_size; i != 0; i--) {
1085 Metadata old_data = (Metadata)metadata.get(i - 1);
1086 if(old_data.getElement().equals(data.getElement()) && old_data.isFileLevel()) {
1087 // We have a match. Remove this metadata.
1088 fireMetadataChanged(id, record, old_data, null);
1089 // Add it to our undo buffer.
1090 undo.add(old_data);
1091 }
1092 }
1093 }
1094 // Ensure the metadata will accumulate or overwrite as the user wishes.
1095 if(user_action == MetaEditPrompt.ACCUMULATE || user_action == MetaEditPrompt.ACCUMULATE_ALL) {
1096 data.setAccumulate(true);
1097 }
1098 else if(user_action == MetaEditPrompt.OVERWRITE || user_action == MetaEditPrompt.OVERWRITE_ALL) {
1099 data.setAccumulate(false);
1100 }
1101 // Unless cancelled, add the metadata after checking we don't already have it in the metadata (obviously not if we're overwriting but we'd better check anyway). Also if we've skipped the file we should do so, but move on to the next child...
1102 if((user_action == MetaEditPrompt.ACCUMULATE || user_action == MetaEditPrompt.ACCUMULATE_ALL || user_action == MetaEditPrompt.OVERWRITE || user_action == MetaEditPrompt.OVERWRITE_ALL) && !metadata.contains(data)) {
1103 ///ystem.err.println("Adding metadata " + data);
1104 fireMetadataChanged(id, record, null, data);
1105 // The last element in undo is the new element.
1106 undo.add(data);
1107 }
1108 // Store the undo list in our undo buffer.
1109 undo_buffer.put(record, undo);
1110 }
1111 }
1112 // If we've been cancelled, roll back the addition.
1113 if(action == MetaEditPrompt.CANCEL) {
1114 undoAdd(id, record);
1115 }
1116 return action;
1117 }
1118 /** addMetadata(long, FileNode, Metadata, int, boolean) */
1119
1120 /** Create the hidden mds, used for custom classifiers. */
1121 private MetadataSet createHidden() {
1122 MetadataSet hidden_mds = new MetadataSet(Utility.METADATA_SET_TEMPLATE);
1123 hidden_mds.setAttribute("creator","The Gatherer");
1124 hidden_mds.setAttribute("contact","gatherer@greenstone");
1125 hidden_mds.setAttribute("description","A hidden metadata set used to create custom classifiers.");
1126 hidden_mds.setAttribute("family","Gatherer Hidden Metadata");
1127 hidden_mds.setAttribute("lastchanged","");
1128 hidden_mds.setAttribute("name","Gatherer Hidden Metadata");
1129 hidden_mds.setAttribute("namespace",HIDDEN);
1130 mds_hashtable.put(HIDDEN, hidden_mds);
1131 fireSetChanged(hidden_mds);
1132 return hidden_mds;
1133 }
1134
1135 /** Creates a new profiler, which in turn will attempt to load previous profile information. */
1136 private void loadProfiler() {
1137 profiler = new MSMProfiler();
1138 addMSMListener(profiler);
1139 }
1140
1141 /** In order to remove metadata from the tree you first call this method providing it with the metadata you want removed. This will remove any occurance of said metadata from the given FileNode (using fireMetadataChanged()).
1142 * @param id a unique long identifier common to all actions caused by a single gesture.
1143 * @param record the FileNode who we are removing metadata from.
1144 * @param data the <strong>Metadata</strong> you wish removed from the tree.
1145 * @param action an <i>int</i> specifying the wanted prompting action.
1146 * @param fire_event <i>true</i> if this action should fire metadata changed events.
1147 * @param multiple_selection the number of records in the selection, as an <i>int</i>. Used to determine prompt controls.
1148 * @return an <i>int</i> specifying the current action. Thus changes in lower parts of the tree continue to effect other disjoint subtrees.
1149 */
1150 private int removeMetadata(long id, FileNode record, Metadata data, int action, boolean multiple_selection) {
1151 ArrayList metadata = Gatherer.c_man.getCollection().gdm.getMetadata(record.getFile());
1152 int user_action = MetaEditPrompt.REMOVE;
1153 // See if we even have this metadata.
1154 if(metadata.contains(data)) {
1155 ArrayList undo = new ArrayList();
1156 // We do have it. If action == CONFIRM, show user prompt.
1157 if(action == MetaEditPrompt.CONFIRM) {
1158 MetaEditPrompt mep = new MetaEditPrompt(MetaEditPrompt.REMOVE_PROMPT, multiple_selection, record.getFile(), data.getElement().toString(), data.getValue(), "");
1159 user_action = mep.display();
1160 }
1161 // Set action to match the user_action under certain circumstances.
1162 if(user_action == MetaEditPrompt.CANCEL || user_action == MetaEditPrompt.REMOVE_ALL) {
1163 action = user_action;
1164 }
1165 if(action == MetaEditPrompt.REMOVE_ALL || user_action == MetaEditPrompt.REMOVE) {
1166 fireMetadataChanged(id, record, data, null);
1167 undo.add(data);
1168 }
1169 // Store undo information
1170 undo_buffer.put(record, undo);
1171 }
1172 // If we've been cancelled higher up, undo action.
1173 if(action == MetaEditPrompt.CANCEL) {
1174 undoRemove(id, record);
1175 }
1176 return action;
1177 }
1178
1179 /** Rollback any changes made as part of a single metadata add process (only valid during the action itself, ie if a user presses cancel).
1180 * @param id the unique identify of all actions created as part of a single gesture, as a <i>long</i>.
1181 * @param record the FileNode whose metadata was changed.
1182 */
1183 private void undoAdd(long id, FileNode record) {
1184 // Retrieve the undo data from the buffer
1185 ArrayList undo = (ArrayList) undo_buffer.get(record);
1186 // If there is no undo then we can't do anything, but there should be
1187 if(undo != null && undo.size() > 0) {
1188 // The last piece of data in an add actions undo buffer is the metadata that was added
1189 Metadata data = (Metadata) undo.remove(undo.size() - 1);
1190 // Remove the data
1191 fireMetadataChanged(id, record, data, null);
1192 // If we removed other metadata when adding this metadata restore it too
1193 for(int i = 0; i < undo.size(); i++) {
1194 Metadata old_data = (Metadata) undo.get(i);
1195 fireMetadataChanged(id, record, null, old_data);
1196 }
1197 }
1198 }
1199 /** Rollback any changes made as part of a single metadata remove process (only valid during the action itself, ie if a user presses cancel).
1200 * @param id the unique identify of all actions created as part of a single gesture, as a long.
1201 * @param record the FileNode metadata was removed from.
1202 */
1203 private void undoRemove(long id, FileNode record) {
1204 // Retrieve undo information
1205 ArrayList undo = (ArrayList) undo_buffer.get(record);
1206 // Ensure that we have something to undo
1207 if(undo != null && undo.size() == 1) {
1208 // The undo buffer should contain exactly one entry, the metadata removed
1209 Metadata data = (Metadata) undo.get(0);
1210 fireMetadataChanged(id, record, null, data);
1211 }
1212 }
1213 /** Roll back any changes made as part of a single metadata update process (only valid during the action itself, ie if a user presses cancel).
1214 * @param id the unique identify of all actions created as part of a single gesture, as a long.
1215 * @param record the FileNode whose metadata was changed.
1216 */
1217 private void undoUpdate(long id, FileNode record) {
1218 // Retrieve undo information
1219 ArrayList undo = (ArrayList) undo_buffer.get(record);
1220 if(undo != null && undo.size() == 2) {
1221 Metadata old_data = (Metadata) undo.get(0);
1222 Metadata new_data = (Metadata) undo.get(1);
1223 fireMetadataChanged(id, record, new_data, null);
1224 if(old_data != new_data) { // Correct reference comparison
1225 fireMetadataChanged(id, record, null, old_data);
1226 }
1227 }
1228 }
1229
1230 /** Used to update the values of one of the metadata elements within this node. Has the same trickiness as Add but only half the number of options.
1231 * @param id the unique identify of all actions created as part of a single gesture, as a <i>long</i>.
1232 * @param record the FileNode whose metadata we are changing.
1233 * @param old_data The old existing <strong>Metadata</strong>.
1234 * @param new_data The new updated <strong>Metadata</strong>.
1235 * @param action An <i>int</i> indicating what we are going to do about it.
1236 * @param multiple_selection The number of records in the selection, as an <i>int</i>. Used to determine prompt controls.
1237 * @return An <i>int</i> specifying the current action. Thus changes in lower parts of the tree continue to effect other disjoint subtrees.
1238 */
1239 private int updateMetadata(long id, FileNode record, Metadata old_data, Metadata new_data, int action, boolean multiple_selection, boolean file_level) {
1240 ArrayList metadata;
1241 if(file_level) {
1242 metadata = Gatherer.c_man.getCollection().gdm.getMetadata(record.getFile());
1243 }
1244 else {
1245 metadata = Gatherer.c_man.getCollection().gdm.getAllMetadata(record.getFile());
1246 }
1247 int user_action = MetaEditPrompt.OVERWRITE;
1248 // Standard case of updating an existing metadata value.
1249 if(metadata.contains(old_data)) {
1250 ArrayList undo = new ArrayList();
1251 // If we are to prompt the user, do so.
1252 if(action == MetaEditPrompt.CONFIRM) {
1253 MetaEditPrompt mep = new MetaEditPrompt(MetaEditPrompt.UPDATE_PROMPT, multiple_selection, record.getFile(), old_data.getElement().toString(), old_data.getValue(), new_data.getValue());
1254 user_action = mep.display();
1255 }
1256 // Some user actions should have a continuous effect.
1257 if(user_action == MetaEditPrompt.OVERWRITE_ALL || user_action == MetaEditPrompt.CANCEL) {
1258 action = user_action;
1259 }
1260 // And if the update chose update, do so.
1261 if(action == MetaEditPrompt.OVERWRITE_ALL || user_action == MetaEditPrompt.OVERWRITE || user_action == MetaEditPrompt.UPDATE_ONCE) {
1262 ///ystem.err.println("Updating:\n"+old_data+"\nto\n"+new_data);
1263 // If this is file level then we can do a normal replace
1264 if(file_level) {
1265 fireMetadataChanged(id, record, old_data, new_data);
1266 undo.add(old_data);
1267 undo.add(new_data);
1268 }
1269 // Otherwise we are dealing with someone attempting to override inherited metadata, so we actually fire an add. To this end we add new data twice to the undo buffer, thus we can detect if this has happened.
1270 else {
1271 fireMetadataChanged(id, record, null, new_data);
1272 undo.add(new_data);
1273 undo.add(new_data);
1274 }
1275 }
1276 // Store the undo information
1277 undo_buffer.put(record, undo);
1278 }
1279 // If we've been cancelled undo.
1280 if(action == MetaEditPrompt.CANCEL) {
1281 undoUpdate(id, record);
1282 }
1283 return action;
1284 }
1285}
Note: See TracBrowser for help on using the repository browser.