source: gli/branches/rtl-gli/src/org/greenstone/gatherer/cdm/CollectionMetaManager.java@ 18368

Last change on this file since 18368 was 12808, checked in by mdewsnip, 18 years ago

Removed CollectionManager.configurationChanged() and all 50 calls to it. The CollectionConfiguration class now works out itself whether it needs to be changed or not -- a far more reliable approach.

  • Property svn:keywords set to Author Date Id Revision
File size: 6.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 * Author: John Thompson, Greenstone Digital Library, University of Waikato
9 *
10 * Copyright (C) 1999 New Zealand Digital Library Project
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 *########################################################################
26 */
27package org.greenstone.gatherer.cdm;
28
29import java.awt.*;
30import java.awt.event.*;
31import java.util.*;
32import javax.swing.*;
33import javax.swing.event.*;
34import org.greenstone.gatherer.Configuration;
35import org.greenstone.gatherer.DebugStream;
36import org.greenstone.gatherer.Gatherer;
37import org.greenstone.gatherer.util.StaticStrings;
38import org.w3c.dom.*;
39
40/** This class is responsible for maintaining a list of assigned collection level metadata, and for allows manipulations on the aforementioned data.
41 * @author John Thompson, Greenstone Digital Library, University of Waikato
42 * @version 2.3d
43 */
44public class CollectionMetaManager
45 extends DOMProxyListModel {
46
47 /** Constructor. */
48 public CollectionMetaManager() {
49 super(CollectionDesignManager.collect_config.getDocumentElement(), StaticStrings.COLLECTIONMETADATA_ELEMENT, new CollectionMeta(""));
50 DebugStream.println("CollectionMetaManager: " + getSize() + " metadata parsed.");
51 }
52 /** Method to add a new piece of metadata.
53 * @param metadata the new CollectionMeta
54 */
55 public void addMetadatum(CollectionMeta metadata) {
56 if(!contains(metadata)) {
57 Element element = metadata.getElement();
58 // Locate where we should insert this new metadata.
59 Node target_node = CollectionConfiguration.findInsertionPoint(element);
60 add(root, metadata, target_node);
61 }
62 }
63
64 public CollectionMeta get(int i) {
65 return (CollectionMeta) getElementAt(i);
66 }
67
68 /** Retrieve the languages in use for the metadata assigned to this collection
69 * @return an TreeSet containing the languages used
70 */
71 public TreeSet getLanguages() {
72 TreeSet result = new TreeSet();
73 int size = getSize();
74 for(int i = 0; i < size; i++) {
75 CollectionMeta metadata = (CollectionMeta) getElementAt(i);
76 String language = metadata.getLanguage();
77 result.add(language);
78 }
79 return result;
80 }
81
82 /** Retrieve all of the general metadata. */
83 public ArrayList getMetadata() {
84 ArrayList result = new ArrayList();
85 int size = getSize();
86 for(int i = 0; i < size; i++) {
87 CollectionMeta metadata = (CollectionMeta) getElementAt(i);
88 if(!metadata.getName().startsWith(StaticStrings.STOP_CHARACTER)) {
89 result.add(metadata);
90 }
91 }
92 return result;
93 }
94
95 /** Retrieve all of the metadata for the given feature, regardless of language. */
96 public ArrayList getMetadata(String name) {
97 ArrayList result = new ArrayList();
98 int size = getSize(); // Refresh DOM Model
99 for(int i = 0; i < size; i++) {
100 CollectionMeta metadata = (CollectionMeta) getElementAt(i);
101 if(metadata.getName().equals(name)) {
102 result.add(metadata);
103 }
104 }
105 return result;
106 }
107
108 /** Retrieve the named piece of metadata, in the default language, if available. If no such metadata is available then it is created.
109 * @param name the name of the metadatum to retrieve as a String
110 * @return the dom Element containing the specified metadatum
111 */
112 public CollectionMeta getMetadatum(String name) {
113 return getMetadatum(name, true);
114 }
115
116 public CollectionMeta getMetadatum(String name, boolean add_if_not_found) {
117 //DebugStream.println("Get the metadata for " + name + " in the default language.");
118 int size = getSize();
119 for(int i = 0; i < size; i++) {
120 CollectionMeta metadatum = (CollectionMeta) getElementAt(i);
121 if(metadatum.getName().equals(name) && metadatum.getLanguage().equals(Configuration.getLanguage())) {
122 DebugStream.println("Found '" + metadatum + "'");
123 return metadatum;
124 }
125 else {
126 //DebugStream.println("No match with: " + metadatum.getName() + " [l=" + metadatum.getLanguage() + "] \"" + metadatum.getValue() + "\"");
127 }
128 metadatum = null;
129 }
130 if(add_if_not_found) {
131 CollectionMeta result = new CollectionMeta(name);
132 addMetadatum(result);
133 DebugStream.println("Added new metadata: " + name);
134 return result;
135 }
136 else {
137 return null;
138 }
139 }
140
141 /** Method to retrieve a certain piece of metadata based on its name and language.
142 * @param name the name of the metadata as an Object (as it may actually be a refernce to an Index or SubIndex)
143 * @param language the language of the metadata.
144 * @param partial <i>true</i> to return the first partial match (ie matches name but not language).
145 * @return The <strong>CollectionMeta</strong> requested, or <i>null</i> if no such metadata.
146 */
147 public CollectionMeta getMetadata(String name, String language, boolean partial) {
148 CollectionMeta partial_match = null;
149 for(int i = 0; i < getSize(); i++) {
150 CollectionMeta metadata = (CollectionMeta) getElementAt(i);
151 Object metadata_name = metadata.getName();
152 // We test the case of an object match (ie Index to Index)...
153 if(metadata_name.equals(name)) {
154 if (metadata.getLanguage().equals(language)) {
155 return metadata;
156 }
157 partial_match = metadata;
158 }
159 }
160 if(partial) {
161 return partial_match;
162 }
163 return null;
164 }
165
166 /** Method to remove a piece of metadata.
167 * @param metadata metadata
168 */
169 public void removeMetadata(CollectionMeta metadata) {
170 if(metadata != null) {
171 String name = metadata.getName();
172 String language = metadata.getLanguage();
173 for(int i = 0; i < getSize(); i++) {
174 CollectionMeta other = (CollectionMeta) getElementAt(i);
175 if(name.equals(other.getName()) && language.equals(other.getLanguage())) {
176 remove(i);
177 return;
178 }
179 other = null;
180 }
181 language = null;
182 name = null;
183 }
184 }
185
186 /** Removes all of the metadata with a certain name, regardless of language or value. */
187 public void removeMetadata(String name) {
188 for(int i = getSize(); i != 0; i--) {
189 CollectionMeta other = (CollectionMeta) getElementAt(i - 1);
190 if(name.equals(other.getName())) {
191 remove(i - 1);
192 }
193 other = null;
194 }
195 }
196
197 public int size() {
198 return getSize();
199 }
200}
Note: See TracBrowser for help on using the repository browser.