1 32 33 package com.knowgate.hipergate; 34 35 import java.sql.SQLException ; 36 import java.sql.PreparedStatement ; 37 import java.sql.ResultSet ; 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 49 50 public class Thesauri { 51 52 public Thesauri() { 53 } 54 55 68 public static String createRootTerm(JDCConnection oConn, String sTxTerm, String sTxTermPlural, String sDeTerm, String sIdLanguage, String sIdScope, int iIdDomain, String sGuWorkArea) throws SQLException { 69 PreparedStatement oStmt; 70 String 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 } 114 115 128 public static String createTerm(JDCConnection oConn, String sGuParent, String sTxTerm, String sTxTermPlural, String sDeTerm, String sIdLanguage, String sIdScope, int iIdDomain) throws SQLException { 129 ResultSet oRSet; 130 PreparedStatement oStmt; 131 String sGuTerm = Gadgets.generateUUID(); 132 String sGuRootTerm; 133 Object [] oTerm = new Object [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 ("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 } 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 } 215 220 public static void delete(JDCConnection oConn, String sGuTerm) 221 throws SQLException { 222 223 PreparedStatement 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 } 255 public static String createSynonym (JDCConnection oConn, String sGuMainTerm, String sTxTerm, String sTxTermPlural, String sDeTerm) 256 throws SQLException { 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 []{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 294 public static String getTerm (JDCConnection oConn, int iDomainId, int iTermId, int iLevel) 295 throws SQLException { 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 sTermGUID; 303 PreparedStatement 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 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 |