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.botpress.web;
36  
37  import fr.paris.lutece.plugins.botpress.business.BPBot;
38  import fr.paris.lutece.plugins.botpress.business.BPBotHome;
39  import fr.paris.lutece.plugins.botpress.service.BotRegistrationService;
40  import fr.paris.lutece.plugins.botpress.service.ConverseService;
41  import fr.paris.lutece.plugins.botpress.service.LanguageService;
42  import fr.paris.lutece.plugins.chatbot.service.avatar.AvatarRendererService;
43  import fr.paris.lutece.portal.service.i18n.I18nService;
44  import fr.paris.lutece.portal.service.message.AdminMessage;
45  import fr.paris.lutece.portal.service.message.AdminMessageService;
46  import fr.paris.lutece.portal.service.util.AppException;
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.portal.web.util.LocalizedPaginator;
53  import fr.paris.lutece.util.html.Paginator;
54  import fr.paris.lutece.util.url.UrlItem;
55  
56  import java.util.List;
57  import java.util.Map;
58  import javax.servlet.http.HttpServletRequest;
59  
60  /**
61   * This class provides the user interface to manage BPBot features ( manage, create, modify, remove )
62   */
63  @Controller( controllerJsp = "ManageBPBots.jsp", controllerPath = "jsp/admin/plugins/botpress/", right = "BOTPRESS_MANAGEMENT" )
64  public class BPBotJspBean extends MVCAdminJspBean
65  {
66      // Rights
67      public static final String RIGHT_MANAGEBOTPRESSBOTS = "BOTPRESS_MANAGEMENT";
68  
69      // Parameters
70      private static final String PARAMETER_PAGE_INDEX = "page_index";
71      private static final String PARAMETER_ID_BPBOT = "id";
72  
73      // Markers
74      private static final String MARK_PAGINATOR = "paginator";
75      private static final String MARK_NB_ITEMS_PER_PAGE = "nb_items_per_page";
76      private static final String MARK_STATUS_LIST = "status_list";
77      private static final String MARK_LANGUAGES_LIST = "languages_list";
78      private static final String MARK_MODES_LIST = "modes_list";
79      private static final String MARK_BPBOT_LIST = "bpbot_list";
80      private static final String MARK_BPBOT = "bpbot";
81      private static final String MARK_VERSIONS_LIST = "versions_list";
82      private static final String MARK_AVATAR_RENDERERS = "avatar_renderers_list";
83  
84      // Templates
85      private static final String TEMPLATE_MANAGE_BPBOTS = "/admin/plugins/botpress/manage_bpbots.html";
86      private static final String TEMPLATE_CREATE_BPBOT = "/admin/plugins/botpress/create_bpbot.html";
87      private static final String TEMPLATE_MODIFY_BPBOT = "/admin/plugins/botpress/modify_bpbot.html";
88  
89      // Properties
90      private static final String PROPERTY_DEFAULT_LIST_ITEM_PER_PAGE = "botpress.listItems.itemsPerPage";
91      private static final String PROPERTY_PAGE_TITLE_MANAGE_BPBOTS = "botpress.manage_bpbots.pageTitle";
92      private static final String PROPERTY_PAGE_TITLE_MODIFY_BPBOT = "botpress.modify_bpbot.pageTitle";
93      private static final String PROPERTY_PAGE_TITLE_CREATE_BPBOT = "botpress.create_bpbot.pageTitle";
94  
95      private static final String JSP_MANAGE_BPBOTS = "jsp/admin/plugins/botpress/ManageBPBots.jsp";
96  
97      // Properties
98      private static final String MESSAGE_CONFIRM_REMOVE_BPBOT = "botpress.message.confirmRemoveBPBot";
99      private static final String MESSAGE_ERROR_CREATING_BOT = "botpress.message.error.create";
100     private static final String MESSAGE_ERROR_MODIFYING_BOT = "botpress.message.error.modify";
101 
102     // Validations
103     private static final String VALIDATION_ATTRIBUTES_PREFIX = "botpress.model.entity.bpbot.attribute.";
104 
105     // Views
106     private static final String VIEW_MANAGE_BPBOTS = "manageBPBots";
107     private static final String VIEW_CREATE_BPBOT = "createBPBot";
108     private static final String VIEW_MODIFY_BPBOT = "modifyBPBot";
109 
110     // Actions
111     private static final String ACTION_CREATE_BPBOT = "createBPBot";
112     private static final String ACTION_MODIFY_BPBOT = "modifyBPBot";
113     private static final String ACTION_REMOVE_BPBOT = "removeBPBot";
114     private static final String ACTION_CONFIRM_REMOVE_BPBOT = "confirmRemoveBPBot";
115 
116     // Infos
117     private static final String INFO_BPBOT_CREATED = "botpress.info.bpbot.created";
118     private static final String INFO_BPBOT_UPDATED = "botpress.info.bpbot.updated";
119     private static final String INFO_BPBOT_REMOVED = "botpress.info.bpbot.removed";
120 
121     // Session variable to store working values
122     private BPBot _bpbot;
123     private String _strCurrentPageIndex;
124     private int _nItemsPerPage;
125 
126     /**
127      * Return a model that contains the list and paginator infos
128      * @param <T> Object class
129      * @param request
130      *            The HTTP request
131      * @param strBookmark
132      *            The bookmark
133      * @param list
134      *            The list of item
135      * @param strManageJsp
136      *            The JSP
137      * @return The model
138      */
139     private <T> Map<String, Object> getPaginatedListModel( HttpServletRequest request, String strBookmark, List<T> list, String strManageJsp )
140     {
141         _strCurrentPageIndex = Paginator.getPageIndex( request, Paginator.PARAMETER_PAGE_INDEX, _strCurrentPageIndex );
142         int nDefaultItemsPerPage = AppPropertiesService.getPropertyInt( PROPERTY_DEFAULT_LIST_ITEM_PER_PAGE, 50 );
143         _nItemsPerPage = Paginator.getItemsPerPage( request, Paginator.PARAMETER_ITEMS_PER_PAGE, _nItemsPerPage, nDefaultItemsPerPage );
144 
145         UrlItem url = new UrlItem( strManageJsp );
146         String strUrl = url.getUrl( );
147 
148         // PAGINATOR
149         LocalizedPaginator<T> paginator = new LocalizedPaginator<T>( list, _nItemsPerPage, strUrl, PARAMETER_PAGE_INDEX, _strCurrentPageIndex, getLocale( ) );
150 
151         Map<String, Object> model = getModel( );
152 
153         model.put( MARK_NB_ITEMS_PER_PAGE, String.valueOf( _nItemsPerPage ) );
154         model.put( MARK_PAGINATOR, paginator );
155         model.put( strBookmark, paginator.getPageItems( ) );
156 
157         return model;
158     }
159 
160     /**
161      * Build the Manage View
162      * 
163      * @param request
164      *            The HTTP request
165      * @return The page
166      */
167     @View( value = VIEW_MANAGE_BPBOTS, defaultView = true )
168     public String getManageBPBots( HttpServletRequest request )
169     {
170         _bpbot = null;
171         List<BPBot> listBPBots = BPBotHome.getBPBotsList( );
172         Map<String, Object> model = getPaginatedListModel( request, MARK_BPBOT_LIST, listBPBots, JSP_MANAGE_BPBOTS );
173 
174         return getPage( PROPERTY_PAGE_TITLE_MANAGE_BPBOTS, TEMPLATE_MANAGE_BPBOTS, model );
175     }
176 
177     /**
178      * Returns the form to create a bpbot
179      *
180      * @param request
181      *            The Http request
182      * @return the html code of the bpbot form
183      */
184     @View( VIEW_CREATE_BPBOT )
185     public String getCreateBPBot( HttpServletRequest request )
186     {
187         _bpbot = ( _bpbot != null ) ? _bpbot : new BPBot( );
188 
189         Map<String, Object> model = getModel( );
190         model.put( MARK_BPBOT, _bpbot );
191         model.put( MARK_STATUS_LIST, BotRegistrationService.getBotsStatusList( getLocale( ) ) );
192         model.put( MARK_LANGUAGES_LIST, LanguageService.getLanguages( getLocale( ) ) );
193         model.put( MARK_MODES_LIST, BotRegistrationService.getModes( getLocale( ) ) );
194         model.put( MARK_VERSIONS_LIST, ConverseService.getApiVersions( ) );
195         model.put( MARK_AVATAR_RENDERERS, AvatarRendererService.getAvatarRenderersList( ) );
196 
197         return getPage( PROPERTY_PAGE_TITLE_CREATE_BPBOT, TEMPLATE_CREATE_BPBOT, model );
198     }
199 
200     /**
201      * Process the data capture form of a new bpbot
202      *
203      * @param request
204      *            The Http Request
205      * @return The Jsp URL of the process result
206      */
207     @Action( ACTION_CREATE_BPBOT )
208     public String doCreateBPBot( HttpServletRequest request )
209     {
210         populate( _bpbot, request, request.getLocale( ) );
211 
212         // Check constraints
213         if ( !validateBean( _bpbot, VALIDATION_ATTRIBUTES_PREFIX ) )
214         {
215             return redirectView( request, VIEW_CREATE_BPBOT );
216         }
217 
218         try
219         {
220             BPBotHome.create( _bpbot );
221         }
222         catch( AppException e )
223         {
224             addError( I18nService.getLocalizedString( MESSAGE_ERROR_CREATING_BOT, getLocale( ) ) );
225             return redirectView( request, VIEW_CREATE_BPBOT );
226         }
227         addInfo( INFO_BPBOT_CREATED, getLocale( ) );
228 
229         return redirectView( request, VIEW_MANAGE_BPBOTS );
230     }
231 
232     /**
233      * Manages the removal form of a bpbot whose identifier is in the http request
234      *
235      * @param request
236      *            The Http request
237      * @return the html code to confirm
238      */
239     @Action( ACTION_CONFIRM_REMOVE_BPBOT )
240     public String getConfirmRemoveBPBot( HttpServletRequest request )
241     {
242         int nId = Integer.parseInt( request.getParameter( PARAMETER_ID_BPBOT ) );
243         UrlItem url = new UrlItem( getActionUrl( ACTION_REMOVE_BPBOT ) );
244         url.addParameter( PARAMETER_ID_BPBOT, nId );
245 
246         String strMessageUrl = AdminMessageService.getMessageUrl( request, MESSAGE_CONFIRM_REMOVE_BPBOT, url.getUrl( ), AdminMessage.TYPE_CONFIRMATION );
247 
248         return redirect( request, strMessageUrl );
249     }
250 
251     /**
252      * Handles the removal form of a bpbot
253      *
254      * @param request
255      *            The Http request
256      * @return the jsp URL to display the form to manage bpbots
257      */
258     @Action( ACTION_REMOVE_BPBOT )
259     public String doRemoveBPBot( HttpServletRequest request )
260     {
261         int nId = Integer.parseInt( request.getParameter( PARAMETER_ID_BPBOT ) );
262         BPBotHome.remove( nId );
263         addInfo( INFO_BPBOT_REMOVED, getLocale( ) );
264 
265         return redirectView( request, VIEW_MANAGE_BPBOTS );
266     }
267 
268     /**
269      * Returns the form to update info about a bpbot
270      *
271      * @param request
272      *            The Http request
273      * @return The HTML form to update info
274      */
275     @View( VIEW_MODIFY_BPBOT )
276     public String getModifyBPBot( HttpServletRequest request )
277     {
278         int nId = Integer.parseInt( request.getParameter( PARAMETER_ID_BPBOT ) );
279 
280         if ( _bpbot == null || ( _bpbot.getId( ) != nId ) )
281         {
282             _bpbot = BPBotHome.findByPrimaryKey( nId );
283         }
284 
285         Map<String, Object> model = getModel( );
286         model.put( MARK_BPBOT, _bpbot );
287         model.put( MARK_STATUS_LIST, BotRegistrationService.getBotsStatusList( getLocale( ) ) );
288         model.put( MARK_LANGUAGES_LIST, LanguageService.getLanguages( getLocale( ) ) );
289         model.put( MARK_MODES_LIST, BotRegistrationService.getModes( getLocale( ) ) );
290         model.put( MARK_VERSIONS_LIST, ConverseService.getApiVersions( ) );
291         model.put( MARK_AVATAR_RENDERERS, AvatarRendererService.getAvatarRenderersList( ) );
292 
293         return getPage( PROPERTY_PAGE_TITLE_MODIFY_BPBOT, TEMPLATE_MODIFY_BPBOT, model );
294     }
295 
296     /**
297      * Process the change form of a bpbot
298      *
299      * @param request
300      *            The Http request
301      * @return The Jsp URL of the process result
302      */
303     @Action( ACTION_MODIFY_BPBOT )
304     public String doModifyBPBot( HttpServletRequest request )
305     {
306         populate( _bpbot, request, request.getLocale( ) );
307 
308         // Check constraints
309         if ( !validateBean( _bpbot, VALIDATION_ATTRIBUTES_PREFIX ) )
310         {
311             return redirect( request, VIEW_MODIFY_BPBOT, PARAMETER_ID_BPBOT, _bpbot.getId( ) );
312         }
313         try
314         {
315             BPBotHome.update( _bpbot );
316         }
317         catch( AppException e )
318         {
319             addError( I18nService.getLocalizedString( MESSAGE_ERROR_MODIFYING_BOT, getLocale( ) ) );
320             return redirect( request, VIEW_MODIFY_BPBOT, PARAMETER_ID_BPBOT, _bpbot.getId( ) );
321         }
322         addInfo( INFO_BPBOT_UPDATED, getLocale( ) );
323 
324         return redirectView( request, VIEW_MANAGE_BPBOTS );
325     }
326 }