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.participatorybudget.service;
35
36 import java.io.IOException;
37 import java.sql.Date;
38 import java.text.ParseException;
39 import java.util.Arrays;
40 import java.util.Calendar;
41 import java.util.List;
42 import java.util.Locale;
43 import java.util.regex.Matcher;
44
45 import org.apache.commons.lang.StringUtils;
46 import org.joda.time.DateTime;
47 import org.joda.time.LocalDate;
48 import org.joda.time.Years;
49 import org.joda.time.format.DateTimeFormat;
50 import org.joda.time.format.DateTimeFormatter;
51
52 import fr.paris.lutece.plugins.leaflet.business.GeolocItem;
53 import fr.paris.lutece.plugins.participatorybudget.business.Civility;
54 import fr.paris.lutece.plugins.participatorybudget.business.MyInfosForm;
55 import fr.paris.lutece.plugins.participatorybudget.service.campaign.CampaignService;
56 import fr.paris.lutece.plugins.participatorybudget.util.ParticipatoryBudgetConstants;
57 import fr.paris.lutece.portal.service.datastore.DatastoreService;
58 import fr.paris.lutece.portal.service.i18n.I18nService;
59 import fr.paris.lutece.portal.service.prefs.IPortalUserPreferencesService;
60 import fr.paris.lutece.portal.service.prefs.UserPreferencesService;
61 import fr.paris.lutece.portal.service.security.LuteceUser;
62 import fr.paris.lutece.portal.service.util.AppLogService;
63 import fr.paris.lutece.portal.service.util.AppPropertiesService;
64 import fr.paris.lutece.util.ReferenceList;
65
66
67
68
69 public final class MyInfosService
70 {
71
72 private static final java.util.regex.Pattern patternPostalCode = java.util.regex.Pattern.compile( ", (75[0-1][0-2][0-9]) PARIS" );
73 public static final String UNDEFINED_POSTALCODE_KEY = "undefined";
74 public static final String LUTECE_USER_KEY_VERIFIED = "user.validated";
75 private static final String PREF_KEY_EMAIL = "participatorybudget.email";
76 private static final String PREF_KEY_FIRSTNAME = "participatorybudget.firstname";
77 private static final String PREF_KEY_CREATIONDATE = "participatorybudget.creationdate";
78 private static final String PREF_KEY_CIVILITY = "participatorybudget.civility";
79 private static final String PREF_KEY_LASTNAME = "participatorybudget.lastname";
80 private static final String PREF_KEY_ADDRESS = "participatorybudget.address";
81 private static final String PREF_KEY_GEO_JSON = "participatorybudget.geojson";
82 private static final String PREF_KEY_BIRTHDATE = "participatorybudget.birthdate";
83 private static final String PREF_KEY_ARRONDISSEMENT_VOTE = "participatorybudget.arrondissement";
84 private static final String PREF_KEY_POSTAL_CODE = "participatorybudget.postalCode";
85 private static final String PREF_KEY_ILIVEINPARIS = "participatorybudget.iliveinparis";
86
87 private static final String PREF_KEY_SEND_ACCOUNT_VALIDATION = "participatorybudget.sendaccountvalidation";
88 private static final String PREF_KEY_ACCOUNT_VERIFIED = "participatorybudget.accountVerified";
89 private static final String PROPERTY_ARRONDISSEMENT = "participatorybudget.arrondissement.";
90 private static final String PROPERTY_SELECT_ARRONDISSEMENT = "participatorybudget.mes_infos.arrondissement";
91 private static final String PROPERTY_SELECT_POSTAL_CODE = "participatorybudget.mes_infos.postalCode";
92 private static final String PROPERTY_SELECT_POSTAL_CODE_16_KEY = "participatorybudget.mes_infos.postalCode16Key";
93 private static final String PROPERTY_SELECT_POSTAL_CODE_16_LABEL = "participatorybudget.mes_infos.postalCode16Label";
94 private static final String PREF_VALUE_ON = "on";
95 private static final String PREF_VALUE_OFF = "off";
96 private static final String DEFAULT_CHECKED = "default";
97 private static final String ADDRESS_SEPARATOR = " ";
98
99 private static final String KEY_SEND_ACCOUNT_VALIDATION_BACK_URL = "participatorybudget.site_property.send_account_validation_back_url";
100 private static final String KEY_ENABLE_VALIDATION_ACCOUNT = "participatorybudget.site_property.enable_validation_account";
101
102 private static final String TYPE_VALIDATE_ACCOUNT = "validate_account";
103 private static volatile ReferenceList _listArrondissements;
104 private static volatile ReferenceList _listPostalCode;
105 private static List<String> _listAuthorizedCities = Arrays.asList( AppPropertiesService
106 .getProperty( ParticipatoryBudgetConstants.PROPERTY_AUTHORIZED_CITIES ).split( ParticipatoryBudgetConstants.AUTHORIZED_CITIES_SEPARATOR ) );
107
108
109 private MyInfosService( )
110 {
111 }
112
113
114
115
116
117
118
119
120 public static MyInfosForm loadUserInfos( LuteceUser user )
121 {
122 MyInfosFormicipatorybudget/business/MyInfosForm.html#MyInfosForm">MyInfosForm myInfos = new MyInfosForm( );
123
124 IPortalUserPreferencesService instance = UserPreferencesService.instance( );
125
126
127 myInfos.setIsValid( true );
128
129
130 populateMyInfosData( myInfos, instance, user );
131
132 return myInfos;
133 }
134
135
136
137
138
139
140
141
142
143
144
145 private static void populateMyInfosData( MyInfosForm myInfos, IPortalUserPreferencesService instance, LuteceUser user )
146 {
147 populateNickname( myInfos, instance, user );
148 populateCivility( myInfos, instance, user );
149 populateFirstName( myInfos, instance, user );
150 populateLastName( myInfos, instance, user );
151 populateBirthDate( myInfos, instance, user );
152 populateAddress( myInfos, instance, user );
153 populatePostalCode( myInfos, instance, user );
154 populateArrondissement( myInfos, instance, user );
155 populateGeoJson( myInfos, instance, user );
156 populateILiveInParisFlag( myInfos, instance, user );
157 populateSendAccountValidationFlag( myInfos, instance, user );
158 populateAccountVerifiedFlag( myInfos, instance, user );
159
160 }
161
162
163
164
165
166
167
168
169 public static String loadUserNickname( String strLuteceUserName )
170 {
171
172 return UserPreferencesService.instance( ).getNickname( strLuteceUserName );
173
174 }
175
176
177
178
179
180
181
182
183 public static boolean isNicknameAlreadyExist( String strNickName )
184 {
185
186 return !StringUtils.isEmpty( strNickName ) && UserPreferencesService.instance( ).existsNickname( strNickName.trim( ) );
187
188 }
189
190
191
192
193
194
195
196
197 public static boolean isAccountVerified( LuteceUser user )
198 {
199
200 String bEnableValidationAccount = DatastoreService.getDataValue( KEY_ENABLE_VALIDATION_ACCOUNT, "false" );
201
202 if ( new Boolean( bEnableValidationAccount ) )
203 {
204
205
206 boolean bpVerified = UserPreferencesService.instance( ).getBoolean( user.getName( ), PREF_KEY_ACCOUNT_VERIFIED, false );
207 boolean openamVerified = user.getUserInfo( LUTECE_USER_KEY_VERIFIED ) != null ? new Boolean( user.getUserInfo( LUTECE_USER_KEY_VERIFIED ) ) : false;
208
209 return openamVerified || bpVerified;
210 }
211
212 return true;
213 }
214
215
216
217
218
219
220
221
222
223 public static void saveUserInfos( String strLuteceUserName, MyInfosForm myInfos )
224 {
225 IPortalUserPreferencesService instance = UserPreferencesService.instance( );
226
227
228 String strCreationDate = instance.get( strLuteceUserName, PREF_KEY_CREATIONDATE, StringUtils.EMPTY );
229
230 if ( strCreationDate == null || strCreationDate.isEmpty( ) )
231 {
232 Date dCreationDate = new Date( Calendar.getInstance( ).getTime( ).getTime( ) );
233 instance.put( strLuteceUserName, PREF_KEY_CREATIONDATE, dCreationDate.toString( ) );
234 }
235
236 instance.put( strLuteceUserName, PREF_KEY_FIRSTNAME, myInfos.getFirstname( ) );
237 instance.put( strLuteceUserName, PREF_KEY_LASTNAME, myInfos.getLastname( ) );
238 instance.put( strLuteceUserName, PREF_KEY_ADDRESS, myInfos.getAddress( ) );
239 instance.put( strLuteceUserName, PREF_KEY_BIRTHDATE, myInfos.getBirthdate( ) );
240 instance.put( strLuteceUserName, PREF_KEY_ILIVEINPARIS, myInfos.getIliveinparis( ) );
241 instance.put( strLuteceUserName, PREF_KEY_CIVILITY, myInfos.getCivility( ) );
242 instance.put( strLuteceUserName, PREF_KEY_POSTAL_CODE, myInfos.getPostalCode( ) );
243 instance.put( strLuteceUserName, PREF_KEY_ARRONDISSEMENT_VOTE, myInfos.getArrondissement( ) );
244
245 instance.put( strLuteceUserName, PREF_KEY_SEND_ACCOUNT_VALIDATION, myInfos.getSendaccountvalidation( ) );
246
247 instance.put( strLuteceUserName, PREF_KEY_GEO_JSON, myInfos.getGeojson( ) );
248
249
250 myInfos.setIsValid( isUserValid( myInfos ) );
251
252 if ( !StringUtils.isEmpty( myInfos.getNickname( ) ) && !StringUtils.isEmpty( instance.getNickname( strLuteceUserName ) )
253 && !myInfos.getNickname( ).equals( instance.getNickname( strLuteceUserName ) ) )
254 {
255 instance.setNickname( strLuteceUserName, myInfos.getNickname( ) );
256 MyInfosListenerService.updateNickName( strLuteceUserName, myInfos.getNickname( ) );
257
258 }
259 else
260 if ( StringUtils.isEmpty( instance.getNickname( strLuteceUserName ) ) )
261 {
262
263 MyInfosListenerService.createNickName( strLuteceUserName, myInfos.getNickname( ) );
264 }
265 instance.setNickname( strLuteceUserName, myInfos.getNickname( ) );
266
267 }
268
269
270
271
272
273
274
275
276
277 public static void saveUserEmail( String strLuteceUserName, String strEmail )
278 {
279 IPortalUserPreferencesService instance = UserPreferencesService.instance( );
280 instance.put( strLuteceUserName, PREF_KEY_EMAIL, strEmail );
281 }
282
283
284
285
286
287
288
289
290 public static String getUserEmail( String strLuteceUserName )
291 {
292 return UserPreferencesService.instance( ).get( strLuteceUserName, PREF_KEY_EMAIL, StringUtils.EMPTY );
293 }
294
295
296
297
298
299
300
301
302
303
304 public static boolean mustSendAccountValidationMail( String strLuteceUserName, MyInfosForm myInfos )
305 {
306
307 String bEnableValidationAccount = DatastoreService.getDataValue( KEY_ENABLE_VALIDATION_ACCOUNT, "false" );
308
309 return new Boolean( bEnableValidationAccount ) && StringUtils.isNotEmpty( myInfos.getSendaccountvalidation( ) );
310 }
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325 public static ReferenceList getArrondissements( )
326 {
327 if ( _listArrondissements == null )
328 {
329 synchronized( MyInfosService.class )
330 {
331 if ( _listArrondissements == null )
332 {
333 _listArrondissements = new ReferenceList( );
334 _listArrondissements.addItem( UNDEFINED_POSTALCODE_KEY, I18nService.getLocalizedString( PROPERTY_SELECT_ARRONDISSEMENT, Locale.FRENCH ) );
335
336 _listArrondissements.addItem( "area_1", "Area 1" );
337 _listArrondissements.addItem( "area_2", "Area 2" );
338 _listArrondissements.addItem( "area_3", "Area 3" );
339 _listArrondissements.addItem( "whole_city", "Whole city" );
340
341
342
343
344
345
346
347 }
348 }
349 }
350
351 return _listArrondissements;
352 }
353
354
355
356
357
358
359 public static ReferenceList getPostalCodes( )
360 {
361 if ( _listPostalCode == null )
362 {
363 synchronized( MyInfosService.class )
364 {
365 if ( _listPostalCode == null )
366 {
367 _listPostalCode = new ReferenceList( );
368 _listPostalCode.addItem( UNDEFINED_POSTALCODE_KEY, I18nService.getLocalizedString( PROPERTY_SELECT_POSTAL_CODE, Locale.FRENCH ) );
369
370 for ( int i = 0; i < 20; i++ )
371 {
372 String strCode = "" + ( 75001 + i );
373 _listPostalCode.addItem( strCode, strCode );
374 if ( i == 15 )
375 {
376 _listPostalCode.addItem( I18nService.getLocalizedString( PROPERTY_SELECT_POSTAL_CODE_16_KEY, Locale.FRENCH ),
377 I18nService.getLocalizedString( PROPERTY_SELECT_POSTAL_CODE_16_LABEL, Locale.FRENCH ) );
378 }
379 }
380 }
381 }
382 }
383
384 return _listPostalCode;
385 }
386
387
388
389
390
391
392
393
394
395
396 public static int getAge( String strBirthDate ) throws ParseException
397 {
398 DateTimeFormatter formatter = DateTimeFormat.forPattern( "dd/MM/yyyy" );
399 DateTime dateBirthdate = formatter.parseDateTime( strBirthDate );
400 Years years = Years.yearsBetween( new LocalDate( dateBirthdate ), new LocalDate( ) );
401
402 return years.getYears( );
403 }
404
405 public static boolean setAdressValid( MyInfosForm myInfos )
406 {
407
408 GeolocItem geolocItem = null;
409 try
410 {
411 geolocItem = GeolocItem.fromJSON( myInfos.getGeojson( ) );
412
413 Matcher m = patternPostalCode.matcher( geolocItem.getAddress( ) );
414 m.find( );
415
416 String strPostalCode = m.group( 1 );
417
418 if ( !StringUtils.isEmpty( strPostalCode ) )
419 {
420 myInfos.setLatitude( geolocItem.getLat( ) );
421 myInfos.setLongitude( geolocItem.getLon( ) );
422 myInfos.setAddress( geolocItem.getAddress( ) );
423 myInfos.setPostalCode( strPostalCode );
424 return true;
425 }
426
427 }
428 catch( IOException e )
429 {
430
431 AppLogService.error( "MyInfosApp: malformed data from client: address = " + myInfos.getGeojson( ) + "; exeception " + e );
432 }
433
434 return false;
435
436 }
437
438
439
440
441
442
443
444
445 private static boolean isUserValid( MyInfosForm myInfos )
446 {
447 return StringUtils.isNotBlank( myInfos.getCivility( ) )
448
449 && StringUtils.isNotBlank( myInfos.getAddress( ) ) && StringUtils.isNotBlank( myInfos.getPostalCode( ) )
450 && StringUtils.isNotBlank( myInfos.getArrondissement( ) ) && !UNDEFINED_POSTALCODE_KEY.equals( myInfos.getArrondissement( ) )
451 && StringUtils.isNotBlank( myInfos.getBirthdate( ) )
452 && ( StringUtils.isNotBlank( myInfos.getIliveinparis( ) ) && myInfos.getIliveinparis( ).equals( PREF_VALUE_ON ) );
453 }
454
455
456
457
458
459
460
461
462
463
464
465 private static void populateNickname( MyInfosForm myInfos, IPortalUserPreferencesService userPreferenceService, LuteceUser user )
466 {
467 String strNickname = userPreferenceService.getNickname( user );
468
469 if ( !CampaignService.getInstance( ).isDuring( ParticipatoryBudgetConstants.VOTE ) && StringUtils.isBlank( strNickname ) )
470 {
471 myInfos.setIsValid( false );
472 }
473
474 myInfos.setNickname( strNickname );
475 }
476
477
478
479
480
481
482
483
484
485
486
487 private static void populateCivility( MyInfosForm myInfos, IPortalUserPreferencesService userPreferenceService, LuteceUser user )
488 {
489 String strCivility = userPreferenceService.get( user.getName( ), PREF_KEY_CIVILITY, null );
490
491 if ( strCivility == null )
492 {
493 myInfos.setIsValid( false );
494
495 strCivility = Civility.fromNumericCode( user.getUserInfo( LuteceUser.GENDER ) ).getLabelCode( );
496 }
497
498 myInfos.setCivility( strCivility );
499 }
500
501
502
503
504
505
506
507
508
509
510
511 private static void populateFirstName( MyInfosForm myInfos, IPortalUserPreferencesService userPreferenceService, LuteceUser user )
512 {
513 String strFirstName = userPreferenceService.get( user.getName( ), PREF_KEY_FIRSTNAME, null );
514
515 myInfos.setFirstname( strFirstName != null ? strFirstName : user.getUserInfo( LuteceUser.NAME_GIVEN ) );
516 }
517
518
519
520
521
522
523
524
525
526
527
528 private static void populateLastName( MyInfosForm myInfos, IPortalUserPreferencesService userPreferenceService, LuteceUser user )
529 {
530 String strLastName = userPreferenceService.get( user.getName( ), PREF_KEY_LASTNAME, null );
531
532 if ( strLastName == null )
533 {
534 strLastName = user.getUserInfo( LuteceUser.NAME_FAMILY );
535
536 if ( StringUtils.isEmpty( strLastName ) )
537 {
538 strLastName = user.getUserInfo( LuteceUser.NAME_CIVILITY );
539 }
540 }
541
542 myInfos.setLastname( strLastName );
543 }
544
545
546
547
548
549
550
551
552
553
554
555 private static void populateBirthDate( MyInfosForm myInfos, IPortalUserPreferencesService userPreferenceService, LuteceUser user )
556 {
557 String strBirthDate = userPreferenceService.get( user.getName( ), PREF_KEY_BIRTHDATE, null );
558
559 if ( strBirthDate == null )
560 {
561 myInfos.setIsValid( false );
562
563 strBirthDate = user.getUserInfo( LuteceUser.BDATE );
564 }
565
566 myInfos.setBirthdate( strBirthDate );
567 }
568
569
570
571
572
573
574
575
576
577
578
579 private static void populateAddress( MyInfosForm myInfos, IPortalUserPreferencesService userPreferenceService, LuteceUser user )
580 {
581 String strAddress = userPreferenceService.get( user.getName( ), PREF_KEY_ADDRESS, null );
582
583 if ( strAddress == null )
584 {
585
586
587
588 String strCity = user.getUserInfo( LuteceUser.HOME_INFO_POSTAL_CITY );
589
590 if ( _listAuthorizedCities.contains( strCity.trim( ).toLowerCase( ) ) )
591 {
592 strAddress = user.getUserInfo( LuteceUser.HOME_INFO_POSTAL_STREET ) + ADDRESS_SEPARATOR
593 + user.getUserInfo( LuteceUser.HOME_INFO_POSTAL_POSTALCODE ) + ADDRESS_SEPARATOR + strCity;
594 }
595 }
596
597 myInfos.setAddress( strAddress );
598 }
599
600
601
602
603
604
605
606
607
608
609
610 private static void populatePostalCode( MyInfosForm myInfos, IPortalUserPreferencesService userPreferenceService, LuteceUser user )
611 {
612 String strPostalCode = userPreferenceService.get( user.getName( ), PREF_KEY_POSTAL_CODE, StringUtils.EMPTY );
613
614 if ( StringUtils.isBlank( strPostalCode ) )
615 {
616
617
618 }
619
620 myInfos.setPostalCode( strPostalCode );
621 }
622
623
624
625
626
627
628
629
630
631
632
633 private static void populateArrondissement( MyInfosForm myInfos, IPortalUserPreferencesService userPreferenceService, LuteceUser user )
634 {
635 String strArrondissement = userPreferenceService.get( user.getName( ), PREF_KEY_ARRONDISSEMENT_VOTE, StringUtils.EMPTY );
636
637 if ( CampaignService.getInstance( ).isAfterBeginning( ParticipatoryBudgetConstants.SUBMIT )
638 && CampaignService.getInstance( ).isBeforeEnd( ParticipatoryBudgetConstants.VOTE )
639 && ( StringUtils.isBlank( strArrondissement ) || UNDEFINED_POSTALCODE_KEY.equals( strArrondissement ) ) )
640 {
641 myInfos.setIsValid( false );
642 }
643
644 myInfos.setArrondissement( strArrondissement );
645 }
646
647
648
649
650
651
652
653
654
655
656
657 private static void populateGeoJson( MyInfosForm myInfos, IPortalUserPreferencesService userPreferenceService, LuteceUser user )
658 {
659 String strGeoJson = userPreferenceService.get( user.getName( ), PREF_KEY_GEO_JSON, StringUtils.EMPTY );
660
661 if ( StringUtils.isBlank( strGeoJson ) )
662 {
663
664
665 }
666
667 myInfos.setGeojson( strGeoJson );
668 }
669
670
671
672
673
674
675
676
677
678
679
680 private static void populateILiveInParisFlag( MyInfosForm myInfos, IPortalUserPreferencesService userPreferenceService, LuteceUser user )
681 {
682 if ( userPreferenceService.existsKey( user.getName( ), PREF_KEY_ILIVEINPARIS ) )
683 {
684 String strILiveInParis = userPreferenceService.get( user.getName( ), PREF_KEY_ILIVEINPARIS, PREF_VALUE_OFF );
685
686 if ( PREF_VALUE_OFF.equals( strILiveInParis ) )
687 {
688 myInfos.setIsValid( false );
689 }
690
691 myInfos.setIliveinparis( strILiveInParis );
692 }
693 else
694 {
695 myInfos.setIsValid( false );
696
697 myInfos.setIliveinparis( DEFAULT_CHECKED );
698 }
699 }
700
701
702
703
704
705
706
707
708
709
710
711 private static void populateSendAccountValidationFlag( MyInfosForm myInfos, IPortalUserPreferencesService userPreferenceService, LuteceUser user )
712 {
713 if ( userPreferenceService.existsKey( user.getName( ), PREF_KEY_SEND_ACCOUNT_VALIDATION ) )
714 {
715 myInfos.setSendaccountvalidation( userPreferenceService.get( user.getName( ), PREF_KEY_SEND_ACCOUNT_VALIDATION, PREF_VALUE_OFF ) );
716 }
717 else
718 {
719 myInfos.setSendaccountvalidation( DEFAULT_CHECKED );
720 }
721 }
722
723
724
725
726
727
728
729
730
731
732
733 private static void populateAccountVerifiedFlag( MyInfosForm myInfos, IPortalUserPreferencesService userPreferenceService, LuteceUser user )
734 {
735 myInfos.setAccountVerified( userPreferenceService.getBoolean( user.getName( ), PREF_KEY_ACCOUNT_VERIFIED, false ) );
736 }
737
738 }