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 |
|
---|
27 | package org.greenstone.gatherer.metadata;
|
---|
28 |
|
---|
29 |
|
---|
30 | import java.io.*;
|
---|
31 | import java.util.*;
|
---|
32 | import org.greenstone.gatherer.gui.MetadataImportMappingPrompt;
|
---|
33 |
|
---|
34 |
|
---|
35 | /** This class is a static class that manages the metadata sets */
|
---|
36 | public class MetadataSetManager
|
---|
37 | {
|
---|
38 | static public final String EXTRACTED_METADATA_NAMESPACE = "ex";
|
---|
39 | static public final String EXPLODED_METADATA_NAMESPACE = "exp";
|
---|
40 |
|
---|
41 | static private ArrayList metadata_sets = new ArrayList();
|
---|
42 |
|
---|
43 |
|
---|
44 | static public void clearMetadataSets()
|
---|
45 | {
|
---|
46 | metadata_sets.clear();
|
---|
47 | }
|
---|
48 |
|
---|
49 |
|
---|
50 | static public int compareMetadataElements(MetadataElement metadata_element_a, MetadataElement metadata_element_b)
|
---|
51 | {
|
---|
52 | // First compare the namespaces of the two metadata elements
|
---|
53 | if (!metadata_element_a.getNamespace().equals(metadata_element_b.getNamespace())) {
|
---|
54 | if (metadata_element_a.getNamespace().equals(EXTRACTED_METADATA_NAMESPACE)) {
|
---|
55 | return 1;
|
---|
56 | }
|
---|
57 | if (metadata_element_b.getNamespace().equals(EXTRACTED_METADATA_NAMESPACE)) {
|
---|
58 | return -1;
|
---|
59 | }
|
---|
60 | return metadata_element_a.getNamespace().compareTo(metadata_element_b.getNamespace());
|
---|
61 | }
|
---|
62 |
|
---|
63 | // Otherwise compare the two elements within the one set
|
---|
64 | MetadataSet metadata_set = getMetadataSet(metadata_element_a.getNamespace());
|
---|
65 | return metadata_set.compareMetadataElements(metadata_element_a, metadata_element_b);
|
---|
66 | }
|
---|
67 |
|
---|
68 |
|
---|
69 | static public ArrayList getEveryMetadataSetElement()
|
---|
70 | {
|
---|
71 | ArrayList every_metadata_set_element = new ArrayList();
|
---|
72 |
|
---|
73 | for (int i = 0; i < metadata_sets.size(); i++) {
|
---|
74 | MetadataSet metadata_set = (MetadataSet) metadata_sets.get(i);
|
---|
75 | every_metadata_set_element.addAll(metadata_set.getMetadataSetElements());
|
---|
76 | }
|
---|
77 |
|
---|
78 | return every_metadata_set_element;
|
---|
79 | }
|
---|
80 |
|
---|
81 |
|
---|
82 | static public MetadataSet getMetadataSet(String metadata_set_namespace)
|
---|
83 | {
|
---|
84 | // Find the desired metadata set in the list
|
---|
85 | for (int i = 0; i < metadata_sets.size(); i++) {
|
---|
86 | MetadataSet metadata_set = (MetadataSet) metadata_sets.get(i);
|
---|
87 | if (metadata_set.getNamespace().equals(metadata_set_namespace)) {
|
---|
88 | return metadata_set;
|
---|
89 | }
|
---|
90 | }
|
---|
91 |
|
---|
92 | // Not found
|
---|
93 | return null;
|
---|
94 | }
|
---|
95 |
|
---|
96 |
|
---|
97 | static public ArrayList getMetadataSets()
|
---|
98 | {
|
---|
99 | // We don't want anyone to modify the list, so return a copy of it
|
---|
100 | return (ArrayList) metadata_sets.clone();
|
---|
101 | }
|
---|
102 |
|
---|
103 |
|
---|
104 | static public ArrayList listMetadataSets(File directory)
|
---|
105 | {
|
---|
106 | // Make sure the directory (metadata) exists
|
---|
107 | if (directory.exists() == false) {
|
---|
108 | return null;
|
---|
109 | }
|
---|
110 |
|
---|
111 | // Load just those .mds files in this directory, and return them
|
---|
112 | ArrayList directory_metadata_sets = new ArrayList();
|
---|
113 | File[] directory_files = directory.listFiles();
|
---|
114 | for (int i = 0; i < directory_files.length; i++) {
|
---|
115 | File child_file = directory_files[i];
|
---|
116 | if (!child_file.isDirectory() && child_file.getName().endsWith(".mds")) {
|
---|
117 | MetadataSet metadata_set = new MetadataSet(child_file);
|
---|
118 | directory_metadata_sets.add(metadata_set);
|
---|
119 | }
|
---|
120 | }
|
---|
121 |
|
---|
122 | return directory_metadata_sets;
|
---|
123 | }
|
---|
124 |
|
---|
125 |
|
---|
126 | static public void loadMetadataSets(File directory)
|
---|
127 | {
|
---|
128 | // Make sure the directory (metadata) exists
|
---|
129 | if (directory.exists() == false) {
|
---|
130 | return;
|
---|
131 | }
|
---|
132 |
|
---|
133 | // Look recursively at each subfile of the directory for .mds files
|
---|
134 | File[] directory_files = directory.listFiles();
|
---|
135 | for (int i = 0; i < directory_files.length; i++) {
|
---|
136 | File child_file = directory_files[i];
|
---|
137 | if (child_file.isDirectory()) {
|
---|
138 | loadMetadataSets(child_file);
|
---|
139 | }
|
---|
140 | else if (child_file.getName().endsWith(".mds")) {
|
---|
141 | loadMetadataSet(child_file);
|
---|
142 | }
|
---|
143 | }
|
---|
144 | }
|
---|
145 |
|
---|
146 |
|
---|
147 | static public void loadMetadataSet(File metadata_set_file)
|
---|
148 | {
|
---|
149 | MetadataSet metadata_set = new MetadataSet(metadata_set_file);
|
---|
150 |
|
---|
151 | // Ignore legacy crap
|
---|
152 | if (metadata_set.getNamespace().equals("hidden")) {
|
---|
153 | return;
|
---|
154 | }
|
---|
155 |
|
---|
156 | metadata_sets.add(metadata_set);
|
---|
157 | }
|
---|
158 |
|
---|
159 |
|
---|
160 | static public String mapUnloadedMetadataElement(String metadata_element_name_full)
|
---|
161 | {
|
---|
162 | // Check if we have an import mapping for this metadata element
|
---|
163 | String target_metadata_element_name_full = ProfileXMLFileManager.getMetadataElementFor(metadata_element_name_full);
|
---|
164 | if (target_metadata_element_name_full != null) {
|
---|
165 | // Yes, so return it
|
---|
166 | return target_metadata_element_name_full;
|
---|
167 | }
|
---|
168 |
|
---|
169 | // If there are no metadata sets (except extracted) loaded then there is nothing to map the element into
|
---|
170 | if (metadata_sets.size() <= 1) {
|
---|
171 | return null;
|
---|
172 | }
|
---|
173 |
|
---|
174 | // Ask the user how they want to deal with this element
|
---|
175 | MetadataImportMappingPrompt metadata_import_mapping_prompt = new MetadataImportMappingPrompt(metadata_element_name_full);
|
---|
176 | int result = metadata_import_mapping_prompt.getResult();
|
---|
177 |
|
---|
178 | // Add the element into an existing metadata set
|
---|
179 | if (result == MetadataImportMappingPrompt.ADD_BUTTON_PRESSED) {
|
---|
180 | MetadataSet target_metadata_set = metadata_import_mapping_prompt.getSelectedMetadataSet();
|
---|
181 | String metadata_element_name = MetadataTools.getMetadataElementName(metadata_element_name_full);
|
---|
182 | target_metadata_set.addMetadataElementForThisSession(metadata_element_name);
|
---|
183 | target_metadata_element_name_full = target_metadata_set.getNamespace() + "." + metadata_element_name;
|
---|
184 | }
|
---|
185 |
|
---|
186 | // Replace the element with an element in an existing metadata set
|
---|
187 | if (result == MetadataImportMappingPrompt.MERGE_BUTTON_PRESSED) {
|
---|
188 | MetadataElement target_metadata_element = metadata_import_mapping_prompt.getSelectedMetadataElement();
|
---|
189 | target_metadata_element_name_full = target_metadata_element.getFullName();
|
---|
190 | }
|
---|
191 |
|
---|
192 | // Ignore the element
|
---|
193 | if (result == MetadataImportMappingPrompt.IGNORE_BUTTON_PRESSED) {
|
---|
194 | target_metadata_element_name_full = "";
|
---|
195 | }
|
---|
196 |
|
---|
197 | // Store this import mapping for future elements with the same name
|
---|
198 | ProfileXMLFileManager.mapElement(metadata_element_name_full, target_metadata_element_name_full);
|
---|
199 |
|
---|
200 | return target_metadata_element_name_full;
|
---|
201 | }
|
---|
202 |
|
---|
203 |
|
---|
204 | static public void unloadMetadataSet(MetadataSet metadata_set)
|
---|
205 | {
|
---|
206 | // Find the metadata set in the list of loaded sets, and remove it
|
---|
207 | for (int i = 0; i < metadata_sets.size(); i++) {
|
---|
208 | if (metadata_set == (MetadataSet) metadata_sets.get(i)) {
|
---|
209 | metadata_sets.remove(i);
|
---|
210 | break;
|
---|
211 | }
|
---|
212 | }
|
---|
213 | }
|
---|
214 | }
|
---|