[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;
|
---|
[5590] | 28 |
|
---|
[4932] | 29 | import java.util.*;
|
---|
| 30 | import org.greenstone.gatherer.util.StaticStrings;
|
---|
[8015] | 31 | import org.greenstone.gatherer.util.XMLTools;
|
---|
[4932] | 32 | import org.w3c.dom.*;
|
---|
[5590] | 33 |
|
---|
[4932] | 34 | /** This class encapsulates a single indexing pair.
|
---|
| 35 | * @author John Thompson, Greenstone Digital Library, University of Waikato
|
---|
| 36 | * @version 2.4
|
---|
| 37 | */
|
---|
| 38 | public class SubcollectionIndex
|
---|
| 39 | implements Comparable, DOMProxyListEntry {
|
---|
| 40 |
|
---|
| 41 | private ArrayList sources = null;
|
---|
| 42 | /** The element this index is based upon. */
|
---|
| 43 | private Element element = null;
|
---|
| 44 | /** The unique, if cryptic, identifier of an index. */
|
---|
| 45 | private String id = null;
|
---|
| 46 |
|
---|
| 47 | /** Default constructor, which should only be used during DOMProxyListModel creation. */
|
---|
| 48 | public SubcollectionIndex() {
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | /** Constructor. */
|
---|
| 52 | public SubcollectionIndex(Element element) {
|
---|
| 53 | this.element = element;
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | public SubcollectionIndex(Object[] raw_sources) {
|
---|
| 57 | this.sources = new ArrayList();
|
---|
| 58 | // Create a new element
|
---|
[12798] | 59 | element = CollectionConfiguration.createElement(StaticStrings.INDEX_ELEMENT);
|
---|
[4932] | 60 | // For each source add a content element
|
---|
| 61 | int size = raw_sources.length;
|
---|
| 62 | for(int i = 0; i < size; i++) {
|
---|
| 63 | Subcollection subcollection = (Subcollection) raw_sources[i];
|
---|
| 64 | String subcollection_name = subcollection.getName();
|
---|
| 65 | sources.add(subcollection_name);
|
---|
[12798] | 66 | Element content_element = CollectionConfiguration.createElement(StaticStrings.CONTENT_ELEMENT);
|
---|
[12641] | 67 | content_element.setAttribute(StaticStrings.NAME_ATTRIBUTE, subcollection_name);
|
---|
[4932] | 68 | element.appendChild(content_element);
|
---|
| 69 | content_element = null;
|
---|
| 70 | }
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | /** Method to compare two indexes.
|
---|
| 74 | * @param object The other index as an <strong>Object</strong>.
|
---|
| 75 | * @return An <i>int</i> which indicates how the indexes compare.
|
---|
| 76 | * @see java.lang.String
|
---|
| 77 | */
|
---|
| 78 | public int compareTo(Object object) {
|
---|
[9161] | 79 | return getID().compareTo(((SubcollectionIndex)object).getID());
|
---|
[4932] | 80 | }
|
---|
| 81 |
|
---|
| 82 | public DOMProxyListEntry create(Element element) {
|
---|
| 83 | return new SubcollectionIndex(element);
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | /** Method to test for the equality of two indexes.
|
---|
| 87 | * @param object The other index as an <strong>Object</strong>.
|
---|
| 88 | * @return A <i>boolean</i> which is <i>true</i> if the two indexes are equal, <i>false</i> otherwise.
|
---|
| 89 | */
|
---|
| 90 | public boolean equals(Object object) {
|
---|
| 91 | return (compareTo(object) == 0);
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | public Element getElement() {
|
---|
| 95 | return element;
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | public String getID() {
|
---|
| 99 | if(id == null) {
|
---|
| 100 | StringBuffer id_buffer = new StringBuffer();
|
---|
| 101 | // Write data information. Retrieve each of the content sources and add them in a comma separated list.
|
---|
| 102 | ArrayList sources = getSources();
|
---|
| 103 | int sources_size = sources.size();
|
---|
| 104 | for(int i = 0; i < sources_size; i++) {
|
---|
| 105 | id_buffer.append((String)sources.get(i));
|
---|
| 106 | id_buffer.append(StaticStrings.COMMA_CHARACTER);
|
---|
| 107 | }
|
---|
| 108 | sources = null;
|
---|
| 109 | id = id_buffer.substring(0, id_buffer.length() - 1);
|
---|
| 110 | }
|
---|
| 111 | return id;
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 | /** Retrieve the sources of this index.
|
---|
| 115 | * @return the sources as an ArrayList
|
---|
| 116 | */
|
---|
| 117 | public ArrayList getSources() {
|
---|
| 118 | if(sources == null) {
|
---|
| 119 | sources = new ArrayList();
|
---|
[12641] | 120 | NodeList content_elements = element.getElementsByTagName(StaticStrings.CONTENT_ELEMENT);
|
---|
[4932] | 121 | int content_elements_length = content_elements.getLength();
|
---|
| 122 | for(int i = 0; i < content_elements_length; i++) {
|
---|
| 123 | Element content_element = (Element) content_elements.item(i);
|
---|
[12641] | 124 | sources.add(content_element.getAttribute(StaticStrings.NAME_ATTRIBUTE));
|
---|
[4932] | 125 | }
|
---|
| 126 | content_elements = null;
|
---|
| 127 | Collections.sort(sources);
|
---|
| 128 | }
|
---|
| 129 | return sources;
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | public boolean isAssigned() {
|
---|
[12641] | 133 | return (element != null && !element.getAttribute(StaticStrings.ASSIGNED_ATTRIBUTE).equals(StaticStrings.FALSE_STR));
|
---|
[4932] | 134 | }
|
---|
| 135 |
|
---|
| 136 | public void setAssigned(boolean assigned) {
|
---|
| 137 | if(element != null) {
|
---|
[12641] | 138 | element.setAttribute(StaticStrings.ASSIGNED_ATTRIBUTE, (assigned ? StaticStrings.TRUE_STR : StaticStrings.FALSE_STR));
|
---|
[4932] | 139 | }
|
---|
| 140 | }
|
---|
| 141 |
|
---|
| 142 | public void setElement(Element element) {
|
---|
| 143 | this.element = element;
|
---|
| 144 | this.id = null;
|
---|
| 145 | this.sources = null;
|
---|
| 146 | }
|
---|
| 147 |
|
---|
| 148 | /** Method to set the sources for this index which can only be used for the default index.
|
---|
| 149 | * @param sources an ArrayList of source names
|
---|
| 150 | */
|
---|
| 151 | public void setSources(ArrayList sources) {
|
---|
| 152 | if(element != null) {
|
---|
| 153 | // Erase old sources
|
---|
[8015] | 154 | XMLTools.clear(element);
|
---|
[4932] | 155 | // For each entry in the sources array add a new content element.
|
---|
| 156 | int size = sources.size();
|
---|
| 157 | for(int i = 0; i < size; i++) {
|
---|
[12641] | 158 | Element content_element = element.getOwnerDocument().createElement(StaticStrings.CONTENT_ELEMENT);
|
---|
| 159 | content_element.setAttribute(StaticStrings.NAME_ATTRIBUTE, (String) sources.get(i));
|
---|
[4932] | 160 | element.appendChild(content_element);
|
---|
| 161 | content_element = null;
|
---|
| 162 | }
|
---|
| 163 | this.id = null; // Regenerate ID.
|
---|
| 164 | this.sources = sources;
|
---|
| 165 | }
|
---|
| 166 | }
|
---|
| 167 |
|
---|
| 168 | /** Method to turn this object into a string representation ready to be placed in the collection configuration file.
|
---|
| 169 | * @return A <strong>String</strong> containing the information of this class.
|
---|
| 170 | */
|
---|
[12090] | 171 |
|
---|
[4932] | 172 | public String toString() {
|
---|
[12090] | 173 | return getID();
|
---|
[4932] | 174 | }
|
---|
| 175 | }
|
---|