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.util.html.HtmlTemplate;
46  
47  import java.util.Date;
48  import java.util.HashMap;
49  import java.util.List;
50  import java.util.Map;
51  
52  import javax.servlet.http.HttpServletRequest;
53  
54  
55  /**
56   * This class provides a calendar view by Day.
57   */
58  public class DayCalendarView implements CalendarView
59  {
60      private static final String TEMPLATE_VIEW_DAY = "skin/plugins/calendar/calendar_view_day.html";
61      private static final String TEMPLATE_VIEW_DAY_EVENT = "skin/plugins/calendar/calendar_view_day_event.html";
62  
63      /**
64       * Returns the HTML view of the Month corresponding to the given date and
65       * displaying
66       * events of a given agenda
67       * @return The view in HTML
68       * @param options The options
69       * @param strDate The date code
70       * @param agenda An agenda
71       * @param request the request
72       */
73      public String getCalendarView( String strDate, MultiAgenda agenda, CalendarUserOptions options,
74              HttpServletRequest request )
75      {
76          Map<String, Object> dayModel = new HashMap<String, Object>( );
77          StringBuffer sbEvents = new StringBuffer( );
78  
79          if ( agenda.hasEvents( strDate ) )
80          {
81              Date date = Utils.getDate( strDate );
82              Plugin plugin = PluginService.getPlugin( CalendarPlugin.PLUGIN_NAME );
83              List<Event> listIndexedEvents = CalendarSearchService.getInstance( ).getSearchResults(
84                      agenda.getAgendaIds( ), null, "", date, date, plugin );
85  
86              for ( Event event : listIndexedEvents )
87              {
88                  MultiAgendaEvent multiAgendaEvent = new MultiAgendaEvent( event,
89                          String.valueOf( event.getIdCalendar( ) ) );
90                  Map<String, Object> eventModel = new HashMap<String, Object>( );
91                  HtmlUtils.fillEventTemplate( eventModel, multiAgendaEvent, strDate );
92  
93                  HtmlTemplate tEvent = AppTemplateService.getTemplate( TEMPLATE_VIEW_DAY_EVENT, options.getLocale( ),
94                          eventModel );
95                  sbEvents.append( tEvent.getHtml( ) );
96              }
97          }
98  
99          dayModel.put( Constants.MARK_EVENTS, sbEvents.toString( ) );
100 
101         HtmlTemplate tDay = AppTemplateService.getTemplate( TEMPLATE_VIEW_DAY, options.getLocale( ), dayModel );
102 
103         return tDay.getHtml( );
104     }
105 
106     /**
107      * Returns the next code date corresponding to the current view and the
108      * current date
109      * @param strDate The current date code
110      * @return The next code date
111      */
112     public String getNext( String strDate )
113     {
114         return Utils.getNextDay( strDate );
115     }
116 
117     /**
118      * Returns the previous code date corresponding to the current view and the
119      * current date
120      * @param strDate The current date code
121      * @return The previous code date
122      */
123     public String getPrevious( String strDate )
124     {
125         return Utils.getPreviousDay( strDate );
126     }
127 
128     /**
129      * Returns the view title
130      * @return The view title
131      * @param options The options
132      * @param strDate The current date code
133      */
134     public String getTitle( String strDate, CalendarUserOptions options )
135     {
136         return Utils.getDayLabel( strDate, options.getLocale( ) );
137     }
138 
139     /**
140      * Returns the view path
141      * @return The view path
142      * @param options The options
143      * @param strDate The current date code
144      */
145     public String getPath( String strDate, CalendarUserOptions options )
146     {
147         return getTitle( strDate, options );
148     }
149 
150     /**
151      * Returns the view type
152      * @return The view type
153      */
154     public int getType( )
155     {
156         return CalendarView.TYPE_DAY;
157     }
158 }