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.Map;
54  
55  import javax.servlet.http.HttpServletRequest;
56  
57  
58  /**
59   * This class provides a calendar view by Month.
60   */
61  public class MonthCalendarView implements CalendarView
62  {
63      private static final String TEMPLATE_VIEW_MONTH = "skin/plugins/calendar/calendar_view_month.html";
64      private static final String TEMPLATE_VIEW_WEEK = "skin/plugins/calendar/calendar_view_month_week.html";
65      private static final String TEMPLATE_VIEW_DAY = "skin/plugins/calendar/calendar_view_month_day.html";
66      private static final String TEMPLATE_VIEW_DAY_EVENT = "skin/plugins/calendar/calendar_view_month_event.html";
67      private static final String TEMPLATE_VIEW_EMPTY_DAY = "skin/plugins/calendar/calendar_view_month_empty_day.html";
68  
69      /**
70       * Returns the HTML view of the Month corresponding to the given date and
71       * displaying
72       * events of a given agenda
73       * @return The view in HTML
74       * @param options The options
75       * @param strDate The date code
76       * @param agenda An agenda
77       * @param request HttpServletRequest
78       */
79      public String getCalendarView( String strDate, MultiAgenda agenda, CalendarUserOptions options,
80              HttpServletRequest request )
81      {
82          Map<String, Object> model = new HashMap<String, Object>( );
83          HtmlTemplate tEmptyDay = AppTemplateService.getTemplate( TEMPLATE_VIEW_EMPTY_DAY );
84  
85          Calendar calendar = new GregorianCalendar( );
86          calendar.set( Utils.getYear( strDate ), Utils.getMonth( strDate ), 1 );
87  
88          int nDayOfWeek = calendar.get( Calendar.DAY_OF_WEEK );
89  
90          if ( nDayOfWeek == 1 )
91          {
92              nDayOfWeek = 8;
93          }
94  
95          StringBuffer sbWeeks = new StringBuffer( );
96  
97          boolean bDone = false;
98          boolean bStarted = false;
99  
100         while ( !bDone )
101         {
102             StringBuffer sbDays = new StringBuffer( );
103 
104             for ( int i = 0; i < 7; i++ )
105             {
106                 if ( ( ( ( i + 2 ) != nDayOfWeek ) && !bStarted ) || bDone )
107                 {
108                     sbDays.append( tEmptyDay.getHtml( ) );
109 
110                     continue;
111                 }
112                 bStarted = true;
113 
114                 sbDays.append( getDay( calendar, agenda, options, request ) );
115 
116                 int nDay = calendar.get( Calendar.DAY_OF_MONTH );
117                 calendar.roll( Calendar.DAY_OF_MONTH, true );
118 
119                 int nNewDay = calendar.get( Calendar.DAY_OF_MONTH );
120 
121                 if ( nNewDay < nDay )
122                 {
123                     bDone = true;
124                 }
125             }
126 
127             model.put( Constants.MARK_DAYS, sbDays.toString( ) );
128 
129             HtmlTemplate weekTemplate = AppTemplateService
130                     .getTemplate( TEMPLATE_VIEW_WEEK, options.getLocale( ), model );
131             sbWeeks.append( weekTemplate.getHtml( ) );
132         }
133 
134         model.put( Constants.MARK_WEEKS, sbWeeks.toString( ) );
135         model.put( Constants.MARK_MONTH_LABEL, Utils.getMonthLabel( strDate, options.getLocale( ) ) );
136 
137         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_VIEW_MONTH, options.getLocale( ), model );
138 
139         return template.getHtml( );
140     }
141 
142     /**
143      * Build the day view of a given
144      * @return The HTML of a day
145      * @param options The options
146      * @param calendar A calendar object positioned on a given day
147      * @param agenda The agenda
148      * @param request HttpServletRequest
149      */
150     private String getDay( Calendar calendar, MultiAgenda agenda, CalendarUserOptions options,
151             HttpServletRequest request )
152     {
153         Map<String, Object> dayModel = new HashMap<String, Object>( );
154         StringBuffer sbEvents = new StringBuffer( );
155         String strDate = Utils.getDate( calendar );
156 
157         if ( agenda.hasEvents( strDate ) )
158         {
159             Date date = Utils.getDate( Utils.getDate( calendar ) );
160             Plugin plugin = PluginService.getPlugin( CalendarPlugin.PLUGIN_NAME );
161             List<Event> listIndexedEvents = CalendarSearchService.getInstance( ).getSearchResults(
162                     agenda.getAgendaIds( ), null, "", date, date, plugin );
163 
164             for ( Event event : listIndexedEvents )
165             {
166                 MultiAgendaEvent multiAgendaEvent = new MultiAgendaEvent( event,
167                         String.valueOf( event.getIdCalendar( ) ) );
168                 Map<String, Object> eventModel = new HashMap<String, Object>( );
169                 HtmlUtils.fillEventTemplate( eventModel, multiAgendaEvent, strDate );
170                 eventModel.put( Constants.MARK_JSP_URL,
171                         AppPropertiesService.getProperty( Constants.PROPERTY_RUNAPP_JSP_URL ) );
172 
173                 HtmlTemplate tEvent = AppTemplateService.getTemplate( TEMPLATE_VIEW_DAY_EVENT, options.getLocale( ),
174                         eventModel );
175                 sbEvents.append( tEvent.getHtml( ) );
176             }
177         }
178 
179         String strDateLink = Utils.getDate( calendar );
180         dayModel.put( Constants.MARK_DAY_LINK, strDateLink );
181         dayModel.put( Constants.MARK_DAY_CLASS, getDayClass( calendar ) );
182         dayModel.put( Constants.MARK_DAY, calendar.get( Calendar.DAY_OF_MONTH ) );
183         dayModel.put( Constants.MARK_EVENTS, sbEvents.toString( ) );
184         dayModel.put( Constants.MARK_DATE, strDate );
185 
186         //we only show link on the calendar for days with events
187         if ( agenda.hasEvents( strDate ) )
188         {
189             dayModel.put( Constants.MARK_JSP_URL, AppPropertiesService.getProperty( Constants.PROPERTY_RUNAPP_JSP_URL ) );
190         }
191         else
192         {
193             dayModel.put( Constants.MARK_JSP_URL, "" );
194         }
195 
196         HtmlTemplate tDay = AppTemplateService.getTemplate( TEMPLATE_VIEW_DAY, options.getLocale( ), dayModel );
197 
198         return tDay.getHtml( );
199     }
200 
201     /**
202      * Calculate the style class to render the day
203      * @param calendar A calendar object positionned on the day to render
204      * @return A CSS style
205      */
206     private String getDayClass( Calendar calendar )
207     {
208         String strClass = Constants.STYLE_CLASS_VIEW_MONTH_DAY;
209         String strDate = Utils.getDate( calendar );
210         String strToday = Utils.getDateToday( );
211 
212         if ( Utils.isDayOff( calendar ) )
213         {
214             strClass += Constants.STYLE_CLASS_SUFFIX_OFF;
215         }
216         else if ( strDate.compareTo( strToday ) < 0 )
217         {
218             strClass += Constants.STYLE_CLASS_SUFFIX_OLD;
219         }
220         else if ( strDate.equals( strToday ) )
221         {
222             strClass += Constants.STYLE_CLASS_SUFFIX_TODAY;
223         }
224 
225         return strClass;
226     }
227 
228     /**
229      * Returns the next code date corresponding to the current view and the
230      * current date
231      * @param strDate The current date code
232      * @return The next code date
233      */
234     public String getNext( String strDate )
235     {
236         return Utils.getNextMonth( strDate );
237     }
238 
239     /**
240      * Returns the previous code date corresponding to the current view and the
241      * current date
242      * @param strDate The current date code
243      * @return The previous code date
244      */
245     public String getPrevious( String strDate )
246     {
247         return Utils.getPreviousMonth( strDate );
248     }
249 
250     /**
251      * Returns the view title
252      * @return The view title
253      * @param options The options
254      * @param strDate The current date code
255      */
256     public String getTitle( String strDate, CalendarUserOptions options )
257     {
258         String strTitle = Utils.getMonthLabel( strDate, options.getLocale( ) );
259 
260         return strTitle;
261     }
262 
263     /**
264      * Returns the view path
265      * @return The view path
266      * @param options The options
267      * @param strDate The current date code
268      */
269     public String getPath( String strDate, CalendarUserOptions options )
270     {
271         return getTitle( strDate, options );
272     }
273 
274     /**
275      * Returns the view type
276      * @return The view type
277      */
278     public int getType( )
279     {
280         return CalendarView.TYPE_MONTH;
281     }
282 }