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

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

Added a new method to provide a TreeSet of the current languages in use. We use a tree set so each language only appears once, and is automatically available in natural ordering

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