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

Last change on this file since 8853 was 8243, checked in by mdewsnip, 20 years ago

Removed all occurrences of classes explicitly importing other classes in the same package.

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