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