KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > planetamessenger > mos > engine > JProfileManager


1 /*
2     =========================================================================
3     Package engine - Implements the engine package.
4
5     This module is developed and maintained by PlanetaMessenger.org.
6     Specs, New and updated versions can be found in
7     http://www.planetamessenger.org
8     If you want contact the Team please send a email to Project Manager
9     Leidson Campos Alves Ferreira at leidson@planetamessenger.org
10
11     Copyright (C) since 2001 by PlanetaMessenger.org
12
13     This program is free software; you can redistribute it and/or modify
14     it under the terms of the GNU General Public License as published by
15     the Free Software Foundation; either version 2 of the License, or
16     (at your option) any later version.
17
18     This program is distributed in the hope that it will be useful,
19     but WITHOUT ANY WARRANTY; without even the implied warranty of
20     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21     GNU General Public License for more details.
22
23     You should have received a copy of the GNU General Public License
24     along with this program; if not, write to the Free Software
25     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26
27     =========================================================================
28 */

29 /**
30  *
31  * $Id: JProfileManager.java,v 1.23 2007/01/28 17:39:20 popolony2k Exp $
32  * $Author: popolony2k $
33  * $Name: $
34  * $Revision: 1.23 $
35  * $State: Exp $
36  *
37  */

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 JavaDoc strPasswd;
51   
52
53   /**
54    * Constructor. Initializes all Profile class
55    * data.
56    */

57   public JProfileManager() {
58    
59     database = JSharedObjects.getDatabase();
60   }
61
62   /**
63    * Set the System ProfileId.
64    * Call this method causes update in
65    * strProfileDesc (getProfileName())
66    * and strPasswd class variables;
67    * @param nProfileId The new ProfileId;
68    */

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 JavaDoc e ) {
82       System.err.println( "JProfileManager.setProfileId() - SQLException " + e );
83     }
84   }
85
86   /**
87    * Returns the System ProfileId.
88    */

89   public long getProfileId() {
90     
91     return nProfileId;
92   }
93
94   /**
95    * Update the default profile in
96    * the system database.
97    * @param nProfileId The profile that will
98    * be the default in system database;
99    */

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   /**
110    * Returns the default profile
111    * that should be used by system.
112    */

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 JavaDoc e ) {
130       System.err.println( "JProfileManager.getDefaultProfile() - SQLException " + e );
131       return -1;
132     }
133   }
134
135   /**
136    * Add/Update a user into system database.
137    * @param nPluginId The plugin id owner of this user;
138    * @param userInfo The owner user info;
139    */

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       // If INSERT was ok, the plugin has been added for the first time at this profile
150
JPlugin plugin = JSharedObjects.getPluginEngine().get( nPluginId );
151
152       plugin.setUserInfo( userInfo );
153       JSharedObjects.getPluginEngineListener().onAddPlugin( plugin );
154     }
155     
156     return true;
157   }
158
159   /**
160    * Remove the user and the contact list from
161    * this profile.
162    * @param nPluginId The owner pluginId;
163    * @param userInfo The owner user info;
164    */

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       // Dispatch event only if plugin is in active profile
177
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   /**
185    * Update user password into system database.
186    * @param nPluginId The plugin id owner of this user;
187    * @param userInfo The user information object;
188    * @param strPasswd The new password to update;
189    */

190   public boolean updatePassword( int nPluginId, JUserInfo userInfo, String JavaDoc 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   /**
204    * Returns the plugin user info.
205    * @param nProfileId The profile id to retrieve
206    * the user info;
207    * @param nPluginId The PluginId to retrieve
208    * the user info;
209    */

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 JavaDoc e ) {
233       System.err.println( "JProfileManager.getUserInfo() - SQLException " + e );
234       return null;
235     }
236   }
237   
238   /**
239    * Returns the plugin user info for current profile.
240    * @param nPluginId The PluginId to retrieve
241    * the user info;
242    */

