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 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   * This class provides the user interface to insert a link to a document
68   *
69   */
70  public class CalendarServiceJspBean extends InsertServiceJspBean implements InsertServiceSelectionBean
71  {
72      private static final long serialVersionUID = 2694692453596836769L;
73  
74      ////////////////////////////////////////////////////////////////////////////
75      // Constants
76      private static final String REGEX_ID = "^[\\d]+$";
77  
78      // Templates
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      // JSP
84      private static final String JSP_SELECT_EVENT = "SelectEvent.jsp";
85  
86      // Parameters
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      //markers
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      // private
100     private AdminUser _user;
101     private String _input;
102     private Plugin _plugin = PluginService.getPlugin( CalendarPlugin.PLUGIN_NAME );
103 
104     /**
105      * Initialize data
106      *
107      * @param request The HTTP request
108      */
109     public void init( HttpServletRequest request )
110     {
111         _user = AdminUserService.getAdminUser( request );
112         _input = request.getParameter( PARAMETER_INPUT );
113     }
114 
115     /**
116     * Entry point of the insert service
117     *
118     * @param request The Http Request
119     * @return The html form.
120      */
121     public String getInsertServiceSelectorUI( HttpServletRequest request )
122     {
123         return getSelectCalendar( request );
124     }
125 
126     /**
127      * Return the html form for calendar selection.
128      *
129      * @param request The HTTP request
130      * @return The html form of the page selection page
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      * Select and validate the specified calendar
153      *
154      * @param request The http request
155      * @return The url of the portlet selection page
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      * Get the url of the event selection page with the specified calendar id
180      *
181      * @param strCalendarId Id of the calendar
182      * @return The url of the event selection page
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      * Return the html form for event selection.
195      *
196      * @param request The HTTP request
197      * @return The html form of the event selection page
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     * Insert the specified url into HTML content
224     *
225     * @param request The HTTP request
226     * @return The url
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      * Get the default model for selection templates
265      *
266      * @return The default model
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 }