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

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

soem debuggin stuff, some stuff needed for inheritance

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