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

Last change on this file was 36235, checked in by kjdon, 2 years ago

removed some old debug statements

  • Property svn:keywords set to Author Date Id Revision
File size: 7.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 * 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.greenstone.gatherer.util.XMLTools; // jsut for debuggin
39import org.w3c.dom.*;
40
41/** This class is responsible for maintaining a list of assigned collection level metadata, and for allows manipulations on the aforementioned data.
42 * @author John Thompson, Greenstone Digital Library, University of Waikato
43 * @version 2.3d
44 */
45public class CollectionMetaManager
46 extends DOMProxyListModel {
47
48 /** Constructor. */
49 public CollectionMetaManager() {
50 super(CollectionDesignManager.collect_config.getDocumentElement(), StaticStrings.COLLECTIONMETADATA_ELEMENT, new CollectionMeta(""));
51 DebugStream.println("CollectionMetaManager: " + getSize() + " metadata parsed.");
52 }
53 // needed for SearchMetaManager inheritance
54 public CollectionMetaManager (Element root, String tag_name, DOMProxyListEntry class_type) {
55 super(root, tag_name, class_type);
56 }
57
58 /** Method to add a new piece of metadata.
59 * @param metadata the new CollectionMeta
60 */
61 public void addMetadatum(CollectionMeta metadata) {
62 if(!contains(metadata)) {
63
64 Element element = metadata.getElement();
65 // Locate where we should insert this new metadata.
66 Node target_node = CollectionConfiguration.findInsertionPoint(element);
67 add(root, metadata, target_node);
68 }
69 }
70
71 public CollectionMeta get(int i) {
72 return (CollectionMeta) getElementAt(i);
73 }
74
75 /** Retrieve the languages in use for the metadata assigned to this collection
76 * @return an TreeSet containing the languages used
77 */
78 public TreeSet getLanguages() {
79 TreeSet result = new TreeSet();
80 int size = getSize();
81 for(int i = 0; i < size; i++) {
82 CollectionMeta metadata = (CollectionMeta) getElementAt(i);
83 String language = metadata.getLanguage();
84 if (!language.equals("")) {
85 result.add(language);
86 }
87 }
88 return result;
89 }
90
91 /** Retrieve all of the general metadata. */
92 public ArrayList getMetadata() {
93 ArrayList result = new ArrayList();
94 int size = getSize();
95 for(int i = 0; i < size; i++) {
96 CollectionMeta metadata = (CollectionMeta) getElementAt(i);
97 // if(!metadata.hasType() && !metadata.getName().startsWith(StaticStrings.STOP_CHARACTER)) {
98 result.add(metadata);
99 //}
100 }
101 return result;
102 }
103
104 /** Retrieve all of the metadata for the given feature, regardless of language. */
105 public ArrayList getMetadata(String name) {
106 ArrayList result = new ArrayList();
107 int size = getSize(); // Refresh DOM Model
108 for(int i = 0; i < size; i++) {
109 CollectionMeta metadata = (CollectionMeta) getElementAt(i);
110 if(metadata.getName().equals(name)) {
111 result.add(metadata);
112 }
113 }
114 return result;
115 }
116
117 /** Retrieve the named piece of metadata, in the default language, if available. If no such metadata is available then it is created.
118 * @param name the name of the metadatum to retrieve as a String
119 * @return the dom Element containing the specified metadatum
120 */
121 public CollectionMeta getMetadatum(String name) {
122 return getMetadatum(name, true);
123 }
124
125 public CollectionMeta getMetadatum(String name, boolean add_if_not_found) {
126 //DebugStream.println("Get the metadata for " + name + " in the default language.");
127 int size = getSize();
128 for(int i = 0; i < size; i++) {
129 CollectionMeta metadatum = (CollectionMeta) getElementAt(i);
130 if(metadatum.getName().equals(name) && metadatum.getLanguage().equals(Configuration.getLanguage())) {
131 DebugStream.println("Found '" + metadatum + "'");
132 return metadatum;
133 }
134 else {
135 //DebugStream.println("No match with: " + metadatum.getName() + " [l=" + metadatum.getLanguage() + "] \"" + metadatum.getValue() + "\"");
136 }
137 metadatum = null;
138 }
139 if(add_if_not_found) {
140 CollectionMeta result = new CollectionMeta(name);
141 addMetadatum(result);
142 DebugStream.println("Added new metadata: " + name);
143 return result;
144 }
145 else {
146 return null;
147 }
148 }
149
150 /** Method to retrieve a certain piece of metadata based on its name and language.
151 * @param name the name of the metadata as an Object (as it may actually be a refernce to an Index or SubIndex)
152 * @param language the language of the metadata.
153 * @param partial <i>true</i> to return the first partial match (ie matches name but not language).
154 * @return The <strong>CollectionMeta</strong> requested, or <i>null</i> if no such metadata.
155 */
156 public CollectionMeta getMetadata(String name, String language, boolean partial) {
157 CollectionMeta partial_match = null;
158 for(int i = 0; i < getSize(); i++) {
159 CollectionMeta metadata = (CollectionMeta) getElementAt(i);
160 Object metadata_name = metadata.getName();
161 // We test the case of an object match (ie Index to Index)...
162 if(metadata_name.equals(name)) {
163 if (metadata.getLanguage().equals(language)) {
164 return metadata;
165 }
166 partial_match = metadata;
167 }
168 }
169 if(partial) {
170 return partial_match;
171 }
172 return null;
173 }
174
175 /** Method to remove a piece of metadata.
176 * @param metadata metadata
177 */
178 public void removeMetadata(CollectionMeta metadata) {
179 if(metadata != null) {
180 String name = metadata.getName();
181 String language = metadata.getLanguage();
182 for(int i = 0; i < getSize(); i++) {
183 CollectionMeta other = (CollectionMeta) getElementAt(i);
184 if(name.equals(other.getName()) && language.equals(other.getLanguage())) {
185 remove(i);
186 return;
187 }
188 other = null;
189 }
190 language = null;
191 name = null;
192 }
193 }
194
195 /** Removes all of the metadata with a certain name, regardless of language or value. */
196 public void removeMetadata(String name) {
197 for(int i = getSize(); i != 0; i--) {
198 CollectionMeta other = (CollectionMeta) getElementAt(i - 1);
199 if(name.equals(other.getName())) {
200 remove(i - 1);
201 }
202 other = null;
203 }
204 }
205
206 public int size() {
207 return getSize();
208 }
209}
Note: See TracBrowser for help on using the repository browser.