source: main/trunk/gli/src/org/greenstone/gatherer/cdm/Index.java@ 36151

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

Facet and SortField inherit from Index, as basically the same code but diff element names. added getElementName() and getDefaultElementName() so that the subclasses can use their own element names

  • Property svn:keywords set to Author Date Id Revision
File size: 9.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;
28
29import java.util.*;
30import org.greenstone.gatherer.DebugStream;
31import org.greenstone.gatherer.Gatherer;
32import org.greenstone.gatherer.metadata.MetadataElement;
33import org.greenstone.gatherer.metadata.MetadataTools;
34import org.greenstone.gatherer.util.StaticStrings;
35import org.greenstone.gatherer.util.XMLTools;
36import 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 */
43public 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 static final String index_element = StaticStrings.INDEX_ELEMENT;
52 /** The name for the default element */
53 static final String default_index_element = 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(getElementName());
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 protected String getElementName() {
90 return index_element;
91 }
92
93 protected String getDefaultElementName() {
94 return default_index_element;
95 }
96
97 /** Method to compare two indexes.
98 * @param object The other index as an <strong>Object</strong>.
99 * @return An <i>int</i> which indicates how the indexes compare.
100 * @see java.lang.String
101 */
102 public int compareTo(Object object) {
103 if(object == null) {
104 return -1;
105 }
106 String id = getID();
107 return id.compareTo(((Index)object).getID());
108 }
109
110 public DOMProxyListEntry create(Element element) {
111 return new Index(element);
112 }
113 public DOMProxyListEntry create(ArrayList sources) {
114 return new Index(sources);
115 }
116
117 /** Method to test for the equality of two indexes.
118 * @param object The other index as an <strong>Object</strong>.
119 * @return A <i>boolean</i> which is <i>true</i> if the two indexes are equal, <i>false</i> otherwise.
120 */
121 public boolean equals(Object object) {
122 return (compareTo(object) == 0);
123 }
124
125 public Element getElement() {
126 return element;
127 }
128
129 public String getID() {
130 if(element == null) {
131 id="";
132 }
133 else if(id == null) {
134 StringBuffer id_buffer = new StringBuffer();
135 // Write data information. Retrieve each of the content sources and add them in a comma separated list.
136 ArrayList sources = getSources();
137 int sources_size = sources.size();
138 for(int i = 0; i < sources_size; i++) {
139 Object source_object = sources.get(i);
140 if (source_object instanceof MetadataElement) {
141 String full_element_name = ((MetadataElement)source_object).getFullName();
142 if(full_element_name.startsWith(StaticStrings.EXTRACTED_NAMESPACE) && full_element_name.indexOf(StaticStrings.NS_SEP, StaticStrings.EXTRACTED_NAMESPACE.length()) == -1) {
143 id_buffer.append(full_element_name.substring(StaticStrings.EXTRACTED_NAMESPACE.length()));
144 }
145 else {
146 id_buffer.append(full_element_name);
147 }
148 }
149 else {
150 id_buffer.append(source_object.toString());
151 }
152 id_buffer.append(StaticStrings.COMMA_CHARACTER);
153 }
154 sources = null;
155 if (id_buffer.length()==0) {
156 id = "";
157 } else {
158 id = id_buffer.substring(0, id_buffer.length() - 1);
159 }
160 }
161 return id;
162 }
163
164
165 /** Retrieve the sources of this index.
166 * @return the sources as an ArrayList
167 */
168 public ArrayList getSources() {
169 if(sources == null) {
170 sources = new ArrayList();
171 NodeList content_elements = element.getElementsByTagName(StaticStrings.CONTENT_ELEMENT);
172 int content_elements_length = content_elements.getLength();
173 for(int i = 0; i < content_elements_length; i++) {
174 Element content_element = (Element) content_elements.item(i);
175 String metadata_element_name_full = (String) content_element.getAttribute(StaticStrings.NAME_ATTRIBUTE);
176 MetadataElement metadata_element = MetadataTools.getMetadataElementWithName(metadata_element_name_full);
177 if (metadata_element != null) {
178 sources.add(metadata_element);
179 }
180 else {
181 sources.add(metadata_element_name_full);
182 }
183 }
184 content_elements = null;
185 }
186 return sources;
187 }
188
189 public boolean isAssigned() {
190 return (element != null && !element.getAttribute(StaticStrings.ASSIGNED_ATTRIBUTE).equals(StaticStrings.FALSE_STR));
191 }
192
193 public void setAssigned(boolean assigned) {
194 if(element != null) {
195 element.setAttribute(StaticStrings.ASSIGNED_ATTRIBUTE, (assigned ? StaticStrings.TRUE_STR : StaticStrings.FALSE_STR));
196 }
197 }
198
199 public void setElement(Element element) {
200 this.element = element;
201 this.id = null;
202 this.sources = null;
203 }
204
205 /** Method to set the sources for this index which can only be used for the default index.
206 * @param sources an ArrayList of source names
207 */
208 public void setSources(ArrayList sources) {
209 if(element != null && element.getNodeName().equals(getDefaultElementName())) {
210 // Erase old sources
211 XMLTools.clear(element);
212 // For each entry in the sources array add a new content element.
213 int size = sources.size();
214 for(int i = 0; i < size; i++) {
215 Element content_element = element.getOwnerDocument().createElement(StaticStrings.CONTENT_ELEMENT);
216 Object source_object = sources.get(i);
217 if (source_object instanceof MetadataElement) {
218 String name = ((MetadataElement) source_object).getFullName();
219 content_element.setAttribute(StaticStrings.NAME_ATTRIBUTE, name);
220 name = null;
221 }
222 else {
223 //DebugStream.println("Found String as source: " + source_object.toString());
224 content_element.setAttribute(StaticStrings.NAME_ATTRIBUTE, source_object.toString());
225 }
226 source_object = null;
227 element.appendChild(content_element);
228 content_element = null;
229 }
230 this.id = null; // Regenerate ID.
231 this.sources = sources;
232 }
233 else {
234 DebugStream.println("Error! Called setSource() of index other than the default.");
235 }
236 }
237
238 /** Method to turn this object into a string representation ready to be placed in the collection configuration file.
239 * @return A <strong>String</strong> containing the information of this class.
240 */
241 public String toString() {
242 StringBuffer text_buffer = new StringBuffer("");
243 // Generate language dependant id (include extracted metadata namespace)
244 // Write data information. Retrieve each of the content sources and add them in a comma separated list.
245 ArrayList sources = getSources();
246 int sources_size = sources.size();
247 for(int i = 0; i < sources_size; i++) {
248 String source_name = (sources.get(i)).toString();
249 text_buffer.append(source_name);
250 if(i < sources_size - 1) {
251 text_buffer.append(StaticStrings.COMMA_CHARACTER);
252 }
253 }
254 sources = null;
255 return text_buffer.toString();
256 }
257}
Note: See TracBrowser for help on using the repository browser.