View Javadoc
1   /*
2    * Copyright (c) 2002-2014, Mairie de Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
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   * This class provides utils features to manipulate and convert calendars, date
70   * as string, ...
71   */
72  public final class Utils
73  {
74      /**
75       * Date pattern yyyyMMdd
76       */
77      private static final String DATE_PATTERN = "yyyyMMdd";
78  
79      /**
80       * Default constructor
81       */
82      private Utils( )
83      {
84      }
85  
86      /**
87       * Constructs a 8 digits date string code YYYYMMDD
88       * @param nYear The Year
89       * @param nMonth The month index (0-11)
90       * @param nDay The day of the month (1-31)
91       * @return The date string code
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      * Constructs a 8 digits date string YYYYMMDD
107      * @param calendar A calendar positionned on the date
108      * @return The date code
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      * Returns a the date code of today
118      * @return The date code
119      */
120     public static String getDateToday( )
121     {
122         Calendar calendar = new GregorianCalendar( );
123 
124         return getDate( calendar );
125     }
126 
127     /**
128      * Returns the year from a date code
129      * @param strDate The date code
130      * @return The Year
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      * Returns the month from a date code
151      * @param strDate The date code
152      * @return The month index (0 - 11)
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      * Returns the day of month from a date code
173      * @param strDate The date code
174      * @return The day
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      * Returns the month as a formatted string corresponding to the date code
195      * @return The month label
196      * @param locale The locale used for display settings
197      * @param strDate The date code
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      * Returns the Week as a formatted string corresponding to the date code
213      * @return The week label
214      * @param locale The locale used for display settings
215      * @param strDate The date code
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      * Returns the first monday of a week as a formatted string corresponding to
248      * the date code
249      * @param strDate The date code
250      * @return The first day label
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      * Returns the day as an international formatted string corresponding to the
273      * date code
274      * @return The day as a string
275      * @param locale The locale used for display settings
276      * @param strDate The date code
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      * Returns the day as an international formatted string corresponding to the
291      * date code
292      * @return The day as a string
293      * @param locale The locale used for display settings
294      * @param strDate The date code
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      * Returns a date code corresponding to a calendar roll of one month
309      * @param strDate The date code
310      * @return A date code one month later
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      * Returns a date code corresponding to a calendar roll of one month
323      * backward
324      * @param strDate The date code
325      * @return A new date code one month earlier
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      * Returns a date code corresponding to a calendar roll of one week backward
338      * @param strDate The date code
339      * @return A new date code one month earlier
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      * Returns a date code corresponding to a calendar roll of one week forward
352      * @param strDate The date code
353      * @return A new date code one month earlier
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      * Returns a date code corresponding to a calendar roll of one day forward
366      * @param strDate The date code
367      * @return A new date code one month earlier
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      * Returns a date code corresponding to a calendar roll of one day backward
380      * @param strDate The date code
381      * @return A new date code one month earlier
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      * Checks a date code
394      * @param strDate The date code
395      * @return True if valid otherwise false
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      * Checks if the day if Off (ie: Sunday) or not
444      * @param calendar A calendar object positionned on the day to check
445      * @return True if the day if Off, otherwise false
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         // Add other checks here
457         return false;
458     }
459 
460     /**
461      * Return a boolean: if the time is well formed return true, else return
462      * false
463      * @return a boolean
464      * @param strTime The time
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      * Returns a date code corresponding to a calendar roll of n days forward
497      * @return A new date code one month earlier
498      * @param n number of days to roll
499      * @param strDate The date code
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     /* Added in version 2.1.1 */
511     /**
512      * Returns a date code corresponding to a calendar roll of n days forward
513      * @return A new date code with n days forward
514      * @param n number of days to roll
515      * @param dateDayAfter The date
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      * Returns a date code corresponding to a calendar roll of n days forward
528      * @return A new date code one month earlier
529      * @param dateDay the reference date
530      * @param nPeriodicity the frequence of an occurrence day, week, month
531      * @param nOccurrence the number of occurrences
532      * @param arrayExcludedDays list of excluded days
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         // All days are excluded
542         if ( arrayExcludedDays != null && arrayExcludedDays.length == 7 )
543         {
544             calendar.add( Calendar.DATE, -1 );
545             return calendar.getTime( );
546         }
547 
548         //the very first occurrence is omitted for the final count
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      * Constructs a Date object from YYYYMMDD
613      * @param strDate a 8 digits date string YYYYMMDD
614      * @return The date code
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      * Constructs a digit string from a date object
626      * @param dateDigit The date code
627      * @return strDate a 8 digits date string YYYYMMDD
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      * Returns a date code corresponding to a calendar roll of one month
643      * @param strDateRef The date reference
644      * @param nCptDate The number of month to add
645      * @return A date code incremented with strDateRef parameter
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      * Returns a date code corresponding to a calendar roll of one month
658      * @param dateStart The date start
659      * @param dateEnd The date end
660      * @param arrayExcludedDays list of excluded days
661      * @return A date code incremented with strDateRef parameter
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      * Get a specified agenda from database with events
695      * @param strAgenda The name of the agenda to get
696      * @param request The HTTP request
697      * @return An agenda object
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                 // Check security access
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      * Get a specified agenda from database with occurrences
747      * @param strAgenda The agenda to get
748      * @param request The HTTP request
749      * @return An agenda object
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                         // Check security access
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      * Get a specified agenda from database with occurrences
797      * @param strAgenda The agenda to get
798      * @param request The HTTP request
799      * @return An agenda object
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                         // Check security access
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      * Get all agendas from database checkin the security
847      * @param request The request
848      * @return An agenda object
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                 // Check security access
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      * Get the multi agenda from database
892      * @return An agenda object
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      * Get the multi agenda from database
910      * @return An agenda object
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      * Get the multi agenda from database with occurrences ordered by id
928      * @return An agenda object
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      * Return the agenda
946      * @param agenda The agenda
947      * @param plugin The plugin
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      * Return the occurrences of an agenda ordered by id
967      * @param agenda The agenda
968      * @param plugin The plugin
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      * Return the agenda
988      * @param agenda The agenda
989      * @param plugin The plugin
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      * Checks a date code
1007      * @param dateEvent The date code
1008      * @return True if valid otherwise false
1009      */
1010     public static boolean isValidDate( Date dateEvent )
1011     {
1012         return isValid( getDate( dateEvent ) );
1013     }
1014 
1015     /**
1016      * Parse a HTML string into plain text
1017      * @param strHTML The HTMl to parse
1018      * @return The Plain text describing the given HTML
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      * Get all calendar ids that the user is authorized to visualize
1045      * @param request HttpServletRequest
1046      * @return String array
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             // Check security access
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      * Get the day of week of a given date using the pattern
1092      * {@link #DATE_PATTERN }
1093      * @param strDate The date to parse
1094      * @return The day of week of the given date, or -1 if the date could not be
1095      *         parsed
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      * Check if a day is in the list of excluded days
1116      * @param nDayOfWeek the day to check
1117      * @param arrayExcludedDays the array of excluded days
1118      * @return true if it is excluded, false otherwise
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 }