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.calendar.service;
35
36 import fr.paris.lutece.plugins.calendar.business.Agenda;
37 import fr.paris.lutece.plugins.calendar.business.CalendarHome;
38 import fr.paris.lutece.plugins.calendar.business.OccurrenceEvent;
39 import fr.paris.lutece.plugins.calendar.business.SimpleAgenda;
40 import fr.paris.lutece.plugins.calendar.business.SimpleEvent;
41 import fr.paris.lutece.plugins.calendar.web.Constants;
42 import fr.paris.lutece.portal.service.plugin.Plugin;
43 import fr.paris.lutece.portal.service.plugin.PluginService;
44 import fr.paris.lutece.portal.service.security.SecurityService;
45 import fr.paris.lutece.portal.service.spring.SpringContextService;
46 import fr.paris.lutece.portal.service.util.AppLogService;
47 import fr.paris.lutece.portal.service.util.AppPropertiesService;
48
49 import java.text.DateFormat;
50 import java.text.ParseException;
51 import java.text.SimpleDateFormat;
52 import java.util.ArrayList;
53 import java.util.Calendar;
54 import java.util.Date;
55 import java.util.GregorianCalendar;
56 import java.util.List;
57 import java.util.Locale;
58
59 import javax.servlet.http.HttpServletRequest;
60
61 import org.apache.commons.lang.StringUtils;
62 import org.htmlparser.Parser;
63 import org.htmlparser.lexer.Lexer;
64 import org.htmlparser.util.NodeIterator;
65 import org.htmlparser.util.ParserException;
66
67
68
69
70
71
72 public final class Utils
73 {
74
75
76
77 private static final String DATE_PATTERN = "yyyyMMdd";
78
79
80
81
82 private Utils( )
83 {
84 }
85
86
87
88
89
90
91
92
93 public static String getDate( int nYear, int nMonth, int nDay )
94 {
95 String strDate;
96 strDate = "" + nYear;
97
98 int nMonthIndex = nMonth + 1;
99 strDate += ( ( nMonthIndex < 10 ) ? ( "0" + nMonthIndex ) : ( "" + nMonthIndex ) );
100 strDate += ( ( nDay < 10 ) ? ( "0" + nDay ) : ( "" + nDay ) );
101
102 return strDate;
103 }
104
105
106
107
108
109
110 public static String getDate( Calendar calendar )
111 {
112 return getDate( calendar.get( Calendar.YEAR ), calendar.get( Calendar.MONTH ),
113 calendar.get( Calendar.DAY_OF_MONTH ) );
114 }
115
116
117
118
119
120 public static String getDateToday( )
121 {
122 Calendar calendar = new GregorianCalendar( );
123
124 return getDate( calendar );
125 }
126
127
128
129
130
131
132 public static int getYear( String strDate )
133 {
134 SimpleDateFormat format = new SimpleDateFormat( DATE_PATTERN );
135 Date date = null;
136 try
137 {
138 date = format.parse( strDate );
139 }
140 catch ( ParseException ex )
141 {
142 return -1;
143 }
144 GregorianCalendar calendar = new GregorianCalendar( );
145 calendar.setTime( date );
146 return calendar.get( Calendar.YEAR );
147 }
148
149
150
151
152
153
154 public static int getMonth( String strDate )
155 {
156 SimpleDateFormat format = new SimpleDateFormat( DATE_PATTERN );
157 Date date = null;
158 try
159 {
160 date = format.parse( strDate );
161 }
162 catch ( ParseException ex )
163 {
164 return -1;
165 }
166 GregorianCalendar calendar = new GregorianCalendar( );
167 calendar.setTime( date );
168 return calendar.get( Calendar.MONTH );
169 }
170
171
172
173
174
175
176 public static int getDay( String strDate )
177 {
178 SimpleDateFormat format = new SimpleDateFormat( DATE_PATTERN );
179 Date date = null;
180 try
181 {
182 date = format.parse( strDate );
183 }
184 catch ( ParseException ex )
185 {
186 return -1;
187 }
188 GregorianCalendar calendar = new GregorianCalendar( );
189 calendar.setTime( date );
190 return calendar.get( Calendar.DAY_OF_MONTH );
191 }
192
193
194
195
196
197
198
199 public static String getMonthLabel( String strDate, Locale locale )
200 {
201 Calendar calendar = new GregorianCalendar( );
202 calendar.set( Utils.getYear( strDate ), Utils.getMonth( strDate ), 1 );
203
204 String strFormat = AppPropertiesService.getProperty( Constants.PROPERTY_LABEL_FORMAT_MONTH );
205 DateFormat formatDate = new SimpleDateFormat( strFormat, locale );
206 String strLabel = formatDate.format( calendar.getTime( ) );
207
208 return strLabel;
209 }
210
211
212
213
214
215
216
217 public static String getWeekLabel( String strDate, Locale locale )
218 {
219 Calendar calendar = new GregorianCalendar( );
220 Calendar calendarFirstDay = new GregorianCalendar( );
221 Calendar calendarLastDay = new GregorianCalendar( );
222 calendar.set( Utils.getYear( strDate ), Utils.getMonth( strDate ), Utils.getDay( strDate ) );
223
224 int nDayOfWeek = calendar.get( Calendar.DAY_OF_WEEK );
225
226 if ( nDayOfWeek == 1 )
227 {
228 nDayOfWeek = 8;
229 }
230
231 calendarFirstDay = calendar;
232 calendarFirstDay.add( Calendar.DATE, Calendar.MONDAY - nDayOfWeek );
233 calendarLastDay = (GregorianCalendar) calendarFirstDay.clone( );
234 calendarLastDay.add( Calendar.DATE, 6 );
235
236 String strFormat = AppPropertiesService.getProperty( Constants.PROPERTY_LABEL_FORMAT_DATE_OF_DAY );
237 DateFormat formatDate = new SimpleDateFormat( strFormat, locale );
238 String strLabelFirstDay = formatDate.format( calendarFirstDay.getTime( ) );
239 String strLabelLastDay = formatDate.format( calendarLastDay.getTime( ) );
240 calendarFirstDay.clear( );
241 calendarLastDay.clear( );
242
243 return strLabelFirstDay + "-" + strLabelLastDay;
244 }
245
246
247
248
249
250
251
252 public static Calendar getFirstDayOfWeek( String strDate )
253 {
254 Calendar calendar = new GregorianCalendar( );
255 Calendar calendarFirstDay = new GregorianCalendar( );
256 calendar.set( Utils.getYear( strDate ), Utils.getMonth( strDate ), Utils.getDay( strDate ) );
257
258 int nDayOfWeek = calendar.get( Calendar.DAY_OF_WEEK );
259
260 if ( nDayOfWeek == 1 )
261 {
262 nDayOfWeek = 8;
263 }
264
265 calendarFirstDay = calendar;
266 calendarFirstDay.add( Calendar.DATE, Calendar.MONDAY - nDayOfWeek );
267
268 return calendarFirstDay;
269 }
270
271
272
273
274
275
276
277
278 public static String getDayLabel( String strDate, Locale locale )
279 {
280 Calendar calendar = new GregorianCalendar( locale );
281 calendar.set( Utils.getYear( strDate ), Utils.getMonth( strDate ), Utils.getDay( strDate ) );
282
283 String strFormat = AppPropertiesService.getProperty( Constants.PROPERTY_LABEL_FORMAT_DAY );
284 DateFormat formatDate = new SimpleDateFormat( strFormat, locale );
285
286 return formatDate.format( calendar.getTime( ) );
287 }
288
289
290
291
292
293
294
295
296 public static String getWeekDayLabel( String strDate, Locale locale )
297 {
298 Calendar calendar = new GregorianCalendar( locale );
299 calendar.set( Utils.getYear( strDate ), Utils.getMonth( strDate ), Utils.getDay( strDate ) );
300
301 String strFormat = AppPropertiesService.getProperty( Constants.PROPERTY_LABEL_FORMAT_WEEK_DAY );
302 DateFormat formatDate = new SimpleDateFormat( strFormat, locale );
303
304 return formatDate.format( calendar.getTime( ) );
305 }
306
307
308
309
310
311
312 public static String getNextMonth( String strDate )
313 {
314 Calendar calendar = new GregorianCalendar( );
315 calendar.set( Utils.getYear( strDate ), Utils.getMonth( strDate ), Utils.getDay( strDate ) );
316 calendar.add( Calendar.MONTH, 1 );
317
318 return getDate( calendar );
319 }
320
321
322
323
324
325
326
327 public static String getPreviousMonth( String strDate )
328 {
329 Calendar calendar = new GregorianCalendar( );
330 calendar.set( Utils.getYear( strDate ), Utils.getMonth( strDate ), Utils.getDay( strDate ) );
331 calendar.add( Calendar.MONTH, -1 );
332
333 return getDate( calendar );
334 }
335
336
337
338
339
340
341 public static String getPreviousWeek( String strDate )
342 {
343 Calendar calendar = new GregorianCalendar( );
344 calendar.set( Utils.getYear( strDate ), Utils.getMonth( strDate ), Utils.getDay( strDate ) );
345 calendar.add( Calendar.DATE, -7 );
346
347 return getDate( calendar );
348 }
349
350
351
352
353
354
355 public static String getNextWeek( String strDate )
356 {
357 Calendar calendar = new GregorianCalendar( );
358 calendar.set( Utils.getYear( strDate ), Utils.getMonth( strDate ), Utils.getDay( strDate ) );
359 calendar.add( Calendar.DATE, 7 );
360
361 return getDate( calendar );
362 }
363
364
365
366
367
368
369 public static String getNextDay( String strDate )
370 {
371 Calendar calendar = new GregorianCalendar( );
372 calendar.set( Utils.getYear( strDate ), Utils.getMonth( strDate ), Utils.getDay( strDate ) );
373 calendar.add( Calendar.DATE, 1 );
374
375 return getDate( calendar );
376 }
377
378
379
380
381
382
383 public static String getPreviousDay( String strDate )
384 {
385 Calendar calendar = new GregorianCalendar( );
386 calendar.set( Utils.getYear( strDate ), Utils.getMonth( strDate ), Utils.getDay( strDate ) );
387 calendar.add( Calendar.DATE, -1 );
388
389 return getDate( calendar );
390 }
391
392
393
394
395
396
397 public static boolean isValid( String strDate )
398 {
399 if ( strDate == null )
400 {
401 return false;
402 }
403
404 if ( strDate.length( ) != 8 )
405 {
406 return false;
407 }
408
409 int nYear;
410 int nMonth;
411 int nDay;
412
413 try
414 {
415 nYear = getYear( strDate );
416 nMonth = getMonth( strDate );
417 nDay = getDay( strDate );
418 }
419 catch ( NumberFormatException e )
420 {
421 return false;
422 }
423
424 if ( ( nYear < 1900 ) || ( nYear > 2100 ) )
425 {
426 return false;
427 }
428
429 if ( ( nMonth < 0 ) || ( nMonth > 11 ) )
430 {
431 return false;
432 }
433
434 if ( ( nDay < 1 ) || ( nDay > 31 ) )
435 {
436 return false;
437 }
438
439 return true;
440 }
441
442
443
444
445
446
447 public static boolean isDayOff( Calendar calendar )
448 {
449 int nDayOfWeek = calendar.get( Calendar.DAY_OF_WEEK );
450
451 if ( ( nDayOfWeek == Calendar.SATURDAY ) || ( nDayOfWeek == Calendar.SUNDAY ) )
452 {
453 return true;
454 }
455
456
457 return false;
458 }
459
460
461
462
463
464
465
466 public static boolean checkTime( String strTime )
467 {
468 boolean bCheck = false;
469
470 if ( strTime.equals( "" ) )
471 {
472 bCheck = true;
473 }
474 else if ( strTime.length( ) == 5 )
475 {
476 try
477 {
478 int nHour = Integer.parseInt( strTime.substring( 0, 2 ) );
479 int nMinute = Integer.parseInt( strTime.substring( 3, 5 ) );
480
481 if ( ( strTime.charAt( 2 ) == ':' ) && ( nHour < 25 ) && ( nMinute < 60 ) )
482 {
483 bCheck = true;
484 }
485 }
486 catch ( NumberFormatException e )
487 {
488 bCheck = false;
489 }
490 }
491
492 return bCheck;
493 }
494
495
496
497
498
499
500
501 public static String getDayAfter( String strDate, int n )
502 {
503 Calendar calendar = new GregorianCalendar( );
504 calendar.set( Utils.getYear( strDate ), Utils.getMonth( strDate ), Utils.getDay( strDate ) );
505 calendar.add( Calendar.DATE, n );
506
507 return getDate( calendar );
508 }
509
510
511
512
513
514
515
516
517 public static Date getDayAfter( Date dateDayAfter, int n )
518 {
519 Calendar calendar = new GregorianCalendar( );
520 calendar.setTime( dateDayAfter );
521 calendar.add( Calendar.DATE, n );
522
523 return calendar.getTime( );
524 }
525
526
527
528
529
530
531
532
533
534 public static Date getDateForward( Date dateDay, int nPeriodicity, int nOccurrence, String[] arrayExcludedDays )
535 {
536 int nOccurrenceDiff;
537 int nOccurrenceInit = nOccurrence;
538 Calendar calendar = new GregorianCalendar( );
539 calendar.setTime( dateDay );
540
541
542 if ( arrayExcludedDays != null && arrayExcludedDays.length == 7 )
543 {
544 calendar.add( Calendar.DATE, -1 );
545 return calendar.getTime( );
546 }
547
548
549 if ( nOccurrenceInit != 0 )
550 {
551 nOccurrenceInit -= 1;
552 }
553
554 switch ( nPeriodicity )
555 {
556 case Constants.PARAM_DAY:
557 calendar.add( Calendar.DATE, nOccurrenceInit );
558 do
559 {
560 nOccurrenceDiff = getOccurrenceWithinTwoDates( dateDay, calendar.getTime( ), arrayExcludedDays );
561 if ( nOccurrenceDiff < nOccurrence )
562 {
563 calendar.add( Calendar.DATE, nOccurrence - nOccurrenceDiff );
564 }
565 }
566 while ( nOccurrenceDiff < nOccurrence && nOccurrenceDiff != 0 );
567
568 break;
569
570 case Constants.PARAM_WEEK:
571 String strDate = getDate( dateDay );
572 if ( !isDayExcluded( getDayOfWeek( strDate ), arrayExcludedDays ) )
573 {
574 calendar.add( Calendar.DATE, nOccurrenceInit * 7 );
575 }
576
577 break;
578
579 case Constants.PARAM_MONTH:
580 calendar.add( Calendar.MONTH, nOccurrenceInit );
581 do
582 {
583 nOccurrenceDiff = getOccurrenceWithinTwoDates( dateDay, calendar.getTime( ), arrayExcludedDays );
584 if ( nOccurrenceDiff < nOccurrence )
585 {
586 calendar.add( Calendar.MONTH, nOccurrence - nOccurrenceDiff );
587 }
588 }
589 while ( nOccurrenceDiff < nOccurrence && nOccurrenceDiff != 0 );
590
591 break;
592
593 default:
594 calendar.add( Calendar.DATE, nOccurrenceInit );
595 do
596 {
597 nOccurrenceDiff = getOccurrenceWithinTwoDates( dateDay, calendar.getTime( ), arrayExcludedDays );
598 if ( nOccurrenceDiff < nOccurrence )
599 {
600 calendar.add( Calendar.DATE, nOccurrence - nOccurrenceDiff );
601 }
602 }
603 while ( nOccurrenceDiff < nOccurrence && nOccurrenceDiff != 0 );
604
605 break;
606 }
607
608 return calendar.getTime( );
609 }
610
611
612
613
614
615
616 public static Date getDate( String strDate )
617 {
618 Calendar calendar = new GregorianCalendar( );
619 calendar.set( Utils.getYear( strDate ), Utils.getMonth( strDate ), Utils.getDay( strDate ) );
620
621 return calendar.getTime( );
622 }
623
624
625
626
627
628
629 public static String getDate( Date dateDigit )
630 {
631 if ( dateDigit == null )
632 {
633 return "";
634 }
635 Calendar calendar = new GregorianCalendar( );
636 calendar.setTime( dateDigit );
637
638 return getDate( calendar );
639 }
640
641
642
643
644
645
646
647 public static String getNextMonth( String strDateRef, int nCptDate )
648 {
649 Calendar calendar = new GregorianCalendar( );
650 calendar.set( Utils.getYear( strDateRef ), Utils.getMonth( strDateRef ), Utils.getDay( strDateRef ) );
651 calendar.add( Calendar.MONTH, nCptDate );
652
653 return getDate( calendar );
654 }
655
656
657
658
659
660
661
662
663 public static int getOccurrenceWithinTwoDates( Date dateStart, Date dateEnd, String[] arrayExcludedDays )
664 {
665 int cptDate = 0;
666 Calendar calendar1 = new GregorianCalendar( );
667 Calendar calendar2 = new GregorianCalendar( );
668 calendar1.setTime( dateStart );
669 calendar2.setTime( dateEnd );
670
671 if ( calendar1.equals( calendar2 ) )
672 {
673 String strDate = getDate( dateStart );
674 if ( isDayExcluded( getDayOfWeek( strDate ), arrayExcludedDays ) )
675 {
676 return cptDate;
677 }
678 return ++cptDate;
679 }
680
681 while ( !calendar1.after( calendar2 ) )
682 {
683 if ( !isDayExcluded( calendar1.get( Calendar.DAY_OF_WEEK ), arrayExcludedDays ) )
684 {
685 ++cptDate;
686 }
687 calendar1.add( Calendar.DATE, 1 );
688 }
689
690 return cptDate;
691 }
692
693
694
695
696
697
698
699 public static Agenda getAgendaWithEvents( String strAgenda, HttpServletRequest request )
700 {
701 CalendarService calendarService = (CalendarService) SpringContextService
702 .getBean( Constants.BEAN_CALENDAR_CALENDARSERVICE );
703 Agenda agenda = null;
704
705 if ( strAgenda != null )
706 {
707 AgendaResource agendaResource = calendarService.getAgendaResource( strAgenda );
708 Agenda a = null;
709
710 if ( agendaResource != null )
711 {
712 a = agendaResource.getAgenda( );
713 }
714
715 if ( a != null )
716 {
717
718 String strRole = agendaResource.getRole( );
719
720 if ( StringUtils.isNotBlank( strRole ) && ( request != null )
721 && ( !Constants.PROPERTY_ROLE_NONE.equals( strRole ) ) )
722 {
723 if ( SecurityService.isAuthenticationEnable( ) )
724 {
725 if ( SecurityService.getInstance( ).isUserInRole( request, strRole ) )
726 {
727 agenda = a;
728 }
729 }
730 }
731 else
732 {
733 agenda = a;
734 }
735 }
736 }
737 else
738 {
739 agenda = new SimpleAgenda( );
740 }
741
742 return agenda;
743 }
744
745
746
747
748
749
750
751 public static Agenda getAgendaWithOccurrences( String strAgenda, HttpServletRequest request )
752 {
753 Agenda agenda = null;
754
755 if ( strAgenda != null )
756 {
757 for ( AgendaResource agendaResource : getAgendaResourcesWithOccurrences( ) )
758 {
759 if ( agendaResource.getAgenda( ).getKeyName( ).equals( strAgenda ) )
760 {
761 Agenda a = agendaResource.getAgenda( );
762
763 if ( a != null )
764 {
765
766 String strRole = agendaResource.getRole( );
767
768 if ( StringUtils.isNotBlank( strRole ) && ( request != null )
769 && ( !Constants.PROPERTY_ROLE_NONE.equals( strRole ) ) )
770 {
771 if ( SecurityService.isAuthenticationEnable( ) )
772 {
773 if ( SecurityService.getInstance( ).isUserInRole( request, strRole ) )
774 {
775 agenda = a;
776 }
777 }
778 }
779 else
780 {
781 agenda = a;
782 }
783 }
784 }
785 }
786 }
787 else
788 {
789 agenda = new SimpleAgenda( );
790 }
791
792 return agenda;
793 }
794
795
796
797
798
799
800
801 public static Agenda getAgendaWithOccurrencesOrderedbyId( String strAgenda, HttpServletRequest request )
802 {
803 Agenda agenda = null;
804
805 if ( strAgenda != null )
806 {
807 for ( AgendaResource agendaResource : getAgendaResourcesWithOccurrencesIds( ) )
808 {
809 if ( agendaResource.getAgenda( ).getKeyName( ).equals( strAgenda ) )
810 {
811 Agenda a = agendaResource.getAgenda( );
812
813 if ( a != null )
814 {
815
816 String strRole = agendaResource.getRole( );
817
818 if ( StringUtils.isNotBlank( strRole ) && ( request != null )
819 && ( !Constants.PROPERTY_ROLE_NONE.equals( strRole ) ) )
820 {
821 if ( SecurityService.isAuthenticationEnable( ) )
822 {
823 if ( SecurityService.getInstance( ).isUserInRole( request, strRole ) )
824 {
825 agenda = a;
826 }
827 }
828 }
829 else
830 {
831 agenda = a;
832 }
833 }
834 }
835 }
836 }
837 else
838 {
839 agenda = new SimpleAgenda( );
840 }
841
842 return agenda;
843 }
844
845
846
847
848
849
850 public static List<AgendaResource> getAgendaResourcesWithOccurrences( HttpServletRequest request )
851 {
852 Plugin plugin = PluginService.getPlugin( CalendarPlugin.PLUGIN_NAME );
853
854 List<AgendaResource> listCalendar = CalendarHome.findAgendaResourcesList( plugin );
855 List<AgendaResource> listAuthaurizedAgenda = new ArrayList<AgendaResource>( );
856
857 for ( AgendaResource a : listCalendar )
858 {
859 if ( a != null )
860 {
861
862 String strRole = a.getRole( );
863
864 if ( StringUtils.isNotBlank( strRole ) && ( request != null )
865 && ( !Constants.PROPERTY_ROLE_NONE.equals( strRole ) ) )
866 {
867 if ( SecurityService.isAuthenticationEnable( ) )
868 {
869 if ( SecurityService.getInstance( ).isUserInRole( request, strRole ) )
870 {
871 listAuthaurizedAgenda.add( a );
872 }
873 }
874 }
875 else
876 {
877 listAuthaurizedAgenda.add( a );
878 }
879 }
880 }
881
882 for ( AgendaResource a : listAuthaurizedAgenda )
883 {
884 loadAgendaOccurrences( a, plugin );
885 }
886
887 return listAuthaurizedAgenda;
888 }
889
890
891
892
893
894 public static List<AgendaResource> getAgendaResourcesWithOccurrences( )
895 {
896 Plugin plugin = PluginService.getPlugin( CalendarPlugin.PLUGIN_NAME );
897
898 List<AgendaResource> listCalendar = CalendarHome.findAgendaResourcesList( plugin );
899
900 for ( AgendaResource a : listCalendar )
901 {
902 loadAgendaOccurrences( a, plugin );
903 }
904
905 return listCalendar;
906 }
907
908
909
910
911
912 public static List<AgendaResource> getAgendaResourcesWithEvents( )
913 {
914 Plugin plugin = PluginService.getPlugin( CalendarPlugin.PLUGIN_NAME );
915
916 List<AgendaResource> listCalendar = CalendarHome.findAgendaResourcesList( plugin );
917
918 for ( AgendaResource a : listCalendar )
919 {
920 loadAgendaEvents( a, plugin );
921 }
922
923 return listCalendar;
924 }
925
926
927
928
929
930 public static List<AgendaResource> getAgendaResourcesWithOccurrencesIds( )
931 {
932 Plugin plugin = PluginService.getPlugin( CalendarPlugin.PLUGIN_NAME );
933
934 List<AgendaResource> listCalendar = CalendarHome.findAgendaResourcesList( plugin );
935
936 for ( AgendaResource a : listCalendar )
937 {
938 loadAgendaOccurrencesOrderedById( a, plugin );
939 }
940
941 return listCalendar;
942 }
943
944
945
946
947
948
949 public static void loadAgendaOccurrences( AgendaResource agenda, Plugin plugin )
950 {
951 SimpleAgenda a = new SimpleAgenda( );
952
953 for ( OccurrenceEvent occurrence : CalendarHome.findOccurrencesList( Integer.parseInt( agenda.getId( ) ), 1,
954 plugin ) )
955 {
956 a.addEvent( occurrence );
957 }
958
959 a.setName( agenda.getName( ) );
960 a.setKeyName( agenda.getId( ) );
961 agenda.setAgenda( a );
962 agenda.setResourceType( AppPropertiesService.getProperty( Constants.PROPERTY_READ_WRITE ) );
963 }
964
965
966
967
968
969
970 public static void loadAgendaOccurrencesOrderedById( AgendaResource agenda, Plugin plugin )
971 {
972 SimpleAgenda a = new SimpleAgenda( );
973
974 for ( OccurrenceEvent occurrence : CalendarHome.findOccurrencesByIdList( Integer.parseInt( agenda.getId( ) ),
975 plugin ) )
976 {
977 a.addEvent( occurrence );
978 }
979
980 a.setName( agenda.getName( ) );
981 a.setKeyName( agenda.getId( ) );
982 agenda.setAgenda( a );
983 agenda.setResourceType( AppPropertiesService.getProperty( Constants.PROPERTY_READ_WRITE ) );
984 }
985
986
987
988
989
990
991 public static void loadAgendaEvents( AgendaResource agenda, Plugin plugin )
992 {
993 SimpleAgenda a = new SimpleAgenda( );
994
995 for ( SimpleEvent event : CalendarHome.findEventsList( Integer.parseInt( agenda.getId( ) ), 1, plugin ) )
996 {
997 a.addEvent( event );
998 }
999 a.setName( agenda.getName( ) );
1000 a.setKeyName( agenda.getId( ) );
1001 agenda.setAgenda( a );
1002 agenda.setResourceType( AppPropertiesService.getProperty( Constants.PROPERTY_READ_WRITE ) );
1003 }
1004
1005
1006
1007
1008
1009
1010 public static boolean isValidDate( Date dateEvent )
1011 {
1012 return isValid( getDate( dateEvent ) );
1013 }
1014
1015
1016
1017
1018
1019
1020 public static String parseHtmlToPlainTextString( String strHTML )
1021 {
1022 StringBuilder sbText = new StringBuilder( );
1023
1024 try
1025 {
1026 Lexer lexer = new Lexer( strHTML );
1027 Parser parser = new Parser( lexer );
1028 NodeIterator i = parser.elements( );
1029
1030 while ( i.hasMoreNodes( ) )
1031 {
1032 sbText.append( i.nextNode( ).toPlainTextString( ) );
1033 }
1034 }
1035 catch ( ParserException e )
1036 {
1037 AppLogService.error( "Parsing html to plain text error : " + e.getMessage( ), e );
1038 }
1039
1040 return sbText.toString( );
1041 }
1042
1043
1044
1045
1046
1047
1048 public static String[] getCalendarIds( HttpServletRequest request )
1049 {
1050 String[] arrayCalendarIds = null;
1051 CalendarService calendarService = (CalendarService) SpringContextService
1052 .getBean( Constants.BEAN_CALENDAR_CALENDARSERVICE );
1053
1054 List<AgendaResource> listCalendar = calendarService.getAgendaResources( request );
1055 List<String> listCalendarIds = new ArrayList<String>( );
1056
1057 for ( AgendaResource a : listCalendar )
1058 {
1059
1060 String strRole = a.getRole( );
1061
1062 if ( StringUtils.isNotBlank( strRole ) && ( request != null )
1063 && ( !Constants.PROPERTY_ROLE_NONE.equals( strRole ) ) )
1064 {
1065 if ( SecurityService.isAuthenticationEnable( ) )
1066 {
1067 if ( SecurityService.getInstance( ).isUserInRole( request, strRole ) )
1068 {
1069 listCalendarIds.add( a.getAgenda( ).getKeyName( ) );
1070 }
1071 }
1072 }
1073 else
1074 {
1075 listCalendarIds.add( a.getAgenda( ).getKeyName( ) );
1076 }
1077 }
1078 if ( listCalendarIds.size( ) != 0 )
1079 {
1080 arrayCalendarIds = new String[listCalendarIds.size( )];
1081 for ( int i = 0; i < listCalendarIds.size( ); i++ )
1082 {
1083 arrayCalendarIds[i] = listCalendarIds.get( i );
1084 }
1085 }
1086
1087 return arrayCalendarIds;
1088 }
1089
1090
1091
1092
1093
1094
1095
1096
1097 public static int getDayOfWeek( String strDate )
1098 {
1099 SimpleDateFormat format = new SimpleDateFormat( DATE_PATTERN );
1100 Date date = null;
1101 try
1102 {
1103 date = format.parse( strDate );
1104 }
1105 catch ( ParseException ex )
1106 {
1107 return -1;
1108 }
1109 GregorianCalendar calendar = new GregorianCalendar( );
1110 calendar.setTime( date );
1111 return calendar.get( Calendar.DAY_OF_WEEK );
1112 }
1113
1114
1115
1116
1117
1118
1119
1120 public static boolean isDayExcluded( int nDayOfWeek, String[] arrayExcludedDays )
1121 {
1122 if ( arrayExcludedDays == null || arrayExcludedDays.length == 0 )
1123 {
1124 return false;
1125 }
1126
1127 for ( int i = 0; i < arrayExcludedDays.length; i++ )
1128 {
1129 if ( StringUtils.isNotBlank( arrayExcludedDays[i] ) && StringUtils.isNumeric( arrayExcludedDays[i] ) )
1130 {
1131 int nExcludedDay = Integer.parseInt( arrayExcludedDays[i] );
1132 if ( nDayOfWeek == nExcludedDay )
1133 {
1134 return true;
1135 }
1136 }
1137 }
1138 return false;
1139 }
1140 }