View Javadoc
1   /*
2    * Copyright (c) 2002-2017, 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.modulenotifygrumappingmanager.web;
36  
37  import java.util.List;
38  import java.util.Map;
39  
40  import javax.servlet.http.HttpServletRequest;
41  
42  import fr.paris.lutece.plugins.modulenotifygrumappingmanager.business.NotifygruMappingManager;
43  import fr.paris.lutece.plugins.modulenotifygrumappingmanager.business.NotifygruMappingManagerHome;
44  import fr.paris.lutece.plugins.modulenotifygrumappingmanager.service.NotifygruMappingManagerService;
45  import fr.paris.lutece.plugins.modulenotifygrumappingmanager.web.rs.Constants;
46  import fr.paris.lutece.portal.service.message.AdminMessage;
47  import fr.paris.lutece.portal.service.message.AdminMessageService;
48  import fr.paris.lutece.portal.service.util.AppLogService;
49  import fr.paris.lutece.portal.service.util.AppPathService;
50  import fr.paris.lutece.portal.util.mvc.admin.annotations.Controller;
51  import fr.paris.lutece.portal.util.mvc.commons.annotations.Action;
52  import fr.paris.lutece.portal.util.mvc.commons.annotations.View;
53  import fr.paris.lutece.util.ReferenceItem;
54  import fr.paris.lutece.util.ReferenceList;
55  import fr.paris.lutece.util.url.UrlItem;
56  import java.util.HashMap;
57  
58  /**
59   * This class provides the user interface to manage NotifygruMappingManager features ( manage, create, modify, remove )
60   */
61  @Controller( controllerJsp = "ManageNotifygruMappingManagers.jsp", controllerPath = "jsp/admin/plugins/modulenotifygrumappingmanager/", right = "MODULENOTIFYGRUMAPPINGMANAGER_MANAGEMENT" )
62  public class NotifygruMappingManagerJspBean extends ManageModulenotifygrumappingmanagerJspBean
63  {
64  
65      // //////////////////////////////////////////////////////////////////////////
66      // Constants
67  
68      /**
69       * Generated serial ID
70       */
71      private static final long serialVersionUID = 230502602226206951L;
72  
73      // templates
74      private static final String TEMPLATE_MANAGE_NOTIFYGRUMAPPINGMANAGERS = "/admin/plugins/modulenotifygrumappingmanager/manage_notifygrumappingmanagers.html";
75      private static final String TEMPLATE_CREATE_NOTIFYGRUMAPPINGMANAGER = "/admin/plugins/modulenotifygrumappingmanager/create_notifygrumappingmanager.html";
76      private static final String TEMPLATE_MODIFY_NOTIFYGRUMAPPINGMANAGER = "/admin/plugins/modulenotifygrumappingmanager/modify_notifygrumappingmanager.html";
77  
78      // Parameters
79      private static final String PARAMETER_ID_NOTIFYGRUMAPPINGMANAGER = "id";
80      private static final String PARAMS_REQUEST_BEAN_KEY = "beankey";
81      private static final String PARAMS_REQUEST_EMAIL = "email";
82      private static final String PARAMS_REQUEST_CONNECTION_ID = "connectionid";
83      private static final String PARAMS_REQUEST_CUSTOMER_ID = "customerid";
84      private static final String PARAMS_REQUEST_MOBILE_PHONE_NUMBER = "mobilephonenumber";
85      private static final String PARAMS_REQUEST_FIXED_PHONE_NUMBER = "fixedphonenumber";
86      private static final String PARAMS_REQUEST_DEMANDETYPE = "demandetype";
87      private static final String PARAMS_REQUEST_DEMAND_REFERENCE = "demandreference";
88  
89      // Properties for page titles
90      private static final String PROPERTY_PAGE_TITLE_MANAGE_NOTIFYGRUMAPPINGMANAGERS = "modulenotifygrumappingmanager.manage_notifygrumappingmanagers.pageTitle";
91      private static final String PROPERTY_PAGE_TITLE_MODIFY_NOTIFYGRUMAPPINGMANAGER = "modulenotifygrumappingmanager.modify_notifygrumappingmanager.pageTitle";
92      private static final String PROPERTY_PAGE_TITLE_CREATE_NOTIFYGRUMAPPINGMANAGER = "modulenotifygrumappingmanager.create_notifygrumappingmanager.pageTitle";
93  
94      // Markers
95      private static final String MARK_NOTIFYGRUMAPPINGMANAGER_LIST = "notifygrumappingmanager_list";
96      private static final String MARK_NOTIFYGRUMAPPINGMANAGER = "notifygrumappingmanager";
97      private static final String MARK_NOTIFYGRU_FORM_LIST_PROVIDER = "list_provider";
98      private static final String MARK_NOTIFYGRU_FORM_LIST_POSITION = "list_position";
99      private static final String MARK_NOTIFYGRU_FORM_CURRENT_BEAN_KEY = "beankey";
100     private static final String MARK_BASE_URL = "BASE_URL";
101     private static final String MARK_KEY_AJAX = "AJAX_KEY";
102     private static final String MARK_NOTIFYGRU_FORM_MAP_POSITION_LABEL = "mapPositionLabel";
103 
104     private static final String JSP_MANAGE_NOTIFYGRUMAPPINGMANAGERS = "jsp/admin/plugins/modulenotifygrumappingmanager/ManageNotifygruMappingManagers.jsp";
105 
106     // Properties
107     private static final String MESSAGE_CONFIRM_REMOVE_NOTIFYGRUMAPPINGMANAGER = "modulenotifygrumappingmanager.message.confirmRemoveNotifygruMappingManager";
108 
109     private static final String VALIDATION_ATTRIBUTES_PREFIX = "modulenotifygrumappingmanager.model.entity.notifygrumappingmanager.attribute.";
110 
111     // Views
112     private static final String VIEW_MANAGE_NOTIFYGRUMAPPINGMANAGERS = "manageNotifygruMappingManagers";
113     private static final String VIEW_CREATE_NOTIFYGRUMAPPINGMANAGER = "createNotifygruMappingManager";
114     private static final String VIEW_MODIFY_NOTIFYGRUMAPPINGMANAGER = "modifyNotifygruMappingManager";
115 
116     // Actions
117     private static final String ACTION_CREATE_NOTIFYGRUMAPPINGMANAGER = "createNotifygruMappingManager";
118     private static final String ACTION_MODIFY_NOTIFYGRUMAPPINGMANAGER = "modifyNotifygruMappingManager";
119     private static final String ACTION_REMOVE_NOTIFYGRUMAPPINGMANAGER = "removeNotifygruMappingManager";
120     private static final String ACTION_CONFIRM_REMOVE_NOTIFYGRUMAPPINGMANAGER = "confirmRemoveNotifygruMappingManager";
121 
122     // Infos
123     private static final String INFO_NOTIFYGRUMAPPINGMANAGER_CREATED = "modulenotifygrumappingmanager.info.notifygrumappingmanager.created";
124     private static final String INFO_NOTIFYGRUMAPPINGMANAGER_UPDATED = "modulenotifygrumappingmanager.info.notifygrumappingmanager.updated";
125     private static final String INFO_NOTIFYGRUMAPPINGMANAGER_REMOVED = "modulenotifygrumappingmanager.info.notifygrumappingmanager.removed";
126 
127     // Session variable to store working values
128     private NotifygruMappingManager _notifygrumappingmanager;
129 
130     /**
131      * Build the Manage View
132      * 
133      * @param request
134      *            The HTTP request
135      * @return The page
136      */
137     @View( value = VIEW_MANAGE_NOTIFYGRUMAPPINGMANAGERS, defaultView = true )
138     public String getManageNotifygruMappingManagers( HttpServletRequest request )
139     {
140         _notifygrumappingmanager = null;
141         ReferenceList referenceListBean = NotifygruMappingManagerService.getListProvider( );
142         List<NotifygruMappingManager> listNotifygruMappingManagers = NotifygruMappingManagerHome.getNotifygruMappingManagersList( );
143         
144         //Fill a map with correspondances between fields position and fields name for all the provider
145         Map<String,ReferenceList> mapBeanListPosition = new HashMap<>();
146         for ( NotifygruMappingManager notifyGruMappingManager : listNotifygruMappingManagers )
147         {
148             if ( !referenceListBean.isEmpty( ) )
149             {
150                 mapBeanListPosition.put( notifyGruMappingManager.getBeanKey(), NotifygruMappingManagerService.getMappingPropertiesOfProvider( notifyGruMappingManager.getBeanKey() ) );
151             }
152         }
153         
154         Map<String, Object> model = getPaginatedListModel( request, MARK_NOTIFYGRUMAPPINGMANAGER_LIST, listNotifygruMappingManagers,
155                 JSP_MANAGE_NOTIFYGRUMAPPINGMANAGERS );
156         model.put( MARK_NOTIFYGRU_FORM_LIST_PROVIDER, referenceListBean.toMap() );
157         model.put( MARK_NOTIFYGRU_FORM_MAP_POSITION_LABEL, mapBeanListPosition );
158         return getPage( PROPERTY_PAGE_TITLE_MANAGE_NOTIFYGRUMAPPINGMANAGERS, TEMPLATE_MANAGE_NOTIFYGRUMAPPINGMANAGERS, model );
159     }
160 
161     /**
162      * Returns the form to create a notifygrumappingmanager
163      *
164      * @param request
165      *            The Http request
166      * @return the html code of the notifygrumappingmanager form
167      */
168     @View( VIEW_CREATE_NOTIFYGRUMAPPINGMANAGER )
169     public String getCreateNotifygruMappingManager( HttpServletRequest request )
170     {
171         _notifygrumappingmanager = ( _notifygrumappingmanager != null ) ? _notifygrumappingmanager : new NotifygruMappingManager( );
172 
173         ReferenceList referenceListBean = NotifygruMappingManagerService.getListProvider( );
174         String strCurrentBeanKey = request.getParameter( PARAMS_REQUEST_BEAN_KEY );
175 
176         Map<String, Object> model = getModel( );
177         model.put( MARK_NOTIFYGRUMAPPINGMANAGER, _notifygrumappingmanager );
178         model.put( MARK_NOTIFYGRU_FORM_LIST_PROVIDER, referenceListBean );
179 
180         ReferenceList listPosition = new ReferenceList( );
181 
182         if ( strCurrentBeanKey != null )
183         {
184             listPosition = NotifygruMappingManagerService.getMappingPropertiesOfProvider( strCurrentBeanKey );
185         }
186         else
187         {
188             if ( !referenceListBean.isEmpty( ) )
189             {
190                 listPosition = NotifygruMappingManagerService.getMappingPropertiesOfProvider( referenceListBean.get( 0 ).getCode( ) );
191                 strCurrentBeanKey = referenceListBean.get( 0 ).getCode( );
192             }
193         }
194 
195         UrlItem url = new UrlItem( AppPathService.getBaseUrl( request ) );
196 
197         model.put( MARK_NOTIFYGRU_FORM_LIST_POSITION, listPosition );
198         model.put( MARK_NOTIFYGRU_FORM_CURRENT_BEAN_KEY, strCurrentBeanKey );
199         model.put( MARK_BASE_URL, url );
200         model.put( MARK_KEY_AJAX, Constants.KEY_BEAN );
201 
202         return getPage( PROPERTY_PAGE_TITLE_CREATE_NOTIFYGRUMAPPINGMANAGER, TEMPLATE_CREATE_NOTIFYGRUMAPPINGMANAGER, model );
203     }
204 
205     /**
206      * Process the data capture form of a new notifygrumappingmanager
207      *
208      * @param request
209      *            The Http Request
210      * @return The Jsp URL of the process result
211      */
212     @Action( ACTION_CREATE_NOTIFYGRUMAPPINGMANAGER )
213     public String doCreateNotifygruMappingManager( HttpServletRequest request )
214     {
215 
216         _notifygrumappingmanager = ( _notifygrumappingmanager != null ) ? _notifygrumappingmanager : new NotifygruMappingManager( );
217 
218         populate( request );
219 
220         // Check constraints
221         if ( !validateBean( _notifygrumappingmanager, VALIDATION_ATTRIBUTES_PREFIX ) )
222         {
223             return redirectView( request, VIEW_CREATE_NOTIFYGRUMAPPINGMANAGER );
224         }
225 
226         if ( exitMapping( ) )
227         {
228 
229             addError( "modulenotifygrumappingmanager.validation.notifygrumappingmanager.BeanKey.exist", getLocale( ) );
230             return redirectView( request, VIEW_CREATE_NOTIFYGRUMAPPINGMANAGER );
231 
232         }
233 
234         NotifygruMappingManagerHome.create( _notifygrumappingmanager );
235         addInfo( INFO_NOTIFYGRUMAPPINGMANAGER_CREATED, getLocale( ) );
236 
237         return redirectView( request, VIEW_MANAGE_NOTIFYGRUMAPPINGMANAGERS );
238     }
239 
240     protected void populate( HttpServletRequest request )
241     {
242 
243         try
244         {
245             int nId = Integer.parseInt( request.getParameter( PARAMETER_ID_NOTIFYGRUMAPPINGMANAGER ) );
246             _notifygrumappingmanager.setId( nId );
247         }
248         catch( NumberFormatException nfe )
249         {
250             AppLogService.info( "insertion of mapping" );
251         }
252 
253         _notifygrumappingmanager.setBeanKey( request.getParameter( PARAMS_REQUEST_BEAN_KEY ) );
254         _notifygrumappingmanager.setEmail( Integer.parseInt( request.getParameter( PARAMS_REQUEST_EMAIL ) ) );
255         _notifygrumappingmanager.setConnectionId( Integer.parseInt( request.getParameter( PARAMS_REQUEST_CONNECTION_ID ) ) );
256         _notifygrumappingmanager.setCustomerId( Integer.parseInt( request.getParameter( PARAMS_REQUEST_CUSTOMER_ID ) ) );
257         _notifygrumappingmanager.setMobilePhoneNumber( Integer.parseInt( request.getParameter( PARAMS_REQUEST_MOBILE_PHONE_NUMBER ) ) );
258         _notifygrumappingmanager.setFixedPhoneNumber( Integer.parseInt( request.getParameter( PARAMS_REQUEST_FIXED_PHONE_NUMBER ) ) );
259         _notifygrumappingmanager.setDemandReference( Integer.parseInt( request.getParameter( PARAMS_REQUEST_DEMAND_REFERENCE ) ) );
260 
261         try
262         {
263             _notifygrumappingmanager.setDemandeTypeId( Integer.parseInt( request.getParameter( PARAMS_REQUEST_DEMANDETYPE ) ) );
264         }
265         catch( NumberFormatException nfe )
266         {
267             _notifygrumappingmanager.setDemandeTypeId( 0 );
268             AppLogService.info( "Unable to parse entered value for integer DemandTypeId. Value set to 0" );
269         }
270 
271     }
272 
273     private Boolean exitMapping( )
274     {
275         // not dublicate
276         return NotifygruMappingManagerHome.findByPrimaryKey( _notifygrumappingmanager.getBeanKey( ) ) != null;
277     }
278 
279     /**
280      * Manages the removal form of a notifygrumappingmanager whose identifier is in the http request
281      *
282      * @param request
283      *            The Http request
284      * @return the html code to confirm
285      */
286     @Action( ACTION_CONFIRM_REMOVE_NOTIFYGRUMAPPINGMANAGER )
287     public String getConfirmRemoveNotifygruMappingManager( HttpServletRequest request )
288     {
289         int nId = Integer.parseInt( request.getParameter( PARAMETER_ID_NOTIFYGRUMAPPINGMANAGER ) );
290         UrlItem url = new UrlItem( getActionUrl( ACTION_REMOVE_NOTIFYGRUMAPPINGMANAGER ) );
291         url.addParameter( PARAMETER_ID_NOTIFYGRUMAPPINGMANAGER, nId );
292 
293         String strMessageUrl = AdminMessageService.getMessageUrl( request, MESSAGE_CONFIRM_REMOVE_NOTIFYGRUMAPPINGMANAGER, url.getUrl( ),
294                 AdminMessage.TYPE_CONFIRMATION );
295 
296         return redirect( request, strMessageUrl );
297     }
298 
299     /**
300      * Handles the removal form of a notifygrumappingmanager
301      *
302      * @param request
303      *            The Http request
304      * @return the jsp URL to display the form to manage notifygrumappingmanagers
305      */
306     @Action( ACTION_REMOVE_NOTIFYGRUMAPPINGMANAGER )
307     public String doRemoveNotifygruMappingManager( HttpServletRequest request )
308     {
309         int nId = Integer.parseInt( request.getParameter( PARAMETER_ID_NOTIFYGRUMAPPINGMANAGER ) );
310         NotifygruMappingManagerHome.remove( nId );
311         addInfo( INFO_NOTIFYGRUMAPPINGMANAGER_REMOVED, getLocale( ) );
312 
313         return redirectView( request, VIEW_MANAGE_NOTIFYGRUMAPPINGMANAGERS );
314     }
315 
316     /**
317      * Returns the form to update info about a notifygrumappingmanager
318      *
319      * @param request
320      *            The Http request
321      * @return The HTML form to update info
322      */
323     @View( VIEW_MODIFY_NOTIFYGRUMAPPINGMANAGER )
324     public String getModifyNotifygruMappingManager( HttpServletRequest request )
325     {
326         int nId = Integer.parseInt( request.getParameter( PARAMETER_ID_NOTIFYGRUMAPPINGMANAGER ) );
327 
328         if ( _notifygrumappingmanager == null || ( _notifygrumappingmanager.getId( ) != nId ) )
329         {
330             _notifygrumappingmanager = NotifygruMappingManagerHome.findByPrimaryKey( nId );
331         }
332 
333         ReferenceList referenceListBean = new ReferenceList( );
334         ReferenceList listPosition = new ReferenceList( );
335 
336         if ( _notifygrumappingmanager != null )
337         {
338             for ( ReferenceItem provider : NotifygruMappingManagerService.getListProvider( ) )
339             {
340 
341                 AppLogService.debug( "\n\n\n\n\n\n  provider.getCode() = " + provider.getCode( ) + "    _notifygrumappingmanager.getBeanKey() = "
342                         + _notifygrumappingmanager.getBeanKey( ) );
343                 if ( provider.getCode( ).equals( _notifygrumappingmanager.getBeanKey( ) ) )
344                 {
345                     referenceListBean.add( provider );
346                     listPosition = NotifygruMappingManagerService.getMappingPropertiesOfProvider( provider.getCode( ) );
347                 }
348             }
349         }
350 
351         Map<String, Object> model = getModel( );
352         model.put( MARK_NOTIFYGRUMAPPINGMANAGER, _notifygrumappingmanager );
353 
354         model.put( MARK_NOTIFYGRU_FORM_LIST_PROVIDER, referenceListBean );
355 
356         model.put( MARK_NOTIFYGRU_FORM_LIST_POSITION, listPosition );
357 
358         return getPage( PROPERTY_PAGE_TITLE_MODIFY_NOTIFYGRUMAPPINGMANAGER, TEMPLATE_MODIFY_NOTIFYGRUMAPPINGMANAGER, model );
359     }
360 
361     /**
362      * Process the change form of a notifygrumappingmanager
363      *
364      * @param request
365      *            The Http request
366      * @return The Jsp URL of the process result
367      */
368     @Action( ACTION_MODIFY_NOTIFYGRUMAPPINGMANAGER )
369     public String doModifyNotifygruMappingManager( HttpServletRequest request )
370     {
371         populate( request );
372 
373         // Check constraints
374         if ( !validateBean( _notifygrumappingmanager, VALIDATION_ATTRIBUTES_PREFIX ) )
375         {
376             return redirect( request, VIEW_MODIFY_NOTIFYGRUMAPPINGMANAGER, PARAMETER_ID_NOTIFYGRUMAPPINGMANAGER, _notifygrumappingmanager.getId( ) );
377         }
378 
379         NotifygruMappingManagerHome.update( _notifygrumappingmanager );
380         addInfo( INFO_NOTIFYGRUMAPPINGMANAGER_UPDATED, getLocale( ) );
381 
382         return redirectView( request, VIEW_MANAGE_NOTIFYGRUMAPPINGMANAGERS );
383     }
384 }