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.web;
35  
36  import fr.paris.lutece.plugins.calendar.business.Event;
37  import fr.paris.lutece.plugins.calendar.business.MultiAgenda;
38  import fr.paris.lutece.plugins.calendar.business.MultiAgendaEvent;
39  import fr.paris.lutece.plugins.calendar.service.CalendarPlugin;
40  import fr.paris.lutece.plugins.calendar.service.Utils;
41  import fr.paris.lutece.plugins.calendar.service.search.CalendarSearchService;
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.template.AppTemplateService;
45  import fr.paris.lutece.portal.service.util.AppPropertiesService;
46  import fr.paris.lutece.util.html.HtmlTemplate;
47  
48  import java.util.Calendar;
49  import java.util.Date;
50  import java.util.GregorianCalendar;
51  import java.util.HashMap;
52  import java.util.List;
53  import java.util.Locale;
54  
55  import javax.servlet.http.HttpServletRequest;
56  
57  
58  /**
59   * An implementation of an EvenList, listing all events of a month
60   */
61  public class WeekEventList implements EventList
62  {
63      private static final String TEMPLATE_WEEK_EVENT_LIST = "skin/plugins/calendar/calendar_eventlist_week.html";
64      private static final String TEMPLATE_EVENT_WEEK_LIST_DAY = "skin/plugins/calendar/calendar_eventlist_week_day.html";
65      private static final String TEMPLATE_WEEK_EVENT_LIST_EVENT = "skin/plugins/calendar/calendar_eventlist_week_event.html";
66  
67      /**
68       * Build an event list corresponding to the date, the agenda and privileges
69       * stored in the HTTP request
70       * @return The HTML code of the event list formatted
71       * @param locale The locale
72       * @param strWeekDate The date of the event list
73       * @param agenda The multi agenda object
74       * @param request HttpServletRequest
75       */
76      public String getEventList( String strWeekDate, MultiAgenda agenda, Locale locale, HttpServletRequest request )
77      {
78          HashMap<String, Object> model = new HashMap<String, Object>( );
79          int nYear = Utils.getYear( strWeekDate );
80          int nMonth = Utils.getMonth( strWeekDate );
81          int nDay = Utils.getDay( strWeekDate );
82  
83          Calendar calendar = new GregorianCalendar( );
84          calendar.set( nYear, nMonth, nDay );
85  
86          StringBuffer sbDays = new StringBuffer( );
87  
88          // Go backward to the Monday day
89          while ( calendar.get( Calendar.DAY_OF_WEEK ) != Calendar.MONDAY )
90          {
91              calendar.add( Calendar.DATE, -1 );
92          }
93  
94          // Search events for the 7 next days
95          for ( int i = 0; i < 7; i++ )
96          {
97              String strDate = Utils.getDate( calendar );
98  
99              if ( agenda.hasEvents( strDate ) )
100             {
101                 sbDays.append( getDay( strDate, agenda, locale, request ) );
102             }
103 
104             calendar.add( Calendar.DATE, 1 );
105         }
106 
107         model.put( Constants.MARK_DAYS, sbDays.toString( ) );
108 
109         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_WEEK_EVENT_LIST, locale, model );
110 
111         return template.getHtml( );
112     }
113 
114     /**
115      * Build the day
116      * @return The day formatted
117      * @param locale The locale
118      * @param strDate The date of the Day
119      * @param agenda The agenda that contains events
120      * @param request HttpServletRequest
121      */
122     private String getDay( String strDate, MultiAgenda agenda, Locale locale, HttpServletRequest request )
123     {
124         StringBuffer sbEvents = new StringBuffer( );
125         Date date = Utils.getDate( strDate );
126         Plugin plugin = PluginService.getPlugin( CalendarPlugin.PLUGIN_NAME );
127         List<Event> listIndexedEvents = CalendarSearchService.getInstance( ).getSearchResults( agenda.getAgendaIds( ),
128                 null, Constants.EMPTY_STRING, date, date, plugin );
129         boolean bHasIndexedEvent = Boolean.FALSE;
130 
131         for ( Event event : listIndexedEvents )
132         {
133             MultiAgendaEvent multiAgendaEvent = new MultiAgendaEvent( event, String.valueOf( event.getIdCalendar( ) ) );
134             HashMap<String, Object> eventModel = new HashMap<String, Object>( );
135             HtmlUtils.fillEventTemplate( eventModel, multiAgendaEvent, strDate );
136 
137             HtmlTemplate tEvent = AppTemplateService.getTemplate( TEMPLATE_WEEK_EVENT_LIST_EVENT, locale, eventModel );
138             sbEvents.append( tEvent.getHtml( ) );
139             bHasIndexedEvent = Boolean.TRUE;
140         }
141 
142         HashMap<String, Object> dayModel = new HashMap<String, Object>( );
143 
144         if ( bHasIndexedEvent )
145         {
146             dayModel.put( Constants.MARK_DAY, Utils.getDayLabel( strDate, locale ) );
147             dayModel.put( Constants.MARK_EVENTS, sbEvents.toString( ) );
148             dayModel.put( Constants.MARK_DATE, strDate );
149             dayModel.put( Constants.MARK_JSP_URL, AppPropertiesService.getProperty( Constants.PROPERTY_RUNAPP_JSP_URL ) );
150             HtmlTemplate tDay = AppTemplateService.getTemplate( TEMPLATE_EVENT_WEEK_LIST_DAY, locale, dayModel );
151 
152             return tDay.getHtml( );
153         }
154 
155         return Constants.EMPTY_STRING;
156     }
157 }