source: other-projects/FileTransfer-WebSocketPair/testGXTWithGreenstone/src/org/greenstone/gatherer/gems/MetadataSetManager.java@ 33053

Last change on this file since 33053 was 33053, checked in by ak19, 5 years ago

I still had some stuff of Nathan Kelly's (FileTransfer-WebSocketPair) sitting on my USB. Had already commited the Themes folder at the time, 2 years back. Not sure if he wanted this additional folder commited. But I didn't want to delete it and decided it will be better off on SVN. When we use his project, if we find we didn't need this test folder, we can remove it from svn then.

File size: 12.3 KB
Line 
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 */
37package org.greenstone.gatherer.gems;
38
39import java.io.*;
40import javax.swing.JFileChooser;
41import javax.swing.JOptionPane;
42import java.util.ArrayList;
43
44import org.greenstone.gatherer.Configuration;
45import org.greenstone.gatherer.util.Utility;
46import org.greenstone.gatherer.file.*;
47import org.greenstone.gatherer.util.XMLTools;
48import org.greenstone.gatherer.Dictionary;
49
50import org.apache.xerces.parsers.*;
51import org.apache.xml.serialize.*;
52import org.w3c.dom.*;
53import 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 */
61public 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 public MetadataSetManager(String gsdl_path, String gsdl3_path) {
74 // Determine the GLI user directory path
75 String gli_user_directory_path = System.getProperty("user.home") + File.separator;
76
77 if (Utility.isWindows()) {
78 gli_user_directory_path += "Application Data" + File.separator + "Greenstone" + File.separator + "GLI" + File.separator;
79 }
80 else {
81 gli_user_directory_path += ".gli" + File.separator;
82 }
83
84 new Configuration(gli_user_directory_path, gsdl_path, gsdl3_path, null, null, null);
85 current_language = Configuration.getLanguage();
86
87 languageList = new ArrayList();
88 }
89
90 public ArrayList getLanguageList(){
91 //already loaded
92 if (languageList.size() > 0 ) return languageList;
93
94 Document document = XMLTools.parseXMLFile(getLanguageXMLPath(), true);
95
96 NodeList languages = document.getDocumentElement().getElementsByTagName(GEMSConstants.LANGUAGE_ELEMENT);
97 for(int i=0; i< languages.getLength();i++){
98 Element language_element = (Element)languages.item(i);
99 languageList.add(language_element.getAttribute(GEMSConstants.CODE_ATTRIBUTE).toLowerCase()+" "+language_element.getAttribute(GEMSConstants.NAME_ATTRIBUTE));
100 }
101
102 return languageList;
103 }
104
105
106 public boolean isAttributeRequired(String name){
107 String tmp_name = name.trim();
108 for(int i=0;i<GEMSConstants.SET_REQUIRED_ATTRIBUTES.length;i++){
109 if (tmp_name.equals(GEMSConstants.SET_REQUIRED_ATTRIBUTES[i])) return true;
110 }
111 return false;
112 }
113
114 public void save(boolean confirm) {
115 MetadataSetInfo meta_info = metadata_set_model.getMetadataSetInfo();
116
117 if (meta_info ==null) return;
118
119 if (confirm){
120
121 //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"));
122 int result = JOptionPane.showConfirmDialog(null, Dictionary.get("GEMS.Confirm_Save"), Dictionary.get("GEMS.Confirm_Save_Title"), JOptionPane.YES_NO_OPTION);
123 if (result != JOptionPane.YES_OPTION) return;
124 }
125
126
127 File file_path = null;
128 //get new file name
129 if(meta_info.isNew()){
130 JFileChooser file_chooser = new JFileChooser(new File(getGLIMetadataDirectoryPath()));
131 file_chooser.setMultiSelectionEnabled(false);
132 file_chooser.setSelectedFile(new File(getGLIMetadataDirectoryPath(), meta_info.getNamespace()+GEMSConstants.METADATA_SET_FILE_EXTENSION));
133 int result = file_chooser.showSaveDialog(null);
134 if (result == JFileChooser.APPROVE_OPTION){
135 file_path = file_chooser.getSelectedFile();
136 meta_info.setFilePath(file_path.toString());
137 meta_info.setNew(false);
138 }
139 }
140 else {
141 file_path = new File(meta_info.getFilePath());
142 }
143
144 if (file_path != null) save(file_path);
145
146 for(int i=0;i<mds_list.size();i++){
147 MetadataSetInfo infoItem = (MetadataSetInfo)mds_list.get(i);
148 if (infoItem.getFilePath().trim().equals(meta_info.getFilePath().trim())) {
149 mds_list.remove(infoItem);
150 mds_list.add(meta_info);
151 break;
152 }
153 }
154 return;
155
156 }
157
158
159 private void save(File file_path){
160
161 try {
162
163 XMLTools.writeXMLFile(file_path, metadata_set_model.getMetadataSetDocument());
164
165 }
166 catch (Exception error) {
167 error.printStackTrace();
168 }
169
170 }
171
172
173 public Document getMetadataSetDocument(String filePath){
174 if (filePath == null || filePath.equals("")) return null;
175 return XMLTools.parseXMLFile(filePath, true);
176 }
177
178 public void setMetadataSetModel(MetadataSetModel model){
179 metadata_set_model = model;
180 }
181
182 public MetadataSetModel getMetadataSetModel(){
183 return metadata_set_model;
184 }
185
186
187 public ArrayList getAvailableMetadataSets(){
188 mds_list.clear();
189 // Load all the core metadata sets (in the GLI "metadata" directory)
190 File metadata_directory = new File(getGLIMetadataDirectoryPath());
191 if (metadata_directory.exists()) {
192 // Load just those .mds files in this directory, and return them
193 File[] directory_files = metadata_directory.listFiles();
194 for (int i = 0; i < directory_files.length; i++) {
195 File child_file = directory_files[i];
196
197 if (!child_file.isDirectory() && child_file.getName().endsWith(GEMSConstants.METADATA_SET_FILE_EXTENSION)) {
198 Document document = XMLTools.parseXMLFile(child_file.toString(), true);
199 MetadataSetInfo info = constructMetadataInfo(document);
200 info.setFilePath(child_file.toString());
201 mds_list.add(info);
202 }
203 }
204 }
205
206 return mds_list;
207 }
208
209 public boolean isNamespaceAlreadyUsed(String namespace){
210 for(int i=0;i<mds_list.size();i++){
211 MetadataSetInfo info = (MetadataSetInfo)mds_list.get(i);
212 if (info.getNamespace().trim().equals(namespace)) return true;
213 }
214 return false;
215 }
216
217 public MetadataSetInfo getMetadataSet(String metadata_path){
218 MetadataSetInfo info = constructMetadataInfo(XMLTools.parseXMLFile(metadata_path,true));
219
220 // check whether there is metadata set name for the current language
221 // if not reset the current_language to the language first encountered
222 ArrayList attrs = info.getLanguageDependentAttributes();
223 boolean foundLanguage = false;
224 for (int i=0;i<attrs.size();i++){
225 Attribute attr = (Attribute)attrs.get(i);
226 if (attr.getLanguage().equals(current_language)) {
227 info.setCurrentLanguage(current_language);
228 foundLanguage = true;
229 break;
230 }
231
232 }
233 if (!foundLanguage && attrs.size()>0){
234 info.setCurrentLanguage(((Attribute)attrs.get(0)).getLanguage());
235 }
236
237 info.setFilePath(metadata_path);
238 return info;
239
240 }
241
242 public void deleteMetadataSet(MetadataSetInfo info){
243 String filepath = info.getFilePath();
244
245 if(filepath != null && !filepath.trim().equals("")){
246 File file = new File(filepath);
247
248 boolean deleted = file.delete();
249 if (!deleted){
250 JOptionPane.showMessageDialog(null,Dictionary.get("GEMS.File_Deletion_Error_Message"), Dictionary.get("GEMS.File_Deletion_Error"),JOptionPane.ERROR_MESSAGE);
251 }
252 else{
253 for(int i=0;i<mds_list.size();i++){
254 MetadataSetInfo infoItem = (MetadataSetInfo)mds_list.get(i);
255 if (infoItem.getFilePath().trim().equals(info.getFilePath().trim())) {
256 mds_list.remove(infoItem);
257 break;
258 }
259 }
260 }
261 }
262 }
263
264 public String getCurrentLanguage(){
265 return current_language;
266 }
267
268 private MetadataSetInfo constructMetadataInfo(Document document){
269 NamedNodeMap attrs = document.getDocumentElement().getAttributes();
270 ArrayList attrList = new ArrayList();
271 String lang = null;
272 boolean findLang = false;
273
274 for (int i=0;i<attrs.getLength();i++){
275 Attr item = (Attr)attrs.item(i);
276 Attribute attr = new Attribute(item.getName(),item.getValue());
277 attr.setRequired(isAttributeRequired(attr.getName()));
278 attrList.add(attr);
279 }
280
281 ArrayList languageList = new ArrayList();
282
283 //load all <SetLanguage> elements
284 NodeList setLanguages = document.getDocumentElement().getElementsByTagName(GEMSConstants.SET_LANGUAGE_ELEMENT);
285
286 if (setLanguages.getLength() >0){
287 for(int i=0;i<setLanguages.getLength();i++){
288 Element set_language = (Element)setLanguages.item(i);
289 lang = set_language.getAttribute(GEMSConstants.CODE_ATTRIBUTE);
290 setNameAndDescription(languageList, set_language,lang);
291 }
292
293 }
294 //no set langugae elements, try load name and description elements
295 else{
296 setNameAndDescription(languageList, document.getDocumentElement(),lang);
297 }
298
299 MetadataSetInfo info = new MetadataSetInfo();
300 info.setLanguageDependentAttributes(languageList);
301 info.setAttributes(attrList);
302 return info;
303 }
304
305 private void setNameAndDescription(ArrayList languageList, Element parent,String lang){
306
307 ArrayList names = XMLTools.getChildElementsByTagName(parent,GEMSConstants.NAME_ELEMENT);
308 String name_lang = "";
309
310 if (names.size() > 0){
311 Element name_element = (Element)names.get(0);
312 Attribute attr = new Attribute(GEMSConstants.NAME_ATTRIBUTE,XMLTools.getElementTextValue(name_element), true);
313 if (lang == null){
314 attr.setLanguage(name_element.getAttribute(GEMSConstants.LANGUAGE_ATTRIBUTE));
315 name_lang = attr.getLanguage();
316 }
317 else{
318 attr.setLanguage(lang);
319 }
320 languageList.add(attr);
321// if (current_language.equals(attr.getLanguage())){
322// attr.setRequired(true);
323// languageList.add(0,attr);
324// }
325// else{
326// languageList.add(attr);
327// }
328
329 }
330
331
332 ArrayList des = XMLTools.getChildElementsByTagName(parent,GEMSConstants.DESCRIPTION_ELEMENT);
333 if (des.size() > 0){
334 Element des_element = (Element)des.get(0);
335 Attribute attr = new Attribute(GEMSConstants.DESCRIPTION_ATTRIBUTE,XMLTools.getElementTextValue(des_element), true);
336 if (lang == null){
337 attr.setLanguage(des_element.getAttribute(GEMSConstants.LANGUAGE_ATTRIBUTE));
338 }
339 else{
340 attr.setLanguage(lang);
341 }
342 languageList.add(attr);
343 }
344 else{
345 Attribute attr = new Attribute(GEMSConstants.DESCRIPTION_ATTRIBUTE,"", true);
346 if (lang == null){
347 attr.setLanguage(name_lang);
348 }
349 else{
350 attr.setLanguage(lang);
351 }
352
353 languageList.add(attr);
354 }
355
356 }
357
358
359 static public String getGLIDirectoryPath()
360 {
361 return System.getProperty("user.dir") + File.separator;
362 }
363
364
365 static public String getGLIMetadataDirectoryPath()
366 {
367 return getGLIDirectoryPath() + "metadata" + File.separator;
368 }
369
370 static public String getLanguageXMLPath()
371 {
372 return getGLIDirectoryPath() + "classes" + File.separator+"xml"+ File.separator+ "languages.xml";
373 }
374
375 static public String getCollectionPath()
376 {
377 if (Configuration.gsdl3_path != null){
378 return Configuration.gsdl3_path + File.separator + "sites";
379
380 }
381 else{
382 return Configuration.gsdl_path + File.separator + "collect";
383 }
384 }
385}
Note: See TracBrowser for help on using the repository browser.