View Javadoc
1   /*
2    * Copyright (c) 2002-2022, City of 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.List;
37  import java.util.Map;
38  import javax.servlet.http.HttpServletRequest;
39  
40  import org.apache.commons.collections.CollectionUtils;
41  import org.apache.commons.lang3.StringUtils;
42  
43  import fr.paris.lutece.plugins.appointment.business.category.Category;
44  import fr.paris.lutece.plugins.appointment.business.form.FormHome;
45  import fr.paris.lutece.plugins.appointment.service.CategoryService;
46  import fr.paris.lutece.portal.service.admin.AccessDeniedException;
47  import fr.paris.lutece.portal.service.message.AdminMessage;
48  import fr.paris.lutece.portal.service.message.AdminMessageService;
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.url.UrlItem;
53  
54  /**
55   * This class provides the user interface to manage AppointmentForm features ( manage, create, modify, copy, remove )
56   * 
57   * @author L.Payen
58   * 
59   */
60  @Controller( controllerJsp = "ManageAppointmentCategory.jsp", controllerPath = "jsp/admin/plugins/appointment/", right = AppointmentCategoryJspBean.RIGHT_MANAGECATEGORY )
61  public class AppointmentCategoryJspBean extends AbstractAppointmentFormAndSlotJspBean
62  {
63      private static final long serialVersionUID = 5438468406405679511L;
64  
65      /**
66       * Right to manage appointment category
67       */
68      public static final String RIGHT_MANAGECATEGORY = "APPOINTMENT_CATEGORY_MANAGEMENT";
69      private static final String JSP_MANAGE_CATEGORY = "jsp/admin/plugins/appointment/ManageAppointmentCategory.jsp";
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  
84      // Validations
85      private static final String VALIDATION_ATTRIBUTES_PREFIX = "appointment.model.entity.category.attribute.";
86  
87      // Markers
88      private static final String MARK_CATEGORY = "category";
89      private static final String MARK_LIST_CATEGORY = "category_list";
90  
91      // Properties
92      private static final String MESSAGE_CONFIRM_REMOVE_CATEGORY = "appointment.message.confirmRemoveCategory";
93      private static final String MESSAGE_ERROR_REMOVE_CATEGORY = "appointment.message.categoryIsAffected.errorRemoveCategory";
94  
95      // Views
96      private static final String VIEW_MANAGE_CATEGORY = "manageCategory";
97      private static final String VIEW_CREATE_CATEGORY = "createCategory";
98      private static final String VIEW_MODIFY_CATEGORY = "modifyCategory";
99  
100     // Actions
101     private static final String ACTION_CONFIRM_REMOVE_CATEGORY = "confirmRemoveCategory";
102     private static final String ACTION_CREATE_CATEGORY = "createCategory";
103     private static final String ACTION_MODIFY_CATEGORY = "modifyCategory";
104     private static final String ACTION_REMOVE_CATEGORY = "removeCategory";
105 
106     // Infos
107     private static final String INFO_CATEGORY_CREATED = "appointment.info.category.created";
108     private static final String INFO_CATEGORY_UPDATED = "appointment.info.category.updated";
109     private static final String INFO_CATEGORY_REMOVED = "appointment.info.category.removed";
110 
111     // Session variables
112     private Category _category;
113 
114     /**
115      * Get the page to manage appointment categories
116      * 
117      * @param request
118      *            the request
119      * @return The HTML content to display
120      */
121     @View( value = VIEW_MANAGE_CATEGORY, defaultView = true )
122     public String getManageCategory( HttpServletRequest request )
123     {
124         _category = null;
125         List<Category> listCategory = CategoryService.findAllCategories( );
126         Map<String, Object> model = getPaginatedListModel( request, MARK_LIST_CATEGORY, listCategory, JSP_MANAGE_CATEGORY );
127 
128         return getPage( PROPERTY_PAGE_TITLE_MANAGE_CATEGORY, TEMPLATE_MANAGE_CATEGORY, model );
129     }
130 
131     /**
132      * Display a popup to ask the user if he really wants to delete the category he selected
133      * 
134      * @param request
135      *            the request
136      * @return the HTML code to confirm
137      * @throws AccessDeniedException
138      *             If the user is not authorized
139      */
140     @Action( ACTION_CONFIRM_REMOVE_CATEGORY )
141     public String getConfirmRemoveCategory( HttpServletRequest request )
142     {
143         String strIdCategory = request.getParameter( PARAMETER_ID_CATEGORY );
144         if ( StringUtils.isEmpty( strIdCategory ) )
145         {
146             return redirectView( request, VIEW_MANAGE_CATEGORY );
147         }
148         int nIdCategory = Integer.parseInt( strIdCategory );
149         UrlItem url = new UrlItem( getActionUrl( ACTION_REMOVE_CATEGORY ) );
150         url.addParameter( PARAMETER_ID_CATEGORY, nIdCategory );
151         String strMessageUrl = AdminMessageService.getMessageUrl( request, MESSAGE_CONFIRM_REMOVE_CATEGORY, url.getUrl( ), AdminMessage.TYPE_CONFIRMATION );
152         return redirect( request, strMessageUrl );
153     }
154 
155     /**
156      * Remove the category selected
157      * 
158      * @param request
159      *            the request
160      * @return The JSP URL of the process result
161      * @throws AccessDeniedException
162      *             If the user is not authorized
163      */
164     @Action( ACTION_REMOVE_CATEGORY )
165     public String doRemoveCategory( HttpServletRequest request )
166     {
167         String strIdCategory = request.getParameter( PARAMETER_ID_CATEGORY );
168         if ( CollectionUtils.isNotEmpty( FormHome.findByCategory( Integer.parseInt( strIdCategory ) ) ) )
169         {
170             addError( MESSAGE_ERROR_REMOVE_CATEGORY, getLocale( ) );
171             return redirectView( request, VIEW_MANAGE_CATEGORY );
172         }
173         int nIdCategory = Integer.parseInt( strIdCategory );
174         CategoryService.removeCategory( nIdCategory );
175         addInfo( INFO_CATEGORY_REMOVED, getLocale( ) );
176         return redirectView( request, VIEW_MANAGE_CATEGORY );
177     }
178 
179     /**
180      * Display the screen to create a new category
181      * 
182      * @param request
183      *            the request
184      * @return The HTML content to display
185      * @throws AccessDeniedException
186      *             If the user is not authorized
187      */
188     @View( VIEW_CREATE_CATEGORY )
189     public String getCreateCategory( HttpServletRequest request )
190     {
191         Map<String, Object> model = getModel( );
192         _category = ( _category == null ) ? new Category( ) : _category;
193         model.put( MARK_CATEGORY, _category );
194         return getPage( PROPERTY_PAGE_TITLE_CREATE_CATEGORY, TEMPLATE_CREATE_CATEGORY, model );
195     }
196 
197     /**
198      * Create a new category with the fields completed
199      * 
200      * @param request
201      *            the request
202      * @return The JSP URL of the process result
203      * @throws AccessDeniedException
204      *             If the user is not authorized
205      */
206     @Action( ACTION_CREATE_CATEGORY )
207     public String doCreateCategory( HttpServletRequest request )
208     {
209         populate( _category, request );
210         // Check constraints
211         if ( !validateBean( _category, VALIDATION_ATTRIBUTES_PREFIX ) )
212         {
213             return redirectView( request, VIEW_CREATE_CATEGORY );
214         }
215 
216         CategoryService.saveCategory( _category );
217         addInfo( INFO_CATEGORY_CREATED, getLocale( ) );
218         return redirectView( request, VIEW_MANAGE_CATEGORY );
219     }
220 
221     /**
222      * Get the view to modify an existed category
223      * 
224      * @param request
225      *            the request
226      * @return The HTML content to display
227      * @throws AccessDeniedException
228      *             If the user is not authorized
229      */
230     @View( VIEW_MODIFY_CATEGORY )
231     public String getModifyCategory( HttpServletRequest request )
232     {
233         String strIdCategory = request.getParameter( PARAMETER_ID_CATEGORY );
234         int nIdCategory = Integer.parseInt( strIdCategory );
235         _category = CategoryService.findCategoryById( nIdCategory );
236         Map<String, Object> model = getModel( );
237         model.put( MARK_CATEGORY, _category );
238         return getPage( PROPERTY_PAGE_TITLE_MODIFY_CATEGORY, TEMPLATE_MODIFY_CATEGORY, model );
239     }
240 
241     /**
242      * Modify a category
243      * 
244      * @param request
245      *            the request
246      * @return The JSP URL of the process result
247      * @throws AccessDeniedException
248      *             If the user is not authorized
249      */
250     @Action( ACTION_MODIFY_CATEGORY )
251     public String doModifyCategory( HttpServletRequest request )
252     {
253         String strIdCategory = request.getParameter( PARAMETER_ID_CATEGORY );
254         int nIdCategory = Integer.parseInt( strIdCategory );
255         _category = ( _category == null || _category.getIdCategory( ) != nIdCategory ) ? new Category( ) : _category;
256         _category.setIdCategory( nIdCategory );
257         populate( _category, request );
258         // Check constraints
259         if ( !validateBean( _category, VALIDATION_ATTRIBUTES_PREFIX ) )
260         {
261             return redirectView( request, VIEW_MODIFY_CATEGORY );
262         }
263         CategoryService.updateCategory( _category );
264         addInfo( INFO_CATEGORY_UPDATED, getLocale( ) );
265         return redirectView( request, VIEW_MANAGE_CATEGORY );
266     }
267 }