1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  package fr.paris.lutece.plugins.parisconnect.service;
35  
36  import java.io.UnsupportedEncodingException;
37  import java.net.URLEncoder;
38  import java.util.HashMap;
39  import java.util.Map;
40  
41  import net.sf.json.JSONException;
42  import net.sf.json.JSONObject;
43  import net.sf.json.JSONSerializer;
44  
45  import org.apache.commons.lang.StringUtils;
46  import org.apache.log4j.Logger;
47  
48  import fr.paris.lutece.plugins.parisconnect.business.Message;
49  import fr.paris.lutece.portal.service.spring.SpringContextService;
50  import fr.paris.lutece.portal.service.util.AppLogService;
51  import fr.paris.lutece.portal.service.util.AppPropertiesService;
52  
53  
54  
55  
56  
57  public final class ParisConnectAPIService
58  {
59  	
60      public static final String USER_UID = "uid";
61      public static final String PCUID = "pcuid";
62      public static final String MESSAGE = "message";
63      public static final String PARAMETER_PCUID = "pcuid";
64      public static final String PARAMETER_ID_ALERTES = "idalertes";
65      public static final String PARAMETER_NEWSLETTER = "newsletter";
66      public static final String PARAMETER_ID_EMAIL = "id_mail";
67      public static final String PARAMETER_ID_USERS = "idusers";
68      public static final String PARAMETER_ID_PARENT = "idparent";
69      public static final String PARAMETER_ID_MESSAGES = "idmessages";
70      public static final String PARAMETER_ID_USERS_FROM= "idusers_from";
71      public static final String PARAMETER_PARENT_MESSAGE_ID="parent_message_id";
72      
73      public static final String PARAMETER_ID_USERS_TO= "idusers_to";
74      public static final String PARAMETER_SUBJECT= "subject";
75      public static final String PARAMETER_BODY= "body";
76      public static final String PARAMETER_ID_STATUTS = "idstatuts";
77      public static final String PARAMETER_ID_MESSAGE_EMP = "id_message_emp";
78      public static final String PARAMETER_PARENT_EMP = "parent_emp";
79      public static final String PARAMETER_READ = "read";
80      private static final String PARAMETER_MESSAGE = "message";
81      private static final String PARAMETER_CATEGORY = "cat";
82     
83       
84      
85      private static final String METHOD_IS_SUBSCRIBED = "is_subscribed";
86      private static final String METHOD_SUBSCIBE_USER = "subscribe_user";
87      private static final String METHOD_UNSUBSCIBE_USER = "unsubscribe_user";
88      private static final String METHOD_SUBSCIBE_NEWSLETTER = "subscribe_newsletter";
89      private static final String METHOD_UNSUBSCIBE_NEWSLETTER = "unsubscribe_newsletter";
90      private static final String METHOD_GET_NEWSLETTERS = "get_newsletters";
91      private static final String METHOD_GET_USER_NEWSLETTERS = "get_user_newsletters";
92      private static final String METHOD_GET_ALERTES = "get_alertes";
93      private static final String METHOD_GET_USER_ALERTS = "get_user_alerts";
94      private static final String METHOD_GET_USER_MESSAGES = "get_user_messages";
95      private static final String METHOD_GET_MESSAGE = "get_message";
96      private static final String METHOD_SEND_MESSAGE = "send_message";
97      private static final String METHOD_MARK_AS_READ = "mark_as_read";
98      private static final String METHOD_MARK_AS_UNREAD = "mark_as_unread";
99      
100      
101     
102     
103     private static final String METHOD_GET_TICKET = "get_ticket";
104     private static final String METHOD_GET_USERNAME = "get_username";
105     private static final String METHOD_GET_USER = "get_user";
106     private static final String METHOD_GET_PCUID_BY_EMAIL = "get_pcuid_by_email";
107     private static final String METHOD_SET_ACCOUNT_SHADOW = "set_account_shadow";
108     private static final String METHOD_CHECK_CONNECTION_COOKIE = "check_connection_cookie";
109     private static final String PARAMETER_EMAIL = "email";
110     private static final String PARAMETER_URL = "url";
111     private static final String PARAMETER_CONNECTION_COOKIE = "connection_cookie";
112     private static final String KEY_PCUID = "pcuid";
113     private static final String KEY_SUCCESS = "success";
114     
115     
116     
117     private static final ParisConnectAPI _usersAPI = SpringContextService.getBean( "parisconnect.apiUsers" );
118     
119     private static final ParisConnectAPI _empAPI = SpringContextService.getBean( 
120             "parisconnect.apiEmp" );
121     
122     private static final ParisConnectAPI _psupAPI = SpringContextService.getBean( "parisconnect.apiPsup" );
123     private static final ParisConnectAPI _newsletterAPI = SpringContextService.getBean( "parisconnect.apiNewsletter" );
124     private static final ParisConnectAPI _messageAPI = SpringContextService.getBean( "parisconnect.apiMessage" );
125     private static final ParisConnectAPI _mibAPI = SpringContextService.getBean( 
126             "parisconnect.apiMib" );
127     
128     private static final ParisConnectAPI _doListAPI = SpringContextService.getBean( "parisconnect.apiDoList" );
129     
130     
131     
132     private static Logger _logger = org.apache.log4j.Logger.getLogger( Constants.LOGGER_PARISCONNECT );
133     
134 
135     
136     private ParisConnectAPIService(  )
137     {
138     }
139 
140     
141     
142     
143 
144 
145 
146     static String getAlerts( )
147     {
148         Map<String, String> mapParameters = new HashMap<String, String>(  );
149      
150         String strResponse = null;
151 
152         try
153         {
154             strResponse = _psupAPI.callMethod( METHOD_GET_ALERTES, mapParameters, true );
155         }
156         catch ( ParisConnectAPIException ex )
157         {
158             _logger.error( ex.getMessage(  ) );
159         }
160 
161         return strResponse;
162     }
163     
164     
165 
166 
167 
168 
169     static String getUserAlerts( String strPCUID )
170     {
171         Map<String, String> mapParameters = new HashMap<String, String>(  );
172         mapParameters.put( PARAMETER_PCUID, strPCUID );
173 
174         String strResponse = null;
175         
176         try
177         {
178             strResponse = _psupAPI.callMethod( METHOD_GET_USER_ALERTS, mapParameters, true );
179         }
180         catch ( ParisConnectAPIException ex )
181         {
182             _logger.error( ex.getMessage(  ) );
183         }
184 
185         return strResponse;
186     }
187     
188     
189     
190 
191     
192 
193 
194 
195     static String getNewsletters( )
196     {
197         Map<String, String> mapParameters = new HashMap<String, String>(  );
198      
199         String strResponse = null;
200 
201         try
202         {
203             strResponse = _newsletterAPI.callMethod( METHOD_GET_NEWSLETTERS, mapParameters, true );
204         }
205         catch ( ParisConnectAPIException ex )
206         {
207             _logger.error( ex.getMessage(  ) );
208         }
209 
210         return strResponse;
211     }
212     
213     
214 
215 
216 
217 
218     static String getUserNewsletters( String strPCUID )
219     {
220         Map<String, String> mapParameters = new HashMap<String, String>(  );
221         mapParameters.put( PARAMETER_PCUID, strPCUID );
222 
223         String strResponse = null;
224         
225         try
226         {
227             strResponse = _newsletterAPI.callMethod( METHOD_GET_USER_NEWSLETTERS, mapParameters, true );
228         }
229         catch ( ParisConnectAPIException ex )
230         {
231             _logger.error( ex.getMessage(  ) );
232         }
233 
234         return strResponse;
235     }
236     
237     
238     
239 
240 
241 
242 
243 
244     static String subscribeNewsletter( String strPCUID, String strIdNewsletter )
245     {
246         Map<String, String> mapParameters = new HashMap<String, String>(  );
247         mapParameters.put( PARAMETER_PCUID, strPCUID );
248         mapParameters.put( PARAMETER_NEWSLETTER, strIdNewsletter );
249 
250         String strResponse = null;
251 
252         try
253         {
254             strResponse = _newsletterAPI.callMethod( METHOD_SUBSCIBE_NEWSLETTER, mapParameters, false );
255         }
256         catch ( ParisConnectAPIException ex )
257         {
258             _logger.error( ex.getMessage(  ) );
259         }
260 
261         return strResponse;
262     }
263     
264     
265 
266 
267 
268 
269 
270     static String unSubscribeNewsletter( String strPCUID, String strIdNewsletter )
271     {
272         Map<String, String> mapParameters = new HashMap<String, String>(  );
273         mapParameters.put( PARAMETER_PCUID, strPCUID );
274          mapParameters.put( PARAMETER_NEWSLETTER, strIdNewsletter );
275 
276         String strResponse = null;
277 
278         try
279         {
280             strResponse = _newsletterAPI.callMethod( METHOD_UNSUBSCIBE_NEWSLETTER, mapParameters, false );
281         }
282         catch ( ParisConnectAPIException ex )
283         {
284             _logger.error( ex.getMessage(  ) );
285         }
286 
287         return strResponse;
288     }
289    
290 
291     
292 
293 
294 
295 
296 
297     static String subscribeAlert( String strPCUID, String strIdAlertes )
298     {
299         Map<String, String> mapParameters = new HashMap<String, String>(  );
300         mapParameters.put( PARAMETER_PCUID, strPCUID );
301          mapParameters.put( PARAMETER_ID_ALERTES, strIdAlertes );
302 
303         String strResponse = null;
304 
305         try
306         {
307             strResponse = _psupAPI.callMethod( METHOD_SUBSCIBE_USER, mapParameters, false );
308         }
309         catch ( ParisConnectAPIException ex )
310         {
311             _logger.error( ex.getMessage(  ) );
312         }
313 
314         return strResponse;
315     }
316 
317     
318 
319 
320 
321 
322 
323     static String unSubscribeAlert( String strPCUID, String strIdAlertes )
324     {
325         Map<String, String> mapParameters = new HashMap<String, String>(  );
326         mapParameters.put( PARAMETER_PCUID, strPCUID );
327         mapParameters.put( PARAMETER_ID_ALERTES, strIdAlertes );
328 
329         String strResponse = null;
330 
331         try
332         {
333             strResponse = _psupAPI.callMethod( METHOD_UNSUBSCIBE_USER, mapParameters, false );
334         }
335         catch ( ParisConnectAPIException ex )
336         {
337             _logger.error( ex.getMessage(  ) );
338         }
339 
340         return strResponse;
341     }
342 
343     
344 
345 
346 
347 
348     static String getPcuidByEmail( String strMail )
349     {
350         Map<String, String> mapParameters = new HashMap<String, String>(  );
351         mapParameters.put( PARAMETER_EMAIL, strMail );
352 
353         String strPcuid = null;
354         String strResponse = null;
355 
356         try
357         {
358             strResponse = _usersAPI.callMethod( METHOD_GET_PCUID_BY_EMAIL, mapParameters, true );
359 
360             JSONObject jo = (JSONObject) JSONSerializer.toJSON( strResponse );
361             strPcuid = jo.getString( KEY_PCUID );
362         }
363         catch ( ParisConnectAPIException ex )
364         {
365             _logger.warn( "Account Shadow  API call : mail=" + strMail + " - " + ex.getMessage(  ) );
366         }
367         catch ( JSONException ex )
368         {
369             _logger.error( "Account Shadow  API call : mail=" + strMail + " - " + ex.getMessage(  ) );
370         }
371 
372         return strPcuid;
373     }
374 
375     
376 
377 
378 
379 
380 
381     static String setAccountShadow( String strMail, String strIdEmail )
382     {
383         Map<String, String> mapParameters = new HashMap<String, String>(  );
384         mapParameters.put( PARAMETER_EMAIL, strMail );
385         mapParameters.put( PARAMETER_ID_EMAIL, strIdEmail );
386 
387         String strPcuid = null;
388         String strResponse = null;
389 
390         try
391         {
392             strResponse = _usersAPI.callMethod( METHOD_SET_ACCOUNT_SHADOW, mapParameters, false );
393             strResponse = strResponse.trim(  );
394 
395             JSONObject jo = (JSONObject) JSONSerializer.toJSON( strResponse );
396             strPcuid = jo.getString( KEY_PCUID );
397         }
398         catch ( ParisConnectAPIException ex )
399         {
400             _logger.warn( "Account Shadow  API call : mail=" + strMail + " - " + ex.getMessage(  ) );
401         }
402         catch ( JSONException ex )
403         {
404             _logger.error( "Account Shadow  API call : mail=" + strMail + " - " + ex.getMessage(  ) );
405         }
406 
407         return strPcuid;
408     }
409     
410     
411 
412 
413 
414 
415 
416 
417     static boolean sendMessageEmp( String strMail, String strMessage,String strBackUrl,String strCategory )
418     {
419         Map<String, String> mapParameters = new HashMap<String, String>(  );
420         mapParameters.put( PARAMETER_EMAIL, strMail );
421         mapParameters.put( PARAMETER_MESSAGE, strMessage );
422         mapParameters.put( PARAMETER_URL, strBackUrl );
423         mapParameters.put( PARAMETER_CATEGORY, strCategory );
424         
425        
426         String strResponse = null;
427 
428         try
429         {
430             strResponse = _mibAPI.callMethod( "", mapParameters, false );
431             return true;
432             	
433             
434         }
435         catch ( ParisConnectAPIException ex )
436         {
437             _logger.warn( "Account MIB  API call : mail=" + strMail + " - " + ex.getMessage(  ) );
438         }
439         catch ( JSONException ex )
440         {
441             _logger.error( "Account MIB  API call : mail=" + strMail + " - " + ex.getMessage(  ) );
442         }
443 
444         return false;
445     }
446     
447     
448 
449 
450 
451 
452 
453 
454     static String sendUserResponseEmp( String strIdMessageParentEmp,String strMessage,String strPcuid )
455     {
456         Map<String, String> mapParameters = new HashMap<String, String>(  );
457         mapParameters.put( PARAMETER_ID_USERS_FROM, strPcuid );
458         
459         try
460         {
461             strMessage= URLEncoder.encode(strMessage,  AppPropertiesService.getProperty( ParisConnectAPI.PROPERTY_CONTENT_CHARSET, ParisConnectAPI.DEFAULT_CHARSET ));
462         }
463         catch( UnsupportedEncodingException e )
464         {
465             AppLogService.error( e );
466         }
467         mapParameters.put( PARAMETER_MESSAGE, strMessage  );
468         mapParameters.put( PARAMETER_PARENT_MESSAGE_ID, strIdMessageParentEmp );
469         
470        
471         String strResponse = null;
472 
473         try
474         {
475             strResponse = _empAPI.callMethodGet( "", mapParameters, false );
476             
477                 
478             
479         }
480         catch ( ParisConnectAPIException ex )
481         {
482             _logger.warn( "sendUserResponseEmp-" + ex.getMessage(  ) );
483         }
484         catch ( JSONException ex )
485         {
486             _logger.error( "sendUserResponseEmp-"  + ex.getMessage(  ) );
487         }
488 
489         return strResponse;
490     }
491     
492     
493     
494 
495 
496 
497 
498     static String getUserMessages( String strIdUSers )
499     {
500         Map<String, String> mapParameters = new HashMap<String, String>(  );
501         mapParameters.put( PARAMETER_ID_USERS, strIdUSers );
502 
503         String strResponse = null;
504         
505         try
506         {
507             strResponse = _messageAPI.callMethod( METHOD_GET_USER_MESSAGES, mapParameters, true );
508         }
509         catch ( ParisConnectAPIException ex )
510         {
511             _logger.error( ex.getMessage(  ) );
512         }
513 
514         return strResponse;
515     }
516     
517     
518 
519 
520 
521 
522     static String sendMessage( Message message)
523     {
524         Map<String, String> mapParameters = new HashMap<String, String>(  );
525        if(!StringUtils.isEmpty(message.getIdUsersFrom()))
526         {
527         	mapParameters.put( PARAMETER_ID_USERS_FROM, message.getIdUsersFrom() );
528         }
529         if(!StringUtils.isEmpty(message.getIdUsersTo()))
530         {
531         	mapParameters.put( PARAMETER_ID_USERS_TO, message.getIdUsersTo() );
532         }
533         
534         if(!StringUtils.isEmpty(message.getSubject()))
535         {
536         	mapParameters.put( PARAMETER_SUBJECT, message.getSubject() );
537         }
538         if(!StringUtils.isEmpty(message.getBody()))
539         {
540         	mapParameters.put( PARAMETER_BODY, message.getBody() );
541         }
542         if(!StringUtils.isEmpty(message.getIdStatus()))
543         {
544         	mapParameters.put( PARAMETER_ID_STATUTS, message.getIdStatus() );
545         }
546         if(!StringUtils.isEmpty(message.getIdMessageEmp()))
547         {
548         	mapParameters.put( ParisConnectAPIService.PARAMETER_ID_MESSAGE_EMP, message.getIdMessageEmp() );
549         }
550         if(!StringUtils.isEmpty(message.getIdParentEmp()))
551         {
552         	mapParameters.put( ParisConnectAPIService.PARAMETER_PARENT_EMP, message.getIdParentEmp() );
553         }
554         if(!StringUtils.isEmpty(message.getRead()))
555         {
556         	mapParameters.put( ParisConnectAPIService.PARAMETER_READ, message.getRead());
557         }
558         
559         
560         String strResponse = null;
561         
562         try
563         {
564             strResponse = _messageAPI.callMethod( METHOD_SEND_MESSAGE, mapParameters, false );
565         }
566         catch ( ParisConnectAPIException ex )
567         {
568             _logger.error( ex.getMessage(  ) );
569         }
570 
571         return strResponse;
572     }
573     
574     
575     
576 
577 
578 
579 
580     static String markMessageAsRead(String strIdMessage)
581     {
582         Map<String, String> mapParameters = new HashMap<String, String>(  );
583         mapParameters.put( ParisConnectAPIService.PARAMETER_ID_MESSAGES, strIdMessage);
584         String strResponse = null;
585         
586         try
587         {
588             strResponse = _messageAPI.callMethod( METHOD_MARK_AS_READ, mapParameters, false );
589         }
590         catch ( ParisConnectAPIException ex )
591         {
592             _logger.error( ex.getMessage(  ) );
593         }
594 
595         return strResponse;
596     }
597     
598 
599     
600 
601 
602 
603 
604     static String markMessageAsUnRead(String strIdMessage)
605     {
606         Map<String, String> mapParameters = new HashMap<String, String>(  );
607         mapParameters.put( ParisConnectAPIService.PARAMETER_ID_MESSAGES, strIdMessage);
608         String strResponse = null;
609         
610         try
611         {
612             strResponse = _messageAPI.callMethod( METHOD_MARK_AS_UNREAD, mapParameters, false );
613         }
614         catch ( ParisConnectAPIException ex )
615         {
616             _logger.error( ex.getMessage(  ) );
617         }
618 
619         return strResponse;
620     }
621     
622     
623     
624     
625     
626 
627 
628 
629 
630     static String getMessage( String strIdMessage )
631     {
632         Map<String, String> mapParameters = new HashMap<String, String>(  );
633         mapParameters.put( PARAMETER_ID_MESSAGES, strIdMessage );
634 
635         String strResponse = null;
636         
637         try
638         {
639             strResponse = _messageAPI.callMethod( METHOD_GET_MESSAGE, mapParameters, true );
640         }
641         catch ( ParisConnectAPIException ex )
642         {
643             _logger.error( ex.getMessage(  ) );
644         }
645 
646         return strResponse;
647     }
648     
649     
650 
651 
652 
653 
654     static String getTicket( String strIdMessageParent )
655     {
656         Map<String, String> mapParameters = new HashMap<String, String>(  );
657         mapParameters.put( PARAMETER_ID_PARENT, strIdMessageParent );
658 
659         String strResponse = null;
660         
661         try
662         {
663             strResponse = _messageAPI.callMethod( METHOD_GET_TICKET, mapParameters, true );
664         }
665         catch ( ParisConnectAPIException ex )
666         {
667             _logger.error( ex.getMessage(  ) );
668         }
669 
670         return strResponse;
671     }
672     
673     
674     
675 
676 
677 
678 
679     static String getUserName( String strIdUSers  )
680     {
681         Map<String, String> mapParameters = new HashMap<String, String>(  );
682         mapParameters.put( PARAMETER_ID_USERS, strIdUSers );
683 
684         String strResponse = null;
685         
686         try
687         {
688             strResponse = _messageAPI.callMethod( METHOD_GET_USERNAME, mapParameters, false );
689         }
690         catch ( ParisConnectAPIException ex )
691         {
692             _logger.error( ex.getMessage(  ) );
693         }
694 
695         return strResponse;
696     }
697     
698     
699     
700     
701 
702 
703 
704 
705     static String getUser( String strPcuid )
706     {
707         Map<String, String> mapParameters = new HashMap<String, String>(  );
708         mapParameters.put( PARAMETER_PCUID, strPcuid );
709         
710 
711         String strResponse = null;
712         
713         try
714         {
715             strResponse = _usersAPI.callMethod( METHOD_GET_USER, mapParameters, true );
716         }
717         catch ( ParisConnectAPIException ex )
718         {
719             _logger.error( ex.getMessage(  ) );
720         }
721 
722         return strResponse;
723     }
724     
725     
726     
727     
728     
729  
730     
731 
732 
733 
734 
735 
736     static String subscribeUserDoList( String strMail )
737     {
738         Map<String, String> mapParameters = new HashMap<String, String>(  );
739         mapParameters.put( PARAMETER_EMAIL, strMail );
740 
741 
742         String strResponse = null;
743 
744         try
745         {
746             strResponse = _doListAPI.callMethodGet( METHOD_SUBSCIBE_USER, mapParameters, false );
747         }
748         catch ( ParisConnectAPIException ex )
749         {
750             _logger.error( ex.getMessage(  ) );
751         }
752 
753         return strResponse;
754     }
755     
756     
757 
758 
759 
760 
761 
762     static String unSubscribeUserDoList( String strMail )
763     {
764         Map<String, String> mapParameters = new HashMap<String, String>(  );
765         mapParameters.put( PARAMETER_EMAIL, strMail );
766 
767         String strResponse = null;
768 
769         try
770         {
771             strResponse = _doListAPI.callMethodGet( METHOD_UNSUBSCIBE_USER, mapParameters, false );
772         }
773         catch ( ParisConnectAPIException ex )
774         {
775             _logger.error( ex.getMessage(  ) );
776         }
777 
778         return strResponse;
779     }
780     
781     
782     
783 
784 
785 
786 
787 
788     static String isSubscribedDoList( String strMail )
789     {
790         Map<String, String> mapParameters = new HashMap<String, String>(  );
791         mapParameters.put( PARAMETER_EMAIL, strMail );
792 
793         String strResponse = null;
794 
795         try
796         {
797             strResponse = _doListAPI.callMethodGet( METHOD_IS_SUBSCRIBED, mapParameters, false );
798         }
799         catch ( ParisConnectAPIException ex )
800         {
801             _logger.error( ex.getMessage(  ) );
802         }
803 
804         return strResponse;
805     }
806     
807    
808 
809     
810     
811     
812     
813     
814     
815     
816     
817     
818     
819     
820 }