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.util.Map;
37  
38  import javax.servlet.http.HttpServletRequest;
39  
40  import org.apache.commons.lang.StringUtils;
41  
42  import fr.paris.lutece.plugins.appointment.business.category.Category;
43  import fr.paris.lutece.plugins.appointment.service.CategoryService;
44  import fr.paris.lutece.portal.service.admin.AccessDeniedException;
45  import fr.paris.lutece.portal.service.message.AdminMessage;
46  import fr.paris.lutece.portal.service.message.AdminMessageService;
47  import fr.paris.lutece.portal.service.util.AppPropertiesService;
48  import fr.paris.lutece.portal.util.mvc.admin.MVCAdminJspBean;
49  import fr.paris.lutece.portal.util.mvc.admin.annotations.Controller;
50  import fr.paris.lutece.portal.util.mvc.commons.annotations.Action;
51  import fr.paris.lutece.portal.util.mvc.commons.annotations.View;
52  import fr.paris.lutece.util.datatable.DataTableManager;
53  import fr.paris.lutece.util.url.UrlItem;
54  
55  /**
56   * This class provides the user interface to manage AppointmentForm features ( manage, create, modify, copy, remove )
57   * 
58   * @author L.Payen
59   * 
60   */
61  @Controller( controllerJsp = "ManageAppointmentCategory.jsp", controllerPath = "jsp/admin/plugins/appointment/", right = AppointmentCategoryJspBean.RIGHT_MANAGECATEGORY )
62  public class AppointmentCategoryJspBean extends MVCAdminJspBean
63  {
64      private static final long serialVersionUID = 5438468406405679511L;
65  
66      /**
67       * Right to manage appointment category
68       */
69      public static final String RIGHT_MANAGECATEGORY = "APPOINTMENT_CATEGORY_MANAGEMENT";
70  
71      // templates
72      private static final String TEMPLATE_MANAGE_CATEGORY = "/admin/plugins/appointment/category/manage_category.html";
73      private static final String TEMPLATE_CREATE_CATEGORY = "/admin/plugins/appointment/category/create_category.html";
74      private static final String TEMPLATE_MODIFY_CATEGORY = "/admin/plugins/appointment/category/modify_category.html";
75  
76      // Parameters
77      private static final String PARAMETER_ID_CATEGORY = "id_category";
78  
79      // Properties for page titles
80      private static final String PROPERTY_PAGE_TITLE_MANAGE_CATEGORY = "appointment.adminFeature.manageCategories.name";
81      private static final String PROPERTY_PAGE_TITLE_CREATE_CATEGORY = "appointment.create.category.title";
82      private static final String PROPERTY_PAGE_TITLE_MODIFY_CATEGORY = "appointment.modify.category.title";
83      private static final String PROPERTY_DEFAULT_LIST_APPOINTMENT_PER_PAGE = "appointment.listAppointments.itemsPerPage";
84      private static final String PROPERTY_ID = "idCategory";
85      private static final String PROPERTY_LABEL = "label";
86  
87      // Markers
88      private static final String MARK_CATEGORY = "category";
89      private static final String MARK_DATA_TABLE_MANAGER = "dataTableManager";
90  
91      // Properties
92      private static final String MESSAGE_CONFIRM_REMOVE_CATEGORY = "appointment.message.confirmRemoveCategory";
93      private static final String MESSAGE_COLUMN_TITLE_ID = "appointment.manageCategory.columnId";
94      private static final String MESSAGE_COLUMN_TITLE_LABEL = "appointment.manageCategory.columnLabel";
95      private static final String MESSAGE_COLUMN_TITLE_ACTIONS = "portal.util.labelActions";
96  
97      // Views
98      private static final String VIEW_MANAGE_CATEGORY = "manageCategory";
99      private static final String VIEW_CREATE_CATEGORY = "createCategory";
100     private static final String VIEW_MODIFY_CATEGORY = "modifyCategory";
101 
102     // Actions
103     private static final String ACTION_CONFIRM_REMOVE_CATEGORY = "confirmRemoveCategory";
104     private static final String ACTION_CREATE_CATEGORY = "createCategory";
105     private static final String ACTION_MODIFY_CATEGORY = "modifyCategory";
106     private static final String ACTION_REMOVE_CATEGORY = "removeCategory";
107 
108     // Infos
109     private static final String INFO_CATEGORY_CREATED = "appointment.info.category.created";
110     private static final String INFO_CATEGORY_UPDATED = "appointment.info.category.updated";
111     private static final String INFO_CATEGORY_REMOVED = "appointment.info.category.removed";
112 
113     // Session variables
114     private DataTableManager<Category> _dataTableManager;
115 
116     /**
117      * Default constructor
118      */
119     public AppointmentCategoryJspBean( )
120     {
121     }
122 
123     /**
124      * Get the page to manage appointment categories
125      * 
126      * @param request
127      *            the request
128      * @return The HTML content to display
129      */
130     @View( value = VIEW_MANAGE_CATEGORY, defaultView = true )
131     public String getManageCategory( HttpServletRequest request )
132     {
133         if ( _dataTableManager == null )
134         {
135             _dataTableManager = new DataTableManager<Category>( getViewFullUrl( VIEW_MANAGE_CATEGORY ), null, AppPropertiesService.getPropertyInt(
136                     PROPERTY_DEFAULT_LIST_APPOINTMENT_PER_PAGE, 50 ), true );
137             _dataTableManager.addColumn( MESSAGE_COLUMN_TITLE_ID, PROPERTY_ID, true );
138             _dataTableManager.addColumn( MESSAGE_COLUMN_TITLE_LABEL, PROPERTY_LABEL, true );
139             _dataTableManager.addActionColumn( MESSAGE_COLUMN_TITLE_ACTIONS );
140         }
141         _dataTableManager.filterSortAndPaginate( request, CategoryService.findAllCategories( ) );
142         Map<String, Object> model = getModel( );
143         model.put( MARK_DATA_TABLE_MANAGER, _dataTableManager );
144         String strContent = getPage( PROPERTY_PAGE_TITLE_MANAGE_CATEGORY, TEMPLATE_MANAGE_CATEGORY, model );
145         _dataTableManager.clearItems( );
146         return strContent;
147 
148     }
149 
150     /**
151      * Display a popup to ask the user if he really wants to delete the category he selected
152      * 
153      * @param request
154      *            the request
155      * @return the HTML code to confirm
156      * @throws AccessDeniedException
157      *             If the user is not authorized
158      */
159     @Action( ACTION_CONFIRM_REMOVE_CATEGORY )
160     public String getConfirmRemoveAppointmentForm( HttpServletRequest request )
161     {
162         String strIdCategory = request.getParameter( PARAMETER_ID_CATEGORY );
163         if ( StringUtils.isEmpty( strIdCategory ) )
164         {
165             return redirectView( request, VIEW_MANAGE_CATEGORY );
166         }
167         int nIdCategory = Integer.parseInt( strIdCategory );
168         UrlItem url = new UrlItem( getActionUrl( ACTION_REMOVE_CATEGORY ) );
169         url.addParameter( PARAMETER_ID_CATEGORY, nIdCategory );
170         String strMessageUrl = AdminMessageService.getMessageUrl( request, MESSAGE_CONFIRM_REMOVE_CATEGORY, url.getUrl( ), AdminMessage.TYPE_CONFIRMATION );
171         return redirect( request, strMessageUrl );
172     }
173 
174     /**
175      * Remove the category selected
176      * 
177      * @param request
178      *            the request
179      * @return The JSP URL of the process result
180      * @throws AccessDeniedException
181      *             If the user is not authorized
182      */
183     @Action( ACTION_REMOVE_CATEGORY )
184     public String doRemoveCategory( HttpServletRequest request )
185     {
186         String strIdCategory = request.getParameter( PARAMETER_ID_CATEGORY );
187         if ( StringUtils.isEmpty( strIdCategory ) )
188         {
189             return redirectView( request, VIEW_MANAGE_CATEGORY );
190         }
191         int nIdCategory = Integer.parseInt( strIdCategory );
192         CategoryService.removeCategory( nIdCategory );
193         addInfo( INFO_CATEGORY_REMOVED, getLocale( ) );
194         return redirectView( request, VIEW_MANAGE_CATEGORY );
195     }
196 
197     /**
198      * Display the screen to create a new category
199      * 
200      * @param request
201      *            the request
202      * @return The HTML content to display
203      * @throws AccessDeniedException
204      *             If the user is not authorized
205      */
206     @View( VIEW_CREATE_CATEGORY )
207     public String getCreateCategory( HttpServletRequest request )
208     {
209         Map<String, Object> model = getModel( );
210         model.put( MARK_CATEGORY, new Category( ) );
211         return getPage( PROPERTY_PAGE_TITLE_CREATE_CATEGORY, TEMPLATE_CREATE_CATEGORY, model );
212     }
213 
214     /**
215      * Create a new category with the fields completed
216      * 
217      * @param request
218      *            the request
219      * @return The JSP URL of the process result
220      * @throws AccessDeniedException
221      *             If the user is not authorized
222      */
223     @Action( ACTION_CREATE_CATEGORY )
224     public String doCreateCategory( HttpServletRequest request )
225     {
226         Category category = new Category( );
227         populate( category, request );
228         CategoryService.saveCategory( category );
229         addInfo( INFO_CATEGORY_CREATED, getLocale( ) );
230         return redirectView( request, VIEW_MANAGE_CATEGORY );
231     }
232 
233     /**
234      * Get the view to modify an existed category
235      * 
236      * @param request
237      *            the request
238      * @return The HTML content to display
239      * @throws AccessDeniedException
240      *             If the user is not authorized
241      */
242     @View( VIEW_MODIFY_CATEGORY )
243     public String getModifyCategory( HttpServletRequest request )
244     {
245         String strIdCategory = request.getParameter( PARAMETER_ID_CATEGORY );
246         int nIdCategory = Integer.parseInt( strIdCategory );
247         Category category = CategoryService.findCategoryById( nIdCategory );
248         Map<String, Object> model = getModel( );
249         model.put( MARK_CATEGORY, category );
250         return getPage( PROPERTY_PAGE_TITLE_MODIFY_CATEGORY, TEMPLATE_MODIFY_CATEGORY, model );
251     }
252 
253     /**
254      * Modify a category
255      * 
256      * @param request
257      *            the request
258      * @return The JSP URL of the process result
259      * @throws AccessDeniedException
260      *             If the user is not authorized
261      */
262     @Action( ACTION_MODIFY_CATEGORY )
263     public String doModifyCategory( HttpServletRequest request )
264     {
265         String strIdCategory = request.getParameter( PARAMETER_ID_CATEGORY );
266         int nIdCategory = Integer.parseInt( strIdCategory );
267         Category category = new Category( );
268         category.setIdCategory( nIdCategory );
269         populate( category, request );
270         CategoryService.updateCategory( category );
271         addInfo( INFO_CATEGORY_UPDATED, getLocale( ) );
272         return redirectView( request, VIEW_MANAGE_CATEGORY );
273     }
274 
275 }