KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quikj > application > web > talk > feature > proactive > client > ProactiveOperatorListHandler


1 /*
2  * ProactiveOperatorListHandler.java
3  *
4  * Created on May 11, 2003, 5:16 AM
5  */

6
7 package com.quikj.application.web.talk.feature.proactive.client;
8 import java.util.*;
9 import java.awt.*;
10 import com.quikj.client.framework.*;
11 import com.quikj.client.beans.*;
12 import com.quikj.application.web.talk.client.*;
13 import com.quikj.application.web.talk.messaging.*;
14 import com.quikj.application.web.talk.feature.proactive.messaging.*;
15 /**
16  *
17  * @author amit
18  */

19 public class ProactiveOperatorListHandler
20 implements NetworkApplicationInterface
21 {
22     private static ProactiveOperatorListHandler instance = null;
23     private Locale locale;
24     private ProactiveOperatorList list = null;
25     private Vector sessionList = new Vector(); // element = SessionElement
26
private String JavaDoc group = "operator";
27     private Timer timer = null;
28     
29     /** Creates a new instance of ProactiveOperatorListHandler */
30     public ProactiveOperatorListHandler()
31     {
32         locale = TalkFrame.Instance().getLocale();
33         instance = this;
34     }
35     
36     public static ProactiveOperatorListHandler getInstance()
37     {
38         return instance;
39     }
40     
41     public void init(Hashtable params)
42     {
43         group = (String JavaDoc)params.get("group");
44 // System.out.println("Group=" + group);
45
if (group == null)
46         {
47             group = "operator";
48         }
49     }
50     
51     public void menuItemClicked()
52     {
53         if (list == null)
54         {
55             list = new ProactiveOperatorList();
56             
57             int size = sessionList.size();
58             for (int i = 0; i < size; i++)
59             {
60                 SessionElement e = (SessionElement)sessionList.elementAt(i);
61                 list.addListElement(formatListElementText(e));
62             }
63             
64             // and center it
65
Rectangle pbounds = TalkFrame.Instance().getBounds();
66             Point mid = new Point(pbounds.x + (pbounds.width/2), pbounds.y + (pbounds.height/2));
67             Rectangle bounds = list.getBounds();
68             int x = mid.x - (bounds.width/2);
69             int y = mid.y - (bounds.height/2);
70             if (x < 0) x = pbounds.x;
71             if (y < 0) y = pbounds.y;
72             list.setBounds(x, y, bounds.width, bounds.height);
73         }
74         
75         list.show();
76         TalkFrame.Instance().addToFrameList(list);
77     }
78     
79     public String JavaDoc menuItemName()
80     {
81         return java.util.ResourceBundle.getBundle("com.quikj.application.web.talk.feature.proactive.client.language",
82         locale).getString("Visitor_List");
83     }
84     
85     public void messageReceived(int req_id,
86     int status,
87     String JavaDoc content_type,
88     int http_status,
89     String JavaDoc reason,
90     String JavaDoc message)
91     {
92         ProactiveMessageParser parser = null;
93         try
94         {
95             parser = new ProactiveMessageParser();
96         }
97         catch (Exception JavaDoc ex)
98         {
99             // print error message
100
if (list != null)
101             {
102                 new InformationDialog(list,
103                 java.util.ResourceBundle.getBundle("com.quikj.application.web.talk.feature.proactive.client.language",
104                 locale).getString("Error"),
105                 java.util.ResourceBundle.getBundle("com.quikj.application.web.talk.feature.proactive.client.language",
106                 locale).getString("Error_initializing_XML_parser"),
107                 5000);
108             }
109             return;
110         }
111         
112         if (parser.parse(message) == false)
113         {
114             // display the error and return
115
if (list != null)
116             {
117                 new InformationDialog(list,
118                 java.util.ResourceBundle.getBundle("com.quikj.application.web.talk.feature.proactive.client.language",
119                 locale).getString("Error"),
120                 java.util.ResourceBundle.getBundle("com.quikj.application.web.talk.feature.proactive.client.language",
121                 locale).getString("Error_parsing_the_received_message"),
122                 5000);
123             }
124             
125             return;
126         }
127         
128         ProactiveMessageInterface pm = parser.getMessage();
129         if ((pm instanceof ProactiveNotificationMessage) == true)
130         {
131             processProactiveNotificationMessage((ProactiveNotificationMessage)pm);
132         }
133         else
134         {
135             // display the error and return
136
if (list != null)
137             {
138                 new InformationDialog(list,
139                 java.util.ResourceBundle.getBundle("com.quikj.application.web.talk.feature.proactive.client.language",
140                 locale).getString("Error"),
141                 java.util.ResourceBundle.getBundle("com.quikj.application.web.talk.feature.proactive.client.language",
142                 locale).getString("Unknown_type_of_message_received"),
143                 5000);
144             }
145             return;
146         }
147     }
148     
149     private void processProactiveNotificationMessage(ProactiveNotificationMessage message)
150     {
151         if (timer != null)
152         {
153             timer.cancel();
154             timer = null;
155         }
156         
157         int size = sessionList.size();
158         for (int i = 0; i < size; i++)
159         {
160             SessionElement element = (SessionElement)sessionList.elementAt(i);
161             if (element.getStopCount() > 0)
162             {
163                 element.setStopCount(element.getStopCount() + 1);
164             }
165         }
166         
167         Date d = new Date();
168         size = message.numProactiveSessionElements();
169         for (int i = 0; i < size; i++)
170         {
171             ProactiveSessionElement element = message.elementAt(i);
172             
173             // find the element
174
int index = getSessionListIndex(element.getSessionId());
175             int operation = element.getOperation();
176             
177             switch (operation)
178             {
179                 case ProactiveSessionElement.OPERATION_ADD:
180                     SessionElement se = new SessionElement();
181                     se.setAccessTime(element.getAccessTime());
182                     se.setSessionId(element.getSessionId());
183                     se.setStopCount(0);
184                     se.setUrl(element.getUrl());
185                     if (index == -1)
186                     {
187                         se.setSiteEntryTime(d);
188                         se.setPageCount(element.getPageCount());
189                         sessionList.addElement(se);
190                         
191                         if (list != null)
192                         {
193                             list.addListElement(formatListElementText(se));
194                         }
195                     }
196                     else // exists
197
{
198                         se.setPageCount(element.getPageCount());
199                         se.setSiteEntryTime(((SessionElement)sessionList.elementAt(index)).getSiteEntryTime());
200                         sessionList.setElementAt(se, index);
201                         
202                         if (list != null)
203                         {
204                             list.replaceListElement(formatListElementText(se), index);
205                         }
206                     }
207                     break;
208                     
209                 case ProactiveSessionElement.OPERATION_REMOVE:
210                     if (index != -1)
211                     {
212                         SessionElement sel = (SessionElement)sessionList.elementAt(index);
213                         sel.setStopCount(sel.getStopCount() + 1);
214                         if (sel.getStopCount() > 1)
215                         {
216                             sessionList.removeElementAt(index);
217                             
218                             if (list != null)
219                             {
220                                 list.removeListElement(index);
221                             }
222                         }
223                         else
224                         {
225                             if (timer == null) // if the timer has not been started already
226
{
227                                 timer = new Timer();
228                                 timer.start();
229                             }
230                         }
231                     }
232                     break;
233             }
234         }
235         
236         size = sessionList.size();
237         for (int i = 0; i < size; i++)
238         {
239             SessionElement element = (SessionElement)sessionList.elementAt(i);
240             if (element.getStopCount() > 1)
241             {
242                 // remove the element
243
sessionList.removeElementAt(i);
244                 if (list != null)
245                 {
246                     list.removeListElement(i);
247                 }
248                 i--;
249                 size--;
250             }
251         }
252         
253         if (list != null)
254         {
255             list.setStatusBarText(java.util.ResourceBundle.getBundle("com.quikj.application.web.talk.feature.proactive.client.language",
256             locale).getString("Number_of_sessions") + ": " + sessionList.size());
257         }
258     }
259     
260     private String JavaDoc formatListElementText(SessionElement session)
261     {
262         return pad(session.getSessionId(), ' ', 25)
263         + " " + session.getSiteEntryTime()
264         + " " + pad((new Integer JavaDoc(session.getPageCount())).toString(), ' ', 3)
265         + ((session.getUrl() != null) ? (" " + session.getUrl()) : "");
266     }
267     
268     private String JavaDoc pad(String JavaDoc str, char c, int size)
269     {
270         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(str.substring(0,
271         str.length() > size? size:str.length()));
272         for (int i = buffer.length(); i < size; i++)
273         {
274             buffer.append(c);
275         }
276         
277         return buffer.toString();
278     }
279     
280     private int getSessionListIndex(String JavaDoc name)
281     {
282         int size = sessionList.size();
283         for (int i = 0; i < size; i++)
284         {
285             SessionElement element = (SessionElement)sessionList.elementAt(i);
286             if (element.getSessionId().equals(name) == true)
287             {
288                 return i;
289             }
290         }
291         
292         return -1;
293     }
294     
295     protected void disposed()
296     {
297         if (list != null)
298         {
299             TalkFrame.Instance().removeFromFrameList(list);
300             list = null;
301         }
302     }
303     
304     public void destroy()
305     {
306         if (list != null)
307         {
308             TalkFrame.Instance().removeFromFrameList(list);
309             list.dispose();
310             list = null;
311         }
312         
313         if (timer != null)
314         {
315             timer.cancel();
316             timer = null;
317         }
318     }
319     
320     protected void callOperationSelected(int index)
321     {
322         try
323         {
324             SessionElement se = (SessionElement)sessionList.elementAt(index);
325             UserToUserMessage message = new UserToUserMessage();
326             message.setEndPointName(group);
327             
328             CallRequestMessage crm = new CallRequestMessage();
329             crm.setCalled(se.getSessionId());
330             crm.setOperator(TalkFrame.Instance().getUserInformation().getUserName());
331             message.setApplicationMessage(crm.format());
332             
333             // send the message
334
int req_id = TalkFrame.Instance().getCom().sendRequestMessage("text/xml",
335             message.format(),
336             new ResponseListener(),
337             10000);
338             if (req_id < 0)
339             {
340                 // print error message
341
if (list != null)
342                 {
343                     new InformationDialog(list,
344                     java.util.ResourceBundle.getBundle("com.quikj.application.web.talk.feature.proactive.client.language",
345                     locale).getString("Error"),
346                     java.util.ResourceBundle.getBundle("com.quikj.application.web.talk.feature.proactive.client.language",
347                     locale).getString("Error_sending_request_message_to_the_server"),
348                     5000);
349                 }
350             }
351             
352         }
353         catch (ArrayIndexOutOfBoundsException JavaDoc ex)
354         {
355         }
356     }
357     
358     protected void infoOperationSelected(int index)
359     {
360         try
361         {
362             SessionElement se = (SessionElement)sessionList.elementAt(index);
363             UserToUserMessage message = new UserToUserMessage();
364             message.setEndPointName(group);
365             
366             InformationRequestMessage irm = new InformationRequestMessage();
367             irm.setSessionId(se.getSessionId());
368             message.setApplicationMessage(irm.format());
369             
370             // send the message
371
int req_id = TalkFrame.Instance().getCom().sendRequestMessage("text/xml",
372             message.format(),
373             new ResponseListener(),
374             10000);
375             if (req_id < 0)
376             {
377                 // print error message
378
if (list != null)
379                 {
380                     new InformationDialog(list,
381                     java.util.ResourceBundle.getBundle("com.quikj.application.web.talk.feature.proactive.client.language",
382                     locale).getString("Error"),
383                     java.util.ResourceBundle.getBundle("com.quikj.application.web.talk.feature.proactive.client.language",
384                     locale).getString("Error_sending_request_message_to_the_server"),
385                     5000);
386                 }
387             }
388             
389         }
390         catch (ArrayIndexOutOfBoundsException JavaDoc ex)
391         {
392         }
393     }
394     
395     public void timerExpired()
396     {
397         timer = null;
398         int size = sessionList.size();
399         for (int i = 0; i < size; i++)
400         {
401             SessionElement element = (SessionElement)sessionList.elementAt(i);
402             if (element.getStopCount() > 0)
403             {
404                 sessionList.removeElementAt(i);
405                 if (list != null)
406                 {
407                     list.removeListElement(i);
408                 }
409                 i--;
410                 size--;
411             }
412         }
413         
414         if (list != null)
415         {
416             list.setStatusBarText(java.util.ResourceBundle.getBundle("com.quikj.application.web.talk.feature.proactive.client.language",
417             locale).getString("Number_of_sessions") + ": " + sessionList.size());
418         }
419     }
420     
421     class Timer extends Thread JavaDoc
422     {
423         public void cancel()
424         {
425             interrupted = true;
426         }
427         
428         public void run()
429         {
430             int ticks = 0;
431             
432             try
433             {
434                 while (ticks < TIMER)
435                 {
436                     sleep(1000L);
437                     if (interrupted == true)
438                     {
439                         break;
440                     }
441                     ticks++;
442                 }
443                 
444                 if (interrupted == false)
445                 {
446                     timerExpired();
447                 }
448             }
449             catch (InterruptedException JavaDoc ex)
450             {
451             }
452         }
453         
454         private boolean interrupted = false;
455         private int TIMER = 10;
456     }
457     
458     class ResponseListener implements HTTPMessageListenerInterface
459     {
460         public void messageReceived(int req_id,
461         int status,
462         String JavaDoc content_type,
463         int http_status,
464         String JavaDoc reason,
465         String JavaDoc message)
466         {
467             if (status == HTTPMessageListenerInterface.TIMEOUT)
468             {
469                 // print time-out message
470
if (list != null)
471                 {
472                     new InformationDialog(list,
473                     java.util.ResourceBundle.getBundle("com.quikj.application.web.talk.feature.proactive.client.language",
474                     locale).getString("Error"),
475                     java.util.ResourceBundle.getBundle("com.quikj.application.web.talk.feature.proactive.client.language",
476                     locale).getString("Timed_out_receving_message_from_the_server"),
477                     5000);
478                 }
479                 
480             }
481             else if (status == HTTPMessageListenerInterface.RECEIVED)
482             {
483                 if (http_status == HTTPRspMessage.OK)
484                 {
485                     TalkMessageParser parser = null;
486                     
487                     try
488                     {
489                         parser = new TalkMessageParser();
490                     }
491                     catch (Exception JavaDoc ex)
492                     {
493                         // print error message
494
if (list != null)
495                         {
496                             new InformationDialog(list,
497                             java.util.ResourceBundle.getBundle("com.quikj.application.web.talk.feature.proactive.client.language",
498                             locale).getString("Error"),
499                             java.util.ResourceBundle.getBundle("com.quikj.application.web.talk.feature.proactive.client.language",
500                             locale).getString("Error_initializing_XML_parser"),
501                             5000);
502                         }
503                         return;
504                     }
505                     
506                     if (parser.parse(message, false) == false)
507                     {
508                         // print error message
509
if (list != null)
510                         {
511                             new InformationDialog(list,
512                             java.util.ResourceBundle.getBundle("com.quikj.application.web.talk.feature.proactive.client.language",
513                             locale).getString("Error"),
514                             java.util.ResourceBundle.getBundle("com.quikj.application.web.talk.feature.proactive.client.language",
515                             locale).getString("Error_parsing_the_received_message"),
516                             5000);
517                         }
518                         return;
519                     }
520                     
521                     TalkMessageInterface parsed_message = parser.getMessage();
522                     if ((parsed_message instanceof UserToUserMessage) == true)
523                     {
524                         UserToUserMessage response_msg = (UserToUserMessage)parsed_message;
525                         
526                         // parse the body to get the status
527
ProactiveMessageParser pmp = null;
528                         try
529                         {
530                             pmp = new ProactiveMessageParser();
531                         }
532                         catch (Exception JavaDoc ex)
533                         {
534                             // print error message
535
if (list != null)
536                             {
537                                 new InformationDialog(list,
538                                 java.util.ResourceBundle.getBundle("com.quikj.application.web.talk.feature.proactive.client.language",
539                                 locale).getString("Error"),
540                                 java.util.ResourceBundle.getBundle("com.quikj.application.web.talk.feature.proactive.client.language",
541                                 locale).getString("Error_initializing_XML_parser"),
542                                 5000);
543                             }
544                             return;
545                         }
546                         
547                         if (pmp.parse(response_msg.getApplicationMessage()) == false)
548                         {
549                             // print error message
550
if (list != null)
551                             {
552                                 new InformationDialog(list,
553                                 java.util.ResourceBundle.getBundle("com.quikj.application.web.talk.feature.proactive.client.language",
554                                 locale).getString("Error"),
555                                 java.util.ResourceBundle.getBundle("com.quikj.application.web.talk.feature.proactive.client.language",
556                                 locale).getString("Error_parsing_the_received_message"),
557                                 5000);
558                             }
559                             return;
560                         }
561                         
562                         ProactiveMessageInterface mi = pmp.getMessage();
563                         if ((mi instanceof CallResponseMessage) == true)
564                         {
565                             int call_status = ((CallResponseMessage)mi).getStatus();
566                             
567                             switch (call_status)
568                             {
569                                 case CallResponseMessage.NOT_PRESENT:
570                                     if (list != null)
571                                     {
572                                         new InformationDialog(list,
573                                         java.util.ResourceBundle.getBundle("com.quikj.application.web.talk.feature.proactive.client.language",
574                                         locale).getString("Error"),
575                                         java.util.ResourceBundle.getBundle("com.quikj.application.web.talk.feature.proactive.client.language",
576                                         locale).getString("The_user_has_left_the_site"),
577                                         5000);
578                                     }
579                                     break;
580                                 case CallResponseMessage.OK:
581                                     break;
582                                 case CallResponseMessage.TAKEN:
583                                     if (list != null)
584                                     {
585                                         new InformationDialog(list,
586                                         java.util.ResourceBundle.getBundle("com.quikj.application.web.talk.feature.proactive.client.language",
587                                         locale).getString("Error"),
588                                         java.util.ResourceBundle.getBundle("com.quikj.application.web.talk.feature.proactive.client.language",
589                                         locale).getString("The_user_is_being_served_by_another_agent"),
590                                         5000);
591                                     }
592                                     break;
593                                 default:
594                                     
595                             }
596                         }
597                         else if ((mi instanceof InformationResponseMessage) == true)
598                         {
599                             InformationResponseMessage msg = (InformationResponseMessage)mi;
600                             if (msg.getStatus() == InformationResponseMessage.STATUS_NOT_FOUND)
601                             {
602                                 new InformationDialog(list,
603                                 java.util.ResourceBundle.getBundle("com.quikj.application.web.talk.feature.proactive.client.language",
604                                 locale).getString("Not_found"),
605                                 java.util.ResourceBundle.getBundle("com.quikj.application.web.talk.feature.proactive.client.language",
606                                 locale).getString("Session") + ": "
607                                 + msg.getSessionId() + " "
608                                 + java.util.ResourceBundle.getBundle("com.quikj.application.web.talk.feature.proactive.client.language",
609                                 locale).getString("not_found"),
610                                 5000);
611                                 return;
612                             }
613                             
614                             StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
615                             
616                             buffer.append(java.util.ResourceBundle.getBundle("com.quikj.application.web.talk.feature.proactive.client.language",
617                             locale).getString("Session_identifier") + ": "
618                             + msg.getSessionId());
619                                                         
620                             if (msg.getStatus() == InformationResponseMessage.STATUS_OK)
621                             {
622                                 buffer.append("\n"
623                                 + java.util.ResourceBundle.getBundle("com.quikj.application.web.talk.feature.proactive.client.language",
624                                 locale).getString("Status") + ": "
625                                 + java.util.ResourceBundle.getBundle("com.quikj.application.web.talk.feature.proactive.client.language",
626                                 locale).getString("Active"));
627                             }
628                             else if (msg.getStatus() == InformationResponseMessage.STATUS_INACTIVE)
629                             {
630                                 buffer.append("\n"
631                                 + java.util.ResourceBundle.getBundle("com.quikj.application.web.talk.feature.proactive.client.language",
632                                 locale).getString("Status") + ": "
633                                 + java.util.ResourceBundle.getBundle("com.quikj.application.web.talk.feature.proactive.client.language",
634                                 locale).getString("Inactive"));
635                             }
636                             
637                             if (msg.isServed() == true)
638                             {
639                                 buffer.append("\n"
640                                 + java.util.ResourceBundle.getBundle("com.quikj.application.web.talk.feature.proactive.client.language",
641                                 locale).getString("Served") + ": "
642                                 + java.util.ResourceBundle.getBundle("com.quikj.application.web.talk.feature.proactive.client.language",
643                                 locale).getString("Yes"));
644                             }
645                             else
646                             {
647                                 buffer.append("\n"
648                                 + java.util.ResourceBundle.getBundle("com.quikj.application.web.talk.feature.proactive.client.language",
649                                 locale).getString("Served") + ": "
650                                 + java.util.ResourceBundle.getBundle("com.quikj.application.web.talk.feature.proactive.client.language",
651                                 locale).getString("No"));
652                             }
653                             
654                             if (msg.getLastAccessTime() != null)
655                             {
656                                 buffer.append("\n"
657                                 + java.util.ResourceBundle.getBundle("com.quikj.application.web.talk.feature.proactive.client.language",
658                                 locale).getString("Last_access_time") + ": "
659                                 + msg.getLastAccessTime());
660                             }
661                             
662                             buffer.append ("\n"
663                             + java.util.ResourceBundle.getBundle("com.quikj.application.web.talk.feature.proactive.client.language",
664                             locale).getString("Number_of_pages_visited") + ": "
665                             + msg.getPageCount());
666                             
667                             buffer.append ("\n"
668                             + java.util.ResourceBundle.getBundle("com.quikj.application.web.talk.feature.proactive.client.language",
669                             locale).getString("Session_information") + ":\n");
670                             int size = msg.numWebInfoElements();
671                             for (int i = 0; i < size; i++)
672                             {
673                                 WebInfoElement e = msg.elementAt(i);
674                                 buffer.append (" " + e.getUrl() + " -- "
675                                 + e.getAccessTime()
676                                 + "\n");
677                             }
678                                                         
679                             TextAreaDialog td = new TextAreaDialog(list,
680                             java.util.ResourceBundle.getBundle("com.quikj.application.web.talk.feature.proactive.client.language",
681                             locale).getString("Visitor_session_information"),
682                             java.util.ResourceBundle.getBundle("com.quikj.application.web.talk.feature.proactive.client.language",
683                             locale).getString("Visitor_information"),
684                             java.util.ResourceBundle.getBundle("com.quikj.application.web.talk.feature.proactive.client.language",
685                             locale).getString("Close"),
686                             buffer.toString());
687                         }
688                         else
689                         {
690                             // print error message
691
if (list != null)
692                             {
693                                 new InformationDialog(list,
694                                 java.util.ResourceBundle.getBundle("com.quikj.application.web.talk.feature.proactive.client.language",
695                                 locale).getString("Error"),
696                                 java.util.ResourceBundle.getBundle("com.quikj.application.web.talk.feature.proactive.client.language",
697                                 locale).getString("Unknown_type_of_message_received"),
698                                 5000);
699                             }
700                             return;
701                         }
702                     }
703                     else
704                     {
705                         // print error message
706
if (list != null)
707                         {
708                             new InformationDialog(list,
709                             java.util.ResourceBundle.getBundle("com.quikj.application.web.talk.feature.proactive.client.language",
710                             locale).getString("Error"),
711                             java.util.ResourceBundle.getBundle("com.quikj.application.web.talk.feature.proactive.client.language",
712                             locale).getString("Unknown_type_of_message_received"),
713                             5000);
714                         } return;
715                     }
716                 }
717                 else
718                 {
719                     // print the message
720
if (list != null)
721                     {
722                         new InformationDialog(list,
723                         java.util.ResourceBundle.getBundle("com.quikj.application.web.talk.feature.proactive.client.language",
724                         locale).getString("Error"),
725                         java.util.ResourceBundle.getBundle("com.quikj.application.web.talk.feature.proactive.client.language",
726                         locale).getString("Unexpected_HTTP_status") + ": " + http_status,
727                         5000);
728                     }
729                     return;
730                 }
731             }
732             else // unexpected status
733
{
734                 // print error message
735
if (list != null)
736                 {
737                     new InformationDialog(list,
738                     java.util.ResourceBundle.getBundle("com.quikj.application.web.talk.feature.proactive.client.language",
739                     locale).getString("Error"),
740                     java.util.ResourceBundle.getBundle("com.quikj.application.web.talk.feature.proactive.client.language",
741                     locale).getString("Unexpected_message_status") + ": " + status,
742                     5000);
743                 }
744                 return;
745             }
746         }
747     }
748 }
749
Popular Tags