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

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

Major CDM rewrite so it uses DOM.

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