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.Agenda;
37  import fr.paris.lutece.plugins.calendar.business.CalendarHome;
38  import fr.paris.lutece.plugins.calendar.business.Event;
39  import fr.paris.lutece.plugins.calendar.business.MultiAgenda;
40  import fr.paris.lutece.plugins.calendar.service.CalendarPlugin;
41  import fr.paris.lutece.plugins.calendar.service.Utils;
42  import fr.paris.lutece.plugins.calendar.service.search.CalendarSearchService;
43  import fr.paris.lutece.portal.service.plugin.Plugin;
44  import fr.paris.lutece.portal.service.plugin.PluginService;
45  import fr.paris.lutece.portal.service.template.AppTemplateService;
46  import fr.paris.lutece.portal.service.util.AppPathService;
47  import fr.paris.lutece.portal.service.util.AppPropertiesService;
48  import fr.paris.lutece.util.date.DateUtil;
49  import fr.paris.lutece.util.html.HtmlTemplate;
50  import fr.paris.lutece.util.url.UrlItem;
51  
52  import java.util.ArrayList;
53  import java.util.Calendar;
54  import java.util.Date;
55  import java.util.GregorianCalendar;
56  import java.util.HashMap;
57  import java.util.List;
58  import java.util.Map;
59  
60  import org.apache.commons.lang.StringUtils;
61  
62  
63  /**
64   * This class provides a Small Html Month calendar.
65   */
66  public final class SmallMonthCalendar
67  {
68      // Templates
69      private static final String TEMPLATE_VIEW_MONTH = "skin/plugins/calendar/small_month_calendar.html";
70      private static final String TEMPLATE_WEEK = "skin/plugins/calendar/small_month_calendar_week.html";
71      private static final String TEMPLATE_DAY = "skin/plugins/calendar/small_month_calendar_day.html";
72      private static final String TEMPLATE_EMPTY_DAY = "skin/plugins/calendar/small_month_calendar_empty_day.html";
73  
74      /**
75       * Default constructor
76       */
77      private SmallMonthCalendar( )
78      {
79      }
80  
81      /**
82       * Provides a small HTML month calendar displaying days with links
83       * @return The HTML code of the month.
84       * @param options The options which contains displaying settings
85       * @param strDate The code date defining the month to display
86       * @param agenda An agenda to hilight some days.
87       * @param bIsSelectedDay true if the date is the selected day, false
88       *            otherwise
89       */
90      public static String getSmallMonthCalendar( String strDate, Agenda agenda, CalendarUserOptions options,
91              boolean bIsSelectedDay )
92      {
93          Map<String, Object> model = new HashMap<String, Object>( );
94          Calendar calendar = new GregorianCalendar( );
95          calendar.set( Utils.getYear( strDate ), Utils.getMonth( strDate ), 1 );
96  
97          Calendar firstDayOfMonth = new GregorianCalendar( );
98          firstDayOfMonth.set( Utils.getYear( strDate ), Utils.getMonth( strDate ),
99                  calendar.getMinimum( Calendar.DAY_OF_MONTH ) );
100 
101         Date dFirstDayOfMonth = firstDayOfMonth.getTime( );
102 
103         Calendar lastDayOfMonth = new GregorianCalendar( );
104         lastDayOfMonth.set( Utils.getYear( strDate ), Utils.getMonth( strDate ),
105                 calendar.getMaximum( Calendar.DAY_OF_MONTH ) );
106 
107         Date dLastDayOfMonth = lastDayOfMonth.getTime( );
108 
109         int nDayOfWeek = calendar.get( Calendar.DAY_OF_WEEK );
110 
111         if ( nDayOfWeek == 1 )
112         {
113             nDayOfWeek = 8;
114         }
115 
116         StringBuffer sbWeeks = new StringBuffer( );
117 
118         boolean bDone = false;
119         boolean bStarted = false;
120 
121         while ( !bDone )
122         {
123             Map<String, Object> weekModel = new HashMap<String, Object>( );
124 
125             //HtmlTemplate tWeek = new HtmlTemplate( templateWeek );
126             StringBuffer sbDays = new StringBuffer( );
127 
128             for ( int i = 0; i < 7; i++ )
129             {
130                 if ( ( ( ( i + 2 ) != nDayOfWeek ) && !bStarted ) || bDone )
131                 {
132                     sbDays.append( AppTemplateService.getTemplate( TEMPLATE_EMPTY_DAY ).getHtml( ) );
133 
134                     continue;
135                 }
136                 bStarted = true;
137 
138                 if ( strDate.equals( Utils.getDate( calendar ) ) && bIsSelectedDay )
139                 {
140                     sbDays.append( getDay( calendar, agenda, options, true ) );
141                 }
142                 else
143                 {
144                     sbDays.append( getDay( calendar, agenda, options, false ) );
145                 }
146 
147                 int nDay = calendar.get( Calendar.DAY_OF_MONTH );
148                 calendar.roll( Calendar.DAY_OF_MONTH, true );
149 
150                 int nNewDay = calendar.get( Calendar.DAY_OF_MONTH );
151 
152                 if ( nNewDay < nDay )
153                 {
154                     bDone = true;
155                 }
156             }
157 
158             weekModel.put( Constants.MARK_DAYS, sbDays.toString( ) );
159             sbWeeks.append( AppTemplateService.getTemplate( TEMPLATE_WEEK, options.getLocale( ), weekModel ).getHtml( ) );
160         }
161 
162         model.put( Constants.MARK_MONTH_LABEL, Utils.getMonthLabel( strDate, options.getLocale( ) ) );
163         model.put( Constants.MARK_PREVIOUS, Utils.getPreviousMonth( strDate ) );
164         model.put( Constants.MARK_DATE, strDate );
165         model.put( Constants.MARK_NEXT, Utils.getNextMonth( strDate ) );
166         model.put( Constants.MARK_DATE_START, dFirstDayOfMonth );
167         model.put( Constants.MARK_DATE_END, dLastDayOfMonth );
168 
169         model.put( Constants.MARK_WEEKS, sbWeeks.toString( ) );
170         model.put( Constants.MARK_JSP_URL, AppPropertiesService.getProperty( Constants.PROPERTY_RUNAPP_JSP_URL ) );
171 
172         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_VIEW_MONTH, options.getLocale( ), model );
173 
174         return template.getHtml( );
175     }
176 
177     /**
178      * Build the day view of a given
179      * @return The HTML of a day
180      * @param options The options which stores the display settings
181      * @param calendar A calendar object positioned on a given day
182      * @param agenda The agenda
183      * @param bIsSelectedDay true if the date is the selected day, false
184      *            otherwise
185      */
186     private static String getDay( Calendar calendar, Agenda agenda, CalendarUserOptions options, boolean bIsSelectedDay )
187     {
188         HashMap<String, Object> model = new HashMap<String, Object>( );
189         String strDate = Utils.getDate( calendar );
190         Date date = Utils.getDate( strDate );
191         String strLinkClass = AppPropertiesService.getProperty( Constants.PROPERTY_SMALLCALENDAR_LINKCLASS_NO_EVENT );
192         Plugin plugin = PluginService.getPlugin( CalendarPlugin.PLUGIN_NAME );
193         String[] strAgendaIds = null;
194         if ( agenda instanceof fr.paris.lutece.plugins.calendar.business.MultiAgenda )
195         {
196             MultiAgenda multiAgenda = (MultiAgenda) agenda;
197             if ( multiAgenda.getAgendaIds( ) != null && multiAgenda.getAgendaIds( ).length > 0 )
198             {
199                 strAgendaIds = multiAgenda.getAgendaIds( );
200             }
201         }
202         List<Event> listEvent;
203 
204         if ( agenda.hasEvents( strDate ) )
205         {
206             listEvent = CalendarSearchService.getInstance( ).getSearchResults( strAgendaIds, null, StringUtils.EMPTY,
207                     date, date, plugin );
208         }
209         else
210         {
211             listEvent = new ArrayList<Event>( );
212         }
213 
214         if ( listEvent.size( ) != 0 )
215         {
216             strLinkClass = AppPropertiesService.getProperty( Constants.PROPERTY_SMALLCALENDAR_LINKCLASS_HAS_EVENTS );
217         }
218 
219         model.put( Constants.MARK_LINK_CLASS, strLinkClass );
220         model.put( Constants.MARK_DATE, strDate );
221         model.put( Constants.MARK_DAY, calendar.get( Calendar.DAY_OF_MONTH ) );
222         model.put( Constants.MARK_DAY_CLASS, getDayClass( calendar, bIsSelectedDay ) );
223 
224         //model.put( Constants.MARK_JSP_URL, AppPropertiesService.getProperty( Constants.PROPERTY_RUNAPP_JSP_URL ) );
225 
226         //we only show link days with events on calendar
227         if ( listEvent.size( ) != 0 && !options.isShowSearchEngine( ) )
228         {
229             UrlItem urlDay = new UrlItem( AppPathService.getPortalUrl( ) );
230             urlDay.addParameter( Constants.PARAMETER_PAGE, Constants.PLUGIN_NAME );
231             urlDay.addParameter( Constants.PARAMETER_DATE, strDate );
232             model.put( Constants.MARK_JSP_URL, urlDay.getUrl( ) );
233         }
234         else if ( listEvent.size( ) != 0 && options.isShowSearchEngine( ) )
235         {
236             UrlItem urlDay = new UrlItem( AppPathService.getPortalUrl( ) );
237             urlDay.addParameter( Constants.PARAMETER_PAGE, Constants.PLUGIN_NAME );
238             urlDay.addParameter( Constants.PARAMETER_ACTION, Constants.ACTION_DO_SEARCH );
239             urlDay.addParameter( Constants.PARAMETER_DATE_START,
240                     DateUtil.getDateString( Utils.getDate( strDate ), options.getLocale( ) ) );
241             urlDay.addParameter( Constants.PARAMETER_DATE_END,
242                     DateUtil.getDateString( Utils.getDate( strDate ), options.getLocale( ) ) );
243             urlDay.addParameter( Constants.PARAMETER_PERIOD, Constants.PROPERTY_PERIOD_RANGE );
244             model.put( Constants.MARK_JSP_URL, urlDay.getUrl( ) );
245         }
246         else
247         {
248             model.put( Constants.MARK_JSP_URL, "" );
249         }
250 
251         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_DAY, options.getLocale( ), model );
252 
253         return template.getHtml( );
254     }
255 
256     /**
257      * Calculate the style class to render the day
258      * @param calendar A calendar object positionned on the day to render
259      * @param bIsSelectedDay true if the date is the selected day, false
260      *            otherwise
261      * @return A CSS style
262      */
263     public static String getDayClass( Calendar calendar, boolean bIsSelectedDay )
264     {
265         String strClass = Constants.STYLE_CLASS_SMALLMONTH_DAY;
266         String strDate = Utils.getDate( calendar );
267         String strToday = Utils.getDateToday( );
268 
269         if ( CalendarHome.hasOccurrenceEvent( calendar, PluginService.getPlugin( Constants.PLUGIN_NAME ) ) )
270         {
271             strClass += Constants.STYLE_CLASS_SUFFIX_EVENT;
272         }
273         else if ( Utils.isDayOff( calendar ) )
274         {
275             strClass += Constants.STYLE_CLASS_SUFFIX_OFF;
276         }
277         else if ( strDate.compareTo( strToday ) < 0 )
278         {
279             strClass += Constants.STYLE_CLASS_SUFFIX_OLD;
280         }
281         else if ( strDate.equals( strToday ) )
282         {
283             strClass += Constants.STYLE_CLASS_SUFFIX_TODAY;
284         }
285 
286         if ( bIsSelectedDay )
287         {
288             strClass += Constants.SPACE + Constants.STYLE_CLASS_SELECTED_DAY;
289         }
290 
291         return strClass;
292     }
293 }