source: trunk/gli/src/org/greenstone/gatherer/cdm/CollectionMetaManager.java@ 4675

Last change on this file since 4675 was 4675, checked in by jmt12, 21 years ago

Sunday's work

  • Property svn:keywords set to Author Date Id Revision
File size: 15.9 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: John Thompson, Greenstone Digital Library, University of Waikato
11 *
12 * <BR><BR>
13 *
14 * Copyright (C) 1999 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
38
39
40
41
42
43/* GPL_HEADER */
44package org.greenstone.gatherer.cdm;
45/**************************************************************************************
46 * Title: Gatherer
47 * Description: The Gatherer: a tool for gathering and enriching a digital collection.
48 * Copyright: Copyright (c) 2001
49 * Company: The University of Waikato
50 * Written: /05/02
51 * Revised: 22/08/02 Revamped, Optimized and Commented.
52 **************************************************************************************/
53import java.awt.*;
54import java.awt.event.*;
55import java.util.*;
56import javax.swing.*;
57import javax.swing.event.*;
58import org.greenstone.gatherer.Gatherer;
59import org.greenstone.gatherer.cdm.CollectionDesignManager;
60import org.greenstone.gatherer.cdm.CollectionMeta;
61import org.greenstone.gatherer.cdm.Index;
62import org.greenstone.gatherer.cdm.Language;
63import org.greenstone.gatherer.gui.EditorDialog;
64import org.greenstone.gatherer.util.Utility;
65/** This class is responsible for maintaining a list of assigned collection level metadata, and for allows manipulations on the aforementioned data.
66 * @author John Thompson, Greenstone Digital Library, University of Waikato
67 * @version 2.3
68 */
69// ####################################################################################
70// Optimization Saving
71// ####################################################################################
72// Removed Vector and Hashtable + Memory, + Processor
73// ####################################################################################
74public class CollectionMetaManager
75 extends DefaultListModel {
76 /** A reference to the cdm manager so we can access indexes and languages. */
77 private CollectionDesignManager manager = null;
78 /** A reference to ourself so that inner classes can use us as a model. */
79 private DefaultListModel model = null;
80 /** A reference to the Gatherer. */
81 private Gatherer gatherer = null;
82 /** The language the most recent metadata returned was in. */
83 private Language current_language = null;
84 /** We can't safely parse metadata commands until after all the other commands have been parsed, so we store commands here for now. */
85 private ArrayList unresolved_commands = null;
86 /** Constructor.
87 * @param gatherer A reference to the <strong>Gatherer</strong>.
88 * @param manager A reference to the <strong>CollectionDesignManager</strong> for access to other configuration managers.
89 */
90 public CollectionMetaManager(Gatherer gatherer, CollectionDesignManager manager) {
91 super();
92 this.gatherer = gatherer;
93 this.manager = manager;
94 this.model = this;
95 this.unresolved_commands = new ArrayList();
96 }
97 /** Method to add a new piece of metadata.
98 * @param metadata The new <strong>CollectionMeta</strong>.
99 */
100 public void addMetadata(CollectionMeta metadata) {
101 CollectionMeta existing = getMetadata(metadata.getName().toString(), metadata.getLanguage(), false);
102 if(existing != null) {
103 removeElement(existing);
104 }
105 addElement(metadata);
106 gatherer.c_man.configurationChanged();
107 }
108 /** Retrieve the collectionextra metadata in the default language, if present.
109 * @return A <strong>CollectionMeta</strong> containing the collectionextra in the default language if present, or else the first collectionextra of any language, otherwise <i>null</i>.
110 * @see org.greenstone.gatherer.cdm.Language
111 */
112 public CollectionMeta getCollectionExtra() {
113 CollectionMeta result = getMetadata("collectionextra", Gatherer.config.interface_language, true);
114 if(result == null) {
115 result = new CollectionMeta(manager, "collectionextra", Gatherer.config.interface_language, "");
116 addMetadata(result);
117 }
118 return result;
119 }
120 /** Retrieve the collectionname metadata in the default language, if present.
121 * @return A <strong>CollectionMeta</strong> containing the collectionname in the default language if present, or else the first collectionname of any language, otherwise <i>null</i>.
122 * @see org.greenstone.gatherer.cdm.Language
123 */
124 public CollectionMeta getCollectionName() {
125 return getMetadata("collectionname", Gatherer.config.interface_language, true);
126 }
127 /** Retrieve the iconcollection metadata in the default language, if present.
128 * @return A <strong>CollectionMeta</strong> containing the iconcollection in the default language if present, or else the first iconcollection of any language, otherwise <i>null</i>.
129 * @see org.greenstone.gatherer.cdm.Language
130 */
131 public CollectionMeta getIconCollection() {
132 CollectionMeta result = getMetadata("iconcollection", Gatherer.config.interface_language, true);
133 if(result == null) {
134 result = new CollectionMeta(manager, "iconcollection", Gatherer.config.interface_language, "");
135 addMetadata(result);
136 }
137 return result;
138 }
139 /** Retrieve the iconcollection metadata in the default language, if present.
140 * @return A <strong>CollectionMeta</strong> containing the iconcollectionsmall in the default language if present, or else the first iconcollectionsmall of any language, otherwise <i>null</i>.
141 * @see org.greenstone.gatherer.cdm.Language
142 */
143 public CollectionMeta getIconCollectionSmall() {
144 CollectionMeta result = getMetadata("iconcollectionsmall", Gatherer.config.interface_language, true);
145 if(result == null) {
146 result = new CollectionMeta(manager, "iconcollectionsmall", Gatherer.config.interface_language, "");
147 addMetadata(result);
148 }
149 return result;
150 }
151 /** Method to retrieve the list of metadata.
152 * @return An <strong>ArrayList</strong> containing the metadata.
153 */
154 public ArrayList getMetadata() {
155 ArrayList metadata = new ArrayList();
156 for(int i = 0; i < size(); i++) {
157 metadata.add(get(i));
158 }
159 Collections.sort(metadata);
160 return metadata;
161 }
162
163 /** Retrieve all of the metadata for the given feature, regardless of language. */
164 public ArrayList getMetadata(Object name) {
165 ArrayList result = new ArrayList();
166 for(int i = 0; i < size(); i++) {
167 CollectionMeta metadata = (CollectionMeta) get(i);
168 if(metadata.getName().equals(name)) {
169 result.add(metadata);
170 }
171 }
172 return result;
173 }
174 /** Method to retrieve a certain piece of metadata based on its name and language.
175 * @param name the name of the metadata as an Object (as it may actually be a refernce to an Index or SubIndex)
176 * @param language The <strong>Language</strong> of the metadata.
177 * @param partial <i>true</i> to return the first partial match (ie matches name but not language).
178 * @return The <strong>CollectionMeta</strong> requested, or <i>null</i> if no such metadata.
179 */
180 public CollectionMeta getMetadata(Object name, Language language, boolean partial) {
181 CollectionMeta partial_match = null;
182 for(int i = 0; i < size(); i++) {
183 CollectionMeta metadata = (CollectionMeta) get(i);
184 Object metadata_name = metadata.getName();
185 // We test the case of an object match (ie Index to Index)...
186 if(metadata_name.equals(name)) {
187 if (metadata.getLanguage().equals(language)) {
188 return metadata;
189 }
190 partial_match = metadata;
191 }
192 // But if that fails we also try a string match such that "section:dls.Title" == Index
193 if(metadata_name.toString().equals(name.toString())) {
194 if (metadata.getLanguage().equals(language)) {
195 return metadata;
196 }
197 partial_match = metadata;
198 }
199 }
200 if(partial) {
201 return partial_match;
202 }
203 return null;
204 }
205 /** Method that attempts to parse a collection metadata command from the given text. If a command is parsed successfully it is immediately added to the the collections metadata.
206 * @param command The command text we wish to parse, as a <strong>String</strong>.
207 * @return A <i>boolean</i> which is <i>true</i> if a collection metadata command was successfully parsed, <i>false</i> otherwise.
208 * @see org.greenstone.gatherer.cdm.CollectionDesignManager
209 * @see org.greenstone.gatherer.cdm.CommandTokenizer
210 * @see org.greenstone.gatherer.cdm.Language
211 * @see org.greenstone.gatherer.cdm.LanguageManager
212 * @see org.greenstone.gatherer.util.Utility
213 */
214 public boolean parse(String command, boolean finished) {
215 String temp = command.toLowerCase();
216 if(temp.startsWith("collectionmeta")) {
217 if(finished) {
218 CommandTokenizer ct = new CommandTokenizer(command);
219 if(ct.countTokens() >= 3) {
220 ct.nextToken(); // Throw away collectionmeta
221 Object key = ct.nextToken();
222 Language language = null;
223 String language_code = null;
224 String value = ct.nextToken();
225 // Arg. Remember a language token will be '[l=<code>]'
226 if(value.startsWith("[") && value.endsWith("]")) {
227 language_code = value.substring(3, value.length() - 1);
228 ///ystem.err.println("Language code = " + language_code);
229 value = ct.nextToken();
230 }
231 // Check if the key is an index, and if so retrieve it.
232 if(((String)key).startsWith(".")) {
233 ///ystem.err.println("Detected '.' prefix.");
234 String key_str = (String)key;
235 key = null;
236 key_str = key_str.substring(1);
237 Index ind = Index.parseIndexConfig(key_str, manager);
238 if (ind != null) {
239
240 key = manager.indexes.getIndex(ind.toString(false));
241 }
242
243 // If that didn't work, then it might be a subindex so have a bash at retrieving that instead.
244 if(key == null) {
245 key = manager.subcollections.getSubIndex(key_str);
246 //if(key != null) {
247 ///ystem.err.println("Its a subindex.");
248 //}
249 //else {
250 ///ystem.err.println("Error Will Robinson.");
251 //}
252 }
253 //else {
254 ///ystem.err.println("Its an index.");
255 //}
256 }
257 // An if we have a language code, retrieve its object too.
258 if(language_code != null) {
259 language = manager.languages.getLanguage(language_code, false);
260 }
261 // Otherwise set language to the default language.
262 else {
263 language = Gatherer.config.interface_language;
264 }
265 if(key != null) {
266 // Trim any "
267 if(value.equals("\"\"")) {
268 value = "";
269 }
270 else {
271 if(value.startsWith("\"")) {
272 value = value.substring(1);
273 }
274 if(value.endsWith("\"")) {
275 value = value.substring(0, value.length() - 1);
276 }
277 }
278 CollectionMeta meta = new CollectionMeta(manager, key, language, Utility.decodeGreenstone(value));
279 addMetadata(meta);
280 }
281 return true;
282 }
283 }
284 else {
285 unresolved_commands.add(command);
286 return true;
287 }
288 }
289 return false;
290 }
291 /** Ensure that the values being showed are the most up to date. */
292 public void refresh() {
293 fireContentsChanged(this, 0, size());
294 }
295
296 /** Method to remove a piece of metadata.
297 * @param metadata The <strong>CollectionMeta</strong> you wish to remove.
298 */
299 public void removeMetadata(CollectionMeta metadata) {
300 removeElement(metadata);
301 gatherer.c_man.configurationChanged();
302 }
303 /** Method which attempts to reparse obvious metadata commands which used unresovable references.
304 */
305 public void reparseUnresolved() {
306 for(int i = 0; i < unresolved_commands.size(); i++) {
307 parse((String)unresolved_commands.get(i), true);
308 }
309 // Regardless of if they work, clear the commands.
310 unresolved_commands.clear();
311 }
312 /** Sets the value of a certain metadata.
313 * @param name The name of the metadata as a <strong>String</strong>.
314 * @param language The <strong>Language</strong> to use.
315 * @param value The value of the metadata also as a <strong>String</strong>.
316 */
317 public void setMetadata(String name, Language language, String value) {
318 addMetadata(new CollectionMeta(manager, name, language, value));
319 }
320 /** Method to produce the list of metadata in a string such as you would find in the collection configuration file.
321 * @return A <strong>String</strong> containing the list of collection metadata.
322 */
323 public String toString() {
324 // We sort the metadata first.
325 ArrayList metadatum = new ArrayList();
326 for(int i = 0; i < size(); i++) {
327 metadatum.add(get(i));
328 }
329 Collections.sort(metadatum, new CollectionMetaComparator());
330 // Now we print them
331 StringBuffer text = new StringBuffer("");
332 for(int i = 0; i < metadatum.size(); i++) {
333 CollectionMeta data = (CollectionMeta) metadatum.get(i);
334 if(data.valid()) {
335 text.append(data.toString());
336 }
337 }
338 metadatum = null;
339 text.append("\n");
340 return text.toString();
341 }
342 /** Overloaded to call get with both a key and an empty argument array.
343 * @param key A <strong>String</strong> which is mapped to a initial String within the ResourceBundle.
344 * @return A <strong>String</strong> which has been referenced by the key String and that either contains no argument fields, or has had the argument fields automatiically populated with formatting Strings of with argument String provided in the get call.
345 */
346 private String get(String key) {
347 return get(key, null);
348 }
349 /** Used to retrieve a property value from the Locale specific ResourceBundle, based upon the key and arguments supplied. If the key cannot be found or if some other part of the call fails a default (English) error message is returned. <BR>
350 * Here the get recieves a second argument which is an array of Strings used to populate argument fields, denoted {<I>n</I>}, within the value String returned. Note that argument numbers greater than or equal to 32 are automatically mapped to the formatting String named Farg<I>n</I>.
351 * @param key A <strong>String</strong> which is mapped to a initial String within the ResourceBundle.
352 * @param args A <strong>String[]</strong> used to populate argument fields within the complete String.
353 * @return A <strong>String</strong> which has been referenced by the key String and that either contains no argument fields, or has had the argument fields automatiically populated with formatting Strings of with argument String provided in the get call.
354 * @see org.greenstone.gatherer.Gatherer
355 * @see org.greenstone.gatherer.Dictionary
356 */
357 private String get(String key, String args[]) {
358 if(key.indexOf('.') == -1) {
359 key = "CDM.CollectionMetaManager." + key;
360 }
361 return gatherer.dictionary.get(key, args);
362 }
363
364 private class CollectionMetaComparator
365 implements Comparator {
366 public int compare(Object o1, Object o2) {
367 String s1 = o1.toString();
368 String s2 = o2.toString();
369 if(s1.startsWith(".")) {
370 return 1;
371 }
372 else if(s2.startsWith(".")) {
373 return -1;
374 }
375 return o1.toString().compareTo(o2.toString());
376 }
377 public boolean equals(Object o2) {
378 return (compare(this, o2) == 0);
379 }
380 }
381}
Note: See TracBrowser for help on using the repository browser.