source: other-projects/FileTransfer-WebSocketPair/testGXTWithGreenstone/src/org/greenstone/gatherer/cdm/SubcollectionIndex.java@ 33053

Last change on this file since 33053 was 33053, checked in by ak19, 5 years ago

I still had some stuff of Nathan Kelly's (FileTransfer-WebSocketPair) sitting on my USB. Had already commited the Themes folder at the time, 2 years back. Not sure if he wanted this additional folder commited. But I didn't want to delete it and decided it will be better off on SVN. When we use his project, if we find we didn't need this test folder, we can remove it from svn then.

File size: 6.3 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.util.StaticStrings;
31import org.greenstone.gatherer.util.XMLTools;
32import org.w3c.dom.*;
33
34/** This class encapsulates a single indexing pair.
35 * @author John Thompson, Greenstone Digital Library, University of Waikato
36 * @version 2.4
37 */
38public 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
59 element = CollectionConfiguration.createElement(StaticStrings.INDEX_ELEMENT);
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);
66 Element content_element = CollectionConfiguration.createElement(StaticStrings.CONTENT_ELEMENT);
67 content_element.setAttribute(StaticStrings.NAME_ATTRIBUTE, subcollection_name);
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) {
79 return getID().compareTo(((SubcollectionIndex)object).getID());
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();
120 // for the last entry in the assigned subcollection partitions, element is null
121 // why this is null for the last entry?
122 if(element==null){
123 return sources;
124 }
125 NodeList content_elements = element.getElementsByTagName(StaticStrings.CONTENT_ELEMENT);
126 int content_elements_length = content_elements.getLength();
127 for(int i = 0; i < content_elements_length; i++) {
128 Element content_element = (Element) content_elements.item(i);
129 sources.add(content_element.getAttribute(StaticStrings.NAME_ATTRIBUTE));
130 }
131 content_elements = null;
132 Collections.sort(sources);
133 }
134 return sources;
135 }
136
137 public boolean isAssigned() {
138 return (element != null && !element.getAttribute(StaticStrings.ASSIGNED_ATTRIBUTE).equals(StaticStrings.FALSE_STR));
139 }
140
141 public void setAssigned(boolean assigned) {
142 if(element != null) {
143 element.setAttribute(StaticStrings.ASSIGNED_ATTRIBUTE, (assigned ? StaticStrings.TRUE_STR : StaticStrings.FALSE_STR));
144 }
145 }
146
147 public void setElement(Element element) {
148 this.element = element;
149 this.id = null;
150 this.sources = null;
151 }
152
153 /** Method to set the sources for this index which can only be used for the default index.
154 * @param sources an ArrayList of source names
155 */
156 public void setSources(ArrayList sources) {
157 if(element != null) {
158 // Erase old sources
159 XMLTools.clear(element);
160 // For each entry in the sources array add a new content element.
161 int size = sources.size();
162 for(int i = 0; i < size; i++) {
163 Element content_element = element.getOwnerDocument().createElement(StaticStrings.CONTENT_ELEMENT);
164 content_element.setAttribute(StaticStrings.NAME_ATTRIBUTE, (String) sources.get(i));
165 element.appendChild(content_element);
166 content_element = null;
167 }
168 this.id = null; // Regenerate ID.
169 this.sources = sources;
170 }
171 }
172
173 /** Method to turn this object into a string representation ready to be placed in the collection configuration file.
174 * @return A <strong>String</strong> containing the information of this class.
175 */
176
177 public String toString() {
178 return getID();
179 }
180}
Note: See TracBrowser for help on using the repository browser.