source: trunk/gli/src/org/greenstone/gatherer/cdm/SubcollectionIndex.java@ 12749

Last change on this file since 12749 was 12641, checked in by mdewsnip, 18 years ago

Changed all access to the static strings through CollectionConfiguration to directly use StaticStrings.

  • Property svn:keywords set to Author Date Id Revision
File size: 6.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.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 Document document = CollectionDesignManager.collect_config.document;
60 element = document.createElement(StaticStrings.INDEX_ELEMENT);
61 // For each source add a content element
62 int size = raw_sources.length;
63 for(int i = 0; i < size; i++) {
64 Subcollection subcollection = (Subcollection) raw_sources[i];
65 String subcollection_name = subcollection.getName();
66 sources.add(subcollection_name);
67 Element content_element = document.createElement(StaticStrings.CONTENT_ELEMENT);
68 content_element.setAttribute(StaticStrings.NAME_ATTRIBUTE, subcollection_name);
69 element.appendChild(content_element);
70 content_element = null;
71 }
72 document = null;
73 }
74
75 /** Method to compare two indexes.
76 * @param object The other index as an <strong>Object</strong>.
77 * @return An <i>int</i> which indicates how the indexes compare.
78 * @see java.lang.String
79 */
80 public int compareTo(Object object) {
81 return getID().compareTo(((SubcollectionIndex)object).getID());
82 }
83
84 public DOMProxyListEntry create(Element element) {
85 return new SubcollectionIndex(element);
86 }
87
88 /** Method to test for the equality of two indexes.
89 * @param object The other index as an <strong>Object</strong>.
90 * @return A <i>boolean</i> which is <i>true</i> if the two indexes are equal, <i>false</i> otherwise.
91 */
92 public boolean equals(Object object) {
93 return (compareTo(object) == 0);
94 }
95
96 public Element getElement() {
97 return element;
98 }
99
100 public String getID() {
101 if(id == null) {
102 StringBuffer id_buffer = new StringBuffer();
103 // Write data information. Retrieve each of the content sources and add them in a comma separated list.
104 ArrayList sources = getSources();
105 int sources_size = sources.size();
106 for(int i = 0; i < sources_size; i++) {
107 id_buffer.append((String)sources.get(i));
108 id_buffer.append(StaticStrings.COMMA_CHARACTER);
109 }
110 sources = null;
111 id = id_buffer.substring(0, id_buffer.length() - 1);
112 }
113 return id;
114 }
115
116 /** Retrieve the sources of this index.
117 * @return the sources as an ArrayList
118 */
119 public ArrayList getSources() {
120 if(sources == null) {
121 sources = new ArrayList();
122 NodeList content_elements = element.getElementsByTagName(StaticStrings.CONTENT_ELEMENT);
123 int content_elements_length = content_elements.getLength();
124 for(int i = 0; i < content_elements_length; i++) {
125 Element content_element = (Element) content_elements.item(i);
126 sources.add(content_element.getAttribute(StaticStrings.NAME_ATTRIBUTE));
127 }
128 content_elements = null;
129 Collections.sort(sources);
130 }
131 return sources;
132 }
133
134 public boolean isAssigned() {
135 return (element != null && !element.getAttribute(StaticStrings.ASSIGNED_ATTRIBUTE).equals(StaticStrings.FALSE_STR));
136 }
137
138 public void setAssigned(boolean assigned) {
139 if(element != null) {
140 element.setAttribute(StaticStrings.ASSIGNED_ATTRIBUTE, (assigned ? StaticStrings.TRUE_STR : StaticStrings.FALSE_STR));
141 }
142 }
143
144 public void setElement(Element element) {
145 this.element = element;
146 this.id = null;
147 this.sources = null;
148 }
149
150 /** Method to set the sources for this index which can only be used for the default index.
151 * @param sources an ArrayList of source names
152 */
153 public void setSources(ArrayList sources) {
154 if(element != null) {
155 // Erase old sources
156 XMLTools.clear(element);
157 // For each entry in the sources array add a new content element.
158 int size = sources.size();
159 for(int i = 0; i < size; i++) {
160 Element content_element = element.getOwnerDocument().createElement(StaticStrings.CONTENT_ELEMENT);
161 content_element.setAttribute(StaticStrings.NAME_ATTRIBUTE, (String) sources.get(i));
162 element.appendChild(content_element);
163 content_element = null;
164 }
165 this.id = null; // Regenerate ID.
166 this.sources = sources;
167 }
168 }
169
170 /** Method to turn this object into a string representation ready to be placed in the collection configuration file.
171 * @return A <strong>String</strong> containing the information of this class.
172 */
173
174 public String toString() {
175 return getID();
176 }
177}
Note: See TracBrowser for help on using the repository browser.