243   public JUserInfo getUserInfo( int nPluginId ) {
244     
245     return getUserInfo( nProfileId, nPluginId );
246   }
247
248   /**
249    * Verify if the Plugin has user
250    * assigned to it.
251    * @param nPluginId The PluginId that will
252    * be checked.
253    */

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 JavaDoc e ) {
273           System.err.println( "JProfileManager.checkForPluginUser() - SQLException " + e );
274         }
275       }
276     }
277     
278     return false;
279   }
280   
281   /**
282    * Verify if a profile has users (for each plugin)
283    * assigned to it.
284    * @param nProfileId The ProfileId that will
285    * be checked.
286    */

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 JavaDoc e ) {
306         System.err.println( "JProfileManager.hasPluginUserInProfile() - SQLException " + e );
307       }
308     }
309     
310     return false;
311   }
312
313   /**
314    * Add a new profile entry into system database.
315    * @param strProfileName The new profile that will be
316    * added into database;
317    */

318   public int profileAdd( java.lang.String JavaDoc 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 no profile is selected, do this profile the default
337
if( this.nProfileId == -1 ) {
338         this.nProfileId = nProfileId;
339         updateDefaultProfile( nProfileId );
340       }
341       
342       return nProfileId;
343       
344     } catch( java.sql.SQLException JavaDoc e ) {
345       System.err.println( "JProfileManager.profileAdd() - SQLException " + e );
346       return -1;
347     }
348   }
349
350   /**
351    * Remove a profile from system database.
352    * @param nProfileId The Id from profile that
353    * will be removed.
354    */

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   /**
369    * Add a new profile entry into system database.
370    * @param nProfileId The profile id that will be
371    * changed;
372    * @param strProfileName The new profile name for
373    * this ProfileId;
374    */

375   public boolean profileUpdate( int nProfileId, java.lang.String JavaDoc 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   /**
384    * Returns a resultset with all
385    * profiles stored in database.
386    */

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   /**
399    * Returns the profile name.
400    */

401   public java.lang.String JavaDoc getProfileName() {
402     
403     JResultSet resultSet;
404     String JavaDoc 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 JavaDoc e ) {
414       System.err.println( "JProfileManager.getProfileName() - SQLException " + e );
415     }
416
417     return strProfileDesc;
418   }
419
420   /**
421    * Check if the plugin is installed on the current
422    * profile.
423    * @param nPluginId The PluginId that will be checked;
424    * @param nProfileId The ProfileId that will be checked;
425    */

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 JavaDoc e ) {
446           System.err.println( "JProfileManager.checkPluginInstall() - SQLException " + e );
447         }
448       }
449     }
450     
451     return false;
452   }
453   
454   /**
455    * Return the plugin count for specified profile.<br>
456    * @param nProfileId The ProfileId that will be checked;<br>
457    */

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 JavaDoc e ) {
472           System.err.println( "JProfileManager.getPluginCount() - SQLException " + e );
473           return -1;
474         }
475       }
476     }
477
478     return -1;
479   }
480
481   /**
482    * Check if the plugin is installed on the current
483    * profile.
484    * @param nPluginId The PluginId that will be checked;
485    */

486   public boolean checkPluginInstall( int nPluginId ) {
487
488     return checkPluginInstall( nPluginId, nProfileId );
489   }
490
491   /**
492    * Check if a profile name exist in database.
493    * @param strProfileName The profile that will be
494    * checked;
495    */

496   public boolean checkProfile( java.lang.String JavaDoc 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 JavaDoc e ) {
513         System.err.println( "JProfileManager.checkProfile() - SQLException " + e );
514       }
515     }
516     
517     return false;
518   }
519
520   /**
521    * Get the PluginId from a plugin.
522    * @param strClassName The Class name
523    * to find;
524    */

525   public int getPluginId( java.lang.String JavaDoc 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 JavaDoc e ) {
553         System.err.println( "JProfileManager.getPluginId() - SQLException " + e );
554       }
555     }
556     
557     return -1;
558   }
559
560   /**
561    * Returns a resultset with all
562    * plugins stored in database.
563    */

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   /**
576    * Add a plugin in the specified
577    * profile.
578    * @param nPluginId The PluginId that will
579    * be added;
580    * @param nProfileId The ProfileId owner of this
581    * plugin;
582    */

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   /**
592    * Remove a plugin from specified
593    * profile.
594    * @param nPluginId The PluginId that will
595    * be added;
596    * @param nProfileId The ProfileId owner of this
597    * plugin;
598    */

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 // JProfileManager class
Popular Tags