1 29 38 39 package org.planetamessenger.mos.engine; 40 41 import org.planetamessenger.db.*; 42 import org.planetamessenger.plugin.*; 43 44 45 46 public class JProfileManager { 47 48 private JDatabase database = null; 49 private long nProfileId = -1; 50 private String strPasswd; 51 52 53 57 public JProfileManager() { 58 59 database = JSharedObjects.getDatabase(); 60 } 61 62 69 public void setProfileId( long nProfileId ) { 70 71 JResultSet resultSet; 72 73 if( ( resultSet = database.execSQL( "SELECT passwd FROM profile WHERE profile_id =" + nProfileId ) ) == null ) 74 return; 75 76 try { 77 resultSet.getResultSet().next(); 78 this.strPasswd = resultSet.getResultSet().getString( "passwd" ); 79 this.nProfileId = nProfileId; 80 resultSet.close(); 81 } catch( java.sql.SQLException e ) { 82 System.err.println( "JProfileManager.setProfileId() - SQLException " + e ); 83 } 84 } 85 86 89 public long getProfileId() { 90 91 return nProfileId; 92 } 93 94 100 public boolean updateDefaultProfile( int nProfileId ) { 101 102 if( database.execUpdate( "UPDATE settings SET profile_id=" + nProfileId + "WHERE settings_id=1" ) <= 0 ) 103 if( database.execUpdate( "INSERT INTO settings (settings_id, profile_id) VALUES(1," + nProfileId + ")" ) <= 0 ) 104 return false; 105 106 return true; 107 } 108 109 113 public int getDefaultProfile() { 114 115 JResultSet resultSet; 116 int nProfileId = -1; 117 118 119 if( ( resultSet = database.execSQL( "SELECT profile_id FROM settings WHERE settings_id =1" ) ) == null ) 120 return -1; 121 122 try { 123 if( resultSet.getResultSet().next() ) 124 nProfileId = resultSet.getResultSet().getInt( "profile_id" ); 125 126 resultSet.close(); 127 128 return nProfileId; 129 } catch( java.sql.SQLException e ) { 130 System.err.println( "JProfileManager.getDefaultProfile() - SQLException " + e ); 131 return -1; 132 } 133 } 134 135 140 public boolean userAdd( int nPluginId, JUserInfo userInfo ) { 141 142 if( ( nProfileId == -1 ) || ( nPluginId == -1 ) ) 143 return false; 144 145 if( database.execUpdate( "UPDATE user SET alias_name='" + userInfo.getAliasName() + "', passwd='" + userInfo.getPassword() + "', auto_connect=" + userInfo.getAutoConnect() + ", login_status=" + userInfo.getLoginStatus() + " WHERE profile_id=" + userInfo.getUserData( "profile_id" ).toString() + " AND plugin_id=" + nPluginId + " AND user_id='" + userInfo.getUserId() + "'" ) <= 0 ) { 146 if( database.execUpdate( "INSERT INTO user (profile_id, plugin_id, user_id, alias_name, passwd, auto_connect, login_status) VALUES(" + userInfo.getUserData( "profile_id" ).toString() + ", " + nPluginId + ", '" + userInfo.getUserId() + "', '" + userInfo.getAliasName() + "', '" + userInfo.getPassword() + "', " + userInfo.getAutoConnect() + ", " + userInfo.getLoginStatus() + ")" ) <= 0 ) 147 return false; 148 149 JPlugin plugin = JSharedObjects.getPluginEngine().get( nPluginId ); 151 152 plugin.setUserInfo( userInfo ); 153 JSharedObjects.getPluginEngineListener().onAddPlugin( plugin ); 154 } 155 156 return true; 157 } 158 159 165 public boolean userDel( int nPluginId, JUserInfo userInfo ) { 166 167 if( ( userInfo == null ) || ( nPluginId == -1 ) ) 168 return false; 169 170 if( database.execUpdate( "DELETE FROM user WHERE profile_id=" + userInfo.getUserData( "profile_id" ).toString() + " AND plugin_id=" + nPluginId + " AND user_id='" + userInfo.getUserId() + "'" ) <= 0 ) 171 return false; 172 else { 173 database.execUpdate( "DELETE FROM contact_list WHERE profile_id=" + userInfo.getUserData( "profile_id" ).toString() + " AND plugin_id=" + nPluginId ); 174 database.execUpdate( "DELETE FROM messages WHERE profile_id=" + userInfo.getUserData( "profile_id" ).toString() + " AND plugin_id=" + nPluginId ); 175 176 if( Long.parseLong( userInfo.getUserData( "profile_id" ).toString() ) == getProfileId() ) 178 JSharedObjects.getPluginEngineListener().onRemovePlugin( JSharedObjects.getPluginEngine().get( nPluginId ) ); 179 } 180 181 return true; 182 } 183 184 190 public boolean updatePassword( int nPluginId, JUserInfo userInfo, String strPasswd ) { 191 192 if( ( nProfileId == -1 ) || ( nPluginId == -1 ) ) 193 return false; 194 195 userInfo.setPassword( strPasswd ); 196 197 if( database.execUpdate( "UPDATE user SET passwd='" + userInfo.getPassword() + "' WHERE profile_id=" + userInfo.getUserData( "profile_id" ).toString() + " AND plugin_id=" + nPluginId + " AND user_id='" + userInfo.getUserId() + "'" ) <= 0 ) 198 return false; 199 200 return true; 201 } 202 203 210 public JUserInfo getUserInfo( long nProfileId, int nPluginId ) { 211 212 JResultSet resultSet; 213 214 if( ( resultSet = database.execSQL( "SELECT user_id, alias_name, passwd, login_status, auto_connect FROM user WHERE profile_id =" + nProfileId + "AND plugin_id=" + nPluginId ) ) == null ) 215 return null; 216 217 try { 218 JUserInfo userInfo = null; 219 220 if( resultSet.getResultSet().next() ) { 221 userInfo = new JUserInfo( resultSet.getResultSet().getString( "user_id" ), 222 resultSet.getResultSet().getString( "alias_name" ), 223 resultSet.getResultSet().getString( "passwd" ), 224 resultSet.getResultSet().getInt( "login_status" ), 225 resultSet.getResultSet().getShort( "auto_connect" ) ); 226 userInfo.addUserData( "profile_id", Long.toString( nProfileId ) ); 227 } 228 229 resultSet.close(); 230 231 return userInfo; 232 } catch( java.sql.SQLException e ) { 233 System.err.println( "JProfileManager.getUserInfo() - SQLException " + e ); 234 return null; 235 } 236 } 237 238 243 public JUserInfo getUserInfo( int nPluginId ) { 244 245 return getUserInfo( nProfileId, nPluginId ); 246 } 247 248 254 public boolean checkForPluginUser( int nPluginId ) { 255 256 JResultSet resultSet; 257 258 if( nPluginId != -1 ) { 259 if( ( resultSet = database.execSQL( "SELECT COUNT(*) AS user_count FROM user WHERE profile_id =" + nProfileId + "AND plugin_id=" + nPluginId ) ) != null ) { 260 261 try { 262 resultSet.getResultSet().next(); 263 264 if( resultSet.getResultSet().getInt( "user_count" ) > 0 ) { 265 resultSet.close(); 266 return true; 267 } 268 else { 269 resultSet.close(); 270 return false; 271 } 272 } catch( java.sql.SQLException e ) { 273 System.err.println( "JProfileManager.checkForPluginUser() - SQLException " + e ); 274 } 275 } 276 } 277 278 return false; 279 } 280 281 287 public boolean hasPluginUserInProfile( long nProfileId ) { 288 289 JResultSet resultSet; 290 291 292 if( ( resultSet = database.execSQL( "SELECT COUNT(*) AS user_count FROM user WHERE profile_id =" + nProfileId ) ) != null ) { 293 294 try { 295 resultSet.getResultSet().next(); 296 297 if( resultSet.getResultSet().getInt( "user_count" ) > 0 ) { 298 resultSet.close(); 299 return true; 300 } 301 else { 302 resultSet.close(); 303 return false; 304 } 305 } catch( java.sql.SQLException e ) { 306 System.err.println( "JProfileManager.hasPluginUserInProfile() - SQLException " + e ); 307 } 308 } 309 310 return false; 311 } 312 313 318 public int profileAdd( java.lang.String strProfileName ) { 319 320 JResultSet resultSet; 321 int nProfileId = -1; 322 323 324 325 if( database.execUpdate( "INSERT INTO profile (profile_id, profile_desc) VALUES(NULL, '" + strProfileName.toLowerCase() + "')" ) <= 0 ) 326 return -1; 327 328 if( ( resultSet = database.execSQL( "CALL IDENTITY()" ) ) == null ) 329 return -1; 330 331 try { 332 resultSet.getResultSet().next(); 333 nProfileId = resultSet.getResultSet().getInt( 1 ); 334 resultSet.close(); 335 336 if( this.nProfileId == -1 ) { 338 this.nProfileId = nProfileId; 339 updateDefaultProfile( nProfileId ); 340 } 341 342 return nProfileId; 343 344 } catch( java.sql.SQLException e ) { 345 System.err.println( "JProfileManager.profileAdd() - SQLException " + e ); 346 return -1; 347 } 348 } 349 350 355 public boolean profileDel( int nProfileId ) { 356 357 if( database.execUpdate( "DELETE FROM profile WHERE profile_id=" + nProfileId ) <= 0 ) 358 return false; 359 360 database.execUpdate( "DELETE FROM user WHERE profile_id=" + nProfileId ); 361 database.execUpdate( "DELETE FROM contact_list WHERE profile_id=" + nProfileId ); 362 database.execUpdate( "DELETE FROM messages WHERE profile_id=" + nProfileId ); 363 database.execUpdate( "DELETE FROM active_plugins WHERE profile_id=" + nProfileId ); 364 365 return true; 366 } 367 368 375 public boolean profileUpdate( int nProfileId, java.lang.String strProfileName ) { 376 377 if( database.execUpdate( "UPDATE profile set profile_desc='" + strProfileName.toLowerCase() + "' WHERE profile_id=" + nProfileId ) <= 0 ) 378 return false; 379 380 return true; 381 } 382 383 387 public JResultSet getAllProfiles() { 388 389 JResultSet resultSet; 390 391 392 if( ( resultSet = database.execSQL( "SELECT * FROM profile" ) ) == null ) 393 return null; 394 395 return resultSet; 396 } 397 398 401 public java.lang.String getProfileName() { 402 403 JResultSet resultSet; 404 String strProfileDesc = null; 405 406 if( ( resultSet = database.execSQL( "SELECT profile_desc FROM profile WHERE profile_id =" + nProfileId ) ) == null ) 407 return null; 408 409 try { 410 resultSet.getResultSet().next(); 411 strProfileDesc = resultSet.getResultSet().getString( "profile_desc" ); 412 resultSet.close(); 413 } catch( java.sql.SQLException e ) { 414 System.err.println( "JProfileManager.getProfileName() - SQLException " + e ); 415 } 416 417 return strProfileDesc; 418 } 419 420 426 public boolean checkPluginInstall( int nPluginId, long nProfileId ) { 427 428 JResultSet resultSet; 429 430 431 if( nPluginId != -1 ) { 432 if( ( resultSet = database.execSQL( "SELECT COUNT(*) AS plugin_count FROM active_plugins WHERE profile_id =" + nProfileId + "AND plugin_id=" + nPluginId ) ) != null ) { 433 434 try { 435 resultSet.getResultSet().next(); 436 437 if( resultSet.getResultSet().getInt( "plugin_count" ) > 0 ) { 438 resultSet.close(); 439 return true; 440 } 441 else { 442 resultSet.close(); 443 return false; 444 } 445 } catch( java.sql.SQLException e ) { 446 System.err.println( "JProfileManager.checkPluginInstall() - SQLException " + e ); 447 } 448 } 449 } 450 451 return false; 452 } 453 454 458 public int getPluginCount( long nProfileId ) { 459 460 JResultSet resultSet; 461 462 if( nProfileId != -1 ) { 463 if( ( resultSet = database.execSQL( "SELECT COUNT(*) AS plugin_count FROM active_plugins WHERE profile_id =" + nProfileId ) ) != null ) { 464 try { 465 resultSet.getResultSet().next(); 466 467 int nCount = resultSet.getResultSet().getInt( "plugin_count" ); 468 resultSet.close(); 469 470 return nCount; 471 } catch( java.sql.SQLException e ) { 472 System.err.println( "JProfileManager.getPluginCount() - SQLException " + e ); 473 return -1; 474 } 475 } 476 } 477 478 return -1; 479 } 480 481 486 public boolean checkPluginInstall( int nPluginId ) { 487 488 return checkPluginInstall( nPluginId, nProfileId ); 489 } 490 491 496 public boolean checkProfile( java.lang.String strProfileName ) { 497 498 JResultSet resultSet; 499 500 501 if( ( resultSet = database.execSQL( "SELECT COUNT(*) AS profile_count FROM profile WHERE profile_desc='" + strProfileName.toLowerCase() + "'" ) ) != null ) { 502 503 try { 504 resultSet.getResultSet().next(); 505 506 if( resultSet.getResultSet().getInt( "profile_count" ) > 0 ) { 507 resultSet.close(); 508 return true; 509 } 510 else 511 return false; 512 } catch( java.sql.SQLException e ) { 513 System.err.println( "JProfileManager.checkProfile() - SQLException " + e ); 514 } 515 } 516 517 return false; 518 } 519 520 525 public int getPluginId( java.lang.String strClassName ) { 526 527 JResultSet resultSet; 528 529 530 if( ( resultSet = database.execSQL( "SELECT plugin_id FROM plugins WHERE class_name='" + strClassName + "'" ) ) != null ) { 531 532 try { 533 int nPluginId = -1; 534 535 if( resultSet.getResultSet().next() ) 536 nPluginId = resultSet.getResultSet().getInt( "plugin_id" ); 537 else { 538 resultSet.close(); 539 540 if( database.execUpdate( "INSERT INTO plugins (class_name) VALUES('" + strClassName + "')" ) > 0 ) { 541 if( ( resultSet = database.execSQL( "CALL IDENTITY()" ) ) != null ) { 542 resultSet.getResultSet().next(); 543 nPluginId = resultSet.getResultSet().getInt( 1 ); 544 } 545 } 546 } 547 548 resultSet.close(); 549 550 return nPluginId; 551 552 } catch( java.sql.SQLException e ) { 553 System.err.println( "JProfileManager.getPluginId() - SQLException " + e ); 554 } 555 } 556 557 return -1; 558 } 559 560 564 public JResultSet getAllPlugins() { 565 566 JResultSet resultSet; 567 568 569 if( ( resultSet = database.execSQL( "SELECT * FROM plugins" ) ) == null ) 570 return null; 571 572 return resultSet; 573 } 574 575 583 public boolean addPlugin( int nPluginId, long nProfileId ) { 584 585 if( database.execUpdate( "INSERT INTO active_plugins (profile_id, plugin_id) VALUES(" + nProfileId + ", " + nPluginId + ")" ) <= 0 ) 586 return false; 587 588 return true; 589 } 590 591 599 public boolean removePlugin( int nPluginId, long nProfileId ) { 600 601 JUserInfo userInfo = getUserInfo( nProfileId, nPluginId ); 602 603 userDel( nPluginId, userInfo ); 604 database.execUpdate( "DELETE FROM active_plugins WHERE profile_id=" + nProfileId + " AND plugin_id=" + nPluginId ); 605 606 return true; 607 } 608 } 609 610 | Popular Tags |