[4932] | 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.*;
|
---|
[8236] | 30 | import org.greenstone.gatherer.DebugStream;
|
---|
[4932] | 31 | import org.greenstone.gatherer.Gatherer;
|
---|
[8313] | 32 | import org.greenstone.gatherer.metadata.MetadataElement;
|
---|
| 33 | import org.greenstone.gatherer.metadata.MetadataTools;
|
---|
[4932] | 34 | import org.greenstone.gatherer.util.StaticStrings;
|
---|
[8015] | 35 | import org.greenstone.gatherer.util.XMLTools;
|
---|
[4932] | 36 | import org.w3c.dom.*;
|
---|
[5590] | 37 |
|
---|
[36138] | 38 | /** This class encapsulates a single index. MGPP/Lucene/SOLR
|
---|
| 39 | * Can be subclassed for Sort fields and facet fields
|
---|
[4932] | 40 | * @author John Thompson, Greenstone Digital Library, University of Waikato
|
---|
| 41 | * @version 2.3d
|
---|
| 42 | */
|
---|
| 43 | public class Index
|
---|
| 44 | implements Comparable, DOMProxyListEntry {
|
---|
| 45 |
|
---|
[36138] | 46 | protected ArrayList sources = null;
|
---|
| 47 |
|
---|
[4932] | 48 | /** The element this index is based upon. */
|
---|
[36138] | 49 | protected Element element = null;
|
---|
| 50 | /** The name for our elements */
|
---|
[36151] | 51 | static final String index_element = StaticStrings.INDEX_ELEMENT;
|
---|
[36138] | 52 | /** The name for the default element */
|
---|
[36151] | 53 | static final String default_index_element = StaticStrings.INDEX_DEFAULT_ELEMENT;
|
---|
[4932] | 54 | /** The unique, if cryptic, identifier of an index. */
|
---|
[36138] | 55 | protected String id = null;
|
---|
[4932] | 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
|
---|
[36151] | 70 | this.element = CollectionConfiguration.createElement(getElementName());
|
---|
[4932] | 71 | // For each source add a content element
|
---|
| 72 | int size = sources.size();
|
---|
| 73 | for(int i = 0; i < size; i++) {
|
---|
[12798] | 74 | Element content_element = CollectionConfiguration.createElement(StaticStrings.CONTENT_ELEMENT);
|
---|
[4932] | 75 | Object source_object = sources.get(i);
|
---|
[8313] | 76 | if (source_object instanceof MetadataElement) {
|
---|
| 77 | // System.err.println("Constructing new Index with MetadataElement source...");
|
---|
[12641] | 78 | content_element.setAttribute(StaticStrings.NAME_ATTRIBUTE, ((MetadataElement) source_object).getFullName());
|
---|
[4932] | 79 | }
|
---|
| 80 | else {
|
---|
[8313] | 81 | // System.err.println("Constructing new Index with String source...");
|
---|
[12641] | 82 | content_element.setAttribute(StaticStrings.NAME_ATTRIBUTE, source_object.toString());
|
---|
[4932] | 83 | }
|
---|
| 84 | element.appendChild(content_element);
|
---|
| 85 | content_element = null;
|
---|
| 86 | }
|
---|
| 87 | }
|
---|
| 88 |
|
---|
[36151] | 89 | protected String getElementName() {
|
---|
| 90 | return index_element;
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | protected String getDefaultElementName() {
|
---|
| 94 | return default_index_element;
|
---|
| 95 | }
|
---|
| 96 |
|
---|
[4932] | 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) {
|
---|
[5037] | 103 | if(object == null) {
|
---|
| 104 | return -1;
|
---|
| 105 | }
|
---|
| 106 | String id = getID();
|
---|
[4932] | 107 | return id.compareTo(((Index)object).getID());
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | public DOMProxyListEntry create(Element element) {
|
---|
| 111 | return new Index(element);
|
---|
| 112 | }
|
---|
[36151] | 113 | public DOMProxyListEntry create(ArrayList sources) {
|
---|
| 114 | return new Index(sources);
|
---|
| 115 | }
|
---|
[4932] | 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() {
|
---|
[5159] | 130 | if(element == null) {
|
---|
| 131 | id="";
|
---|
| 132 | }
|
---|
| 133 | else if(id == null) {
|
---|
[4932] | 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);
|
---|
[8313] | 140 | if (source_object instanceof MetadataElement) {
|
---|
| 141 | String full_element_name = ((MetadataElement)source_object).getFullName();
|
---|
[24367] | 142 | if(full_element_name.startsWith(StaticStrings.EXTRACTED_NAMESPACE) && full_element_name.indexOf(StaticStrings.NS_SEP, StaticStrings.EXTRACTED_NAMESPACE.length()) == -1) {
|
---|
[7624] | 143 | id_buffer.append(full_element_name.substring(StaticStrings.EXTRACTED_NAMESPACE.length()));
|
---|
[5037] | 144 | }
|
---|
| 145 | else {
|
---|
| 146 | id_buffer.append(full_element_name);
|
---|
| 147 | }
|
---|
[4932] | 148 | }
|
---|
| 149 | else {
|
---|
| 150 | id_buffer.append(source_object.toString());
|
---|
| 151 | }
|
---|
| 152 | id_buffer.append(StaticStrings.COMMA_CHARACTER);
|
---|
| 153 | }
|
---|
| 154 | sources = null;
|
---|
[10964] | 155 | if (id_buffer.length()==0) {
|
---|
| 156 | id = "";
|
---|
| 157 | } else {
|
---|
| 158 | id = id_buffer.substring(0, id_buffer.length() - 1);
|
---|
| 159 | }
|
---|
[4932] | 160 | }
|
---|
| 161 | return id;
|
---|
| 162 | }
|
---|
| 163 |
|
---|
[5375] | 164 |
|
---|
[4932] | 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();
|
---|
[12641] | 171 | NodeList content_elements = element.getElementsByTagName(StaticStrings.CONTENT_ELEMENT);
|
---|
[4932] | 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);
|
---|
[12641] | 175 | String metadata_element_name_full = (String) content_element.getAttribute(StaticStrings.NAME_ATTRIBUTE);
|
---|
[10964] | 176 | MetadataElement metadata_element = MetadataTools.getMetadataElementWithName(metadata_element_name_full);
|
---|
[8313] | 177 | if (metadata_element != null) {
|
---|
| 178 | sources.add(metadata_element);
|
---|
[5037] | 179 | }
|
---|
| 180 | else {
|
---|
[8313] | 181 | sources.add(metadata_element_name_full);
|
---|
[5037] | 182 | }
|
---|
[4932] | 183 | }
|
---|
| 184 | content_elements = null;
|
---|
| 185 | }
|
---|
| 186 | return sources;
|
---|
| 187 | }
|
---|
[36184] | 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 | }
|
---|
[4932] | 193 | public boolean isAssigned() {
|
---|
[12641] | 194 | return (element != null && !element.getAttribute(StaticStrings.ASSIGNED_ATTRIBUTE).equals(StaticStrings.FALSE_STR));
|
---|
[4932] | 195 | }
|
---|
| 196 |
|
---|
| 197 | public void setAssigned(boolean assigned) {
|
---|
| 198 | if(element != null) {
|
---|
[12641] | 199 | element.setAttribute(StaticStrings.ASSIGNED_ATTRIBUTE, (assigned ? StaticStrings.TRUE_STR : StaticStrings.FALSE_STR));
|
---|
[4932] | 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) {
|
---|
[36151] | 213 | if(element != null && element.getNodeName().equals(getDefaultElementName())) {
|
---|
[4932] | 214 | // Erase old sources
|
---|
[8015] | 215 | XMLTools.clear(element);
|
---|
[4932] | 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++) {
|
---|
[12641] | 219 | Element content_element = element.getOwnerDocument().createElement(StaticStrings.CONTENT_ELEMENT);
|
---|
[5159] | 220 | Object source_object = sources.get(i);
|
---|
[8313] | 221 | if (source_object instanceof MetadataElement) {
|
---|
| 222 | String name = ((MetadataElement) source_object).getFullName();
|
---|
[12641] | 223 | content_element.setAttribute(StaticStrings.NAME_ATTRIBUTE, name);
|
---|
[5159] | 224 | name = null;
|
---|
| 225 | }
|
---|
| 226 | else {
|
---|
[8236] | 227 | //DebugStream.println("Found String as source: " + source_object.toString());
|
---|
[12641] | 228 | content_element.setAttribute(StaticStrings.NAME_ATTRIBUTE, source_object.toString());
|
---|
[5159] | 229 | }
|
---|
| 230 | source_object = null;
|
---|
[4932] | 231 | element.appendChild(content_element);
|
---|
| 232 | content_element = null;
|
---|
| 233 | }
|
---|
| 234 | this.id = null; // Regenerate ID.
|
---|
| 235 | this.sources = sources;
|
---|
| 236 | }
|
---|
| 237 | else {
|
---|
[8236] | 238 | DebugStream.println("Error! Called setSource() of index other than the default.");
|
---|
[4932] | 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() {
|
---|
[5037] | 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();
|
---|
[4932] | 260 | }
|
---|
| 261 | }
|
---|