source: trunk/gli/src/org/greenstone/gatherer/metadata/MetadataSetManager.java@ 8248

Last change on this file since 8248 was 8216, checked in by mdewsnip, 20 years ago

Fixed a bug with mapping already-namespaced metadata elements.

  • Property svn:keywords set to Author Date Id Revision
File size: 7.4 KB
Line 
1/**
2 *############################################################################
3 * A component of the Greenstone Librarian Interface, part of the Greenstone
4 * digital library suite from the New Zealand Digital Library Project at the
5 * University of Waikato, New Zealand.
6 *
7 * Author: Michael Dewsnip, NZDL Project, University of Waikato, NZ
8 *
9 * Copyright (C) 2004 New Zealand Digital Library Project
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 *############################################################################
25 */
26
27package org.greenstone.gatherer.metadata;
28
29
30import java.io.*;
31import java.util.*;
32import org.greenstone.gatherer.gui.MetadataImportMappingPrompt;
33
34
35/** This class is a static class that manages the metadata sets */
36public class MetadataSetManager
37{
38 static public final String EXTRACTED_METADATA_NAMESPACE = "ex";
39
40 static private ArrayList metadata_sets = new ArrayList();
41
42
43 static public void clearMetadataSets()
44 {
45 metadata_sets.clear();
46 }
47
48
49 static public int compareMetadataElements(MetadataElement metadata_element_a, MetadataElement metadata_element_b)
50 {
51 // First compare the namespaces of the two metadata elements
52 if (!metadata_element_a.getNamespace().equals(metadata_element_b.getNamespace())) {
53 return metadata_element_a.getNamespace().compareTo(metadata_element_b.getNamespace());
54 }
55
56 // Otherwise compare the two elements within the one set
57 MetadataSet metadata_set = getMetadataSet(metadata_element_a.getNamespace());
58 return metadata_set.compareMetadataElements(metadata_element_a, metadata_element_b);
59 }
60
61
62 static public ArrayList getEveryMetadataSetElement()
63 {
64 ArrayList every_metadata_set_element = new ArrayList();
65
66 for (int i = 0; i < metadata_sets.size(); i++) {
67 MetadataSet metadata_set = (MetadataSet) metadata_sets.get(i);
68 every_metadata_set_element.addAll(metadata_set.getMetadataSetElements());
69 }
70
71 return every_metadata_set_element;
72 }
73
74
75 static public MetadataSet getMetadataSet(String metadata_set_namespace)
76 {
77 // Find the desired metadata set in the list
78 for (int i = 0; i < metadata_sets.size(); i++) {
79 MetadataSet metadata_set = (MetadataSet) metadata_sets.get(i);
80 if (metadata_set.getNamespace().equals(metadata_set_namespace)) {
81 return metadata_set;
82 }
83 }
84
85 // Not found
86 return null;
87 }
88
89
90 static public ArrayList getMetadataSets()
91 {
92 // We don't want anyone to modify the list, so return a copy of it
93 return (ArrayList) metadata_sets.clone();
94 }
95
96
97 static public ArrayList listMetadataSets(File directory)
98 {
99 // Make sure the directory (metadata) exists
100 if (directory.exists() == false) {
101 return null;
102 }
103
104 // Load just those .mds files in this directory, and return them
105 ArrayList directory_metadata_sets = new ArrayList();
106 File[] directory_files = directory.listFiles();
107 for (int i = 0; i < directory_files.length; i++) {
108 File child_file = directory_files[i];
109 if (!child_file.isDirectory() && child_file.getName().endsWith(".mds")) {
110 MetadataSet metadata_set = new MetadataSet(child_file);
111 directory_metadata_sets.add(metadata_set);
112 }
113 }
114
115 return directory_metadata_sets;
116 }
117
118
119 static public void loadMetadataSets(File directory)
120 {
121 // Make sure the directory (metadata) exists
122 if (directory.exists() == false) {
123 return;
124 }
125
126 // Look recursively at each subfile of the directory for .mds files
127 File[] directory_files = directory.listFiles();
128 for (int i = 0; i < directory_files.length; i++) {
129 File child_file = directory_files[i];
130 if (child_file.isDirectory()) {
131 loadMetadataSets(child_file);
132 }
133 else if (child_file.getName().endsWith(".mds")) {
134 loadMetadataSet(child_file);
135 }
136 }
137 }
138
139
140 static public void loadMetadataSet(File metadata_set_file)
141 {
142 MetadataSet metadata_set = new MetadataSet(metadata_set_file);
143 metadata_sets.add(metadata_set);
144 }
145
146
147 static public String mapUnloadedMetadataElement(String metadata_element_name_full)
148 {
149 // Check if we have an import mapping for this metadata element
150 String target_metadata_element_name_full = ProfileXMLFileManager.getMetadataElementFor(metadata_element_name_full);
151 if (target_metadata_element_name_full != null) {
152 // Yes, so return it
153 return target_metadata_element_name_full;
154 }
155
156 // If there are no metadata sets loaded then there is nothing to map the element into
157 if (metadata_sets.size() <= 1) {
158 return null;
159 }
160
161 // Ask the user how they want to deal with this element
162 MetadataImportMappingPrompt metadata_import_mapping_prompt = new MetadataImportMappingPrompt(metadata_element_name_full);
163 int result = metadata_import_mapping_prompt.getResult();
164
165 // Add the element into an existing metadata set
166 if (result == MetadataImportMappingPrompt.ADD_BUTTON_PRESSED) {
167 MetadataSet target_metadata_set = metadata_import_mapping_prompt.getSelectedMetadataSet();
168 String metadata_element_name = MetadataTools.getMetadataElementName(metadata_element_name_full);
169 target_metadata_set.addMetadataElementForThisSession(metadata_element_name);
170 target_metadata_element_name_full = target_metadata_set.getNamespace() + "." + metadata_element_name;
171 }
172
173 // Replace the element with an element in an existing metadata set
174 if (result == MetadataImportMappingPrompt.REPLACE_BUTTON_PRESSED) {
175 MetadataElement target_metadata_element = metadata_import_mapping_prompt.getSelectedMetadataElement();
176 target_metadata_element_name_full = target_metadata_element.getFullName();
177 }
178
179 // Ignore the element
180 if (result == MetadataImportMappingPrompt.IGNORE_BUTTON_PRESSED) {
181 target_metadata_element_name_full = "";
182 }
183
184 // Store this import mapping for future elements with the same name
185 ProfileXMLFileManager.mapElement(metadata_element_name_full, target_metadata_element_name_full);
186
187 return target_metadata_element_name_full;
188 }
189
190
191 static public void unloadMetadataSet(MetadataSet metadata_set)
192 {
193 // Find the metadata set in the list of loaded sets, and remove it
194 for (int i = 0; i < metadata_sets.size(); i++) {
195 if (metadata_set == (MetadataSet) metadata_sets.get(i)) {
196 metadata_sets.remove(i);
197 break;
198 }
199 }
200 }
201
202
203 static public void writeHierarchyFiles(File directory)
204 {
205 // Make sure the directory (etc) exists
206 if (directory.exists() == false) {
207 return;
208 }
209
210 // Write a hierarchy file for each loaded metadata element, except extracted metadata
211 ArrayList every_metadata_set_element = getEveryMetadataSetElement();
212 for (int i = 0; i < every_metadata_set_element.size(); i++) {
213 MetadataElement metadata_element = (MetadataElement) every_metadata_set_element.get(i);
214 if (metadata_element.isExtractedMetadata() == false) {
215 File hierarchy_file = new File(directory, metadata_element.getFullName() + ".txt");
216 metadata_element.writeHierarchyFile(hierarchy_file);
217 }
218 }
219 }
220}
Note: See TracBrowser for help on using the repository browser.