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

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

added getType() method

  • Property svn:keywords set to Author Date Id Revision
File size: 9.2 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 /** used for determining type of associated collection meta
189 * should be overridden by subclasses */
190 public String getType() {
191 return SearchMeta.TYPE_INDEX;
192 }
193 public boolean isAssigned() {
194 return (element != null && !element.getAttribute(StaticStrings.ASSIGNED_ATTRIBUTE).equals(StaticStrings.FALSE_STR));
195 }
196
197 public void setAssigned(boolean assigned) {
198 if(element != null) {
199 element.setAttribute(StaticStrings.ASSIGNED_ATTRIBUTE, (assigned ? StaticStrings.TRUE_STR : StaticStrings.FALSE_STR));
200 }
201 }
202
203 public void setElement(Element element) {
204 this.element = element;
205 this.id = null;
206 this.sources = null;
207 }
208
209 /** Method to set the sources for this index which can only be used for the default index.
210 * @param sources an ArrayList of source names
211 */
212 public void setSources(ArrayList sources) {
213 if(element != null && element.getNodeName().equals(getDefaultElementName())) {
214 // Erase old sources
215 XMLTools.clear(element);
216 // For each entry in the sources array add a new content element.
217 int size = sources.size();
218 for(int i = 0; i < size; i++) {
219 Element content_element = element.getOwnerDocument().createElement(StaticStrings.CONTENT_ELEMENT);
220 Object source_object = sources.get(i);
221 if (source_object instanceof MetadataElement) {
222 String name = ((MetadataElement) source_object).getFullName();
223 content_element.setAttribute(StaticStrings.NAME_ATTRIBUTE, name);
224 name = null;
225 }
226 else {
227 //DebugStream.println("Found String as source: " + source_object.toString());
228 content_element.setAttribute(StaticStrings.NAME_ATTRIBUTE, source_object.toString());
229 }
230 source_object = null;
231 element.appendChild(content_element);
232 content_element = null;
233 }
234 this.id = null; // Regenerate ID.
235 this.sources = sources;
236 }
237 else {
238 DebugStream.println("Error! Called setSource() of index other than the default.");
239 }
240 }
241
242 /** Method to turn this object into a string representation ready to be placed in the collection configuration file.
243 * @return A <strong>String</strong> containing the information of this class.
244 */
245 public String toString() {
246 StringBuffer text_buffer = new StringBuffer("");
247 // Generate language dependant id (include extracted metadata namespace)
248 // Write data information. Retrieve each of the content sources and add them in a comma separated list.
249 ArrayList sources = getSources();
250 int sources_size = sources.size();
251 for(int i = 0; i < sources_size; i++) {
252 String source_name = (sources.get(i)).toString();
253 text_buffer.append(source_name);
254 if(i < sources_size - 1) {
255 text_buffer.append(StaticStrings.COMMA_CHARACTER);
256 }
257 }
258 sources = null;
259 return text_buffer.toString();
260 }
261}
Note: See TracBrowser for help on using the repository browser.