KickJava   Java API By Example, From Geeks To Geeks.

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


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: JContactListManager.java,v 1.38 2007/01/29 18:09:12 popolony2k Exp $
32  * $Author: popolony2k $
33  * $Name: $
34  * $Revision: 1.38 $
35  * $State: Exp $
36  *
37  */

38
39 package org.planetamessenger.mos.engine;
40
41 import org.planetamessenger.mos.forms.*;
42 import org.planetamessenger.mos.ui.*;
43 import org.planetamessenger.plugin.*;
44 import org.planetamessenger.db.*;
45 import org.planetamessenger.util.*;
46 import java.sql.Connection JavaDoc;
47 import java.sql.PreparedStatement JavaDoc;
48 import java.sql.ResultSet JavaDoc;
49 import java.sql.SQLException JavaDoc;
50 import java.util.*;
51
52
53
54 public class JContactListManager implements JContactListListener {
55
56   JMOSWindow parent = null;
57   
58
59   /**
60    * Creates and initializes all
61    * JContactListManager class.
62    */

63   public JContactListManager() {
64
65     parent = JSharedObjects.getMainWindow();
66   }
67
68   /**
69    * Save a message on contactList.<br>
70    * @param sender The plugin that sent this event;<br>
71    * @param strFromUserId Id from user that sent this message;<br>
72    * @param nMessageStatus The message status;<br>
73    * @param strMsg The Message that will be saved;<br>
74    * If the message was saved the function returns true, else false.
75    */

76   public synchronized boolean saveMessage( org.planetamessenger.plugin.JPlugin sender, java.lang.String JavaDoc strFromUserId, int nMessageStatus, java.lang.String JavaDoc strMsg ) {
77
78     strFromUserId = strFromUserId.toLowerCase();
79
80     JContactList contactList = parent.getContactList();
81     JMOSSendMessageDlg messageDlg = JSharedObjects.getPluginEngine().getMessageWindow( sender.getPluginProperties().getPluginId() );
82     long nProfileId = JSharedObjects.getProfileManager().getProfileId();
83     JContactListItem item;
84
85
86     synchronized( contactList ) {
87       item = contactList.getItem( sender.getPluginProperties().getPluginId(), strFromUserId );
88     }
89     
90     // Exchange special chars to work correctly in JTextEditor HTML enabled
91
String JavaDoc strMsgHtml = JTextUtil.encodeHtml( strMsg );
92
93     /*
94      * Verify if contact doesn't exist in contact list
95      * and request authorization to add contact into
96      * contact list.
97      */

98     if( item == null ) {
99
100       int nRet;
101
102       addToContactList( sender, strFromUserId, strFromUserId, true );
103       synchronized( contactList ) {
104         item = contactList.getItem( sender.getPluginProperties().getPluginId(), strFromUserId );
105       }
106       nRet = javax.swing.JOptionPane.showConfirmDialog( null, JSharedObjects.getLanguageManager().getStringEx( "DO_YOU_WANT_ADD_USER" ) + " " + item.getUserId() + " " + JSharedObjects.getLanguageManager().getStringEx( "TO_CONTACT_LIST" ), JSharedObjects.getLanguageManager().getStringEx( "ADD_TO_CONTACT_LIST" ), javax.swing.JOptionPane.YES_NO_OPTION, javax.swing.JOptionPane.QUESTION_MESSAGE );
107
108       if( nRet == javax.swing.JOptionPane.YES_OPTION ) {
109         // Fire the plugin's onAddUserToContactList event to add the user to contact list
110
JExtendedPopupWindow popupWindow = new JExtendedPopupWindow( sender.getPluginProperties().getName(), JSharedObjects.getLanguageManager().getStringEx( "THE_USER" ) + " " + item.getNickName() + " " + JSharedObjects.getLanguageManager().getStringEx( "WAS_ADDED_TO_YOUR_CONTACT_LIST" ), null );
111
112         item.setContactFlag( JContactListItem.CONTACT_IN_LIST );
113         updateContactInfo( item );
114
115         ( ( JPlugin ) JSharedObjects.getPluginEngine().get( sender.getPluginProperties().getPluginId() ) ).fireOnAddUserToContactList( strFromUserId );
116         popupWindow.showPopup();
117       }
118     }
119
120     if( messageDlg.contains( item ) ) {
121       
122       if( !messageDlg.isItemActive( item ) )
123         messageDlg.newTab( item );
124
125       strMsgHtml = "<b>" + item.getNickName() + ": </b>" + strMsgHtml;
126       messageDlg.setMessage( item, strMsgHtml );
127       return false;
128     }
129     else {
130       String JavaDoc strPopupMessage = item.getNickName() + " " + JSharedObjects.getLanguageManager().getStringEx( "SAYS" ) + " : " + strMsg;
131       JExtendedPopupWindow popupWindow = new JExtendedPopupWindow( sender.getPluginProperties().getName(), strPopupMessage, null );
132       Connection JavaDoc conn = null;
133       PreparedStatement JavaDoc st = null;
134
135       
136       popupWindow.showPopup();
137       setItemStatus( sender, strFromUserId, nMessageStatus );
138
139       try{
140         conn = JSharedObjects.getDatabase().getConnection();
141         st = conn.prepareStatement( "INSERT INTO messages (profile_id, plugin_id, user_id, msg_type, msg_datetime, message) VALUES(?, ?, ?, ?, NOW(), ?)" );
142         st.setLong( 1, nProfileId );
143         st.setInt( 2, sender.getPluginProperties().getPluginId() );
144         st.setString( 3, strFromUserId );
145         st.setInt( 4, nMessageStatus );
146         st.setString( 5, strMsg );
147         
148         st.executeUpdate();
149       }catch( SQLException JavaDoc e ) {
150         System.err.println( "JContactListManager.saveMessage() - " + e );
151       } finally{
152         if( st != null )
153           try {
154             st.close();
155           } catch( SQLException JavaDoc e ) {
156             System.err.println( "JContactListManager.saveMessage() - " + e );
157           }
158       }
159     }
160
161     return true;
162   }
163
164   /**
165    * Set the item id status.
166    * @param plugin The plugin owner of this event;
167    * @param strItemId The Item id that status will
168    * be changed.
169    * @param nNewStatus The new status of this item;
170    */

171   public void setItemStatus( org.planetamessenger.plugin.JPlugin plugin, String JavaDoc strItemId, int nNewStatus ) {
172
173     JContactListItem item;
174     JContactList contactList = parent.getContactList();
175
176
177     synchronized( contactList ) {
178       item = contactList.getItem( plugin.getPluginProperties().getPluginId(), strItemId.toLowerCase() );
179     }
180
181     if( item == null ) {
182       System.err.println( "JContactListManager.setItemStatus() - Item not in list. Nothing to do." );
183       return;
184     }
185       
186     // User going from Offline to Online
187
if( ( nNewStatus > JPlugin.STATUS_OFFLINE ) && ( nNewStatus != JPlugin.STATUS_MESSAGE ) && ( item.getStatus() == JPlugin.STATUS_OFFLINE ) ) {
188       JExtendedPopupWindow popupWindow = new JExtendedPopupWindow( plugin.getPluginProperties().getName(), " " + item.getNickName() + " " + JSharedObjects.getLanguageManager().getStringEx( "IS" ) + " " + JSharedObjects.getLanguageManager().getStringEx( "ONLINE" ), null );
189       popupWindow.showPopup();
190     }
191
192     synchronized( contactList ) {
193       contactList.setItemStatus( item, nNewStatus );
194     }
195   }
196
197   /**
198    * Set all items of specified plugin to
199    * a status specified by parameter.
200    * @param plugin The plugin to set the new status
201    * for users;
202    * @param nNewStatus The new status to set;
203    */

204   public void setAllItemsStatus( org.planetamessenger.plugin.JPlugin plugin, int nNewStatus ) {
205     
206     JContactList contactList = parent.getContactList();
207     
208     synchronized( contactList ) {
209       contactList.setAllItemsStatus( plugin.getPluginProperties().getPluginId(), nNewStatus );
210     }
211   }
212   
213   /**
214    * Remove all contact list items. This method affects the user interface only.
215    * The contact remains on database.
216    * @param plugin The plugin that contact will be removed;
217    */

218   public void removeAllUIItems( org.planetamessenger.plugin.JPlugin plugin ) {
219
220     JContactList contactList = parent.getContactList();
221
222     synchronized( contactList ) {
223       contactList.removePluginItems( plugin.getPluginProperties().getPluginId() );
224     }
225   }
226
227   /**
228    * Add a user into contact list.
229    * @param plugin The plugin that sent this event;
230    * @param strNick The user nick;
231    * @param strUserId The user id that will be added;
232    * @param bNotInListUser Notify that the inserted user not
233    * will be added to contact list (is a Not InList user);
234    */

235   boolean addToContactList( org.planetamessenger.plugin.JPlugin plugin, String JavaDoc strNick, String JavaDoc strUserId, boolean bNotInListUser ) {
236
237     Connection JavaDoc conn = null;
238     PreparedStatement JavaDoc st = null;
239     JContactListItem item = null;
240     JContactList contactList = JSharedObjects.getMainWindow().getContactList();
241     long nProfileId = JSharedObjects.getProfileManager().getProfileId();
242     int nPluginId = plugin.getPluginProperties().getPluginId();
243     int nContactFlag = ( bNotInListUser ? JContactListItem.CONTACT_NOT_IN_LIST : JContactListItem.CONTACT_IN_LIST );
244
245
246     strUserId = strUserId.toLowerCase();
247
248     synchronized( contactList ) {
249       item = contactList.getItem( nPluginId, strUserId );
250     }
251
252     if( item == null ) { // User already in list ???
253
item = new JContactListItem( plugin.getPluginProperties().getPluginId(), strUserId, strNick );
254       
255       try {
256         conn = JSharedObjects.getDatabase().getConnection();
257         st = conn.prepareStatement( "INSERT INTO contact_list (profile_id, plugin_id, user_id, nick_name, visible, blocked, contact_flag) VALUES(?,?,?,?,0,0,?)" );
258         st.setLong( 1, nProfileId );
259         st.setInt( 2, nPluginId );
260         st.setString( 3, strUserId );
261         st.setString( 4, strNick );
262         st.setInt( 5, nContactFlag );
263         st.executeUpdate();
264       } catch( SQLException JavaDoc e ) {
265         System.err.println( "JContactListManager.addToContactList() - " + e );
266       } finally{
267         if( st != null )
268           try {
269             st.close();
270           } catch( SQLException JavaDoc e ) {
271             System.err.println( "JContactListManager.addToContactList() - " + e );
272           }
273       }
274     }
275
276     if( !bNotInListUser ) {
277       item.setContactFlag( JContactListItem.CONTACT_IN_LIST );
278       item.setStatus( JPlugin.STATUS_OFFLINE );
279     }
280     else {
281       item.setStatus( JPlugin.STATUS_NOT_IN_LIST );
282       item.setContactFlag( JContactListItem.CONTACT_NOT_IN_LIST );
283     }
284
285     updateContactInfo( item );
286     
287     synchronized( contactList ) {
288       contactList.addItem( item );
289     }
290
291     /*
292      * User has been added into contact list.
293      */

294     if( !bNotInListUser )
295       ( ( JPlugin ) JSharedObjects.getPluginEngine().get( item.getPluginId() ) ).fireOnRequestAuthorization( strUserId, strNick );
296
297     return true;
298   }
299
300   /**
301    * Add a user into contact list.
302    * @param plugin The plugin that sent
303    * this event;
304    * @param strNick The user nick;
305    * @param strUserId The user id that will be
306    * added;
307    */

308   public synchronized boolean addToContactList( org.planetamessenger.plugin.JPlugin plugin, String JavaDoc strNick, String JavaDoc strUserId ) {
309
310     return addToContactList( plugin, strNick, strUserId, false );
311   }
312
313   /**
314    * Remove a user from contact list.
315    * @param plugin The plugin that sent
316    * this event;
317    * @param strUserId The user id that will be
318    * removed;
319    */

320   public synchronized boolean removeFromContactList( org.planetamessenger.plugin.JPlugin plugin, String JavaDoc strUserId ) {
321
322     JContactList contactList = JSharedObjects.getMainWindow().getContactList();
323     JContactListItem item = new JContactListItem( plugin.getPluginProperties().getPluginId(), strUserId.toLowerCase(), "" );
324     long nPluginId = plugin.getPluginProperties().getPluginId();
325     long nProfileId = JSharedObjects.getProfileManager().getProfileId();
326
327
328     // Remove item from user interface contact list
329
synchronized( contactList ) {
330       contactList.removeItem( item );
331     }
332
333     // Remove all messages of this user and the user from contact list database
334
JSharedObjects.getDatabase().execUpdate( "DELETE FROM messages WHERE profile_id=" + nProfileId + " AND plugin_id=" + nPluginId + " AND LCASE(user_id)='" + item.getUserId() + "'" );
335     JSharedObjects.getDatabase().execUpdate( "DELETE FROM contact_list WHERE profile_id=" + nProfileId + " AND plugin_id=" + nPluginId + " AND LCASE(user_id)='" + item.getUserId() + "'" );
336
337     return true;
338   }
339
340   /**
341    * Update a user alias in contact list.
342    * @param plugin The plugin that sent this event;
343    * @param strUserId The user id that will be updated;
344    * @param strNewNick The user new nick name;
345    */

346   public synchronized boolean updateScreenName( org.planetamessenger.plugin.JPlugin plugin, java.lang.String JavaDoc strUserId, java.lang.String JavaDoc strNewNick ) {
347
348     JContactList contactList = parent.getContactList();
349     JContactListItem item;
350
351
352     synchronized( contactList ) {
353       item = contactList.getItem( plugin.getPluginProperties().getPluginId(), strUserId.toLowerCase() );
354     }
355
356     // If the item is null, the screeen name updated is the self user screen name
357
if( item != null ) {
358       int nPluginId = plugin.getPluginProperties().getPluginId();
359       long nProfileId = JSharedObjects.getProfileManager().getProfileId();
360       Connection JavaDoc conn = JSharedObjects.getDatabase().getConnection();
361       PreparedStatement JavaDoc st = null;
362
363       try {
364         item.setNickName( strNewNick );
365
366         st = conn.prepareStatement( "UPDATE contact_list SET nick_name= ? WHERE profile_id= ? AND plugin_id= ? AND user_id= ?" );
367         st.setString( 1, strNewNick );
368         st.setLong( 2, nProfileId );
369         st.setInt( 3, nPluginId );
370         st.setString( 4, item.getUserId() );
371         st.executeUpdate();
372
373         synchronized( contactList ) {
374           ( ( JContactListModel ) contactList.getModel() ).update();
375         }
376
377       } catch( java.sql.SQLException JavaDoc e ) {
378         System.out.println( "JContactListManager.updateScreenName() - SQLException " + e );
379         return false;
380       } finally {
381         if( st != null )
382           try {
383             st.close();
384           } catch( SQLException JavaDoc e ) {
385             System.err.println( "JContactListManager.updateScreenName() - " + e );
386           }
387       }
388     }
389
390     return true;
391   }
392   
393   /**
394    * Update the privacy status of contact list
395    * item;
396    * Parameters:
397    * @param contactItem The contact list item to change;
398    */

399   public synchronized boolean updatePrivacy( JContactListItem contactItem ) {
400
401     JContactList contactList = parent.getContactList();
402     long nProfileId = JSharedObjects.getProfileManager().getProfileId();
403     Connection JavaDoc conn = JSharedObjects.getDatabase().getConnection();
404     PreparedStatement JavaDoc st = null;
405     JContactListItem item;
406
407
408     try {
409
410       synchronized( contactList ) {
411         item = contactList.getItem( contactItem.getPluginId(), contactItem.getUserId().toLowerCase() );
412       }
413
414       // Update contact list (UI) item
415
if( item != null )
416         item.copy( contactItem );
417
418       st = conn.prepareStatement( "UPDATE contact_list SET visible= ? , blocked= ? WHERE profile_id= ? AND plugin_id= ? AND LCASE(user_id)= ? " );
419       st.setInt( 1, ( contactItem.getVisible() ? 1 : 0 ) );
420       st.setInt( 2, ( contactItem.getBlocked() ? 1 : 0 ) );
421       st.setLong( 3, nProfileId );
422       st.setInt( 4, contactItem.getPluginId() );
423       st.setString( 5, contactItem.getUserId().toLowerCase() );
424
425       st.executeUpdate();
426
427     } catch( java.sql.SQLException JavaDoc e ) {
428       System.out.println( "JContactListManager.updatePrivacy() - SQLException " + e );
429       return false;
430     } finally {
431       if( st != null )
432         try {
433           st.close();
434         } catch( SQLException JavaDoc e ) {
435           System.err.println( "JContactListManager.addToContactList() - " + e );
436         }
437     }
438
439     return true;
440   }
441     
442   /**
443    * Update the contact information.<br>
444    * @param contactItem The contact to update;<br>
445    */

446   public synchronized boolean updateContactInfo( JContactListItem contactItem ) {
447
448     PreparedStatement JavaDoc st = null;
449     Connection JavaDoc conn = JSharedObjects.getDatabase().getConnection();
450     long nProfileId = JSharedObjects.getProfileManager().getProfileId();
451     JContactList contactList = parent.getContactList();
452     JContactListItem item;
453     String JavaDoc strQuery;
454
455
456     synchronized( contactList ) {
457       item = contactList.getItem( contactItem.getPluginId(), contactItem.getUserId().toLowerCase() );
458     }
459
460     // Update the contact list (UI) item
461
if( item != null )
462       item.copy( contactItem );
463
464     try {
465       strQuery = "UPDATE contact_list SET first_name= ? , last_name= ? , email= ? , notes= ?, gender= ? , age= ? , contact_flag= ? WHERE profile_id= ? AND plugin_id= ? AND LCASE(user_id)= ?";
466
467       st = conn.prepareStatement( strQuery );
468       
469       st.setString( 1, contactItem.getFirstName() );
470       st.setString( 2, contactItem.getLastName() );
471       st.setString( 3, contactItem.getEmail() );
472       st.setString( 4, contactItem.getNotes() );
473       st.setInt( 5, contactItem.getGender() );
474       st.setInt( 6, contactItem.getAge() );
475       st.setInt( 7, contactItem.getContactFlag() );
476       st.setLong( 8, nProfileId );
477       st.setInt( 9, contactItem.getPluginId() );
478       st.setString( 10, contactItem.getUserId().toLowerCase() );
479
480       st.executeUpdate();
481   
482     } catch( java.sql.SQLException JavaDoc e ) {
483       System.out.println( "JContactListManager.updateContactInfo() - SQLException " + e );
484       return false;
485     } finally {
486       if( st != null )
487         try {
488           st.close();
489         } catch( SQLException JavaDoc e ) {
490           System.err.println( "JContactListManager.updateContactInfo() - " + e );
491         }
492     }
493
494     return true;
495   }
496
497   /**
498    * Get all ContactList from database
499    * @param plugin The plugin owner of this
500    * contact list;
501    */

502   public synchronized Hashtable getContactList( org.planetamessenger.plugin.JPlugin plugin ) {
503
504     if( plugin != null ) {
505       long nProfileId = JSharedObjects.getProfileManager().getProfileId();
506       Connection JavaDoc conn = JSharedObjects.getDatabase().getConnection();
507       PreparedStatement JavaDoc st = null;
508       ResultSet JavaDoc resultSet = null;
509
510
511       try {
512         st = conn.prepareStatement( "SELECT * FROM contact_list WHERE profile_id = ? AND plugin_id = ? " );
513         st.setLong( 1, nProfileId );
514         st.setInt( 2, plugin.getPluginProperties().getPluginId() );
515
516         resultSet = st.executeQuery();
517
518         if( resultSet == null )
519           return null;
520
521         Hashtable contactList = new Hashtable();
522         JContactList contactListUI = parent.getContactList();
523
524         while( resultSet.next() ) {
525
526           JContactListItem contactItem = new JContactListItem();
527           JContactListItem contactItemUI = null;
528
529           contactItem.setUserId( resultSet.getString( "user_id" ).toLowerCase() );
530           contactItem.setNickName( resultSet.getString( "nick_name" ) );
531           contactItem.setFirstName( resultSet.getString( "first_name" ) );
532           contactItem.setLastName( resultSet.getString( "last_name" ) );
533           contactItem.setLastName( resultSet.getString( "last_name" ) );
534           contactItem.setGender( resultSet.getInt( "gender" ) );
535           contactItem.setEmail( resultSet.getString( "email" ) );
536           contactItem.setAge( resultSet.getInt( "age" ) );
537           contactItem.setGroupId( resultSet.getInt( "group_id" ) );
538           contactItem.setPluginId( plugin.getPluginProperties().getPluginId() );
539           contactItem.setNotes( resultSet.getString( "notes" ) );
540           contactItem.setContactFlag( resultSet.getInt( "contact_flag" ) );
541           
542           synchronized( contactListUI ) {
543             contactItemUI = contactListUI.getItem( plugin.getPluginProperties().getPluginId(), contactItem.getUserId() );
544           }
545
546           // Retrieve the contact item status from ContactList component
547
if( contactItemUI != null )
548             contactItem.setStatus( contactItemUI.getStatus() );
549
550           if( resultSet.getInt( "visible" ) == 0 )
551             contactItem.setVisible( false );
552           else
553             contactItem.setVisible( true );
554
555           if( resultSet.getInt( "blocked" ) == 0 )
556             contactItem.setBlocked( false );
557           else
558             contactItem.setBlocked( true );
559
560           contactList.put( contactItem.getUserId(), contactItem );
561         }
562
563         resultSet.close();
564
565         return contactList;
566
567       } catch( java.sql.SQLException JavaDoc e ) {
568         System.out.println( "JContactListManager.getContactList() - SQLException " + e );
569         return null;
570       } finally {
571         if( resultSet != null )
572           try {
573             resultSet.close();
574           } catch( SQLException JavaDoc e ) {
575             System.err.println( "JContactListManager.getContactList()" + e );
576           }
577
578         if( st != null )
579           try {
580             st.close();
581           } catch( SQLException JavaDoc e ) {
582             System.err.println( "JContactListManager.getContactList()" + e );
583           }
584       }
585     }
586     else
587       return null;
588   }
589   
590   /**
591    * Return the contact information stored in dtabase for
592    * specified userid.
593    * @param plugin The plugin owner of this request;
594    * @param strUserId The User id to retrieve information;
595    */

596   public synchronized JContactListItem getContact( org.planetamessenger.plugin.JPlugin plugin, String JavaDoc strUserId ) {
597
598     if( plugin != null ) {
599       long nProfileId = JSharedObjects.getProfileManager().getProfileId();
600       Connection JavaDoc conn = JSharedObjects.getDatabase().getConnection();
601       PreparedStatement JavaDoc st = null;
602       ResultSet JavaDoc resultSet = null;
603       JContactListItem contactItem = null;
604       JContactList contactList = parent.getContactList();
605
606
607       try {
608         st = conn.prepareStatement( "SELECT * FROM contact_list WHERE profile_id = ? AND plugin_id = ? AND user_id = ?" );
609         st.setLong( 1, nProfileId );
610         st.setInt( 2, plugin.getPluginProperties().getPluginId() );
611         st.setString( 3, strUserId.toLowerCase() );
612
613         resultSet = st.executeQuery();
614
615         if( resultSet == null )
616           return null;
617         
618         if( resultSet.next() ) {
619
620           contactItem = new JContactListItem();
621           JContactListItem contactItemUI = null;
622
623           contactItem.setUserId( resultSet.getString( "user_id" ).toLowerCase() );
624           contactItem.setNickName( resultSet.getString( "nick_name" ) );
625           contactItem.setFirstName( resultSet.getString( "first_name" ) );
626           contactItem.setLastName( resultSet.getString( "last_name" ) );
627           contactItem.setLastName( resultSet.getString( "last_name" ) );
628           contactItem.setGender( resultSet.getInt( "gender" ) );
629           contactItem.setEmail( resultSet.getString( "email" ) );
630           contactItem.setAge( resultSet.getInt( "age" ) );
631           contactItem.setGroupId( resultSet.getInt( "group_id" ) );
632           contactItem.setPluginId( plugin.getPluginProperties().getPluginId() );
633           contactItem.setNotes( resultSet.getString( "notes" ) );
634           contactItem.setContactFlag( resultSet.getInt( "contact_flag" ) );
635           
636           synchronized( contactList ) {
637             contactItemUI = contactList.getItem( plugin.getPluginProperties().getPluginId(), contactItem.getUserId() );
638           }
639
640           // Retrieve the contact item status from ContactList component
641
if( contactItemUI != null )
642             contactItem.setStatus( contactItemUI.getStatus() );
643
644           if( resultSet.getInt( "visible" ) == 0 )
645             contactItem.setVisible( false );
646           else
647             contactItem.setVisible( true );
648
649           if( resultSet.getInt( "blocked" ) == 0 )
650             contactItem.setBlocked( false );
651           else
652             contactItem.setBlocked( true );
653
654         }
655
656         resultSet.close();
657
658       } catch( java.sql.SQLException JavaDoc e ) {
659         System.out.println( "JContactListManager.getContact() - SQLException " + e );
660         return null;
661       } finally {
662         if( resultSet != null )
663           try {
664             resultSet.close();
665           } catch( SQLException JavaDoc e ) {
666             System.err.println( "JContactListManager.getContact()" + e );
667           }
668
669         if( st != null )
670           try {
671             st.close();
672           } catch( SQLException JavaDoc e ) {
673             System.err.println( "JContactListManager.getContact()" + e );
674           }
675       }
676       
677       return contactItem;
678     }
679
680     return null;
681   }
682   
683   /**
684    * Return if user is present in contact list.
685    * @param The plugin owner of this request;
686    * @param strUserId The user id to find;
687    */

688   public synchronized boolean isContact( JPlugin plugin, String JavaDoc strUserId ) {
689     
690     long nProfileId = JSharedObjects.getProfileManager().getProfileId();
691     Connection JavaDoc conn = JSharedObjects.getDatabase().getConnection();
692     PreparedStatement JavaDoc st = null;
693     ResultSet JavaDoc resultSet = null;
694     int nRet = 0;
695
696
697     try {
698       st = conn.prepareStatement( "SELECT COUNT(*) AS count_contacts FROM contact_list WHERE profile_id = ? AND plugin_id = ? AND LCASE(user_id) = ?" );
699       st.setLong( 1, nProfileId );
700       st.setInt( 2, plugin.getPluginProperties().getPluginId() );
701       st.setString( 3, strUserId.toLowerCase() );
702
703       resultSet = st.executeQuery();
704   
705       if( resultSet == null )
706         return false;
707   
708       if( resultSet.next() )
709         nRet = resultSet.getInt( "count_contacts" );
710
711       resultSet.close();
712
713       return ( nRet > 0 ? true : false );
714
715     } catch( SQLException JavaDoc e ) {
716       System.err.println( "JContactListManager.isContact()" + e );
717     }
718
719     return true;
720   }
721
722   /**
723    * Update the contact list user interface.
724    */

725   public void updateUI() {
726     
727     JContactList contactList = parent.getContactList();
728     
729     synchronized( contactList ) {
730       contactList.repaint();
731     }
732   }
733
734   /**
735    * Loads all stored messages from
736    * database and update the owner
737    * status (to message) in contact
738    * list.
739    */

740   public synchronized void loadMessagesToContactList() {
741     
742     long nProfileId = JSharedObjects.getProfileManager().getProfileId();
743     JContactListItem item = new JContactListItem();
744     Connection JavaDoc conn = null;
745     PreparedStatement JavaDoc st = null;
746     ResultSet JavaDoc resultSet = null;
747     JContactList contactList = parent.getContactList();
748     
749     try{
750       conn = JSharedObjects.getDatabase().getConnection();
751       st = conn.prepareStatement( "SELECT * FROM messages WHERE profile_id = ?" );
752       st.setLong( 1, nProfileId );
753
754       resultSet = st.executeQuery();
755       
756       int nCount = 0;
757       
758       if( resultSet == null )
759         return;
760
761       while( resultSet.next() ) {
762         item.setPluginId( resultSet.getInt( "plugin_id" ) );
763         item.setUserId( resultSet.getString( "user_id" ).toLowerCase() );
764         
765         synchronized( contactList ) {
766           contactList.setItemStatus( item, resultSet.getInt( "msg_type" ) );
767         }
768         
769         nCount++;
770       }
771       
772       // Popup about unread messages
773
if( nCount > 0 ) {
774         JExtendedPopupWindow popupWindow = new JExtendedPopupWindow( JSharedObjects.getLanguageManager().getStringEx( "MESSAGE" ), JSharedObjects.getLanguageManager().getStringEx( "YOU_HAVE" ) + " " + nCount + " " + JSharedObjects.getLanguageManager().getStringEx( "UNREAD_MESSAGES" ), null );
775         popupWindow.showPopup();
776       }
777
778     } catch( java.sql.SQLException JavaDoc e ) {
779       System.out.println( "JContactListManager.loadMessagesToContactList() - SQLException " + e );
780     }finally{
781       if( resultSet != null )
782         try {
783           resultSet.close();
784         } catch ( SQLException JavaDoc e ) {
785           System.err.println( "JContactListManager.loadMessagesToContactList() - " + e );
786         }
787       if( st != null )
788         try {
789           st.close();
790         } catch ( SQLException JavaDoc e ) {
791           System.err.println( "JContactListManager.loadMessagesToContactList() - " + e );
792         }
793     }
794   }
795
796   /**
797    * Get the last message stored in database to the specified user;
798    * @param item The message owner;
799    */

800   public synchronized String JavaDoc getContactMessages( JContactListItem item ) {
801     
802     long nProfileId = JSharedObjects.getProfileManager().getProfileId();
803     int nCount = 0;
804     StringBuffer JavaDoc strMsg = new StringBuffer JavaDoc();
805     Connection JavaDoc conn = null;
806     ResultSet JavaDoc resultSet = null;
807     PreparedStatement JavaDoc st = null;
808
809
810     try{
811       conn = JSharedObjects.getDatabase().getConnection();
812       st = conn.prepareStatement( "SELECT * FROM messages WHERE profile_id = ? AND plugin_id = ? AND LCASE(user_id) = ? ORDER BY msg_datetime ASC" );
813
814       st.setLong( 1, nProfileId );
815       st.setInt( 2, item.getPluginId() );
816       st.setString( 3, item.getUserId().toLowerCase() );
817       
818       resultSet = st.executeQuery();
819       
820       if( resultSet == null )
821         return null;
822     
823       while( resultSet.next() ) {
824         
825         if( strMsg.length() != 0 )
826           strMsg.append( "<br>" );
827         
828         strMsg.append("<b>")
829         .append( item.getNickName() )
830         .append( ": </b>" )
831         .append(
832           JTextUtil.encodeHtml(
833             resultSet.getString( "message" )
834           )
835         );
836         
837         nCount++;
838       }
839
840       resultSet.close();
841       resultSet = null;
842       st.close();
843       st = null;
844
845       conn = JSharedObjects.getDatabase().getConnection();
846       st = conn.prepareStatement( "DELETE FROM messages WHERE profile_id = ? AND plugin_id = ? AND LCASE(user_id) = ?" );
847
848       st.setLong( 1, nProfileId );
849       st.setInt( 2, item.getPluginId() );
850       st.setString( 3, item.getUserId().toLowerCase() );
851       
852       st.executeUpdate();
853       
854       // Any message ?
855
if( nCount > 0 )
856         return strMsg.toString();
857       else
858         return null;
859       
860     } catch( java.sql.SQLException JavaDoc e ) {
861       System.out.println( "JContactListManager.getContactMessages() - SQLException " + e );
862       return null;
863     }finally {
864       if( resultSet != null )
865         try {
866           resultSet.close();
867         } catch ( SQLException JavaDoc e ) {
868           System.err.println( "JContactListManager.getContactMessages() - " + e );
869         }
870       if( st != null )
871         try {
872           st.close();
873         } catch ( SQLException JavaDoc e ) {
874           System.err.println( "JContactListManager.getContactMessages() - " + e );
875         }
876     }
877   }
878   
879   /**
880    * Count the total of messages for the specified user;<br>
881    * @param item The message owner;
882    */

883   public synchronized int countContactMessages( JContactListItem item ) {
884     
885     long nProfileId = JSharedObjects.getProfileManager().getProfileId();
886     int nCount = 0;
887     PreparedStatement JavaDoc st = null;
888     ResultSet JavaDoc resultSet = null;
889     
890     
891     try{
892       Connection JavaDoc conn = JSharedObjects.getDatabase().getConnection();
893       st = conn.prepareStatement( "SELECT COUNT(*) AS _count FROM messages WHERE profile_id = ? AND plugin_id = ? AND LCASE(user_id) = ? " );
894       
895       st.setLong( 1, nProfileId );
896       st.setInt( 2, item.getPluginId() );
897       st.setString( 3, item.getUserId().toLowerCase() );
898       
899       resultSet = st.executeQuery();
900       
901       if( resultSet == null )
902         return 0;
903   
904       resultSet.next();
905       nCount = resultSet.getInt( "_count" );
906     } catch( java.sql.SQLException JavaDoc e ) {
907       System.out.println( "JContactListManager.countContactMessages() - SQLException " + e );
908       return 0;
909     } finally {
910       if( resultSet != null )
911         try {
912           resultSet.close();
913         } catch ( SQLException JavaDoc e ) {
914           System.err.println( "JContactListManager.countContactMessages() - " + e );
915         }
916       if( st != null )
917         try {
918           st.close();
919         } catch ( SQLException JavaDoc e ) {
920           System.err.println( "JContactListManager.countContactMessages() - " + e );
921         }
922     }
923
924     return nCount;
925   }
926
927   /**
928    * Handle the onBuddyRequestAuthorization
929    * event.
930    * @param strUserId The userId that's requesting
931    * contact list add authorization;
932    * @param strNickName User Alias (NickName);
933    */

934   public boolean onBuddyRequestAuthorization( java.lang.String JavaDoc strUserId, java.lang.String JavaDoc strNickName ) {
935
936     System.err.println( "JContactList.onBuddyRequestAuthorization( " + strUserId + " )" );
937     
938     if( javax.swing.JOptionPane.showConfirmDialog( null, JSharedObjects.getLanguageManager().getStringEx( "THE_USER" ) + " " + strNickName + " " + JSharedObjects.getLanguageManager().getStringEx( "IS_REQUESTING_AUTHORIZATION" ), JSharedObjects.getLanguageManager().getStringEx( "REQUEST_AUTHORIZATION" )/*"Request authorization"*/, javax.swing.JOptionPane.YES_NO_OPTION, javax.swing.JOptionPane.QUESTION_MESSAGE ) == javax.swing.JOptionPane.YES_OPTION )
939       return true;
940     else
941       return false;
942   }
943 }
944
945 // JContactList Class
946
Popular Tags