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 java.util.Collection;
37 import java.util.HashMap;
38 import java.util.Map;
39
40 import javax.servlet.http.HttpServletRequest;
41
42 import org.apache.commons.lang.StringEscapeUtils;
43
44 import fr.paris.lutece.plugins.calendar.business.Agenda;
45 import fr.paris.lutece.plugins.calendar.business.CalendarHome;
46 import fr.paris.lutece.plugins.calendar.business.SimpleEvent;
47 import fr.paris.lutece.plugins.calendar.service.AgendaResource;
48 import fr.paris.lutece.plugins.calendar.service.CalendarPlugin;
49 import fr.paris.lutece.plugins.calendar.service.Utils;
50 import fr.paris.lutece.portal.business.user.AdminUser;
51 import fr.paris.lutece.portal.service.admin.AdminUserService;
52 import fr.paris.lutece.portal.service.message.AdminMessage;
53 import fr.paris.lutece.portal.service.message.AdminMessageService;
54 import fr.paris.lutece.portal.service.plugin.Plugin;
55 import fr.paris.lutece.portal.service.plugin.PluginService;
56 import fr.paris.lutece.portal.service.template.AppTemplateService;
57 import fr.paris.lutece.portal.service.util.AppPathService;
58 import fr.paris.lutece.portal.service.workgroup.AdminWorkgroupService;
59 import fr.paris.lutece.portal.web.constants.Messages;
60 import fr.paris.lutece.portal.web.insert.InsertServiceJspBean;
61 import fr.paris.lutece.portal.web.insert.InsertServiceSelectionBean;
62 import fr.paris.lutece.util.html.HtmlTemplate;
63 import fr.paris.lutece.util.url.UrlItem;
64
65
66
67
68
69
70 public class CalendarServiceJspBean extends InsertServiceJspBean implements InsertServiceSelectionBean
71 {
72 private static final long serialVersionUID = 2694692453596836769L;
73
74
75
76 private static final String REGEX_ID = "^[\\d]+$";
77
78
79 private static final String TEMPLATE_SELECTOR_CALENDAR = "admin/plugins/calendar/calendar_selector.html";
80 private static final String TEMPLATE_SELECTOR_EVENT = "admin/plugins/calendar/event_selector.html";
81 private static final String TEMPLATE_LINK = "admin/plugins/calendar/calendar_link.html";
82
83
84 private static final String JSP_SELECT_EVENT = "SelectEvent.jsp";
85
86
87 private static final String PARAMETER_ALT = "alt";
88 private static final String PARAMETER_TARGET = "target";
89 private static final String PARAMETER_NAME = "name";
90 private static final String PARAMETER_INPUT = "input";
91
92
93 private static final String MARK_ALT = "alt";
94 private static final String MARK_NAME = "name";
95 private static final String MARK_URL = "url";
96 private static final String MARK_TARGET = "target";
97 private static final String MARK_INPUT = "input";
98
99
100 private AdminUser _user;
101 private String _input;
102 private Plugin _plugin = PluginService.getPlugin( CalendarPlugin.PLUGIN_NAME );
103
104
105
106
107
108
109 public void init( HttpServletRequest request )
110 {
111 _user = AdminUserService.getAdminUser( request );
112 _input = request.getParameter( PARAMETER_INPUT );
113 }
114
115
116
117
118
119
120
121 public String getInsertServiceSelectorUI( HttpServletRequest request )
122 {
123 return getSelectCalendar( request );
124 }
125
126
127
128
129
130
131
132 public String getSelectCalendar( HttpServletRequest request )
133 {
134 init( request );
135
136 AdminUser user = AdminUserService.getAdminUser( request );
137
138 Collection<AgendaResource> aResource = CalendarHome.findAgendaResourcesList( _plugin );
139
140 aResource = AdminWorkgroupService.getAuthorizedCollection( aResource, user );
141
142 Map<String, Object> model = getDefaultModel( );
143
144 model.put( Constants.MARK_CALENDARS_LIST, aResource );
145
146 HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_SELECTOR_CALENDAR, _user.getLocale( ), model );
147
148 return template.getHtml( );
149 }
150
151
152
153
154
155
156
157 public String doSelectCalendar( HttpServletRequest request )
158 {
159 init( request );
160
161 String strCalendarId = request.getParameter( Constants.PARAMETER_CALENDAR_ID );
162
163 if ( ( strCalendarId == null ) || !strCalendarId.matches( REGEX_ID ) )
164 {
165 return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
166 }
167
168 Agenda agenda = Utils.getAgendaWithOccurrences( strCalendarId, request );
169
170 if ( agenda == null )
171 {
172 return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
173 }
174
175 return getSelectCalendarUrl( strCalendarId );
176 }
177
178
179
180
181
182
183
184 private String getSelectCalendarUrl( String strCalendarId )
185 {
186 UrlItem url = new UrlItem( JSP_SELECT_EVENT );
187 url.addParameter( Constants.PARAMETER_CALENDAR_ID, strCalendarId );
188 url.addParameter( PARAMETER_INPUT, _input );
189
190 return url.getUrl( );
191 }
192
193
194
195
196
197
198
199 public String getSelectEvents( HttpServletRequest request )
200 {
201 init( request );
202
203 String strCalendarId = request.getParameter( Constants.PARAMETER_CALENDAR_ID );
204
205 if ( ( strCalendarId == null ) || !strCalendarId.matches( REGEX_ID ) )
206 {
207 return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
208 }
209
210 Agenda agenda = Utils.getAgendaWithOccurrences( strCalendarId, request );
211
212 Map<String, Object> model = getDefaultModel( );
213
214 model.put( Constants.MARK_OCCURRENCES_LIST, agenda.getEvents( ) );
215 model.put( Constants.MARK_CALENDAR_ID, strCalendarId );
216
217 HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_SELECTOR_EVENT, _user.getLocale( ), model );
218
219 return template.getHtml( );
220 }
221
222
223
224
225
226
227
228 public String doInsertUrl( HttpServletRequest request )
229 {
230 init( request );
231
232 String strEventId = request.getParameter( Constants.PARAMETER_EVENT_ID );
233 String strTarget = request.getParameter( PARAMETER_TARGET );
234 String strCalendarId = request.getParameter( Constants.PARAMETER_CALENDAR_ID );
235 String strAlt = request.getParameter( PARAMETER_ALT );
236 String strName = request.getParameter( PARAMETER_NAME );
237 HashMap<String, Object> model = new HashMap<String, Object>( );
238
239 if ( ( strEventId == null ) || !strEventId.matches( REGEX_ID ) )
240 {
241 return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
242 }
243
244 SimpleEvent event = CalendarHome.findEvent( Integer.parseInt( strEventId ), _plugin );
245
246 UrlItem url = new UrlItem( AppPathService.getPortalUrl( ) );
247 url.addParameter( Constants.PARAMETER_PAGE, Constants.PLUGIN_NAME );
248 url.addParameter( Constants.PARAMETER_ACTION, Constants.ACTION_SHOW_RESULT );
249 url.addParameter( Constants.PARAMETER_EVENT_ID, event.getId( ) );
250 url.addParameter( Constants.PARAMETER_DOCUMENT_ID, event.getDocumentId( ) );
251 url.addParameter( Constants.PARAM_AGENDA, strCalendarId );
252
253 model.put( MARK_URL, url.getUrl( ) );
254 model.put( MARK_TARGET, strTarget );
255 model.put( MARK_ALT, strAlt );
256 model.put( MARK_NAME, ( strName.length( ) == 0 ) ? event.getTitle( ) : strName );
257
258 HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_LINK, null, model );
259
260 return insertUrl( request, _input, StringEscapeUtils.escapeJavaScript( template.getHtml( ) ) );
261 }
262
263
264
265
266
267
268 private Map<String, Object> getDefaultModel( )
269 {
270 HashMap<String, Object> model = new HashMap<String, Object>( );
271 model.put( MARK_INPUT, _input );
272
273 return model;
274 }
275 }