source: trunk/gsdl3/src/java/org/greenstone/gsdl3/gs3build/classifier/AbstractHierarchyNode.java@ 6288

Last change on this file since 6288 was 6288, checked in by cs025, 20 years ago

Various changes

  • Property svn:keywords set to Author Date Id Revision
File size: 8.1 KB
Line 
1package org.greenstone.gsdl3.gs3build.classifier;
2
3import java.util.List;
4import java.util.ArrayList;
5import java.util.Iterator;
6
7import java.sql.ResultSet;
8import java.sql.SQLException;
9
10import org.greenstone.gsdl3.gs3build.doctypes.DocumentID;
11
12import org.greenstone.gsdl3.gs3build.util.GS3SQLConnection;
13import org.greenstone.gsdl3.gs3build.database.*;
14
15public abstract class AbstractHierarchyNode
16{
17 String prefix; // a basic prefix used in this hierarchy
18 String descriptor; // the textual descriptor used on screen or long-hand
19 String name; // the index number, letter assignment or other item
20 // used to identify the position of the item in the
21 // hierarchy
22 String id; // an identifier used by the GLI for maintenance
23 // purposes; this plays no active role in the
24 // rebuilding process (at the moment)
25 List childNodes; // the child classification nodes of this node
26 List childDocs; // the child documents of this node
27 List matches; // the other metadata values that may be matched
28 // against the classifier
29 AbstractHierarchyNode parent; // the parent of the node
30
31 /**
32 * Simple node
33 */
34 public AbstractHierarchyNode()
35 { this.descriptor = null;
36 this.name = null;
37 this.id = null;
38 this.prefix = null;
39 this.childNodes = new ArrayList();
40 this.childDocs = new ArrayList();
41 this.parent = null;
42 this.matches = new ArrayList();
43 }
44
45 public AbstractHierarchyNode(String prefix, String name, String id, String descriptor)
46 { this.descriptor = descriptor;
47 this.name = name;
48 this.id = id;
49 this.prefix = prefix;
50 this.childNodes = new ArrayList();
51 this.childDocs = new ArrayList();
52 this.parent = null;
53 this.matches = new ArrayList();
54 this.matches.add(this.id);
55 }
56
57 public void addChild(AbstractHierarchyNode child)
58 { this.childNodes.add(child);
59 child.setParent(this);
60
61 if (this.id == null) {
62 System.out.println(child.id.toString() + " added to root");
63 }
64 else {
65 System.out.println(child.id.toString() + " added to " + this.id);
66 }
67 }
68
69 public boolean add(AbstractHierarchyNode child)
70 { if (this.id != null &&
71 !child.id.startsWith(this.id + "."))
72 { return false;
73 }
74
75 Iterator subNodes = this.childNodes.iterator();
76 while (subNodes.hasNext()) {
77 AbstractHierarchyNode subNode = (AbstractHierarchyNode) subNodes.next();
78
79 if (subNode.add(child)) {
80 return true;
81 }
82 }
83
84 this.addChild(child);
85 return true;
86 }
87
88 public void addDocument(DocumentID document)
89 { this.childDocs.add(document);
90 }
91
92 public void setParent(AbstractHierarchyNode parent)
93 { this.parent = parent;
94 }
95
96 public AbstractHierarchyNode getParent()
97 { return this.parent;
98 }
99
100 public String getParentId()
101 {
102 if (this.id == null) {
103 return "";
104 }
105
106 int dotAt = this.id.lastIndexOf('.');
107 if (dotAt < 0) {
108 return "";
109 }
110 return this.id.substring(0, dotAt);
111 }
112
113 public void setDescriptor(String descriptor)
114 { this.descriptor = descriptor;
115 }
116
117 public void setID(String id)
118 { this.id = id;
119 this.matches.add(id);
120 }
121
122 public String getID()
123 { return this.id;
124 }
125
126 /**
127 * Set the name for this hierarchy node - i.e. its brief description
128 *
129 * @param <code>String</code> the new node name.
130 */
131 public void setName(String name)
132 { this.name = name;
133 }
134
135 /**
136 * Get the name for this hierarchy node - i.e. its brief description
137 *
138 * @return <code>String</code> the node's name.
139 */
140 public String getName()
141 { return this.name;
142 }
143
144 /**
145 * Add another string which can be used to describe this hierarchy node - i.e. one which if
146 * found in a document would indicate that that document belongs to this hierarchy node.
147 *
148 * @param <code>String</code> the string to match.
149 */
150 public void addMatch(String match)
151 { this.matches.add(match);
152 }
153
154 /**
155 * Check if a given string matches any of the values given for this hierarchy node...
156 *
157 * @param <code>String</code> the descriptive string to be compared against the hierarchy
158 * @return <code>boolean</code> <code>true</code> whether the string matches this hierarchy
159 */
160 public boolean isMatch (String toMatch)
161 { Iterator thisMatch = this.matches.iterator();
162
163 while (thisMatch.hasNext())
164 { String thisMatchText = thisMatch.next().toString();
165
166 if (thisMatchText.equals(toMatch))
167 { return true;
168 }
169 }
170 return false;
171 }
172
173 /**
174 * Take a document, and find the classifications that it matches against in
175 * the current hierarchy.
176 *
177 * @param <code>DocumentID</code> the id of the document being classified
178 * @param <code>List</code> the values against which the classifier should
179 * test for the document being a match - i.e. the pertinent document
180 * property values.
181 * @param <code>ClassifierObserverInterface</code>
182 * object modifies the document with information about the
183 * classifications that it fell within.
184 */
185 abstract public void getClassifications(DocumentID documentID, List values,
186 ClassifierObserverInterface observer);
187
188 public boolean writeSQL(GS3SQLConnection connection)
189 {
190 int classifyRef;
191 GS3SQLAction action;
192 GS3SQLSelect select;
193 GS3SQLInsert insert;
194
195 // check for an existing instance of this classifier
196 select = new GS3SQLSelect("classifiers");
197 select.addField("ClassifyRef");
198 GS3SQLWhereItem whereItem = new GS3SQLWhereItem("ClassifyID", "=", this.prefix+"."+this.id);
199 GS3SQLWhere where = new GS3SQLWhere(whereItem);
200 select.setWhere(where);
201
202 connection.execute(select.toString());
203
204 // update or insert the classifier as required
205 try {
206 ResultSet results = connection.getResultSet();
207 if (results != null && results.first()) {
208 GS3SQLUpdate update = new GS3SQLUpdate("classifiers");
209 update.setWhere(where);
210 action = update;
211
212 classifyRef = results.getInt("ClassifyRef");
213 }
214 else {
215 insert = new GS3SQLInsert("classifiers");
216
217 String parentId = this.getParentId();
218
219 if (parentId.length() > 0) {
220 insert.addValue("ParentID", this.prefix+"."+this.getParentId());
221 }
222 else {
223 insert.addValue("ParentID", this.prefix);
224 }
225
226 action = insert;
227 }
228 action.addValue("ClassifyID", this.prefix+"."+this.id);
229 action.addValue("Name", this.name);
230 action.addValue("Description", this.descriptor);
231
232 connection.execute(action.toString());
233 classifyRef = -1;
234 }
235 catch (SQLException sqlEx) {
236 System.err.println(sqlEx);
237 return false;
238 }
239
240 // get the ClassifyRef if we don't already have it (have done a
241 // insert action above)...
242 if (classifyRef == -1) {
243 connection.execute(select.toString());
244
245 try {
246 ResultSet results = connection.getResultSet();
247 if (results == null || !results.first()) {
248 return false;
249 }
250
251 classifyRef = results.getInt("ClassifyRef");
252 }
253 catch (SQLException sqlEx) {
254 System.err.println(sqlEx);
255 return false;
256 }
257 }
258 else {
259 // TODO: clear 'dead' child classifications
260
261 // delete child documents
262 GS3SQLDelete delete = new GS3SQLDelete("classdocuments");
263 delete.setWhere(where);
264
265 connection.execute(delete.toString());
266 }
267
268 // post the child nodes...
269 Iterator iterator = this.childNodes.iterator();
270 while (iterator.hasNext()) {
271 AbstractHierarchyNode childNode = (AbstractHierarchyNode) iterator.next();
272
273 if (!childNode.writeSQL(connection)) {
274 return false;
275 }
276 }
277
278 // note the child documents...
279 iterator = this.childDocs.iterator();
280 while (iterator.hasNext()) {
281 DocumentID docId = (DocumentID) iterator.next();
282 insert = new GS3SQLInsert("classdocuments");
283 insert.addValue("ClassifyRef", Integer.toString(classifyRef), GS3SQLField.INTEGER_TYPE);
284 insert.addValue("DocID", docId.toString());
285
286 connection.execute(insert.toString());
287 }
288 return true;
289 }
290}
Note: See TracBrowser for help on using the repository browser.