1 32 33 package com.knowgate.hipergate; 34 35 import java.sql.Statement ; 36 import java.sql.SQLException ; 37 import java.sql.ResultSet ; 38 import java.sql.ResultSetMetaData ; 39 40 import java.util.LinkedList ; 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 55 56 public class Term extends DBPersist { 57 public Term() { 58 super(DB.k_thesauri, "Term"); 59 } 60 61 67 68 public boolean delete (JDCConnection oConn) throws SQLException { 69 return Term.delete(oConn, getString(DB.gu_term)); 70 } 71 72 74 80 public static boolean delete (JDCConnection oConn, String sTermGUID) throws SQLException { 81 82 if (DebugFile.trace) { 83 DebugFile.writeln("Begin Term.delete([Connection], " + sTermGUID + ")"); 84 DebugFile.incIdent(); 85 } 86 87 Statement 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 Statement 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 oRSet = oStmt.executeQuery("SELECT " + DB.gu_rootterm + " FROM " + DB.k_thesauri + " WHERE " + DB.gu_term + "='" + sTermGUID + "'"); 119 120 boolean bExists = oRSet.next(); 121 String 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 []{sTermGUID}); 132 String sTermN = DB.id_term + String.valueOf(oDlte.level()-1); 133 134 oStmt = oConn.createStatement(); 135 136 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 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 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 164 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 196 202 public String getParent (JDCConnection oConn) throws SQLException { 203 int iLevel; 204 String sParentId; 205 206 if (DebugFile.trace) { 207 DebugFile.writeln("Begin Term.getParent([Connection])"); 208 DebugFile.incIdent(); 209 } 210 211 iLevel = level(); 212 213 Statement oStmt; 214 ResultSet 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 ("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 } 250 252 private void browse (JDCConnection oConn, int iScope, LinkedList oTermsList, String sColList, String [] aColArray) 253 throws SQLException { 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 } } 295 296 if (DebugFile.trace) { 297 DebugFile.decIdent(); 298 DebugFile.writeln("End Term.browse() : " + String.valueOf(iChilds)); 299 } 300 } 302 310 public LinkedList getChilds (JDCConnection oConn, int iScope) 311 throws SQLException { 312 LinkedList oTermsList = new LinkedList (); 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 ("Term.getChilds() Attemped to get childs of an unloaded Term"); 319 } 320 321 324 Statement oStmt = oConn.createStatement(); 325 ResultSet oRSet = oStmt.executeQuery("SELECT * FROM " + DB.k_thesauri + " WHERE 1=0"); 326 ResultSetMetaData oMDat = oRSet.getMetaData(); 327 328 int iCols = oMDat.getColumnCount(); 329 String [] aColArray = new String [iCols]; 330 StringBuffer sColList = new StringBuffer (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 344 browse (oConn, iScope, oTermsList, sColList.toString(), aColArray); 345 346 return oTermsList; 347 } 349 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 |