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