1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
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
71
72
73
74
75
76
77
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
144
145
146
147
148
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
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
203
204
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
230
231
232
233
234 public String getNext( String strDate )
235 {
236 return Utils.getNextMonth( strDate );
237 }
238
239
240
241
242
243
244
245 public String getPrevious( String strDate )
246 {
247 return Utils.getPreviousMonth( strDate );
248 }
249
250
251
252
253
254
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
265
266
267
268
269 public String getPath( String strDate, CalendarUserOptions options )
270 {
271 return getTitle( strDate, options );
272 }
273
274
275
276
277
278 public int getType( )
279 {
280 return CalendarView.TYPE_MONTH;
281 }
282 }