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.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
65
66 public final class SmallMonthCalendar
67 {
68
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
76
77 private SmallMonthCalendar( )
78 {
79 }
80
81
82
83
84
85
86
87
88
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
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
179
180
181
182
183
184
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
225
226
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
258
259
260
261
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 }