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 | */
|
---|
27 | package org.greenstone.gatherer.cdm;
|
---|
28 |
|
---|
29 | import java.awt.*;
|
---|
30 | import java.awt.event.*;
|
---|
31 | import java.util.*;
|
---|
32 | import javax.swing.*;
|
---|
33 | import javax.swing.event.*;
|
---|
34 | import org.greenstone.gatherer.Gatherer;
|
---|
35 | import org.greenstone.gatherer.cdm.CollectionDesignManager;
|
---|
36 | import org.greenstone.gatherer.cdm.CollectionMeta;
|
---|
37 | import org.greenstone.gatherer.cdm.DOMProxyListModel;
|
---|
38 | import org.greenstone.gatherer.cdm.Index;
|
---|
39 | import org.greenstone.gatherer.util.StaticStrings;
|
---|
40 | import org.greenstone.gatherer.util.Utility;
|
---|
41 | import 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 | */
|
---|
47 | public 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 | 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 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 | //Gatherer.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(Gatherer.config.getLanguage())) {
|
---|
127 | Gatherer.println("Found '" + metadatum + "'");
|
---|
128 | return metadatum;
|
---|
129 | }
|
---|
130 | else {
|
---|
131 | //Gatherer.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 | Gatherer.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 | }
|
---|