KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2   Copyright (C) 2004 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.Statement JavaDoc;
36 import java.sql.SQLException JavaDoc;
37 import java.sql.ResultSet JavaDoc;
38 import java.sql.ResultSetMetaData JavaDoc;
39
40 import java.util.LinkedList JavaDoc;
41
42 import com.knowgate.debug.DebugFile;
43 import com.knowgate.jdc.JDCConnection;
44 import com.knowgate.dataobjs.DB;
45 import com.knowgate.dataobjs.DBBind;
46 import com.knowgate.dataobjs.DBPersist;
47 import com.knowgate.dataobjs.DBSubset;
48 import com.knowgate.dataobjs.DBTable;
49
50 /**
51  * <p>Thesauri Term</p>
52  * @author Sergio Montoro Ten
53  * @version 1.2
54  */

55
56 public class Term extends DBPersist {
57   public Term() {
58     super(DB.k_thesauri, "Term");
59   }
60
61   /**
62    * Delete a Term and all its synonyms
63    * @param oConn Database Connection
64    * @param sTermGUID Term GUID
65    * @throws SQLException
66    */

67
68   public boolean delete (JDCConnection oConn) throws SQLException JavaDoc {
69     return Term.delete(oConn, getString(DB.gu_term));
70   }
71
72   // ---------------------------------------------------------------------------
73

74   /**
75    * Delete a Term and all its synonyms
76    * @param oConn Database Connection
77    * @param sTermGUID Term GUID
78    * @throws SQLException
79    */

80   public static boolean delete (JDCConnection oConn, String JavaDoc sTermGUID) throws SQLException JavaDoc {
81
82     if (DebugFile.trace) {
83       DebugFile.writeln("Begin Term.delete([Connection], " + sTermGUID + ")");
84       DebugFile.incIdent();
85     }
86
87     Statement JavaDoc oUpdt = oConn.createStatement();
88
89      if (DBBind.exists(oConn, DB.k_companies, "U")) {
90        if (DebugFile.trace)
91          DebugFile.writeln("Statement.executeUpdate(UPDATE " + DB.k_companies + " SET " + DB.gu_geozone + "=NULL WHERE " + DB.gu_geozone + "='" + sTermGUID + "')");
92
93        oUpdt.executeUpdate("UPDATE " + DB.k_companies + " SET " + DB.gu_geozone + "=NULL WHERE " + DB.gu_geozone + "='" + sTermGUID + "'");
94      }
95
96      if (DBBind.exists(oConn, DB.k_contacts, "U")) {
97        if (DebugFile.trace)
98          DebugFile.writeln("Statement.executeUpdate(UPDATE " + DB.k_contacts + " SET " + DB.gu_geozone + "=NULL WHERE " + DB.gu_geozone + "='" + sTermGUID + "')");
99
100        oUpdt.executeUpdate("UPDATE " + DB.k_contacts + " SET " + DB.gu_geozone + "=NULL WHERE " + DB.gu_geozone + "='" + sTermGUID + "'");
101      }
102
103      if (DBBind.exists(oConn, DB.k_member_address, "U")) {
104        if (DebugFile.trace)
105          DebugFile.writeln("Statement.executeUpdate(UPDATE " + DB.k_member_address + " SET " + DB.gu_geozone + "=NULL WHERE " + DB.gu_geozone + "='" + sTermGUID + "')");
106
107        oUpdt.executeUpdate("UPDATE " + DB.k_member_address + " SET " + DB.gu_geozone + "=NULL WHERE " + DB.gu_geozone + "='" + sTermGUID + "'");
108      }
109
110      oUpdt.close();
111
112     // Find root term
113
Statement JavaDoc oStmt = oConn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
114
115     if (DebugFile.trace)
116       DebugFile.writeln("Statement.executeQuery(SELECT " + DB.gu_rootterm + " FROM " + DB.k_thesauri + " WHERE " + DB.gu_term + "='" + sTermGUID + "')");
117
118     ResultSet JavaDoc oRSet = oStmt.executeQuery("SELECT " + DB.gu_rootterm + " FROM " + DB.k_thesauri + " WHERE " + DB.gu_term + "='" + sTermGUID + "'");
119
120     boolean bExists = oRSet.next();
121     String JavaDoc sRootTerm = null;
122
123     if (bExists) sRootTerm = oRSet.getString(1);
124
125     oRSet.close();
126     oStmt.close();
127
128     if (!bExists) return false;
129
130     Term oDlte = new Term();
131     oDlte.load(oConn, new Object JavaDoc[]{sTermGUID});
132     String JavaDoc sTermN = DB.id_term + String.valueOf(oDlte.level()-1);
133
134     oStmt = oConn.createStatement();
135
136     // Delete Synonyms
137
if (DebugFile.trace)
138       DebugFile.writeln("Statement.executeUpdate(DELETE FROM " + DB.k_thesauri + " WHERE " + DB.gu_synonym + "='" + sTermGUID + "')");
139
140     oStmt.executeUpdate("DELETE FROM " + DB.k_thesauri + " WHERE " + DB.gu_synonym + "='" + sTermGUID + "'");
141
142     // Delete Term and Childs
143
if (DebugFile.trace)
144       DebugFile.writeln("Statement.executeUpdate(DELETE FROM " + DB.k_thesauri + " WHERE " + sTermN + "=" + String.valueOf(oDlte.getInt(sTermN)) + " AND " + DB.id_domain + "=" + oDlte.getInt(DB.id_domain) + ")");
145
146     oStmt.executeUpdate("DELETE FROM " + DB.k_thesauri + " WHERE " + sTermN + "=" + String.valueOf(oDlte.getInt(sTermN)) + " AND " + DB.id_domain + "=" + oDlte.getInt(DB.id_domain));
147
148     // Delete root entry if term is a root one
149
if (sRootTerm.equals(sTermGUID))
150       oStmt.executeUpdate("DELETE FROM " + DB.k_thesauri_root + " WHERE " + DB.gu_rootterm + "='" + sTermGUID + "'");
151
152     oStmt.close();
153
154     if (DebugFile.trace) {
155       DebugFile.decIdent();
156       DebugFile.writeln("End Term.delete([Connection])");
157     }
158
159     return true;
160   }
161
162   // ---------------------------------------------------------------------------
163

164   /**
165    * Get term level [1..10]
166    */

167   public int level () {
168     int iLevel;
169
170     if (isNull("id_term1"))
171       iLevel = 1;
172     else if (isNull("id_term2"))
173       iLevel = 2;
174     else if (isNull("id_term3"))
175       iLevel = 3;
176     else if (isNull("id_term4"))
177       iLevel = 4;
178     else if (isNull("id_term5"))
179       iLevel = 5;
180     else if (isNull("id_term6"))
181       iLevel = 6;
182     else if (isNull("id_term7"))
183       iLevel = 7;
184     else if (isNull("id_term8"))
185       iLevel = 8;
186     else if (isNull("id_term9"))
187       iLevel = 9;
188     else
189       iLevel = 10;
190
191     return iLevel;
192   }
193
194   // ---------------------------------------------------------------------------
195

196   /**
197    * Get Term Parent GUID
198    * @param oConn Database Connection
199    * @return Parent Term GUID or <b>null</b> if this is a root term
200    * @throws SQLException
201    */

202   public String JavaDoc getParent (JDCConnection oConn) throws SQLException JavaDoc {
203     int iLevel;
204     String JavaDoc sParentId;
205
206     if (DebugFile.trace) {
207       DebugFile.writeln("Begin Term.getParent([Connection])");
208       DebugFile.incIdent();
209     }
210
211     iLevel = level();
212
213     Statement JavaDoc oStmt;
214     ResultSet JavaDoc oRSet;
215
216     if (1==iLevel) {
217       sParentId = null;
218     }
219     else if (2==iLevel) {
220       sParentId = getString(DB.gu_rootterm);
221     }
222     else {
223       oStmt = oConn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
224
225       if (DebugFile.trace)
226         DebugFile.writeln("Statement.executeQuery(SELECT " + DB.gu_term + " FROM " + DB.k_thesauri + " WHERE " + DB.id_term + String.valueOf(iLevel-2) + "=" + String.valueOf(getInt(DB.id_term + String.valueOf(iLevel-2))) + " AND " + DB.id_term + String.valueOf(iLevel-1) + " IS NULL)");
227
228       oRSet = oStmt.executeQuery("SELECT " + DB.gu_term + " FROM " + DB.k_thesauri + " WHERE " + DB.id_term + String.valueOf(iLevel-2) + "=" + String.valueOf(getInt(DB.id_term + String.valueOf(iLevel-2))) + " AND " + DB.id_term + String.valueOf(iLevel-1) + " IS NULL");
229
230       if (oRSet.next())
231         sParentId = oRSet.getString(1);
232       else
233         sParentId = null;
234
235       oRSet.close();
236       oStmt.close();
237
238       if (null==sParentId)
239         throw new SQLException JavaDoc ("Parent key " + String.valueOf(getInt(DB.id_term + String.valueOf(iLevel-2))) + " not found");
240     }
241
242     if (DebugFile.trace) {
243       DebugFile.decIdent();
244       DebugFile.writeln("End Term.getParent() : " + sParentId);
245     }
246
247     return sParentId;
248   } // getParent
249

250   // ---------------------------------------------------------------------------
251

252   private void browse (JDCConnection oConn, int iScope, LinkedList JavaDoc oTermsList, String JavaDoc sColList, String JavaDoc[] aColArray)
253     throws SQLException JavaDoc {
254
255     if (DebugFile.trace) {
256       DebugFile.writeln("Begin Term.browse(gu_term=" + getStringNull(DB.gu_term,"null") + ",tx_term=" + getStringNull(DB.tx_term,"null") + ",level=" + String.valueOf(level()) + ",columns=" + String.valueOf(aColArray.length) + ")");
257       DebugFile.incIdent();
258     }
259
260     int iChilds = 0;
261     int iLevel = level();
262     int iCols = aColArray.length;
263
264     if (iLevel<10) {
265       DBSubset oChilds;
266
267       if (DebugFile.trace && iLevel>0)
268         DebugFile.writeln("id_term" + String.valueOf(iLevel-1) + "=" + get(DB.id_term + String.valueOf(iLevel-1)));
269
270       if (9==iLevel)
271         oChilds = new DBSubset(DB.k_thesauri, sColList, DB.id_term + "9 IS NOT NULL AND " + DB.id_term + "8=" + String.valueOf(getInt(DB.id_term + "8")), 10);
272       else {
273         oChilds = new DBSubset(DB.k_thesauri, sColList,
274                                DB.id_term + String.valueOf(iLevel) + " IS NOT NULL AND " +
275                                DB.id_term + String.valueOf(iLevel+1) + " IS NULL AND " +
276                                DB.id_term + String.valueOf(iLevel-1) + "=" + String.valueOf(getInt(DB.id_term + String.valueOf(iLevel-1))), 10);
277       }
278
279       iChilds = oChilds.load(oConn);
280
281       Term oChld;
282
283       for (int t=0; t<iChilds; t++) {
284         oChld = new Term();
285
286         for (int c=0; c<iCols; c++)
287           oChld.put(aColArray[c], oChilds.get(c, t));
288
289         oTermsList.addLast(oChld);
290
291         if (SCOPE_ALL==iScope)
292           oChld.browse (oConn, iScope, oTermsList, sColList, aColArray);
293       } // next t
294
}
295
296     if (DebugFile.trace) {
297       DebugFile.decIdent();
298       DebugFile.writeln("End Term.browse() : " + String.valueOf(iChilds));
299     }
300   } // browse
301

302   /**
303    * Get child terms for this term.
304    * @param oConn Database Connection
305    * @param iScope SCOPE_ONE for just first level childs or SCOPE_ALL for all level childs
306    * @return LinkedList with in-depth child walkthrought<BR>
307    * for example, if representing a geographic thesauri: WORLD,AMERICA,NORTH AMERICA,USA,CANADA,MEXICO,SOUTH AMERICA,BRAZIL,ARGENTINA,CHILE,EUROPE,SPAIN,FRANCE,GERMANY
308    * @throws SQLException
309    */

310   public LinkedList JavaDoc getChilds (JDCConnection oConn, int iScope)
311     throws SQLException JavaDoc {
312     LinkedList JavaDoc oTermsList = new LinkedList JavaDoc();
313
314     if (isNull(DB.gu_term)) {
315       if (DebugFile.trace)
316         DebugFile.writeln("ERROR - Term.getChilds() Attemped to get childs of an unloaded Term.");
317
318       throw new NullPointerException JavaDoc("Term.getChilds() Attemped to get childs of an unloaded Term");
319     }
320
321     // ***************************
322
// Get k_thesauri columns list
323

324     Statement JavaDoc oStmt = oConn.createStatement();
325     ResultSet JavaDoc oRSet = oStmt.executeQuery("SELECT * FROM " + DB.k_thesauri + " WHERE 1=0");
326     ResultSetMetaData JavaDoc oMDat = oRSet.getMetaData();
327
328     int iCols = oMDat.getColumnCount();
329     String JavaDoc[] aColArray = new String JavaDoc[iCols];
330     StringBuffer JavaDoc sColList = new StringBuffer JavaDoc(32*iCols);
331
332     for (int c=0; c<iCols; c++) {
333       aColArray[c] = oMDat.getColumnName(c+1).toLowerCase();
334       sColList.append(aColArray[c]);
335       if (c!=iCols-1) sColList.append(",");
336     }
337
338     oRSet.close();
339     oStmt.close();
340
341     // End get columns list
342
// ********************
343

344     browse (oConn, iScope, oTermsList, sColList.toString(), aColArray);
345
346     return oTermsList;
347   } // getChilds
348

349   // **********************************************************
350
// Public Constants
351

352   public static final short ClassId = 9;
353
354   public static final int SCOPE_ONE = 1;
355   public static final int SCOPE_ALL = 2;
356
357 }
Popular Tags