View Javadoc
1   /*
2    * Copyright (c) 2002-2019, 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  
35  package fr.paris.lutece.plugins.chatbot.modules.botavatar.web;
36  
37  import fr.paris.lutece.plugins.chatbot.modules.botavatar.business.AvatarAssociation;
38  import fr.paris.lutece.plugins.chatbot.modules.botavatar.business.AvatarAssociationHome;
39  import fr.paris.lutece.portal.service.message.AdminMessage;
40  import fr.paris.lutece.portal.service.message.AdminMessageService;
41  import fr.paris.lutece.portal.service.util.AppPropertiesService;
42  import fr.paris.lutece.portal.util.mvc.admin.MVCAdminJspBean;
43  import fr.paris.lutece.portal.util.mvc.admin.annotations.Controller;
44  import fr.paris.lutece.portal.util.mvc.commons.annotations.Action;
45  import fr.paris.lutece.portal.util.mvc.commons.annotations.View;
46  import fr.paris.lutece.portal.web.util.LocalizedPaginator;
47  import fr.paris.lutece.util.html.Paginator;
48  import fr.paris.lutece.util.url.UrlItem;
49  
50  import java.util.List;
51  import java.util.Map;
52  import javax.servlet.http.HttpServletRequest;
53  
54  /**
55   * This class provides the user interface to manage AvatarAssociation features ( manage, create, modify, remove )
56   */
57  @Controller( controllerJsp = "ManageAvatarAssociations.jsp", controllerPath = "jsp/admin/plugins/chatbot/modules/botavatar/", right = "BOTAVATAR_MANAGEMENT" )
58  public class AvatarAssociationJspBean extends MVCAdminJspBean
59  {
60      // Templates
61      private static final String TEMPLATE_MANAGE_AVATARASSOCIATIONS = "/admin/plugins/chatbot/modules/botavatar/manage_avatarassociations.html";
62      private static final String TEMPLATE_CREATE_AVATARASSOCIATION = "/admin/plugins/chatbot/modules/botavatar/create_avatarassociation.html";
63      private static final String TEMPLATE_MODIFY_AVATARASSOCIATION = "/admin/plugins/chatbot/modules/botavatar/modify_avatarassociation.html";
64  
65      // Parameters
66      private static final String PARAMETER_ID_AVATARASSOCIATION = "id";
67  
68      // Properties for page titles
69      private static final String PROPERTY_PAGE_TITLE_MANAGE_AVATARASSOCIATIONS = "module.chatbot.botavatar.manage_avatarassociations.pageTitle";
70      private static final String PROPERTY_PAGE_TITLE_MODIFY_AVATARASSOCIATION = "module.chatbot.botavatar.modify_avatarassociation.pageTitle";
71      private static final String PROPERTY_PAGE_TITLE_CREATE_AVATARASSOCIATION = "module.chatbot.botavatar.create_avatarassociation.pageTitle";
72  
73      // Markers
74      private static final String MARK_AVATARASSOCIATION_LIST = "avatarassociation_list";
75      private static final String MARK_AVATARASSOCIATION = "avatarassociation";
76  
77      private static final String JSP_MANAGE_AVATARASSOCIATIONS = "jsp/admin/plugins/chatbot/modules/botavatar/ManageAvatarAssociations.jsp";
78  
79      // Properties
80      private static final String MESSAGE_CONFIRM_REMOVE_AVATARASSOCIATION = "module.chatbot.botavatar.message.confirmRemoveAvatarAssociation";
81  
82      // Validations
83      private static final String VALIDATION_ATTRIBUTES_PREFIX = "module.chatbot.botavatar.model.entity.avatarassociation.attribute.";
84  
85      // Views
86      private static final String VIEW_MANAGE_AVATARASSOCIATIONS = "manageAvatarAssociations";
87      private static final String VIEW_CREATE_AVATARASSOCIATION = "createAvatarAssociation";
88      private static final String VIEW_MODIFY_AVATARASSOCIATION = "modifyAvatarAssociation";
89  
90      // Actions
91      private static final String ACTION_CREATE_AVATARASSOCIATION = "createAvatarAssociation";
92      private static final String ACTION_MODIFY_AVATARASSOCIATION = "modifyAvatarAssociation";
93      private static final String ACTION_REMOVE_AVATARASSOCIATION = "removeAvatarAssociation";
94      private static final String ACTION_CONFIRM_REMOVE_AVATARASSOCIATION = "confirmRemoveAvatarAssociation";
95  
96      // Infos
97      private static final String INFO_AVATARASSOCIATION_CREATED = "module.chatbot.botavatar.info.avatarassociation.created";
98      private static final String INFO_AVATARASSOCIATION_UPDATED = "module.chatbot.botavatar.info.avatarassociation.updated";
99      private static final String INFO_AVATARASSOCIATION_REMOVED = "module.chatbot.botavatar.info.avatarassociation.removed";
100 
101     // Rights
102     public static final String RIGHT_MANAGECHATBOTAVATARS = "BOTAVATAR_MANAGEMENT";
103 
104     // Properties
105     private static final String PROPERTY_DEFAULT_LIST_ITEM_PER_PAGE = "botavatar.listItems.itemsPerPage";
106 
107     // Parameters
108     private static final String PARAMETER_PAGE_INDEX = "page_index";
109 
110     // Markers
111     private static final String MARK_PAGINATOR = "paginator";
112     private static final String MARK_NB_ITEMS_PER_PAGE = "nb_items_per_page";
113 
114     // Variables
115     private int _nDefaultItemsPerPage;
116     private String _strCurrentPageIndex;
117     private int _nItemsPerPage;
118 
119     private AvatarAssociation _avatarassociation;
120 
121     /**
122      * Build the Manage View
123      * 
124      * @param request
125      *            The HTTP request
126      * @return The page
127      */
128     @View( value = VIEW_MANAGE_AVATARASSOCIATIONS, defaultView = true )
129     public String getManageAvatarAssociations( HttpServletRequest request )
130     {
131         _avatarassociation = null;
132         List<AvatarAssociation> listAvatarAssociations = AvatarAssociationHome.getAvatarAssociationsList( );
133         Map<String, Object> model = getPaginatedListModel( request, MARK_AVATARASSOCIATION_LIST, listAvatarAssociations, JSP_MANAGE_AVATARASSOCIATIONS );
134 
135         return getPage( PROPERTY_PAGE_TITLE_MANAGE_AVATARASSOCIATIONS, TEMPLATE_MANAGE_AVATARASSOCIATIONS, model );
136     }
137 
138     /**
139      * Returns the form to create a avatarassociation
140      *
141      * @param request
142      *            The Http request
143      * @return the html code of the avatarassociation form
144      */
145     @View( VIEW_CREATE_AVATARASSOCIATION )
146     public String getCreateAvatarAssociation( HttpServletRequest request )
147     {
148         _avatarassociation = ( _avatarassociation != null ) ? _avatarassociation : new AvatarAssociation( );
149 
150         Map<String, Object> model = getModel( );
151         model.put( MARK_AVATARASSOCIATION, _avatarassociation );
152 
153         return getPage( PROPERTY_PAGE_TITLE_CREATE_AVATARASSOCIATION, TEMPLATE_CREATE_AVATARASSOCIATION, model );
154     }
155 
156     /**
157      * Process the data capture form of a new avatarassociation
158      *
159      * @param request
160      *            The Http Request
161      * @return The Jsp URL of the process result
162      */
163     @Action( ACTION_CREATE_AVATARASSOCIATION )
164     public String doCreateAvatarAssociation( HttpServletRequest request )
165     {
166         populate( _avatarassociation, request, request.getLocale( ) );
167 
168         // Check constraints
169         if ( !validateBean( _avatarassociation, VALIDATION_ATTRIBUTES_PREFIX ) )
170         {
171             return redirectView( request, VIEW_CREATE_AVATARASSOCIATION );
172         }
173 
174         AvatarAssociationHome.create( _avatarassociation );
175         addInfo( INFO_AVATARASSOCIATION_CREATED, getLocale( ) );
176 
177         return redirectView( request, VIEW_MANAGE_AVATARASSOCIATIONS );
178     }
179 
180     /**
181      * Manages the removal form of a avatarassociation whose identifier is in the http request
182      *
183      * @param request
184      *            The Http request
185      * @return the html code to confirm
186      */
187     @Action( ACTION_CONFIRM_REMOVE_AVATARASSOCIATION )
188     public String getConfirmRemoveAvatarAssociation( HttpServletRequest request )
189     {
190         int nId = Integer.parseInt( request.getParameter( PARAMETER_ID_AVATARASSOCIATION ) );
191         UrlItem url = new UrlItem( getActionUrl( ACTION_REMOVE_AVATARASSOCIATION ) );
192         url.addParameter( PARAMETER_ID_AVATARASSOCIATION, nId );
193 
194         String strMessageUrl = AdminMessageService.getMessageUrl( request, MESSAGE_CONFIRM_REMOVE_AVATARASSOCIATION, url.getUrl( ),
195                 AdminMessage.TYPE_CONFIRMATION );
196 
197         return redirect( request, strMessageUrl );
198     }
199 
200     /**
201      * Handles the removal form of a avatarassociation
202      *
203      * @param request
204      *            The Http request
205      * @return the jsp URL to display the form to manage avatarassociations
206      */
207     @Action( ACTION_REMOVE_AVATARASSOCIATION )
208     public String doRemoveAvatarAssociation( HttpServletRequest request )
209     {
210         int nId = Integer.parseInt( request.getParameter( PARAMETER_ID_AVATARASSOCIATION ) );
211         AvatarAssociationHome.remove( nId );
212         addInfo( INFO_AVATARASSOCIATION_REMOVED, getLocale( ) );
213 
214         return redirectView( request, VIEW_MANAGE_AVATARASSOCIATIONS );
215     }
216 
217     /**
218      * Returns the form to update info about a avatarassociation
219      *
220      * @param request
221      *            The Http request
222      * @return The HTML form to update info
223      */
224     @View( VIEW_MODIFY_AVATARASSOCIATION )
225     public String getModifyAvatarAssociation( HttpServletRequest request )
226     {
227         int nId = Integer.parseInt( request.getParameter( PARAMETER_ID_AVATARASSOCIATION ) );
228 
229         if ( _avatarassociation == null || ( _avatarassociation.getId( ) != nId ) )
230         {
231             _avatarassociation = AvatarAssociationHome.findByPrimaryKey( nId );
232         }
233 
234         Map<String, Object> model = getModel( );
235         model.put( MARK_AVATARASSOCIATION, _avatarassociation );
236 
237         return getPage( PROPERTY_PAGE_TITLE_MODIFY_AVATARASSOCIATION, TEMPLATE_MODIFY_AVATARASSOCIATION, model );
238     }
239 
240     /**
241      * Process the change form of a avatarassociation
242      *
243      * @param request
244      *            The Http request
245      * @return The Jsp URL of the process result
246      */
247     @Action( ACTION_MODIFY_AVATARASSOCIATION )
248     public String doModifyAvatarAssociation( HttpServletRequest request )
249     {
250         populate( _avatarassociation, request, request.getLocale( ) );
251 
252         // Check constraints
253         if ( !validateBean( _avatarassociation, VALIDATION_ATTRIBUTES_PREFIX ) )
254         {
255             return redirect( request, VIEW_MODIFY_AVATARASSOCIATION, PARAMETER_ID_AVATARASSOCIATION, _avatarassociation.getId( ) );
256         }
257 
258         AvatarAssociationHome.update( _avatarassociation );
259         addInfo( INFO_AVATARASSOCIATION_UPDATED, getLocale( ) );
260 
261         return redirectView( request, VIEW_MANAGE_AVATARASSOCIATIONS );
262     }
263     
264     /**
265      * Return a model that contains the list and paginator infos
266      * 
267      * @param request
268      *            The HTTP request
269      * @param strBookmark
270      *            The bookmark
271      * @param list
272      *            The list of item
273      * @param strManageJsp
274      *            The JSP
275      * @return The model
276      */
277     private <T> Map<String, Object> getPaginatedListModel( HttpServletRequest request, String strBookmark, List<T> list, String strManageJsp )
278     {
279         _strCurrentPageIndex = Paginator.getPageIndex( request, Paginator.PARAMETER_PAGE_INDEX, _strCurrentPageIndex );
280         _nDefaultItemsPerPage = AppPropertiesService.getPropertyInt( PROPERTY_DEFAULT_LIST_ITEM_PER_PAGE, 50 );
281         _nItemsPerPage = Paginator.getItemsPerPage( request, Paginator.PARAMETER_ITEMS_PER_PAGE, _nItemsPerPage, _nDefaultItemsPerPage );
282 
283         UrlItem url = new UrlItem( strManageJsp );
284         String strUrl = url.getUrl( );
285 
286         // PAGINATOR
287         LocalizedPaginator<T> paginator = new LocalizedPaginator<T>( list, _nItemsPerPage, strUrl, PARAMETER_PAGE_INDEX, _strCurrentPageIndex, getLocale( ) );
288 
289         Map<String, Object> model = getModel( );
290 
291         model.put( MARK_NB_ITEMS_PER_PAGE, String.valueOf( _nItemsPerPage ) );
292         model.put( MARK_PAGINATOR, paginator );
293         model.put( strBookmark, paginator.getPageItems( ) );
294 
295         return model;
296     }    // Session variable to store working values
297     
298 }