1 | /**
|
---|
2 | *#########################################################################
|
---|
3 | *
|
---|
4 | * A component of the Gatherer application, part of the Greenstone digital
|
---|
5 | * library suite from the New Zealand Digital Library Project at the
|
---|
6 | * University of Waikato, New Zealand.
|
---|
7 | *
|
---|
8 | * <BR><BR>
|
---|
9 | *
|
---|
10 | * Author: Shaoqun Wu, Greenstone Digital Library, University of Waikato
|
---|
11 | *
|
---|
12 | * <BR><BR>
|
---|
13 | *
|
---|
14 | * Copyright (C) 2006 New Zealand Digital Library Project
|
---|
15 | *
|
---|
16 | * <BR><BR>
|
---|
17 | *
|
---|
18 | * This program is free software; you can redistribute it and/or modify
|
---|
19 | * it under the terms of the GNU General Public License as published by
|
---|
20 | * the Free Software Foundation; either version 2 of the License, or
|
---|
21 | * (at your option) any later version.
|
---|
22 | *
|
---|
23 | * <BR><BR>
|
---|
24 | *
|
---|
25 | * This program is distributed in the hope that it will be useful,
|
---|
26 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
27 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
28 | * GNU General Public License for more details.
|
---|
29 | *
|
---|
30 | * <BR><BR>
|
---|
31 | *
|
---|
32 | * You should have received a copy of the GNU General Public License
|
---|
33 | * along with this program; if not, write to the Free Software
|
---|
34 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
35 | *########################################################################
|
---|
36 | */
|
---|
37 | package org.greenstone.gatherer.gems;
|
---|
38 |
|
---|
39 | import java.io.*;
|
---|
40 | import javax.swing.JFileChooser;
|
---|
41 | import javax.swing.JOptionPane;
|
---|
42 | import java.util.ArrayList;
|
---|
43 |
|
---|
44 | import org.greenstone.gatherer.Configuration;
|
---|
45 | import org.greenstone.gatherer.util.Utility;
|
---|
46 | import org.greenstone.gatherer.file.*;
|
---|
47 | import org.greenstone.gatherer.util.XMLTools;
|
---|
48 | import org.greenstone.gatherer.Dictionary;
|
---|
49 |
|
---|
50 | import org.apache.xerces.parsers.*;
|
---|
51 | import org.apache.xml.serialize.*;
|
---|
52 | import org.w3c.dom.*;
|
---|
53 | import org.xml.sax.*;
|
---|
54 |
|
---|
55 |
|
---|
56 |
|
---|
57 | /** This class is responsible for managing all the metadata sets in the collection and for providing methods for manipulating the aforementioned sets contents.
|
---|
58 | * @author Shaoqun Wu, Greenstone Digital Library, University of Waikato
|
---|
59 | * @version 2.4
|
---|
60 | */
|
---|
61 | public class MetadataSetManager
|
---|
62 | {
|
---|
63 |
|
---|
64 | private ArrayList mds_list = new ArrayList();
|
---|
65 |
|
---|
66 | private MetadataSetModel metadata_set_model;
|
---|
67 |
|
---|
68 | private ArrayList languageList;
|
---|
69 |
|
---|
70 | private String current_language = GEMSConstants.DEFAULT_LANGUAGE;
|
---|
71 |
|
---|
72 | /** Constructor.
|
---|
73 | * This constructor can be dangerous if launched by a Greenstone application
|
---|
74 | * that already set up the static looking Configuration object, because
|
---|
75 | * this constructor will subtly erase and replace any previously existing
|
---|
76 | * carefully configured Configuration object.
|
---|
77 | * I think this constructor would only be useful if MetadataSetManager is ever launched
|
---|
78 | * as a standalone GS3 application.
|
---|
79 | * If instantiating a MetadataSetManager through GEMS, use the MetadataSetManager() constructor,
|
---|
80 | * including if GEMS is launched through GLI.
|
---|
81 | * Also use MetadataSetManager() if creating a MetadataSetManager object through GLI.
|
---|
82 | */
|
---|
83 | public MetadataSetManager(String gsdl_path, String gsdl3_path) {
|
---|
84 | // Determine the GLI user directory path
|
---|
85 | String gli_user_directory_path = System.getProperty("user.home") + File.separator;
|
---|
86 |
|
---|
87 | if (Utility.isWindows()) {
|
---|
88 | gli_user_directory_path += "Application Data" + File.separator + "Greenstone" + File.separator + "GLI" + File.separator;
|
---|
89 | }
|
---|
90 | else {
|
---|
91 | gli_user_directory_path += ".gli" + File.separator;
|
---|
92 | }
|
---|
93 |
|
---|
94 | new Configuration(gli_user_directory_path, gsdl_path, gsdl3_path, null, null, null);
|
---|
95 | current_language = Configuration.getLanguage();
|
---|
96 |
|
---|
97 | languageList = new ArrayList();
|
---|
98 | }
|
---|
99 |
|
---|
100 | /** Constructor - overloaded to use any existing Configuration already set up.
|
---|
101 | * This Constructor is used by GEMS when GEMS is launched through GLI.
|
---|
102 | */
|
---|
103 | public MetadataSetManager() {
|
---|
104 | current_language = Configuration.getLanguage();
|
---|
105 |
|
---|
106 | languageList = new ArrayList();
|
---|
107 | }
|
---|
108 |
|
---|
109 | public ArrayList getLanguageList(){
|
---|
110 | //already loaded
|
---|
111 | if (languageList.size() > 0 ) return languageList;
|
---|
112 |
|
---|
113 | Document document = XMLTools.parseXMLFile(getLanguageXMLPath(), true);
|
---|
114 |
|
---|
115 | NodeList languages = document.getDocumentElement().getElementsByTagName(GEMSConstants.LANGUAGE_ELEMENT);
|
---|
116 | for(int i=0; i< languages.getLength();i++){
|
---|
117 | Element language_element = (Element)languages.item(i);
|
---|
118 | languageList.add(language_element.getAttribute(GEMSConstants.CODE_ATTRIBUTE).toLowerCase()+" "+language_element.getAttribute(GEMSConstants.NAME_ATTRIBUTE));
|
---|
119 | }
|
---|
120 |
|
---|
121 | return languageList;
|
---|
122 | }
|
---|
123 |
|
---|
124 |
|
---|
125 | public boolean isAttributeRequired(String name){
|
---|
126 | String tmp_name = name.trim();
|
---|
127 | for(int i=0;i<GEMSConstants.SET_REQUIRED_ATTRIBUTES.length;i++){
|
---|
128 | if (tmp_name.equals(GEMSConstants.SET_REQUIRED_ATTRIBUTES[i])) return true;
|
---|
129 | }
|
---|
130 | return false;
|
---|
131 | }
|
---|
132 |
|
---|
133 | public void save(boolean confirm) {
|
---|
134 | MetadataSetInfo meta_info = metadata_set_model.getMetadataSetInfo();
|
---|
135 |
|
---|
136 | if (meta_info ==null) return;
|
---|
137 |
|
---|
138 | if (confirm){
|
---|
139 |
|
---|
140 | //int result = JOptionPane.showOptionDialog(null, Dictionary.get("GEMS.Confirm_Save"), Dictionary.get("GEMS.Confirm_Save_Title"), JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE,null,Dictionary.get("General.No"),Dictionary.get("General.No"));
|
---|
141 | int result = JOptionPane.showConfirmDialog(null, Dictionary.get("GEMS.Confirm_Save"), Dictionary.get("GEMS.Confirm_Save_Title"), JOptionPane.YES_NO_OPTION);
|
---|
142 | if (result != JOptionPane.YES_OPTION) return;
|
---|
143 | }
|
---|
144 |
|
---|
145 |
|
---|
146 | File file_path = null;
|
---|
147 | //get new file name
|
---|
148 | if(meta_info.isNew()){
|
---|
149 | JFileChooser file_chooser = new JFileChooser(new File(getGLIMetadataDirectoryPath()));
|
---|
150 | file_chooser.setMultiSelectionEnabled(false);
|
---|
151 | file_chooser.setSelectedFile(new File(getGLIMetadataDirectoryPath(), meta_info.getNamespace()+GEMSConstants.METADATA_SET_FILE_EXTENSION));
|
---|
152 | int result = file_chooser.showSaveDialog(null);
|
---|
153 | if (result == JFileChooser.APPROVE_OPTION){
|
---|
154 | file_path = file_chooser.getSelectedFile();
|
---|
155 | meta_info.setFilePath(file_path.toString());
|
---|
156 | meta_info.setNew(false);
|
---|
157 | }
|
---|
158 | }
|
---|
159 | else {
|
---|
160 | file_path = new File(meta_info.getFilePath());
|
---|
161 | }
|
---|
162 |
|
---|
163 | if (file_path != null) save(file_path);
|
---|
164 |
|
---|
165 | for(int i=0;i<mds_list.size();i++){
|
---|
166 | MetadataSetInfo infoItem = (MetadataSetInfo)mds_list.get(i);
|
---|
167 | if (infoItem.getFilePath().trim().equals(meta_info.getFilePath().trim())) {
|
---|
168 | mds_list.remove(infoItem);
|
---|
169 | mds_list.add(meta_info);
|
---|
170 | break;
|
---|
171 | }
|
---|
172 | }
|
---|
173 | return;
|
---|
174 |
|
---|
175 | }
|
---|
176 |
|
---|
177 |
|
---|
178 | private void save(File file_path){
|
---|
179 |
|
---|
180 | try {
|
---|
181 |
|
---|
182 | XMLTools.writeXMLFile(file_path, metadata_set_model.getMetadataSetDocument());
|
---|
183 |
|
---|
184 | }
|
---|
185 | catch (Exception error) {
|
---|
186 | error.printStackTrace();
|
---|
187 | }
|
---|
188 |
|
---|
189 | }
|
---|
190 |
|
---|
191 |
|
---|
192 | public Document getMetadataSetDocument(String filePath){
|
---|
193 | if (filePath == null || filePath.equals("")) return null;
|
---|
194 | return XMLTools.parseXMLFile(filePath, true);
|
---|
195 | }
|
---|
196 |
|
---|
197 | public void setMetadataSetModel(MetadataSetModel model){
|
---|
198 | metadata_set_model = model;
|
---|
199 | }
|
---|
200 |
|
---|
201 | public MetadataSetModel getMetadataSetModel(){
|
---|
202 | return metadata_set_model;
|
---|
203 | }
|
---|
204 |
|
---|
205 |
|
---|
206 | public ArrayList getAvailableMetadataSets(){
|
---|
207 | mds_list.clear();
|
---|
208 | // Load all the core metadata sets (in the GLI "metadata" directory)
|
---|
209 | File metadata_directory = new File(getGLIMetadataDirectoryPath());
|
---|
210 | if (metadata_directory.exists()) {
|
---|
211 | // Load just those .mds files in this directory, and return them
|
---|
212 | File[] directory_files = metadata_directory.listFiles();
|
---|
213 | for (int i = 0; i < directory_files.length; i++) {
|
---|
214 | File child_file = directory_files[i];
|
---|
215 |
|
---|
216 | if (!child_file.isDirectory() && child_file.getName().endsWith(GEMSConstants.METADATA_SET_FILE_EXTENSION)) {
|
---|
217 | Document document = XMLTools.parseXMLFile(child_file.toString(), true);
|
---|
218 | MetadataSetInfo info = constructMetadataInfo(document);
|
---|
219 | info.setFilePath(child_file.toString());
|
---|
220 | mds_list.add(info);
|
---|
221 | }
|
---|
222 | }
|
---|
223 | }
|
---|
224 |
|
---|
225 | return mds_list;
|
---|
226 | }
|
---|
227 |
|
---|
228 | public boolean isNamespaceAlreadyUsed(String namespace){
|
---|
229 | for(int i=0;i<mds_list.size();i++){
|
---|
230 | MetadataSetInfo info = (MetadataSetInfo)mds_list.get(i);
|
---|
231 | if (info.getNamespace().trim().equals(namespace)) return true;
|
---|
232 | }
|
---|
233 | return false;
|
---|
234 | }
|
---|
235 |
|
---|
236 | public MetadataSetInfo getMetadataSet(String metadata_path){
|
---|
237 | MetadataSetInfo info = constructMetadataInfo(XMLTools.parseXMLFile(metadata_path,true));
|
---|
238 |
|
---|
239 | // check whether there is metadata set name for the current language
|
---|
240 | // if not reset the current_language to the language first encountered
|
---|
241 | ArrayList attrs = info.getLanguageDependentAttributes();
|
---|
242 | boolean foundLanguage = false;
|
---|
243 | for (int i=0;i<attrs.size();i++){
|
---|
244 | Attribute attr = (Attribute)attrs.get(i);
|
---|
245 | if (attr.getLanguage().equals(current_language)) {
|
---|
246 | info.setCurrentLanguage(current_language);
|
---|
247 | foundLanguage = true;
|
---|
248 | break;
|
---|
249 | }
|
---|
250 |
|
---|
251 | }
|
---|
252 | if (!foundLanguage && attrs.size()>0){
|
---|
253 | info.setCurrentLanguage(((Attribute)attrs.get(0)).getLanguage());
|
---|
254 | }
|
---|
255 |
|
---|
256 | info.setFilePath(metadata_path);
|
---|
257 | return info;
|
---|
258 |
|
---|
259 | }
|
---|
260 |
|
---|
261 | public void deleteMetadataSet(MetadataSetInfo info){
|
---|
262 | String filepath = info.getFilePath();
|
---|
263 |
|
---|
264 | if(filepath != null && !filepath.trim().equals("")){
|
---|
265 | File file = new File(filepath);
|
---|
266 |
|
---|
267 | boolean deleted = file.delete();
|
---|
268 | if (!deleted){
|
---|
269 | JOptionPane.showMessageDialog(null,Dictionary.get("GEMS.File_Deletion_Error_Message"), Dictionary.get("GEMS.File_Deletion_Error"),JOptionPane.ERROR_MESSAGE);
|
---|
270 | }
|
---|
271 | else{
|
---|
272 | for(int i=0;i<mds_list.size();i++){
|
---|
273 | MetadataSetInfo infoItem = (MetadataSetInfo)mds_list.get(i);
|
---|
274 | if (infoItem.getFilePath().trim().equals(info.getFilePath().trim())) {
|
---|
275 | mds_list.remove(infoItem);
|
---|
276 | break;
|
---|
277 | }
|
---|
278 | }
|
---|
279 | }
|
---|
280 | }
|
---|
281 | }
|
---|
282 |
|
---|
283 | public String getCurrentLanguage(){
|
---|
284 | return current_language;
|
---|
285 | }
|
---|
286 |
|
---|
287 | private MetadataSetInfo constructMetadataInfo(Document document){
|
---|
288 | NamedNodeMap attrs = document.getDocumentElement().getAttributes();
|
---|
289 | ArrayList attrList = new ArrayList();
|
---|
290 | String lang = null;
|
---|
291 | boolean findLang = false;
|
---|
292 |
|
---|
293 | for (int i=0;i<attrs.getLength();i++){
|
---|
294 | Attr item = (Attr)attrs.item(i);
|
---|
295 | Attribute attr = new Attribute(item.getName(),item.getValue());
|
---|
296 | attr.setRequired(isAttributeRequired(attr.getName()));
|
---|
297 | attrList.add(attr);
|
---|
298 | }
|
---|
299 |
|
---|
300 | ArrayList languageList = new ArrayList();
|
---|
301 |
|
---|
302 | //load all <SetLanguage> elements
|
---|
303 | NodeList setLanguages = document.getDocumentElement().getElementsByTagName(GEMSConstants.SET_LANGUAGE_ELEMENT);
|
---|
304 |
|
---|
305 | if (setLanguages.getLength() >0){
|
---|
306 | for(int i=0;i<setLanguages.getLength();i++){
|
---|
307 | Element set_language = (Element)setLanguages.item(i);
|
---|
308 | lang = set_language.getAttribute(GEMSConstants.CODE_ATTRIBUTE);
|
---|
309 | setNameAndDescription(languageList, set_language,lang);
|
---|
310 | }
|
---|
311 |
|
---|
312 | }
|
---|
313 | //no set langugae elements, try load name and description elements
|
---|
314 | else{
|
---|
315 | setNameAndDescription(languageList, document.getDocumentElement(),lang);
|
---|
316 | }
|
---|
317 |
|
---|
318 | MetadataSetInfo info = new MetadataSetInfo();
|
---|
319 | info.setLanguageDependentAttributes(languageList);
|
---|
320 | info.setAttributes(attrList);
|
---|
321 | return info;
|
---|
322 | }
|
---|
323 |
|
---|
324 | private void setNameAndDescription(ArrayList languageList, Element parent,String lang){
|
---|
325 |
|
---|
326 | ArrayList names = XMLTools.getChildElementsByTagName(parent,GEMSConstants.NAME_ELEMENT);
|
---|
327 | String name_lang = "";
|
---|
328 |
|
---|
329 | if (names.size() > 0){
|
---|
330 | Element name_element = (Element)names.get(0);
|
---|
331 | Attribute attr = new Attribute(GEMSConstants.NAME_ATTRIBUTE,XMLTools.getElementTextValue(name_element), true);
|
---|
332 | if (lang == null){
|
---|
333 | attr.setLanguage(name_element.getAttribute(GEMSConstants.LANGUAGE_ATTRIBUTE));
|
---|
334 | name_lang = attr.getLanguage();
|
---|
335 | }
|
---|
336 | else{
|
---|
337 | attr.setLanguage(lang);
|
---|
338 | }
|
---|
339 | languageList.add(attr);
|
---|
340 | // if (current_language.equals(attr.getLanguage())){
|
---|
341 | // attr.setRequired(true);
|
---|
342 | // languageList.add(0,attr);
|
---|
343 | // }
|
---|
344 | // else{
|
---|
345 | // languageList.add(attr);
|
---|
346 | // }
|
---|
347 |
|
---|
348 | }
|
---|
349 |
|
---|
350 |
|
---|
351 | ArrayList des = XMLTools.getChildElementsByTagName(parent,GEMSConstants.DESCRIPTION_ELEMENT);
|
---|
352 | if (des.size() > 0){
|
---|
353 | Element des_element = (Element)des.get(0);
|
---|
354 | Attribute attr = new Attribute(GEMSConstants.DESCRIPTION_ATTRIBUTE,XMLTools.getElementTextValue(des_element), true);
|
---|
355 | if (lang == null){
|
---|
356 | attr.setLanguage(des_element.getAttribute(GEMSConstants.LANGUAGE_ATTRIBUTE));
|
---|
357 | }
|
---|
358 | else{
|
---|
359 | attr.setLanguage(lang);
|
---|
360 | }
|
---|
361 | languageList.add(attr);
|
---|
362 | }
|
---|
363 | else{
|
---|
364 | Attribute attr = new Attribute(GEMSConstants.DESCRIPTION_ATTRIBUTE,"", true);
|
---|
365 | if (lang == null){
|
---|
366 | attr.setLanguage(name_lang);
|
---|
367 | }
|
---|
368 | else{
|
---|
369 | attr.setLanguage(lang);
|
---|
370 | }
|
---|
371 |
|
---|
372 | languageList.add(attr);
|
---|
373 | }
|
---|
374 |
|
---|
375 | }
|
---|
376 |
|
---|
377 |
|
---|
378 | static public String getGLIDirectoryPath()
|
---|
379 | {
|
---|
380 | return System.getProperty("user.dir") + File.separator;
|
---|
381 | }
|
---|
382 |
|
---|
383 |
|
---|
384 | static public String getGLIMetadataDirectoryPath()
|
---|
385 | {
|
---|
386 | return getGLIDirectoryPath() + "metadata" + File.separator;
|
---|
387 | }
|
---|
388 |
|
---|
389 | static public String getLanguageXMLPath()
|
---|
390 | {
|
---|
391 | return getGLIDirectoryPath() + "classes" + File.separator+"xml"+ File.separator+ "languages.xml";
|
---|
392 | }
|
---|
393 |
|
---|
394 | static public String getCollectionPath()
|
---|
395 | {
|
---|
396 | if (Configuration.gsdl3_path != null){
|
---|
397 | return Configuration.gsdl3_path + File.separator + "sites";
|
---|
398 |
|
---|
399 | }
|
---|
400 | else{
|
---|
401 | return Configuration.gsdl_path + File.separator + "collect";
|
---|
402 | }
|
---|
403 | }
|
---|
404 | }
|
---|