View Javadoc
1   /*
2    * Copyright (c) 2002-2018, 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.appointment.web;
35  
36  import java.io.File;
37  import java.io.IOException;
38  import java.util.Map;
39  import java.util.Set;
40  
41  import javax.servlet.http.HttpServletRequest;
42  import javax.validation.ConstraintViolation;
43  
44  import org.apache.commons.collections.CollectionUtils;
45  import org.apache.commons.io.FileUtils;
46  import org.apache.commons.lang.StringUtils;
47  
48  import fr.paris.lutece.plugins.appointment.business.calendar.CalendarTemplate;
49  import fr.paris.lutece.plugins.appointment.business.calendar.CalendarTemplateHome;
50  import fr.paris.lutece.portal.service.message.AdminMessage;
51  import fr.paris.lutece.portal.service.message.AdminMessageService;
52  import fr.paris.lutece.portal.service.util.AppLogService;
53  import fr.paris.lutece.portal.service.util.AppPathService;
54  import fr.paris.lutece.portal.service.util.AppPropertiesService;
55  import fr.paris.lutece.portal.util.mvc.admin.MVCAdminJspBean;
56  import fr.paris.lutece.portal.util.mvc.admin.annotations.Controller;
57  import fr.paris.lutece.portal.util.mvc.commons.annotations.Action;
58  import fr.paris.lutece.portal.util.mvc.commons.annotations.View;
59  import fr.paris.lutece.util.ReferenceList;
60  import fr.paris.lutece.util.datatable.DataTableManager;
61  import fr.paris.lutece.util.url.UrlItem;
62  
63  /**
64   * This class provides the user interface to manage calendar templates
65   * 
66   * @author Laurent Payen
67   *
68   */
69  @Controller( controllerJsp = CalendarTemplateJspBean.CONTROLLER_JSP, controllerPath = CalendarTemplateJspBean.CONTROLLER_PATH, right = CalendarTemplateJspBean.RIGHT_MANAGE_CALENDAR_TEMPLATES )
70  public class CalendarTemplateJspBean extends MVCAdminJspBean
71  {
72      /**
73       * Right to manage appointment calendar templates
74       */
75      public static final String RIGHT_MANAGE_CALENDAR_TEMPLATES = "APPOINTMENT_CALENDAR_TEMPLATE";
76  
77      /**
78       * Folder of the JSP of this controller
79       */
80      public static final String CONTROLLER_PATH = "jsp/admin/plugins/appointment/";
81  
82      /**
83       * Name of the JSP of this controller
84       */
85      public static final String CONTROLLER_JSP = "ManageCalendarTemplates.jsp";
86  
87      /**
88       * URL of the JSP of this controller
89       */
90      public static final String JSP_URL_MANAGE_CALENDAR_TEMPLATE = CONTROLLER_PATH + CONTROLLER_JSP;
91  
92      /**
93       * Serial version UID
94       */
95      private static final long serialVersionUID = 3406345413046194795L;
96  
97      // templates
98      private static final String TEMPLATE_MANAGE_CALENDAR_TEMPLATES = "/admin/plugins/appointment/templates/manage_calendar_templates.html";
99      private static final String TEMPLATE_CREATE_MODIFY_CALENDAR_TEMPLATE = "/admin/plugins/appointment/templates/create_modify_calendar_template.html";
100 
101     // Marks
102     private static final String MARK_DATA_TABLE_MANAGER = "dataTableManager";
103     private static final String MARK_TEMPLATE = "template";
104     private static final String MARK_REF_LIST_TEMPLATES = "refListTemplates";
105 
106     // Parameters
107     private static final String PARAMETER_ID_TEMPLATE = "idTemplate";
108     private static final String PARAMETER_TEMPLACE_PATH = "templatePath";
109 
110     // Messages
111     private static final String MESSAGE_COLUMN_TITLE_TITLE = "appointment.labelTitle";
112     private static final String MESSAGE_COLUMN_TITLE_DESCRIPTION = "appointment.labelDescription";
113     private static final String MESSAGE_COLUMN_TITLE_TEMPLATE_PATH = "appointment.manageCalendarTemplates.labelTemplatePath";
114     private static final String MESSAGE_COLUMN_TITLE_ACTIONS = "portal.util.labelActions";
115     private static final String MESSAGE_DEFAULT_PAGE_TITLE = "appointment.adminFeature.manageCalendarTemplates.name";
116     private static final String MESSAGE_CREATE_TEMPLATE_PAGE_TITLE = "appointment.labelAddTemplate";
117     private static final String MESSAGE_MODIFY_TEMPLATE_PAGE_TITLE = "appointment.createModifyCalendarTemplate.pageTitleModify";
118     private static final String MESSAGE_INFO_TEMPLATE_CREATED = "appointment.createModifyCalendarTemplate.infoTemplateCreated";
119     private static final String MESSAGE_INFO_TEMPLATE_UPDATED = "appointment.createModifyCalendarTemplate.infoTemplateUpdated";
120     private static final String MESSAGE_INFO_TEMPLATE_REMOVED = "appointment.removeCalendarTemplate.infoTemplateRemoved";
121     private static final String MESSAGE_CONFIRM_REMOVE_TEMPLATE = "appointment.removeCalendarTemplate.confirmRemoveTemplate";
122 
123     // Properties
124     private static final String PROPERTY_DEFAULT_LIST_APPOINTMENT_PER_PAGE = "appointment.listAppointments.itemsPerPage";
125     private static final String PROPERTY_FOLDER_CALENDAR_TEMPLATES = "appointment.calendarTemplates.calendarTemplatesFolder";
126     private static final String PROPERTY_TITLE = "title";
127     private static final String PROPERTY_DESCRIPTION = "description";
128     private static final String PROPERTY_TEMPLATE_PATH = "templatePath";
129 
130     // Views
131     private static final String VIEW_MANAGE_CALENDAR_TEMPLATES = "viewManageCalendarTemplates";
132     private static final String VIEW_CREATE_MODIFY_TEMPLATE = "getCreateModifyTemplate";
133     private static final String VIEW_CONFIRM_REMOVE_TEMPLATE = "getConfirmRemoveTemplate";
134 
135     // Actions
136     private static final String ACTION_CREATE_MODIFY_TEMPLATE = "doCreateModifyTemplate";
137     private static final String ACTION_REMOVE_TEMPLATE = "doRemoveTemplate";
138     private static final String ACTION_DOWNLOAD_TEMPLATE = "doDownloadTemplate";
139 
140     // Constants
141     private static final String CONSTANT_TEMPLATE_FOLDER = "/WEB-INF/templates/";
142     private static final String CONSTANT_FOLDER_UP = "..";
143 
144     // Session variables
145     private DataTableManager<CalendarTemplate> _dataTableManager;
146     private CalendarTemplate _template;
147 
148     /**
149      * Get the page to manage calendar templates.
150      * 
151      * @param request
152      *            The request
153      * @return The HTML code to display
154      */
155     @View( value = VIEW_MANAGE_CALENDAR_TEMPLATES, defaultView = true )
156     public String getManageAppointmentCalendarTemplates( HttpServletRequest request )
157     {
158         _template = null;
159 
160         if ( _dataTableManager == null )
161         {
162             _dataTableManager = new DataTableManager<CalendarTemplate>( getViewFullUrl( VIEW_MANAGE_CALENDAR_TEMPLATES ), null,
163                     AppPropertiesService.getPropertyInt( PROPERTY_DEFAULT_LIST_APPOINTMENT_PER_PAGE, 50 ), true );
164             _dataTableManager.addColumn( MESSAGE_COLUMN_TITLE_TITLE, PROPERTY_TITLE, true );
165             _dataTableManager.addColumn( MESSAGE_COLUMN_TITLE_DESCRIPTION, PROPERTY_DESCRIPTION, true );
166             _dataTableManager.addColumn( MESSAGE_COLUMN_TITLE_TEMPLATE_PATH, PROPERTY_TEMPLATE_PATH, true );
167             _dataTableManager.addActionColumn( MESSAGE_COLUMN_TITLE_ACTIONS );
168         }
169 
170         _dataTableManager.filterSortAndPaginate( request, CalendarTemplateHome.findAll( ) );
171 
172         Map<String, Object> model = getModel( );
173         model.put( MARK_DATA_TABLE_MANAGER, _dataTableManager );
174 
175         String strContent = getPage( MESSAGE_DEFAULT_PAGE_TITLE, TEMPLATE_MANAGE_CALENDAR_TEMPLATES, model );
176 
177         _dataTableManager.clearItems( );
178 
179         return strContent;
180     }
181 
182     /**
183      * Get the page to create or modify an existing template
184      * 
185      * @param request
186      *            The request
187      * @return The page to create or modify an existing template
188      */
189     @View( VIEW_CREATE_MODIFY_TEMPLATE )
190     public String getCreateModifyTemplate( HttpServletRequest request )
191     {
192         String strIdTemplate = request.getParameter( PARAMETER_ID_TEMPLATE );
193 
194         if ( StringUtils.isNotEmpty( strIdTemplate ) && StringUtils.isNumeric( strIdTemplate ) )
195         {
196             // If the id template is valid, then we load the template from the
197             // database
198             int nIdTemplate = Integer.parseInt( strIdTemplate );
199             _template = CalendarTemplateHome.findByPrimaryKey( nIdTemplate );
200         }
201         else
202         {
203             if ( _template == null )
204             {
205                 // Otherwise, we create a new template
206                 _template = new CalendarTemplate( );
207             }
208         }
209 
210         Map<String, Object> model = getModel( );
211         model.put( MARK_TEMPLATE, _template );
212 
213         String strCalendarTemplatesFolder = AppPropertiesService.getProperty( PROPERTY_FOLDER_CALENDAR_TEMPLATES, StringUtils.EMPTY );
214 
215         File calendarTemplatesFolder = new File( AppPathService.getWebAppPath( ) + CONSTANT_TEMPLATE_FOLDER + strCalendarTemplatesFolder );
216 
217         ReferenceList refListTemplates = new ReferenceList( );
218         refListTemplates.addItem( StringUtils.EMPTY, StringUtils.EMPTY );
219 
220         if ( calendarTemplatesFolder.exists( ) )
221         {
222             if ( calendarTemplatesFolder.isDirectory( ) )
223             {
224                 if ( calendarTemplatesFolder != null )
225                 {
226                     File [ ] listFiles = calendarTemplatesFolder.listFiles( );
227                     if ( listFiles != null && listFiles.length != 0 )
228                     {
229                         for ( File file : listFiles )
230                         {
231                             if ( file != null )
232                             {
233                                 refListTemplates.addItem( strCalendarTemplatesFolder + file.getName( ), file.getName( ) );
234                             }
235                         }
236                     }
237                 }
238             }
239             else
240             {
241                 // If the specified folder is a file, we add that file to the
242                 // reference list
243                 refListTemplates.addItem( strCalendarTemplatesFolder + calendarTemplatesFolder.getName( ), calendarTemplatesFolder.getName( ) );
244             }
245         }
246 
247         model.put( MARK_REF_LIST_TEMPLATES, refListTemplates );
248 
249         return getPage( ( _template.getIdCalendarTemplate( ) > 0 ) ? MESSAGE_MODIFY_TEMPLATE_PAGE_TITLE : MESSAGE_CREATE_TEMPLATE_PAGE_TITLE,
250                 TEMPLATE_CREATE_MODIFY_CALENDAR_TEMPLATE, model );
251     }
252 
253     /**
254      * Do the creation or the modification of a calendar template
255      * 
256      * @param request
257      *            The request
258      * @return The next URL to redirect to
259      */
260     @Action( ACTION_CREATE_MODIFY_TEMPLATE )
261     public String doCreateModifyTemplate( HttpServletRequest request )
262     {
263         // We reset the session template to prevent collisions with any other
264         // tab or page
265         _template = new CalendarTemplate( );
266         populate( _template, request );
267 
268         Set<ConstraintViolation<CalendarTemplate>> listErrors = validate( _template );
269 
270         if ( CollectionUtils.isNotEmpty( listErrors ) )
271         {
272             for ( ConstraintViolation<CalendarTemplate> error : listErrors )
273             {
274                 addError( error.getMessage( ) );
275             }
276 
277             return redirectView( request, VIEW_CREATE_MODIFY_TEMPLATE );
278         }
279 
280         if ( _template.getIdCalendarTemplate( ) > 0 )
281         {
282             CalendarTemplateHome.update( _template );
283             addInfo( MESSAGE_INFO_TEMPLATE_UPDATED, getLocale( ) );
284         }
285         else
286         {
287             CalendarTemplateHome.create( _template );
288             addInfo( MESSAGE_INFO_TEMPLATE_CREATED, getLocale( ) );
289         }
290 
291         return redirectView( request, VIEW_MANAGE_CALENDAR_TEMPLATES );
292     }
293 
294     /**
295      * Get the confirmation page before removing a template
296      * 
297      * @param request
298      *            The request
299      * @return The next URL to redirect to
300      */
301     @View( VIEW_CONFIRM_REMOVE_TEMPLATE )
302     public String getConfirmRemoveTemplate( HttpServletRequest request )
303     {
304         String strIdTemplate = request.getParameter( PARAMETER_ID_TEMPLATE );
305 
306         if ( StringUtils.isEmpty( strIdTemplate ) || !StringUtils.isNumeric( strIdTemplate ) )
307         {
308             return redirectView( request, VIEW_MANAGE_CALENDAR_TEMPLATES );
309         }
310 
311         UrlItem urlItem = new UrlItem( AppPathService.getBaseUrl( request ) + getActionUrl( ACTION_REMOVE_TEMPLATE ) );
312         urlItem.addParameter( PARAMETER_ID_TEMPLATE, strIdTemplate );
313 
314         return redirect( request,
315                 AdminMessageService.getMessageUrl( request, MESSAGE_CONFIRM_REMOVE_TEMPLATE, urlItem.getUrl( ), AdminMessage.TYPE_CONFIRMATION ) );
316     }
317 
318     /**
319      * Do remove a template
320      * 
321      * @param request
322      *            The request
323      * @return The next URL to redirect to
324      */
325     @Action( ACTION_REMOVE_TEMPLATE )
326     public String doRemoveTemplate( HttpServletRequest request )
327     {
328         String strIdTemplate = request.getParameter( PARAMETER_ID_TEMPLATE );
329 
330         if ( StringUtils.isEmpty( strIdTemplate ) || !StringUtils.isNumeric( strIdTemplate ) )
331         {
332             return redirectView( request, VIEW_MANAGE_CALENDAR_TEMPLATES );
333         }
334 
335         CalendarTemplateHome.delete( Integer.parseInt( strIdTemplate ) );
336 
337         addInfo( MESSAGE_INFO_TEMPLATE_REMOVED, getLocale( ) );
338 
339         return redirectView( request, VIEW_MANAGE_CALENDAR_TEMPLATES );
340     }
341 
342     /**
343      * Do download a template
344      * 
345      * @param request
346      *            The request
347      * @return The next URL to redirect to
348      */
349     @Action( ACTION_DOWNLOAD_TEMPLATE )
350     public String doDownloadTemplate( HttpServletRequest request )
351     {
352         String strTemplate = request.getParameter( PARAMETER_TEMPLACE_PATH );
353 
354         String strCalendarTemplatesFolder = AppPropertiesService.getProperty( PROPERTY_FOLDER_CALENDAR_TEMPLATES, StringUtils.EMPTY );
355 
356         if ( StringUtils.isNotEmpty( strTemplate ) && strTemplate.startsWith( strCalendarTemplatesFolder ) && !strTemplate.contains( CONSTANT_FOLDER_UP ) )
357         {
358             File file = new File( AppPathService.getWebAppPath( ) + CONSTANT_TEMPLATE_FOLDER + strTemplate );
359             byte [ ] fileContent;
360 
361             try
362             {
363                 fileContent = FileUtils.readFileToByteArray( file );
364             }
365             catch( IOException e )
366             {
367                 AppLogService.error( e.getMessage( ), e );
368 
369                 return redirectView( request, VIEW_MANAGE_CALENDAR_TEMPLATES );
370             }
371 
372             download( fileContent, file.getName( ), "text/plain" );
373 
374             return null;
375         }
376 
377         return redirectView( request, VIEW_MANAGE_CALENDAR_TEMPLATES );
378     }
379 }