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

Last change on this file since 10556 was 10556, checked in by kjdon, 19 years ago

fixes to make the config file save only if it has changed

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