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.util.*;
|
---|
30 | import org.greenstone.gatherer.DebugStream;
|
---|
31 | import org.greenstone.gatherer.Gatherer;
|
---|
32 | import org.greenstone.gatherer.metadata.MetadataElement;
|
---|
33 | import org.greenstone.gatherer.metadata.MetadataTools;
|
---|
34 | import org.greenstone.gatherer.util.StaticStrings;
|
---|
35 | import org.greenstone.gatherer.util.XMLTools;
|
---|
36 | import org.w3c.dom.*;
|
---|
37 |
|
---|
38 | /** This class encapsulates a single index. MGPP/Lucene/SOLR
|
---|
39 | * Can be subclassed for Sort fields and facet fields
|
---|
40 | * @author John Thompson, Greenstone Digital Library, University of Waikato
|
---|
41 | * @version 2.3d
|
---|
42 | */
|
---|
43 | public class Index
|
---|
44 | implements Comparable, DOMProxyListEntry {
|
---|
45 |
|
---|
46 | protected ArrayList sources = null;
|
---|
47 |
|
---|
48 | /** The element this index is based upon. */
|
---|
49 | protected Element element = null;
|
---|
50 | /** The name for our elements */
|
---|
51 | protected String element_name = StaticStrings.INDEX_ELEMENT;
|
---|
52 | /** The name for the default element */
|
---|
53 | protected String default_element_name = StaticStrings.INDEX_DEFAULT_ELEMENT;
|
---|
54 | /** The unique, if cryptic, identifier of an index. */
|
---|
55 | protected String id = null;
|
---|
56 |
|
---|
57 | /** Default constructor, which should only be used during DOMProxyListModel creation. */
|
---|
58 | public Index() {
|
---|
59 | }
|
---|
60 |
|
---|
61 | /** Constructor. */
|
---|
62 | public Index(Element element) {
|
---|
63 | this.element = element;
|
---|
64 | }
|
---|
65 |
|
---|
66 | /** Constructor for a newly assigned index. */
|
---|
67 | public Index(ArrayList sources) {
|
---|
68 | this.sources = sources;
|
---|
69 | // Create a new element
|
---|
70 | this.element = CollectionConfiguration.createElement(this.element_name);
|
---|
71 | // For each source add a content element
|
---|
72 | int size = sources.size();
|
---|
73 | for(int i = 0; i < size; i++) {
|
---|
74 | Element content_element = CollectionConfiguration.createElement(StaticStrings.CONTENT_ELEMENT);
|
---|
75 | Object source_object = sources.get(i);
|
---|
76 | if (source_object instanceof MetadataElement) {
|
---|
77 | // System.err.println("Constructing new Index with MetadataElement source...");
|
---|
78 | content_element.setAttribute(StaticStrings.NAME_ATTRIBUTE, ((MetadataElement) source_object).getFullName());
|
---|
79 | }
|
---|
80 | else {
|
---|
81 | // System.err.println("Constructing new Index with String source...");
|
---|
82 | content_element.setAttribute(StaticStrings.NAME_ATTRIBUTE, source_object.toString());
|
---|
83 | }
|
---|
84 | element.appendChild(content_element);
|
---|
85 | content_element = null;
|
---|
86 | }
|
---|
87 | }
|
---|
88 |
|
---|
89 | /** Method to compare two indexes.
|
---|
90 | * @param object The other index as an <strong>Object</strong>.
|
---|
91 | * @return An <i>int</i> which indicates how the indexes compare.
|
---|
92 | * @see java.lang.String
|
---|
93 | */
|
---|
94 | public int compareTo(Object object) {
|
---|
95 | if(object == null) {
|
---|
96 | return -1;
|
---|
97 | }
|
---|
98 | String id = getID();
|
---|
99 | return id.compareTo(((Index)object).getID());
|
---|
100 | }
|
---|
101 |
|
---|
102 | public DOMProxyListEntry create(Element element) {
|
---|
103 | return new Index(element);
|
---|
104 | }
|
---|
105 |
|
---|
106 | /** Method to test for the equality of two indexes.
|
---|
107 | * @param object The other index as an <strong>Object</strong>.
|
---|
108 | * @return A <i>boolean</i> which is <i>true</i> if the two indexes are equal, <i>false</i> otherwise.
|
---|
109 | */
|
---|
110 | public boolean equals(Object object) {
|
---|
111 | return (compareTo(object) == 0);
|
---|
112 | }
|
---|
113 |
|
---|
114 | public Element getElement() {
|
---|
115 | return element;
|
---|
116 | }
|
---|
117 |
|
---|
118 | public String getID() {
|
---|
119 | if(element == null) {
|
---|
120 | id="";
|
---|
121 | }
|
---|
122 | else if(id == null) {
|
---|
123 | StringBuffer id_buffer = new StringBuffer();
|
---|
124 | // Write data information. Retrieve each of the content sources and add them in a comma separated list.
|
---|
125 | ArrayList sources = getSources();
|
---|
126 | int sources_size = sources.size();
|
---|
127 | for(int i = 0; i < sources_size; i++) {
|
---|
128 | Object source_object = sources.get(i);
|
---|
129 | if (source_object instanceof MetadataElement) {
|
---|
130 | String full_element_name = ((MetadataElement)source_object).getFullName();
|
---|
131 | if(full_element_name.startsWith(StaticStrings.EXTRACTED_NAMESPACE) && full_element_name.indexOf(StaticStrings.NS_SEP, StaticStrings.EXTRACTED_NAMESPACE.length()) == -1) {
|
---|
132 | id_buffer.append(full_element_name.substring(StaticStrings.EXTRACTED_NAMESPACE.length()));
|
---|
133 | }
|
---|
134 | else {
|
---|
135 | id_buffer.append(full_element_name);
|
---|
136 | }
|
---|
137 | }
|
---|
138 | else {
|
---|
139 | id_buffer.append(source_object.toString());
|
---|
140 | }
|
---|
141 | id_buffer.append(StaticStrings.COMMA_CHARACTER);
|
---|
142 | }
|
---|
143 | sources = null;
|
---|
144 | if (id_buffer.length()==0) {
|
---|
145 | id = "";
|
---|
146 | } else {
|
---|
147 | id = id_buffer.substring(0, id_buffer.length() - 1);
|
---|
148 | }
|
---|
149 | }
|
---|
150 | return id;
|
---|
151 | }
|
---|
152 |
|
---|
153 |
|
---|
154 | /** Retrieve the sources of this index.
|
---|
155 | * @return the sources as an ArrayList
|
---|
156 | */
|
---|
157 | public ArrayList getSources() {
|
---|
158 | if(sources == null) {
|
---|
159 | sources = new ArrayList();
|
---|
160 | NodeList content_elements = element.getElementsByTagName(StaticStrings.CONTENT_ELEMENT);
|
---|
161 | int content_elements_length = content_elements.getLength();
|
---|
162 | for(int i = 0; i < content_elements_length; i++) {
|
---|
163 | Element content_element = (Element) content_elements.item(i);
|
---|
164 | String metadata_element_name_full = (String) content_element.getAttribute(StaticStrings.NAME_ATTRIBUTE);
|
---|
165 | MetadataElement metadata_element = MetadataTools.getMetadataElementWithName(metadata_element_name_full);
|
---|
166 | if (metadata_element != null) {
|
---|
167 | sources.add(metadata_element);
|
---|
168 | }
|
---|
169 | else {
|
---|
170 | sources.add(metadata_element_name_full);
|
---|
171 | }
|
---|
172 | }
|
---|
173 | content_elements = null;
|
---|
174 | }
|
---|
175 | return sources;
|
---|
176 | }
|
---|
177 |
|
---|
178 | public boolean isAssigned() {
|
---|
179 | return (element != null && !element.getAttribute(StaticStrings.ASSIGNED_ATTRIBUTE).equals(StaticStrings.FALSE_STR));
|
---|
180 | }
|
---|
181 |
|
---|
182 | public void setAssigned(boolean assigned) {
|
---|
183 | if(element != null) {
|
---|
184 | element.setAttribute(StaticStrings.ASSIGNED_ATTRIBUTE, (assigned ? StaticStrings.TRUE_STR : StaticStrings.FALSE_STR));
|
---|
185 | }
|
---|
186 | }
|
---|
187 |
|
---|
188 | public void setElement(Element element) {
|
---|
189 | this.element = element;
|
---|
190 | this.id = null;
|
---|
191 | this.sources = null;
|
---|
192 | }
|
---|
193 |
|
---|
194 | /** Method to set the sources for this index which can only be used for the default index.
|
---|
195 | * @param sources an ArrayList of source names
|
---|
196 | */
|
---|
197 | public void setSources(ArrayList sources) {
|
---|
198 | if(element != null && element.getNodeName().equals(this.default_element_name)) {
|
---|
199 | // Erase old sources
|
---|
200 | XMLTools.clear(element);
|
---|
201 | // For each entry in the sources array add a new content element.
|
---|
202 | int size = sources.size();
|
---|
203 | for(int i = 0; i < size; i++) {
|
---|
204 | Element content_element = element.getOwnerDocument().createElement(StaticStrings.CONTENT_ELEMENT);
|
---|
205 | Object source_object = sources.get(i);
|
---|
206 | if (source_object instanceof MetadataElement) {
|
---|
207 | String name = ((MetadataElement) source_object).getFullName();
|
---|
208 | content_element.setAttribute(StaticStrings.NAME_ATTRIBUTE, name);
|
---|
209 | name = null;
|
---|
210 | }
|
---|
211 | else {
|
---|
212 | //DebugStream.println("Found String as source: " + source_object.toString());
|
---|
213 | content_element.setAttribute(StaticStrings.NAME_ATTRIBUTE, source_object.toString());
|
---|
214 | }
|
---|
215 | source_object = null;
|
---|
216 | element.appendChild(content_element);
|
---|
217 | content_element = null;
|
---|
218 | }
|
---|
219 | this.id = null; // Regenerate ID.
|
---|
220 | this.sources = sources;
|
---|
221 | }
|
---|
222 | else {
|
---|
223 | DebugStream.println("Error! Called setSource() of index other than the default.");
|
---|
224 | }
|
---|
225 | }
|
---|
226 |
|
---|
227 | /** Method to turn this object into a string representation ready to be placed in the collection configuration file.
|
---|
228 | * @return A <strong>String</strong> containing the information of this class.
|
---|
229 | */
|
---|
230 | public String toString() {
|
---|
231 | StringBuffer text_buffer = new StringBuffer("");
|
---|
232 | // Generate language dependant id (include extracted metadata namespace)
|
---|
233 | // Write data information. Retrieve each of the content sources and add them in a comma separated list.
|
---|
234 | ArrayList sources = getSources();
|
---|
235 | int sources_size = sources.size();
|
---|
236 | for(int i = 0; i < sources_size; i++) {
|
---|
237 | String source_name = (sources.get(i)).toString();
|
---|
238 | text_buffer.append(source_name);
|
---|
239 | if(i < sources_size - 1) {
|
---|
240 | text_buffer.append(StaticStrings.COMMA_CHARACTER);
|
---|
241 | }
|
---|
242 | }
|
---|
243 | sources = null;
|
---|
244 | return text_buffer.toString();
|
---|
245 | }
|
---|
246 | }
|
---|