KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > knowgate > hipergate > Thesauri


1 /*
2   Copyright (C) 2003 Know Gate S.L. All rights reserved.
3                       C/Oņa, 107 1š2 28050 Madrid (Spain)
4
5   Redistribution and use in source and binary forms, with or without
6   modification, are permitted provided that the following conditions
7   are met:
8
9   1. Redistributions of source code must retain the above copyright
10      notice, this list of conditions and the following disclaimer.
11
12   2. The end-user documentation included with the redistribution,
13      if any, must include the following acknowledgment:
14      "This product includes software parts from hipergate
15      (http://www.hipergate.org/)."
16      Alternately, this acknowledgment may appear in the software itself,
17      if and wherever such third-party acknowledgments normally appear.
18
19   3. The name hipergate must not be used to endorse or promote products
20      derived from this software without prior written permission.
21      Products derived from this software may not be called hipergate,
22      nor may hipergate appear in their name, without prior written
23      permission.
24
25   This library is distributed in the hope that it will be useful,
26   but WITHOUT ANY WARRANTY; without even the implied warranty of
27   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
28
29   You should have received a copy of hipergate License with this code;
30   if not, visit http://www.hipergate.org or mail to info@hipergate.org
31 */

32
33 package com.knowgate.hipergate;
34
35 import java.sql.SQLException JavaDoc;
36 import java.sql.PreparedStatement JavaDoc;
37 import java.sql.ResultSet JavaDoc;
38
39 import com.knowgate.debug.DebugFile;
40 import com.knowgate.dataobjs.DB;
41 import com.knowgate.dataobjs.DBBind;
42 import com.knowgate.jdc.JDCConnection;
43 import com.knowgate.misc.Gadgets;
44
45 /**
46  * @author Sergio Montoro Ten
47  * @version 1.1
48  */

