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.HashMap;
51  import java.util.List;
52  import java.util.Locale;
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 WeekCalendarView implements CalendarView
62  {
63      private static final String TEMPLATE_VIEW_WEEK = "skin/plugins/calendar/calendar_view_week.html";
64      private static final String TEMPLATE_VIEW_WEEK_DAY = "skin/plugins/calendar/calendar_view_week_day.html";
65      private static final String TEMPLATE_VIEW_DAY_EVENT = "skin/plugins/calendar/calendar_view_week_event.html";
66  
67      /**
68       * Returns the HTML view of the Month corresponding to the given date and
69       * displaying
70       * events of a given agenda
71       * @return The view in HTML
72       * @param options The options storing the display settings
73       * @param strDate The date code
74       * @param agenda An agenda
75       * @param request HttpServletRequest
76       */
77      public String getCalendarView( String strDate, MultiAgenda agenda, CalendarUserOptions options,
78              HttpServletRequest request )
79      {
80          Locale locale = options.getLocale( );
81          Calendar calendar = Utils.getFirstDayOfWeek( strDate ); //Fetch the date of the first Monday
82          StringBuffer sbDays = new StringBuffer( );
83  
84          for ( int i = 0; i < 7; i++ )
85          {
86              sbDays.append( getDay( calendar, agenda, locale, request ) );
87              calendar.roll( Calendar.DAY_OF_WEEK, true );
88          }
89  
90          Map<String, Object> model = new HashMap<String, Object>( );
91          model.put( Constants.MARK_DAYS, sbDays.toString( ) );
92  
93          HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_VIEW_WEEK, options.getLocale( ), model );
94  
95          return template.getHtml( );
96      }
97  
98      /**
99       * Build the day view of a given
100      * @return The HTML of a day
101      * @param locale The locale
102      * @param calendar A calendar object positioned on a given day
103      * @param agenda The agenda
104      * @param request HttpServletRequest
105      */
106     private String getDay( Calendar calendar, MultiAgenda agenda, Locale locale, HttpServletRequest request )
107     {
108         StringBuffer sbEvents = new StringBuffer( );
109         String strDate = Utils.getDate( calendar );
110 
111         if ( agenda.hasEvents( strDate ) )
112         {
113             Date date = Utils.getDate( Utils.getDate( calendar ) );
114             Plugin plugin = PluginService.getPlugin( CalendarPlugin.PLUGIN_NAME );
115             List<Event> listIndexedEvents = CalendarSearchService.getInstance( ).getSearchResults(
116                     agenda.getAgendaIds( ), null, "", date, date, plugin );
117 
118             for ( Event event : listIndexedEvents )
119             {
120                 MultiAgendaEvent multiAgendaEvent = new MultiAgendaEvent( event,
121                         String.valueOf( event.getIdCalendar( ) ) );
122                 HashMap<String, Object> eventModel = new HashMap<String, Object>( );
123                 HtmlUtils.fillEventTemplate( eventModel, multiAgendaEvent, strDate );
124                 eventModel.put( Constants.MARK_JSP_URL,
125                         AppPropertiesService.getProperty( Constants.PROPERTY_RUNAPP_JSP_URL ) );
126 
127                 HtmlTemplate tEvent = AppTemplateService.getTemplate( TEMPLATE_VIEW_DAY_EVENT, locale, eventModel );
128                 sbEvents.append( tEvent.getHtml( ) );
129             }
130         }
131 
132         String strDayOfMonth = Utils.getWeekDayLabel( Utils.getDate( calendar ), locale );
133         String strDateLink = Utils.getDate( calendar );
134         HashMap<String, Object> dayModel = new HashMap<String, Object>( );
135         dayModel.put( Constants.MARK_DAY_CLASS, getDayClass( calendar ) );
136         dayModel.put( Constants.MARK_DAY, strDayOfMonth );
137         dayModel.put( Constants.MARK_DAY_LINK, strDateLink );
138         dayModel.put( Constants.MARK_EVENTS, sbEvents.toString( ) );
139         dayModel.put( Constants.MARK_JSP_URL, AppPropertiesService.getProperty( Constants.PROPERTY_RUNAPP_JSP_URL ) );
140 
141         HtmlTemplate tDay = AppTemplateService.getTemplate( TEMPLATE_VIEW_WEEK_DAY, locale, dayModel );
142 
143         return tDay.getHtml( );
144     }
145 
146     /**
147      * Calculate the style class to render the day
148      * @param calendar A calendar object positionned on the day to render
149      * @return A CSS style
150      */
151     private String getDayClass( Calendar calendar )
152     {
153         String strClass = Constants.STYLE_CLASS_VIEW_MONTH_DAY;
154         String strDate = Utils.getDate( calendar );
155         String strToday = Utils.getDateToday( );
156 
157         if ( Utils.isDayOff( calendar ) )
158         {
159             strClass += Constants.STYLE_CLASS_SUFFIX_OFF;
160         }
161         else if ( strDate.compareTo( strToday ) < 0 )
162         {
163             strClass += Constants.STYLE_CLASS_SUFFIX_OLD;
164         }
165         else if ( strDate.equals( strToday ) )
166         {
167             strClass += Constants.STYLE_CLASS_SUFFIX_TODAY;
168         }
169 
170         return strClass;
171     }
172 
173     /**
174      * Returns the next code date corresponding to the current view and the
175      * current date
176      * @param strDate The current date code
177      * @return The next code date
178      */
179     public String getNext( String strDate )
180     {
181         return Utils.getNextWeek( strDate );
182     }
183 
184     /**
185      * Returns the previous code date corresponding to the current view and the
186      * current date
187      * @param strDate The current date code
188      * @return The previous code date
189      */
190     public String getPrevious( String strDate )
191     {
192         return Utils.getPreviousWeek( strDate );
193     }
194 
195     /**
196      * Returns the view title
197      * @return The view title
198      * @param options The options storing the display settings
199      * @param strDate The current date code
200      */
201     public String getTitle( String strDate, CalendarUserOptions options )
202     {
203         String strTitle = Utils.getWeekLabel( strDate, Locale.getDefault( ) );
204 
205         return strTitle;
206     }
207 
208     /**
209      * Returns the view path
210      * @param options The options storing the display settings
211      * @param strDate The current date code
212      * @return The view path
213      */
214     public String getPath( String strDate, CalendarUserOptions options )
215     {
216         return getTitle( strDate, options );
217     }
218 
219     /**
220      * Returns the view type
221      * @return The view type
222      */
223     public int getType( )
224     {
225         return CalendarView.TYPE_WEEK;
226     }
227 }