1 32 33 package com.knowgate.acl; 34 35 import java.util.Map ; 36 import java.util.Date ; 37 import java.util.Arrays ; 38 import java.util.Iterator ; 39 40 import java.sql.Types ; 41 import java.sql.Timestamp ; 42 import java.sql.Connection ; 43 import java.sql.SQLException ; 44 import java.sql.PreparedStatement ; 45 46 import com.knowgate.acl.ACLUser; 47 import com.knowgate.acl.ACLGroup; 48 import com.knowgate.misc.Gadgets; 49 import com.knowgate.debug.DebugFile; 50 import com.knowgate.hipergate.datamodel.ImportLoader; 51 import com.knowgate.hipergate.datamodel.ColumnList; 52 53 58 public class UserLoader implements ImportLoader { 59 60 private Object [] aValues; 61 62 private PreparedStatement oUserUpdt, oUserInsr, oGroupInsr; 63 private int iLastDomainId; 64 private String sLastGroupId, sLastGroupNm; 65 66 public UserLoader() { 67 aValues = new Object [ColumnNames.length]; 68 for (int c = aValues.length - 1; c >= 0; c--) aValues[c] = null; 69 iLastDomainId = 0; 70 sLastGroupId = ""; 71 sLastGroupNm = ""; 72 } 73 74 76 79 public void setAllColumnsToNull() { 80 if (DebugFile.trace) { 81 DebugFile.writeln("Begin ContactLoader.setAllColumnsToNull()"); 82 DebugFile.incIdent(); 83 } 84 85 for (int c=aValues.length-1; c>=0; c--) 86 aValues[c] = null; 87 88 if (DebugFile.trace) { 89 DebugFile.decIdent(); 90 DebugFile.writeln("End ContactLoader.setAllColumnsToNull()"); 91 } 92 } 94 96 103 public int getColumnIndex(String sColumnName) { 104 int iIndex = Arrays.binarySearch(ColumnNames, sColumnName, String.CASE_INSENSITIVE_ORDER); 105 if (iIndex<0) iIndex=-1; 106 return iIndex; 107 } 108 109 111 public int columnCount() { 112 return aValues.length; 113 } 114 115 117 public String [] columnNames() throws IllegalStateException { 118 return ColumnNames; 119 } 120 121 123 129 public void put(int iColumnIndex, Object oValue) 130 throws ArrayIndexOutOfBoundsException { 131 aValues[iColumnIndex] = oValue; 132 } 133 134 136 143 public void put(String sColumnName, Object oValue) 144 throws ArrayIndexOutOfBoundsException { 145 int iColumnIndex = getColumnIndex(sColumnName.toLowerCase()); 146 if (-1==iColumnIndex) throw new ArrayIndexOutOfBoundsException ("Cannot find column named "+sColumnName); 147 aValues[iColumnIndex] = oValue; 148 } 149 150 152 156 public void putAll(Map oValues) { 157 int iColumnIndex; 158 String sColumnName; 159 if (DebugFile.trace) { 160 DebugFile.writeln("Begin UserLoader.putAll()"); 161 DebugFile.incIdent(); 162 } 163 Iterator oIter = oValues.keySet().iterator(); 164 while (oIter.hasNext()) { 165 sColumnName = (String ) oIter.next(); 166 iColumnIndex = getColumnIndex(sColumnName.toLowerCase()); 167 if (iColumnIndex>0) { 168 Object oVal = oValues.get(sColumnName); 169 if (oVal==null) 170 aValues[iColumnIndex] = null; 171 else if (oVal.getClass().getName().startsWith("[L")) { 172 aValues[iColumnIndex] = java.lang.reflect.Array.get(oVal,0); 173 } else { 174 aValues[iColumnIndex] = oVal; 175 } 176 if (DebugFile.trace) DebugFile.writeln(sColumnName.toLowerCase()+"="+aValues[iColumnIndex]); 177 } else { 178 if (DebugFile.trace) DebugFile.writeln(sColumnName + " not found"); 179 } } if (DebugFile.trace) { 182 DebugFile.decIdent(); 183 DebugFile.writeln("End UserLoader.putAll()"); 184 } 185 } 187 189 195 public Object get(int iColumnIndex) 196 throws ArrayIndexOutOfBoundsException { 197 return aValues[iColumnIndex]; 198 } 200 202 208 public Object get(String sColumnName) 209 throws ArrayIndexOutOfBoundsException { 210 int iColumnIndex = getColumnIndex(sColumnName.toLowerCase()); 211 if (-1==iColumnIndex) throw new ArrayIndexOutOfBoundsException ("Cannot find column named "+sColumnName); 212 return aValues[iColumnIndex]; 213 } 214 215 217 private String getColNull (int iColIndex) 218 throws ArrayIndexOutOfBoundsException ,ClassCastException { 219 if (DebugFile.trace) { 220 if (iColIndex<0 || iColIndex>=aValues.length) 221 throw new ArrayIndexOutOfBoundsException ("UserLoader.getColNull() column index "+String.valueOf(iColIndex)+" must be in the range between 0 and "+String.valueOf(aValues.length)); 222 DebugFile.writeln("UserLoader.getColNull("+String.valueOf(iColIndex)+") : "+aValues[iColIndex]); 223 } 224 String sRetVal; 225 if (null==aValues[iColIndex]) 226 sRetVal = null; 227 else { 228 try { 229 sRetVal = aValues[iColIndex].toString(); 230 } catch (ClassCastException cce){ 231 if (aValues[iColIndex]==null) 232 throw new ClassCastException ("UserLoader.getColNull("+String.valueOf(iColIndex)+") could not cast null to String"); 233 else 234 throw new ClassCastException ("UserLoader.getColNull("+String.valueOf(iColIndex)+") could not cast "+aValues[iColIndex].getClass().getName()+" "+aValues[iColIndex]+" to String"); 235 } 236 if (sRetVal.length()==0 || sRetVal.equalsIgnoreCase("null")) 237 sRetVal = null; 238 } 239 return sRetVal; 240 } 242 244 private static boolean test(int iInputValue, int iBitMask) { 245 return (iInputValue&iBitMask)!=0; 246 } 248 250 261 public void prepare(Connection oConn, ColumnList oColList) 262 throws SQLException { 263 264 if (DebugFile.trace) { 265 DebugFile.writeln("Begin UserLoader.prepare()"); 266 DebugFile.incIdent(); 267 } 268 269 oUserInsr = oConn.prepareStatement("INSERT INTO k_users (dt_created,id_domain,tx_nickname,tx_pwd,tx_pwd_sign,bo_change_pwd,bo_searchable,bo_active,len_quota,max_quota,tp_account,id_account,dt_last_update,dt_last_visit,dt_cancel,tx_main_email,tx_alt_email,nm_user,tx_surname1,tx_surname2,tx_challenge,tx_reply,dt_pwd_expires,gu_category,gu_workarea,nm_company,de_title,id_gender,dt_birth,ny_age,marital_status,tx_education,icq_id,sn_passport,tp_passport,tx_comments,gu_user) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"); 270 oUserUpdt = oConn.prepareStatement("UPDATE k_users SET id_domain=?,tx_nickname=?,tx_pwd=?,tx_pwd_sign=?,bo_change_pwd=?,bo_searchable=?,bo_active=?,len_quota=?,max_quota=?,tp_account=?,id_account=?,dt_last_update=?,dt_last_visit=?,dt_cancel=?,tx_main_email=?,tx_alt_email=?,nm_user=?,tx_surname1=?,tx_surname2=?,tx_challenge=?,tx_reply=?,dt_pwd_expires=?,gu_category=?,gu_workarea=?,nm_company=?,de_title=?,id_gender=?,dt_birth=?,ny_age=?,marital_status=?,tx_education=?,icq_id=?,sn_passport=?,tp_passport=?,tx_comments=? WHERE gu_user=?"); 271 oGroupInsr= oConn.prepareStatement("INSERT INTO k_x_group_user (gu_user,gu_acl_group) VALUES(?,?)"); 272 273 if (DebugFile.trace) { 274 DebugFile.decIdent(); 275 DebugFile.writeln("End UserLoader.prepare()"); 276 } 277 } 278 279 281 public void close() throws SQLException { 282 if (null!=oGroupInsr) { oGroupInsr.close(); oGroupInsr=null; } 283 if (null!=oUserUpdt ) { oUserUpdt.close(); oUserUpdt=null; } 284 if (null!=oUserInsr ) { oUserInsr.close(); oUserInsr=null; } 285 } 286 287 289 public void store(Connection oConn, String sWorkArea, int iFlags) 290 throws SQLException ,IllegalArgumentException ,NullPointerException , 291 ClassCastException ,NumberFormatException { 292 293 if (oUserInsr==null || oUserUpdt==null) 294 throw new SQLException ("Invalid command sequece. Must call UserLoader.prepare() before UserLoader.store()"); 295 296 if (!test(iFlags,MODE_APPEND) && !test(iFlags,MODE_UPDATE)) 297 throw new IllegalArgumentException ("UserLoader.store() Flags bitmask must contain either MODE_APPEND, MODE_UPDATE or both"); 298 299 if (null==getColNull(id_domain)) 300 throw new NullPointerException ("UserLoader.store() id_domain cannot be null"); 301 302 if (DebugFile.trace) { 303 DebugFile.writeln("Begin UserLoader.store([Connection],"+sWorkArea+","+String.valueOf(iFlags)+")"); 304 DebugFile.incIdent(); 305 StringBuffer oRow = new StringBuffer (); 306 oRow.append('{'); 307 oRow.append(ColumnNames[0]+"="); 308 oRow.append(aValues[0]==null ? "null" : aValues[0]); 309 for (int d=1; d<aValues.length; d++) { 310 oRow.append(","+ColumnNames[d]+"="); 311 oRow.append(aValues[d]==null ? "null" : aValues[d]); 312 } oRow.append('}'); 314 DebugFile.writeln(oRow.toString()); 315 } 316 317 int iAffected = 0; 318 Timestamp tsNow = new Timestamp (new Date ().getTime()); 319 int iDomainId = Integer.parseInt(get(id_domain).toString()); 320 321 if (null==get(gu_workarea)) { 322 if (DebugFile.trace) DebugFile.writeln("setting workarea to "+sWorkArea); 323 put(gu_workarea, sWorkArea); 324 } else { 325 if (DebugFile.trace) DebugFile.writeln("workarea for current record is "+getColNull(gu_workarea)); 326 } 327 328 if (null==getColNull(gu_user) && null!=getColNull(tx_nickname)) { 329 put(gu_user, ACLUser.getIdFromNick(oConn, iDomainId, getColNull(tx_nickname))); 330 } 331 332 if (test(iFlags,MODE_UPDATE) && null!=getColNull(gu_user)) { 333 oUserUpdt.setInt(1, iDomainId); 334 oUserUpdt.setObject(2, get(tx_nickname), Types.VARCHAR); 335 oUserUpdt.setObject(3, get(tx_pwd), Types.VARCHAR); 336 oUserUpdt.setObject(4, get(tx_pwd_sign), Types.VARCHAR); 337 oUserUpdt.setObject(5, get(bo_change_pwd), Types.SMALLINT); 338 oUserUpdt.setObject(6, get(bo_searchable), Types.SMALLINT); 339 oUserUpdt.setObject(7, get(bo_active), Types.SMALLINT); 340 oUserUpdt.setObject(8, get(len_quota), Types.DECIMAL); 341 oUserUpdt.setObject(9, get(max_quota), Types.DECIMAL); 342 oUserUpdt.setObject(10, get(tp_account), Types.CHAR); 343 oUserUpdt.setObject(11, get(id_account), Types.CHAR); 344 if (aValues[dt_last_update]==null) 345 oUserUpdt.setTimestamp(12, tsNow); 346 else 347 oUserUpdt.setObject(12, aValues[dt_last_update], Types.TIMESTAMP); 348 if (aValues[dt_last_visit]==null) 349 oUserUpdt.setNull(13, Types.TIMESTAMP); 350 else 351 oUserUpdt.setObject(13, aValues[dt_last_visit], Types.TIMESTAMP); 352 if (aValues[dt_cancel]==null) 353 oUserUpdt.setNull(14, Types.TIMESTAMP); 354 else 355 oUserUpdt.setObject(14, aValues[dt_cancel], Types.TIMESTAMP); 356 oUserUpdt.setObject(15, get(tx_main_email), Types.VARCHAR); 357 oUserUpdt.setObject(16, get(tx_alt_email), Types.VARCHAR); 358 oUserUpdt.setObject(17, get(nm_user), Types.VARCHAR); 359 oUserUpdt.setObject(18, get(tx_surname1), Types.VARCHAR); 360 oUserUpdt.setObject(19, get(tx_surname2), Types.VARCHAR); 361 oUserUpdt.setObject(20, get(tx_challenge), Types.VARCHAR); 362 oUserUpdt.setObject(21, get(tx_reply), Types.VARCHAR); 363 if (aValues[dt_pwd_expires]==null) 364 oUserUpdt.setNull(22, Types.TIMESTAMP); 365 else 366 oUserUpdt.setObject(22, aValues[dt_pwd_expires], Types.TIMESTAMP); 367 oUserUpdt.setObject(23, get(gu_category), Types.CHAR); 368 oUserUpdt.setObject(24, get(gu_workarea), Types.CHAR); 369 oUserUpdt.setObject(25, get(nm_company), Types.VARCHAR); 370 oUserUpdt.setObject(26, get(de_title), Types.VARCHAR); 371 oUserUpdt.setObject(27, get(id_gender), Types.CHAR); 372 if (aValues[dt_birth]==null) 373 oUserUpdt.setNull(28, Types.TIMESTAMP); 374 else 375 oUserUpdt.setObject(28, aValues[dt_birth], Types.TIMESTAMP); 376 oUserUpdt.setObject(29, get(ny_age), Types.SMALLINT); 377 oUserUpdt.setObject(30, get(marital_status), Types.CHAR); 378 oUserUpdt.setObject(31, get(tx_education), Types.VARCHAR); 379 oUserUpdt.setObject(32, get(icq_id), Types.VARCHAR); 380 oUserUpdt.setObject(33, get(sn_passport), Types.VARCHAR); 381 oUserUpdt.setObject(34, get(tp_passport), Types.VARCHAR); 382 oUserUpdt.setObject(35, get(tx_comments), Types.VARCHAR); 383 oUserUpdt.setString(36, getColNull(gu_user)); 384 iAffected = oUserUpdt.executeUpdate(); 385 } 386 387 if (0==iAffected && test(iFlags,MODE_APPEND)) { 388 aValues[gu_user] = Gadgets.generateUUID(); 389 oUserInsr.setTimestamp(1, tsNow); 390 oUserInsr.setInt(2, iDomainId); 391 oUserInsr.setObject(3, get(tx_nickname), Types.VARCHAR); 392 oUserInsr.setObject(4, get(tx_pwd), Types.VARCHAR); 393 oUserInsr.setObject(5, get(tx_pwd_sign), Types.VARCHAR); 394 oUserInsr.setObject(6, get(bo_change_pwd), Types.SMALLINT); 395 oUserInsr.setObject(7, get(bo_searchable), Types.SMALLINT); 396 oUserInsr.setObject(8, get(bo_active), Types.SMALLINT); 397 oUserInsr.setObject(9, get(len_quota), Types.DECIMAL); 398 oUserInsr.setObject(10, get(max_quota), Types.DECIMAL); 399 oUserInsr.setObject(11, get(tp_account), Types.CHAR); 400 oUserInsr.setObject(12, get(id_account), Types.CHAR); 401 if (aValues[dt_last_update]==null) 402 oUserInsr.setTimestamp(13, tsNow); 403 else 404 oUserInsr.setObject(13, aValues[dt_last_update], Types.TIMESTAMP); 405 if (aValues[dt_last_visit]==null) 406 oUserInsr.setNull(14, Types.TIMESTAMP); 407 else 408 oUserInsr.setObject(14, aValues[dt_last_visit], Types.TIMESTAMP); 409 if (aValues[dt_cancel]==null) 410 oUserInsr.setNull(15, Types.TIMESTAMP); 411 else 412 oUserInsr.setObject(15, aValues[dt_cancel], Types.TIMESTAMP); 413 oUserInsr.setObject(16, get(tx_main_email), Types.VARCHAR); 414 oUserInsr.setObject(17, get(tx_alt_email), Types.VARCHAR); 415 oUserInsr.setObject(18, get(nm_user), Types.VARCHAR); 416 oUserInsr.setObject(19, get(tx_surname1), Types.VARCHAR); 417 oUserInsr.setObject(20, get(tx_surname2), Types.VARCHAR); 418 oUserInsr.setObject(21, get(tx_challenge), Types.VARCHAR); 419 oUserInsr.setObject(22, get(tx_reply), Types.VARCHAR); 420 if (aValues[dt_pwd_expires]==null) 421 oUserInsr.setNull(23, Types.TIMESTAMP); 422 else 423 oUserInsr.setObject(23, aValues[dt_pwd_expires], Types.TIMESTAMP); 424 oUserInsr.setObject(24, get(gu_category), Types.CHAR); 425 oUserInsr.setObject(25, get(gu_workarea), Types.CHAR); 426 oUserInsr.setObject(26, get(nm_company), Types.VARCHAR); 427 oUserInsr.setObject(27, get(de_title), Types.VARCHAR); 428 oUserInsr.setObject(28, get(id_gender), Types.CHAR); 429 if (aValues[dt_birth]==null) 430 oUserInsr.setNull(29, Types.TIMESTAMP); 431 else 432 oUserInsr.setObject(29, aValues[dt_birth], Types.TIMESTAMP); 433 oUserInsr.setObject(30, get(ny_age), Types.SMALLINT); 434 oUserInsr.setObject(31, get(marital_status), Types.CHAR); 435 oUserInsr.setObject(32, get(tx_education), Types.VARCHAR); 436 oUserInsr.setObject(33, get(icq_id), Types.VARCHAR); 437 oUserInsr.setObject(34, get(sn_passport), Types.VARCHAR); 438 oUserInsr.setObject(35, get(tp_passport), Types.VARCHAR); 439 oUserInsr.setObject(36, get(tx_comments), Types.VARCHAR); 440 oUserInsr.setString(37, getColNull(gu_user)); 441 oUserInsr.execute(); 442 } 443 444 if (null==aValues[gu_acl_group] && null!=aValues[nm_acl_group]) { 445 if (iDomainId==iLastDomainId && sLastGroupNm.equals(aValues[nm_acl_group])) { 446 put (gu_acl_group, sLastGroupId); 447 } else { 448 put(gu_acl_group, ACLGroup.getIdFromName(oConn, iDomainId, getColNull(nm_acl_group))); 449 iLastDomainId=iDomainId; 450 sLastGroupNm=getColNull(nm_acl_group); 451 sLastGroupId=getColNull(gu_acl_group); 452 } 453 } 454 455 if (null!=aValues[gu_acl_group]) { 456 oGroupInsr.setString(1, getColNull(gu_user)); 457 oGroupInsr.setString(1, getColNull(gu_acl_group)); 458 try { 459 oGroupInsr.execute(); 460 } catch (SQLException ignore) { 461 if (DebugFile.trace) 462 DebugFile.writeln("UserLoader.store() User "+getColNull(tx_nickname)+"("+getColNull(gu_user)+") already exists at group "+getColNull(nm_acl_group)+"("+getColNull(gu_acl_group)+")"); 463 } 464 } 466 if (DebugFile.trace) { 467 DebugFile.decIdent(); 468 DebugFile.writeln("End UserLoader.store()"); 469 } 470 } 472 474 private static final String [] ColumnNames = { "","bo_active","bo_change_pwd","bo_searchable","de_title","dt_birth","dt_cancel","dt_created","dt_last_update","dt_last_visit","dt_pwd_expires","gu_acl_group","gu_category","gu_user","gu_workarea","icq_id","id_account","id_domain","id_gender","len_quota","marital_status","max_quota","nm_acl_group","nm_company","nm_user","nm_workarea","ny_age","sn_passport","tp_account","tp_passport","tx_alt_email","tx_challenge","tx_comments","tx_education","tx_main_email","tx_nickname","tx_pwd","tx_pwd_sign","tx_reply","tx_surname1","tx_surname2"}; 476 477 public static int bo_active= 1; 479 public static int bo_change_pwd= 2; 480 public static int bo_searchable= 3; 481 public static int de_title= 4; 482 public static int dt_birth= 5; 483 public static int dt_cancel= 6; 484 public static int dt_created= 7; 485 public static int dt_last_update= 8; 486 public static int dt_last_visit= 9; 487 public static int dt_pwd_expires= 10; 488 public static int gu_acl_group= 11; 489 public static int gu_category= 12; 490 public static int gu_user= 13; 491 public static int gu_workarea= 14; 492 public static int icq_id= 15; 493 public static int id_account= 16; 494 public static int id_domain= 17; 495 public static int id_gender= 18; 496 public static int len_quota= 19; 497 public static int marital_status= 20; 498 public static int max_quota= 21; 499 public static int nm_acl_group= 22; 500 public static int nm_company= 23; 501 public static int nm_user= 24; 502 public static int nm_workarea= 25; 503 public static int ny_age= 26; 504 public static int sn_passport= 27; 505 public static int tp_account= 28; 506 public static int tp_passport= 29; 507 public static int tx_alt_email= 30; 508 public static int tx_challenge= 31; 509 public static int tx_comments= 32; 510 public static int tx_education= 33; 511 public static int tx_main_email= 34; 512 public static int tx_nickname= 35; 513 public static int tx_pwd= 36; 514 public static int tx_pwd_sign= 37; 515 public static int tx_reply= 38; 516 public static int tx_surname1= 39; 517 public static int tx_surname2= 40; 518 519 } 520 | Popular Tags |