49
50 public class Thesauri {
51
52   public Thesauri() {
53   }
54
55   /**
56    * <p>Create Root Term</p>
57    * @param oConn Database Connection
58    * @param sTxTerm Term Text Singular
59    * @param sTxTerm Term Text Plural
60    * @param sDeTerm Term Contextual Description
61    * @param sIdLanguage Language
62    * @param sIdScope Scope
63    * @param iIdDomain Domain Numeric Identifier (from k_domains)
64    * @param sGuWorkArea WorkArea GUID
65    * @return GUID for new term
66    * @throws SQLException
67    */

68   public static String JavaDoc createRootTerm(JDCConnection oConn, String JavaDoc sTxTerm, String JavaDoc sTxTermPlural, String JavaDoc sDeTerm, String JavaDoc sIdLanguage, String JavaDoc sIdScope, int iIdDomain, String JavaDoc sGuWorkArea) throws SQLException JavaDoc {
69     PreparedStatement JavaDoc oStmt;
70     String JavaDoc sGuRootTerm = Gadgets.generateUUID();
71     int iNextVal;
72
73     if (DebugFile.trace) {
74       DebugFile.writeln("Begin Thesauri.createRootTerm([Connection]," + sTxTerm + "," + sTxTermPlural + "," + sDeTerm + "," + sIdLanguage + "," + sIdScope + String.valueOf(iIdDomain) + "," + sGuWorkArea + ")");
75       DebugFile.incIdent();
76       DebugFile.writeln("Connection.prepareStatement(INSERT INTO " + DB.k_thesauri_root + " (" + DB.gu_rootterm + "," + DB.tx_term + "," + DB.tx_term + "2," + DB.id_domain + "," + DB.gu_workarea + "," + DB.id_scope + ") VALUES ('" + sGuRootTerm + "','" + sTxTerm + "'," + String.valueOf(iIdDomain) + ",'" + sGuWorkArea + "','" + sIdScope + "'))");
77     }
78
79     oStmt = oConn.prepareStatement("INSERT INTO " + DB.k_thesauri_root + " (" + DB.gu_rootterm + "," + DB.tx_term + "," + DB.id_domain + "," + DB.gu_workarea + "," + DB.id_scope + ") VALUES (?,?,?,?,?)");
80     oStmt.setString(1, sGuRootTerm);
81     oStmt.setString(2, sTxTerm);
82     oStmt.setInt(3, iIdDomain);
83     oStmt.setString(4, sGuWorkArea);
84     oStmt.setString(5, sIdScope);
85     oStmt.executeUpdate();
86     oStmt.close();
87
88     iNextVal = DBBind.nextVal(oConn, "seq_thesauri");
89
90     if (DebugFile.trace)
91       DebugFile.writeln("Connection.prepareStatement(INSERT INTO " + DB.k_thesauri + " (" + DB.gu_rootterm + "," + DB.gu_term + "," + DB.tx_term + "," + DB.tx_term + "2," + DB.id_language + "," + DB.de_term + "," + DB.id_scope + "," + DB.id_domain + "," + DB.id_term + "0) VALUES ('" + sGuRootTerm + "','" + sGuRootTerm + "','" + sTxTerm + "','" + sIdLanguage + "','" + sDeTerm + "','" + sIdScope + "'," + String.valueOf(iIdDomain) + "," + String.valueOf(iNextVal) + "))");
92
93     oStmt = oConn.prepareStatement("INSERT INTO " + DB.k_thesauri + " (" + DB.gu_rootterm + "," + DB.gu_term + "," + DB.tx_term + "," + DB.tx_term + "2," + DB.id_language + "," + DB.de_term + "," + DB.id_scope + "," + DB.id_domain + "," + DB.id_term + "0) VALUES (?,?,?,?,?,?,?,?,?)");
94     oStmt.setString(1, sGuRootTerm);
95     oStmt.setString(2, sGuRootTerm);
96     oStmt.setString(3, sTxTerm);
97     oStmt.setString(4, sTxTermPlural);
98     oStmt.setString(5, sIdLanguage);
99     oStmt.setString(6, sDeTerm);
100     oStmt.setString(7, sIdScope);
101     oStmt.setInt(8, iIdDomain);
102     oStmt.setInt(9, iNextVal);
103     oStmt.executeUpdate();
104     oStmt.close();
105
106     if (DebugFile.trace) {
107       DebugFile.decIdent();
108       DebugFile.writeln("End Thesauri.createRootTerm() : " + sGuRootTerm);
109     }
110
111     return sGuRootTerm;
112   } // createRootTerm
113

114
115   /**
116    * <p>Create Term</p>
117    * @param oConn Database Connection
118    * @param sGuParent Parent Term
119    * @param sTxTerm Term Text Singular
120    * @param sTxTermPlural Term Text Plural
121    * @param sDeTerm Term Contextual Description
122    * @param sIdLanguage Language
123    * @param sIdScope Scope
124    * @param iIdDomain Domain Numeric Identifier (from k_domains)
125    * @return GUID for new term
126    * @throws SQLException
127    */

128   public static String JavaDoc createTerm(JDCConnection oConn, String JavaDoc sGuParent, String JavaDoc sTxTerm, String JavaDoc sTxTermPlural, String JavaDoc sDeTerm, String JavaDoc sIdLanguage, String JavaDoc sIdScope, int iIdDomain) throws SQLException JavaDoc {
129     ResultSet JavaDoc oRSet;
130     PreparedStatement JavaDoc oStmt;
131     String JavaDoc sGuTerm = Gadgets.generateUUID();
132     String JavaDoc sGuRootTerm;
133     Object JavaDoc [] oTerm = new Object JavaDoc[10];
134     int iTerm;
135     int iNext;
136
137     if (DebugFile.trace) {
138       DebugFile.writeln("Begin Thesauri.createTerm([Connection]," + sGuParent + "," + sTxTerm + "," + sTxTermPlural + "," + sDeTerm + "," + sIdLanguage + "," + sIdScope + String.valueOf(iIdDomain) + ")");
139       DebugFile.incIdent();
140       DebugFile.writeln("Connection.prepareStatement(SELECT " + DB.gu_rootterm + "," + DB.id_term + "0," + DB.id_term + "1," + DB.id_term + "2," + DB.id_term + "3," + DB.id_term + "4," + DB.id_term + "5," + DB.id_term + "6," + DB.id_term + "7," + DB.id_term + "8," + DB.id_term + "9 FROM " + DB.k_thesauri + " WHERE " + DB.gu_term + "='" + sGuParent + "'");
141     }
142
143     oStmt = oConn.prepareStatement("SELECT " + DB.gu_rootterm + "," + DB.id_term + "0," + DB.id_term + "1," + DB.id_term + "2," + DB.id_term + "3," + DB.id_term + "4," + DB.id_term + "5," + DB.id_term + "6," + DB.id_term + "7," + DB.id_term + "8," + DB.id_term + "9 FROM " + DB.k_thesauri + " WHERE " + DB.gu_term + "=?", ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
144     oStmt.setString(1, sGuParent);
145     oRSet = oStmt.executeQuery();
146     oRSet.next();
147     sGuRootTerm = oRSet.getString(1);
148     oTerm[0] = oRSet.getObject(2);
149     oTerm[1] = oRSet.getObject(3);
150     oTerm[2] = oRSet.getObject(4);
151     oTerm[3] = oRSet.getObject(5);
152     oTerm[4] = oRSet.getObject(6);
153     oTerm[5] = oRSet.getObject(7);
154     oTerm[6] = oRSet.getObject(8);
155     oTerm[7] = oRSet.getObject(9);
156     oTerm[8] = oRSet.getObject(10);
157     oTerm[9] = oRSet.getObject(11);
158     oRSet.close();
159     oStmt.close();
160
161     if (DebugFile.trace)
162       DebugFile.writeln("Connection.prepareStatement(INSERT INTO " + DB.k_thesauri + " (" + DB.gu_rootterm + "," + DB.gu_term + "," + DB.tx_term + "," + DB.tx_term + "2," + DB.id_language + "," + DB.de_term + "," + DB.id_scope + "," + DB.id_domain + "," + DB.id_term + "0," + DB.id_term + "1," + DB.id_term + "2," + DB.id_term + "3," + DB.id_term + "4," + DB.id_term + "5," + DB.id_term + "6," + DB.id_term + "7," + DB.id_term + "8," + DB.id_term + "9) VALUES ('" + sGuRootTerm + "','" + sGuTerm + "','" + sTxTerm + "','" + sTxTermPlural + "','" + sIdLanguage + "','" + sDeTerm + "','" + sIdScope + "'," + String.valueOf(iIdDomain) + ",?,?,?,?,?,?,?,?,?,?))");
163
164     oStmt = oConn.prepareStatement("INSERT INTO " + DB.k_thesauri + " (" + DB.gu_rootterm + "," + DB.gu_term + "," + DB.tx_term + "," + DB.tx_term + "2," + DB.id_language + "," + DB.de_term + "," + DB.id_scope + "," + DB.id_domain + "," + DB.id_term + "0," + DB.id_term + "1," + DB.id_term + "2," + DB.id_term + "3," + DB.id_term + "4," + DB.id_term + "5," + DB.id_term + "6," + DB.id_term + "7," + DB.id_term + "8," + DB.id_term + "9) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
165     oStmt.setString(1, sGuRootTerm);
166     oStmt.setString(2, sGuTerm);
167     oStmt.setString(3, sTxTerm);
168     oStmt.setString(4, sTxTermPlural);
169     oStmt.setString(5, sIdLanguage);
170     oStmt.setString(6, sDeTerm);
171     oStmt.setString(7, sIdScope);
172     oStmt.setInt(8, iIdDomain);
173
174     iTerm = 0;
175     do {
176       if (null==oTerm[iTerm]) break;
177
178       if (DebugFile.trace)
179         DebugFile.writeln("PreparedStatement.setObject(" + String.valueOf(iTerm+9) + ", " + oTerm[iTerm] + ", java.sql.Types.INTEGER)");
180
181       oStmt.setObject(iTerm+9, oTerm[iTerm], java.sql.Types.INTEGER);
182     } while (++iTerm<=9);
183
184     if (10==iTerm)
185       throw new SQLException JavaDoc ("Thesauri maximum number of hierarchical levels exceeded");
186
187     iNext = DBBind.nextVal(oConn, "seq_thesauri");
188
189     if (DebugFile.trace)
190       DebugFile.writeln("PreparedStatement.setInt (" + String.valueOf(iTerm+9) + "," + String.valueOf(iNext) + ")");
191
192     oStmt.setInt (iTerm+9, iNext);
193
194     while (++iTerm<=9) {
195       if (DebugFile.trace)
196         DebugFile.writeln("PreparedStatement.setObject(" + String.valueOf(iTerm+9) + ", null, java.sql.Types.INTEGER)");
197
198       oStmt.setObject(iTerm+9, null, java.sql.Types.INTEGER);
199
200     } // wend
201

202     if (DebugFile.trace) DebugFile.writeln("PreparedStatement.executeUpdate();");
203
204     oStmt.executeUpdate();
205     oStmt.close();
206
207     if (DebugFile.trace) {
208       DebugFile.decIdent();
209       DebugFile.writeln("End Thesauri.createTerm() : " + sGuTerm);
210     }
211
212     return sGuTerm;
213   } // createTerm
214

215   /**
216    * Delete a Term and all its childs
217    * @param sGuTerm Term GUID
218    * @throws SQLException
219    */

220   public static void delete(JDCConnection oConn, String JavaDoc sGuTerm)
221     throws SQLException JavaDoc {
222
223     PreparedStatement JavaDoc oStmt;
224
225     if (DebugFile.trace) {
226       DebugFile.writeln("Begin Thesauri.delete([Connection]," + sGuTerm);
227       DebugFile.incIdent();
228       DebugFile.writeln("Connection.prepareStatement(DELETE FROM " + DB.k_thesauri + " WHERE " + DB.gu_term + "=? OR " + DB.id_term + "0=? OR " + DB.id_term + "1=? OR " + DB.id_term + "2=? OR " + DB.id_term + "3=? OR " + DB.id_term + "4=? OR " + DB.id_term + "5=? OR " + DB.id_term + "6=? OR " + DB.id_term + "7=? OR " + DB.id_term + "8=? OR " + DB.id_term + "9=?)");
229     }
230
231     oStmt = oConn.prepareStatement("DELETE FROM " + DB.k_thesauri + " WHERE " + DB.gu_term + "=? OR " + DB.id_term + "0=? OR " + DB.id_term + "1=? OR " + DB.id_term + "2=? OR " + DB.id_term + "3=? OR " + DB.id_term + "4=? OR " + DB.id_term + "5=? OR " + DB.id_term + "6=? OR " + DB.id_term + "7=? OR " + DB.id_term + "8=? OR " + DB.id_term + "9=?");
232
233     oStmt.setString(1, sGuTerm);
234     oStmt.setString(2, sGuTerm);
235     oStmt.setString(3, sGuTerm);
236     oStmt.setString(4, sGuTerm);
237     oStmt.setString(5, sGuTerm);
238     oStmt.setString(6, sGuTerm);
239     oStmt.setString(7, sGuTerm);
240     oStmt.setString(8, sGuTerm);
241     oStmt.setString(9, sGuTerm);
242     oStmt.setString(10, sGuTerm);
243     oStmt.setString(11, sGuTerm);
244
245     oStmt.executeUpdate();
246
247     oStmt.close();
248
249     if (DebugFile.trace) {
250       DebugFile.decIdent();
251       DebugFile.writeln("End Thesauri.delete()");
252     }
253   } // delete
254

255   public static String JavaDoc createSynonym (JDCConnection oConn, String JavaDoc sGuMainTerm, String JavaDoc sTxTerm, String JavaDoc sTxTermPlural, String JavaDoc sDeTerm)
256       throws SQLException JavaDoc {
257     Term oMain = new Term();
258
259     if (DebugFile.trace) {
260       DebugFile.writeln("Begin Thesauri.createSynonym([Connection]," + sGuMainTerm + "," + sTxTerm + "," + sTxTermPlural + "," + sDeTerm);
261       DebugFile.incIdent();
262     }
263
264     oMain.load(oConn, new Object JavaDoc[]{sGuMainTerm});
265
266     int iLevel = oMain.level();
267
268     oMain.replace(DB.gu_term, Gadgets.generateUUID());
269     oMain.replace(DB.gu_synonym, sGuMainTerm);
270     oMain.replace(DB.id_term + String.valueOf(iLevel-1), DBBind.nextVal(oConn, "seq_thesauri"));
271     oMain.replace(DB.tx_term, sTxTerm);
272     oMain.replace(DB.tx_term + "2", sTxTermPlural);
273     oMain.replace(DB.de_term, sDeTerm);
274
275     oMain.store(oConn);
276
277     if (DebugFile.trace) {
278       DebugFile.decIdent();
279       DebugFile.writeln("End Thesauri.createSynonym() : " + oMain.getString(DB.gu_term));
280     }
281
282     return oMain.getString(DB.gu_term);
283   }
284
285   /**
286    * Get Term GUID given its numeric identifier and level
287    * @param oConn Database Connection
288    * @param iDomainId Term Domain identifier
289    * @param iTermId Term numeric identifier
290    * @param iLevel Term level [0..9]
291    * @return Term GUID or <b>null</b> if no term with such numeric identifier and level was found
292    * @throws SQLException
293    */

294   public static String JavaDoc getTerm (JDCConnection oConn, int iDomainId, int iTermId, int iLevel)
295     throws SQLException JavaDoc {
296
297     if (DebugFile.trace) {
298       DebugFile.writeln("Begin Thesauri.getTerm (Connection], " + String.valueOf(iTermId) + "," + String.valueOf(iLevel));
299       DebugFile.incIdent();
300     }
301
302     String JavaDoc sTermGUID;
303     PreparedStatement JavaDoc oStmt;
304
305     if (iLevel<9) {
306       if (DebugFile.trace)
307         DebugFile.writeln("Connection.prepareStatement(SELECT " + DB.gu_term + " FROM " + DB.k_thesauri + " WHERE " + DB.id_domain + "=" + String.valueOf(iDomainId) + " AND " + DB.id_term + String.valueOf(iLevel) + "=" + String.valueOf(iTermId) + " AND " + DB.id_term + String.valueOf(iLevel+1) + " IS NULL)");
308
309       oStmt = oConn.prepareStatement("SELECT " + DB.gu_term + " FROM " + DB.k_thesauri + " WHERE " + DB.id_domain + "=? AND " + DB.id_term + String.valueOf(iLevel) + "=? AND " + DB.id_term + String.valueOf(iLevel+1) + " IS NULL", ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
310     }
311     else {
312       if (DebugFile.trace)
313         DebugFile.writeln("Connection.prepareStatement(SELECT " + DB.gu_term + " FROM " + DB.k_thesauri + " WHERE " + DB.id_domain + "=" + String.valueOf(iDomainId) + " AND " + DB.id_term + String.valueOf(iLevel) + "=" + String.valueOf(iTermId) + ")");
314
315       oStmt = oConn.prepareStatement("SELECT " + DB.gu_term + " FROM " + DB.k_thesauri + " WHERE " + DB.id_domain + "=? AND " + DB.id_term + String.valueOf(iLevel) + "=?", ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
316     }
317
318     oStmt.setInt(1, iDomainId);
319     oStmt.setInt(2, iTermId);
320
321     ResultSet JavaDoc oRSet = oStmt.executeQuery();
322
323     if (oRSet.next())
324       sTermGUID = oRSet.getString(1);
325     else
326       sTermGUID = null;
327
328     oRSet.close();
329     oStmt.close();
330
331     if (DebugFile.trace) {
332       DebugFile.decIdent();
333       DebugFile.writeln("End Thesauri.getTerm() : " + sTermGUID);
334     }
335
336     return sTermGUID;
337   }
338 }
Popular Tags