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.unittree.web;
35  
36  import java.util.ArrayList;
37  import java.util.HashMap;
38  import java.util.List;
39  import java.util.Map;
40  import java.util.Set;
41  
42  import javax.servlet.http.HttpServletRequest;
43  import javax.servlet.http.HttpServletResponse;
44  import javax.validation.ConstraintViolation;
45  import javax.xml.transform.Source;
46  
47  import org.apache.commons.beanutils.ConvertUtils;
48  import org.apache.commons.lang.StringUtils;
49  
50  import fr.paris.lutece.plugins.unittree.business.action.UnitAction;
51  import fr.paris.lutece.plugins.unittree.business.action.UnitUserAction;
52  import fr.paris.lutece.plugins.unittree.business.unit.Unit;
53  import fr.paris.lutece.plugins.unittree.business.unit.Unit.TypeUnit;
54  import fr.paris.lutece.plugins.unittree.service.UnitErrorException;
55  import fr.paris.lutece.plugins.unittree.service.unit.IUnitService;
56  import fr.paris.lutece.plugins.unittree.service.unit.IUnitUserService;
57  import fr.paris.lutece.plugins.unittree.service.unit.UnitAttributeManager;
58  import fr.paris.lutece.plugins.unittree.service.unit.UnitResourceIdService;
59  import fr.paris.lutece.plugins.unittree.service.unit.UnitUserAttributeManager;
60  import fr.paris.lutece.plugins.unittree.web.action.IUnitPluginAction;
61  import fr.paris.lutece.plugins.unittree.web.action.IUnitSearchFields;
62  import fr.paris.lutece.plugins.unittree.web.action.UnitUserSearchFields;
63  import fr.paris.lutece.portal.business.user.AdminUser;
64  import fr.paris.lutece.portal.service.admin.AccessDeniedException;
65  import fr.paris.lutece.portal.service.html.XmlTransformerService;
66  import fr.paris.lutece.portal.service.i18n.I18nService;
67  import fr.paris.lutece.portal.service.message.AdminMessage;
68  import fr.paris.lutece.portal.service.message.AdminMessageService;
69  import fr.paris.lutece.portal.service.plugin.PluginService;
70  import fr.paris.lutece.portal.service.spring.SpringContextService;
71  import fr.paris.lutece.portal.service.template.AppTemplateService;
72  import fr.paris.lutece.portal.service.util.AppException;
73  import fr.paris.lutece.portal.service.util.AppLogService;
74  import fr.paris.lutece.portal.service.util.AppPathService;
75  import fr.paris.lutece.portal.web.admin.PluginAdminPageJspBean;
76  import fr.paris.lutece.portal.web.constants.Messages;
77  import fr.paris.lutece.portal.web.pluginaction.DefaultPluginActionResult;
78  import fr.paris.lutece.portal.web.pluginaction.IPluginActionResult;
79  import fr.paris.lutece.portal.web.pluginaction.PluginActionManager;
80  import fr.paris.lutece.util.UniqueIDGenerator;
81  import fr.paris.lutece.util.beanvalidation.BeanValidationUtil;
82  import fr.paris.lutece.util.html.HtmlTemplate;
83  import fr.paris.lutece.util.url.UrlItem;
84  
85  /**
86   *
87   * UnitJspBean
88   *
89   */
90  public class UnitJspBean extends PluginAdminPageJspBean
91  {
92      public static final String RIGHT_MANAGE_UNITS = "UNITS_MANAGEMENT";
93      private static final long serialVersionUID = 5997434357453313400L;
94  
95      // BEAN
96      private static final String BEAN_UNIT_USER_SERVICE = "unittree.unitUserService";
97  
98      // PROPERTIES
99      private static final String PROPERTY_MANAGE_UNITS_PAGE_TITLE = "unittree.manageUnits.pageTitle";
100     private static final String PROPERTY_CREATE_UNIT_PAGE_TITLE = "unittree.createUnit.pageTitle";
101     private static final String PROPERTY_MODIFY_UNIT_PAGE_TITLE = "unittree.modifyUnit.pageTitle";
102     private static final String PROPERTY_ADD_USERS_PAGE_TITLE = "unittree.addUsers.pageTitle";
103     private static final String PROPERTY_MODIFY_USER_PAGE_TITLE = "unittree.modifyUser.pageTitle";
104     private static final String PROPERTY_MOVE_USER_PAGE_TITLE = "unittree.moveUser.pageTitle";
105     private static final String PROPERTY_MOVE_UNIT_PAGE_TITLE = "unittree.moveSubTree.pageTitle";
106 
107     // MESSAGES
108     private static final String MESSAGE_ERROR_GENERIC_MESSAGE = "unittree.message.error.genericMessage";
109     private static final String MESSAGE_ERROR_UNIT_NOT_FOUND = "unittree.message.error.unitNotFound";
110     private static final String MESSAGE_ERROR_UNIT_HAS_SUB_UNITS = "unittree.message.error.unitHasSubUnits";
111     private static final String MESSAGE_ERROR_USER_ALREADY_IN_UNIT = "unittree.message.error.userAlreadyInUnit";
112     private static final String MESSAGE_ERROR_NO_SUB_UNITS = "unittree.message.error.noSubUnits";
113     private static final String MESSAGE_CONFIRM_REMOVE_UNIT = "unittree.message.removeUnit";
114     private static final String MESSAGE_CONFIRM_REMOVE_USER = "unittree.message.removeUser";
115     private static final String MESSAGE_ACCESS_DENIED = "unittree.message.accessDenied";
116     private static final String MESSAGE_SUB_TREE_MOVED = "unittree.moveSubTree.subTreeMoved";
117     private static final String MESSAGE_CANT_MOVE_SUB_TREE_TO_CHILD = "unittree.moveSubTree.cantMoveSubTreeToChild";
118 
119     // MARKS
120     private static final String MARK_UNIT_TREE = "unitTree";
121     private static final String MARK_PARENT_UNIT = "parentUnit";
122     private static final String MARK_LIST_SUB_UNITS = "listSubUnits";
123     private static final String MARK_LIST_UNIT_ACTIONS = "listUnitActions";
124     private static final String MARK_LIST_UNIT_USER_ACTIONS = "listUnitUserActions";
125     private static final String MARK_LIST_UNIT_USER_PLUGIN_ACTIONS = "listUnitUserPluginActions";
126     private static final String MARK_UNIT = "unit";
127     private static final String MARK_UNIT_TO_MOVE = "unitToMove";
128     private static final String MARK_UNITS = "units";
129     private static final String MARK_UNIT_PARENT = "unitParent";
130     private static final String MARK_USER = "user";
131     private static final String MARK_LIST_UNIT_ATTRIBUTES = "listUnitAttributes";
132     private static final String MARK_LIST_UNIT_USER_ATTRIBUTES = "listUnitUserAttributes";
133     private static final String MARK_MAP_ID_USER_UNIT = "mapIdUserUnit";
134     private static final String MARK_MULTI_AFFECTATION_ENABLED = "multi_affection_enabled";
135     private static final String MARK_FILTER_AFFECTED_USERS = "filterAffectedUsers";
136     private static final String MARK_ADMIN_AVATAR = "adminAvatar";
137 
138     // PARAMETERS
139     private static final String PARAMETER_CANCEL = "cancel";
140     private static final String PARAMETER_ID_UNIT = "idUnit";
141     private static final String PARAMETER_ID_PARENT = "idParent";
142     private static final String PARAMETER_ID_USERS = "idUsers";
143     private static final String PARAMETER_ID_USER = "idUser";
144     private static final String PARAMETER_SESSION = "session";
145     private static final String PARAMETER_SELECT_SUB_UNITS = "selectSubUnits";
146     private static final String PARAMETER_ID_SELECTED_UNIT = "idSelectedUnit";
147     private static final String PARAMETER_FILTER_AFFECTED_USERS = "filterAffectedUsers";
148     private static final String PARAMETER_ID_UNIT_PARENT = "idUnitParent";
149 
150     // TEMPLATES
151     private static final String TEMPLATE_MANAGE_UNITS = "/admin/plugins/unittree/manage_units.html";
152     private static final String TEMPLATE_CREATE_UNIT = "/admin/plugins/unittree/create_unit.html";
153     private static final String TEMPLATE_MODIFY_UNIT = "/admin/plugins/unittree/modify_unit.html";
154     private static final String TEMPLATE_ADD_USERS = "/admin/plugins/unittree/add_users.html";
155     private static final String TEMPLATE_MODIFY_USER = "/admin/plugins/unittree/modify_user.html";
156     private static final String TEMPLATE_MOVE_USER = "/admin/plugins/unittree/move_user.html";
157     private static final String TEMPLATE_MOVE_SUB_UNIT = "admin/plugins/unittree/move_sub_tree.html";
158 
159     // JSP
160     private static final String JSP_MANAGE_UNITS = "ManageUnits.jsp";
161     private static final String JSP_MODIFY_UNIT = "ModifyUnit.jsp";
162     private static final String JSP_MOVE_USER = "MoveUser.jsp";
163     private static final String JSP_URL_MANAGE_UNITS = "jsp/admin/plugins/unittree/ManageUnits.jsp";
164     private static final String JSP_URL_DO_REMOVE_UNIT = "jsp/admin/plugins/unittree/DoRemoveUnit.jsp";
165     private static final String JSP_URL_ADD_USERS = "jsp/admin/plugins/unittree/AddUsers.jsp";
166     private static final String JSP_URL_DO_REMOVE_USER = "jsp/admin/plugins/unittree/DoRemoveUser.jsp";
167 
168     // XSL
169     private static final String UNIT_TREE_XSL_UNIQUE_PREFIX = UniqueIDGenerator.getNewId( ) + "SpacesTree";
170     private static final String XSL_PARAMETER_ID_CURRENT_UNIT = "id-current-unit";
171 
172     // SERVICES
173     private IUnitService _unitService = SpringContextService.getBean( IUnitService.BEAN_UNIT_SERVICE );
174     private IUnitUserService _unitUserService = SpringContextService.getBean( BEAN_UNIT_USER_SERVICE );
175     private IUnitSearchFields _unitUserSearchFields = new UnitUserSearchFields( );
176 
177     private boolean _bAdminAvatar = PluginService.isPluginEnable( "adminavatar" );
178 
179     /**
180      * Get manage units
181      * 
182      * @param request
183      *            the HTTP request
184      * @param response
185      *            the response
186      * @return the HTML code
187      * @throws AccessDeniedException
188      *             exception if there is the user does not have the permission
189      */
190     public IPluginActionResult getManageUnits( HttpServletRequest request, HttpServletResponse response ) throws AccessDeniedException
191     {
192         setPageTitleProperty( PROPERTY_MANAGE_UNITS_PAGE_TITLE );
193 
194         // first - see if there is an invoked action
195         IUnitPluginAction action = PluginActionManager.getPluginAction( request, IUnitPluginAction.class );
196 
197         if ( action != null )
198         {
199             AppLogService.debug( "Processing unittree action " + action.getName( ) );
200 
201             return action.process( request, response, getUser( ), _unitUserSearchFields );
202         }
203 
204         // Get the selected unit
205         String strIdUnit = request.getParameter( PARAMETER_ID_UNIT );
206         Unit unit = null;
207 
208         if ( StringUtils.isNotBlank( strIdUnit ) && StringUtils.isNumeric( strIdUnit ) )
209         {
210             int nIdUnit = Integer.parseInt( strIdUnit );
211             unit = _unitService.getUnit( nIdUnit, false );
212         }
213 
214         if ( unit == null )
215         {
216             unit = _unitService.getRootUnit( false );
217         }
218 
219         // Check if there is some parameters in the session (for user search)
220         if ( request.getParameter( PARAMETER_SESSION ) == null )
221         {
222             reInitSearchFields( request );
223         }
224 
225         // Build the html for units tree
226         String strXmlUnits = _unitService.getXMLUnits( );
227         Source sourceXsl = _unitService.getTreeXsl( );
228         Map<String, String> htXslParameters = new HashMap<String, String>( );
229         htXslParameters.put( XSL_PARAMETER_ID_CURRENT_UNIT, Integer.toString( unit.getIdUnit( ) ) );
230 
231         XmlTransformerService xmlTransformerService = new XmlTransformerService( );
232         String strHtmlUnitsTree = xmlTransformerService.transformBySourceWithXslCache( strXmlUnits, sourceXsl, UNIT_TREE_XSL_UNIQUE_PREFIX, htXslParameters,
233                 null );
234 
235         Map<String, Object> model = new HashMap<String, Object>( );
236 
237         // Add elements for user search form in the model
238         Map<String, Unit> mapIdUserUnit = new HashMap<String, Unit>( );
239         _unitUserSearchFields.setInDepthSearch( request );
240 
241         List<AdminUser> listUsers = _unitUserService.getUsers( unit.getIdUnit( ), mapIdUserUnit, _unitUserSearchFields.isInDepthSearch( ) );
242         String strBaseUrl = AppPathService.getBaseUrl( request ) + JSP_URL_MANAGE_UNITS;
243         _unitUserSearchFields.fillModelForUserSearchForm( listUsers, strBaseUrl, request, model, unit );
244 
245         model.put( MARK_UNIT_TREE, strHtmlUnitsTree );
246         model.put( MARK_UNIT, unit );
247         model.put( MARK_LIST_SUB_UNITS, _unitService.getSubUnits( unit.getIdUnit( ), false ) );
248         model.put( MARK_MAP_ID_USER_UNIT, mapIdUserUnit );
249         model.put( MARK_ADMIN_AVATAR, _bAdminAvatar );
250 
251         // Add actions in the model
252         model.put( MARK_LIST_UNIT_ACTIONS, _unitService.getListActions( UnitAction.ACTION_TYPE, getLocale( ), unit, getUser( ) ) );
253         model.put( MARK_LIST_UNIT_USER_ACTIONS, _unitService.getListActions( UnitUserAction.ACTION_TYPE, getLocale( ), unit, getUser( ) ) );
254         PluginActionManager.fillModel( request, getUser( ), model, IUnitPluginAction.class, MARK_LIST_UNIT_USER_PLUGIN_ACTIONS );
255 
256         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MANAGE_UNITS, getLocale( ), model );
257 
258         IPluginActionResult result = new DefaultPluginActionResult( );
259         result.setHtmlContent( getAdminPage( template.getHtml( ) ) );
260         AdminMessageService.getMessageUrl( request, Messages.USER_ACCESS_DENIED, AdminMessage.TYPE_STOP );
261 
262         return result;
263     }
264 
265     /**
266      * Get create unit
267      * 
268      * @param request
269      *            the HTTP request
270      * @return the HTML code
271      * @throws AccessDeniedException
272      *             exception if the user does not have the permission
273      */
274     public String getCreateUnit( HttpServletRequest request ) throws AccessDeniedException
275     {
276         setPageTitleProperty( PROPERTY_CREATE_UNIT_PAGE_TITLE );
277 
278         Unit unitParent = null;
279         String strIdParent = request.getParameter( PARAMETER_ID_UNIT );
280 
281         if ( StringUtils.isNotBlank( strIdParent ) && StringUtils.isNumeric( strIdParent ) )
282         {
283             int nIdParent = Integer.parseInt( strIdParent );
284             unitParent = _unitService.getUnit( nIdParent, false );
285         }
286 
287         if ( unitParent == null )
288         {
289             unitParent = _unitService.getRootUnit( false );
290         }
291 
292         // Check permissions
293         if ( !_unitService.isAuthorized( unitParent, UnitResourceIdService.PERMISSION_CREATE, getUser( ) )
294                 || !_unitService.canCreateSubUnit( unitParent.getIdUnit( ) ) )
295         {
296             String strErrorMessage = I18nService.getLocalizedString( MESSAGE_ACCESS_DENIED, getLocale( ) );
297             throw new AccessDeniedException( strErrorMessage );
298         }
299 
300         Map<String, Object> model = new HashMap<String, Object>( );
301         model.put( MARK_PARENT_UNIT, unitParent );
302         UnitAttributeManager.fillModel( request, getUser( ), model, MARK_LIST_UNIT_ATTRIBUTES );
303 
304         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_CREATE_UNIT, getLocale( ), model );
305 
306         return getAdminPage( template.getHtml( ) );
307     }
308 
309     /**
310      * Get modify unit
311      * 
312      * @param request
313      *            the HTTP request
314      * @return the HTML code
315      * @throws AccessDeniedException
316      *             exception if the user does not have the permission
317      */
318     public String getModifyUnit( HttpServletRequest request ) throws AccessDeniedException
319     {
320         setPageTitleProperty( PROPERTY_MODIFY_UNIT_PAGE_TITLE );
321 
322         Unit unit = null;
323         String strIdUnit = request.getParameter( PARAMETER_ID_UNIT );
324 
325         if ( StringUtils.isNotBlank( strIdUnit ) && StringUtils.isNumeric( strIdUnit ) )
326         {
327             int nIdUnit = Integer.parseInt( strIdUnit );
328             unit = _unitService.getUnit( nIdUnit, true );
329         }
330 
331         if ( unit == null )
332         {
333             throw new AppException( );
334         }
335 
336         // Check permissions
337         if ( !_unitService.isAuthorized( unit, UnitResourceIdService.PERMISSION_MODIFY, getUser( ) ) )
338         {
339             String strErrorMessage = I18nService.getLocalizedString( MESSAGE_ACCESS_DENIED, getLocale( ) );
340             throw new AccessDeniedException( strErrorMessage );
341         }
342 
343         Unit parentUnit = _unitService.getUnit( unit.getIdParent( ), false );
344 
345         Map<String, Object> model = new HashMap<String, Object>( );
346         model.put( MARK_UNIT, unit );
347         model.put( MARK_PARENT_UNIT, parentUnit );
348         UnitAttributeManager.fillModel( request, getUser( ), model, MARK_LIST_UNIT_ATTRIBUTES );
349 
350         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MODIFY_UNIT, getLocale( ), model );
351 
352         return getAdminPage( template.getHtml( ) );
353     }
354 
355     /**
356      * Get confirm remove unit
357      * 
358      * @param request
359      *            the HTTP request
360      * @return the HTML code
361      */
362     public String getConfirmRemoveUnit( HttpServletRequest request )
363     {
364         String strIdUnit = request.getParameter( PARAMETER_ID_UNIT );
365 
366         if ( StringUtils.isBlank( strIdUnit ) || !StringUtils.isNumeric( strIdUnit ) )
367         {
368             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
369         }
370 
371         // Check permissions
372         if ( !_unitService.isAuthorized( strIdUnit, UnitResourceIdService.PERMISSION_DELETE, getUser( ) ) )
373         {
374             return AdminMessageService.getMessageUrl( request, Messages.USER_ACCESS_DENIED, AdminMessage.TYPE_STOP );
375         }
376 
377         UrlItem url = new UrlItem( JSP_URL_DO_REMOVE_UNIT );
378         url.addParameter( PARAMETER_ID_UNIT, strIdUnit );
379 
380         return AdminMessageService.getMessageUrl( request, MESSAGE_CONFIRM_REMOVE_UNIT, url.getUrl( ), AdminMessage.TYPE_CONFIRMATION );
381     }
382 
383     /**
384      * Get add users
385      * 
386      * @param request
387      *            the HTTP request
388      * @return the HTML code
389      * @throws AccessDeniedException
390      *             exception if the user does not have the permission
391      */
392     public String getAddUsers( HttpServletRequest request ) throws AccessDeniedException
393     {
394         setPageTitleProperty( PROPERTY_ADD_USERS_PAGE_TITLE );
395 
396         // Get the selected unit
397         String strIdUnit = request.getParameter( PARAMETER_ID_UNIT );
398         Unit unit = null;
399 
400         if ( StringUtils.isNotBlank( strIdUnit ) && StringUtils.isNumeric( strIdUnit ) )
401         {
402             int nIdUnit = Integer.parseInt( strIdUnit );
403             unit = _unitService.getUnit( nIdUnit, false );
404         }
405 
406         if ( unit == null )
407         {
408             unit = _unitService.getRootUnit( false );
409         }
410 
411         // Check permissions
412         if ( !_unitService.isAuthorized( unit, UnitResourceIdService.PERMISSION_ADD_USER, getUser( ) ) )
413         {
414             String strErrorMessage = I18nService.getLocalizedString( MESSAGE_ACCESS_DENIED, getLocale( ) );
415             throw new AccessDeniedException( strErrorMessage );
416         }
417 
418         if ( request.getParameter( PARAMETER_SESSION ) == null )
419         {
420             reInitSearchFields( request );
421         }
422 
423         Map<String, Object> model = new HashMap<String, Object>( );
424 
425         boolean bMultiAffectationEnabled = _unitUserService.isMultiAffectationEnabled( );
426         boolean bIncludeMultiAffectedUsers = bMultiAffectationEnabled && !Boolean.valueOf( request.getParameter( PARAMETER_FILTER_AFFECTED_USERS ) );
427         List<AdminUser> listAvailableUsers = _unitUserService.getAvailableUsers( getUser( ), unit.getIdUnit( ), bIncludeMultiAffectedUsers );
428         String strBaseUrl = AppPathService.getBaseUrl( request ) + JSP_URL_ADD_USERS;
429 
430         _unitUserSearchFields.fillModelForUserSearchForm( listAvailableUsers, strBaseUrl, request, model, unit );
431 
432         model.put( MARK_UNIT, unit );
433         model.put( MARK_MULTI_AFFECTATION_ENABLED, bMultiAffectationEnabled );
434         model.put( MARK_FILTER_AFFECTED_USERS, !bIncludeMultiAffectedUsers );
435         model.put( MARK_ADMIN_AVATAR, _bAdminAvatar );
436         UnitUserAttributeManager.fillModel( request, getUser( ), model, MARK_LIST_UNIT_USER_ATTRIBUTES );
437 
438         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_ADD_USERS, getLocale( ), model );
439 
440         return getAdminPage( template.getHtml( ) );
441     }
442 
443     /**
444      * Get modify the user
445      * 
446      * @param request
447      *            the HTTP request
448      * @return the HTML code
449      * @throws AccessDeniedException
450      *             exception if the user does not have the permission
451      */
452     public String getModifyUser( HttpServletRequest request ) throws AccessDeniedException
453     {
454         setPageTitleProperty( PROPERTY_MODIFY_USER_PAGE_TITLE );
455 
456         List<Unit> listUnits = null;
457         AdminUser user = null;
458         String strIdUnit = request.getParameter( PARAMETER_ID_UNIT );
459         String strIdUser = request.getParameter( PARAMETER_ID_USER );
460 
461         if ( StringUtils.isNotBlank( strIdUnit ) && StringUtils.isNumeric( strIdUnit ) && StringUtils.isNotBlank( strIdUser )
462                 && StringUtils.isNumeric( strIdUser ) )
463         {
464             int nIdUser = Integer.parseInt( strIdUser );
465             user = _unitUserService.getUser( nIdUser );
466             listUnits = _unitService.getUnitsByIdUser( nIdUser, false );
467         }
468 
469         if ( ( listUnits == null ) || ( user == null ) )
470         {
471             throw new AppException( );
472         }
473 
474         boolean bPermission = false;
475 
476         for ( Unit unit : listUnits )
477         {
478             bPermission = _unitService.isAuthorized( unit, UnitResourceIdService.PERMISSION_MODIFY_USER, getUser( ) );
479 
480             if ( bPermission )
481             {
482                 break;
483             }
484         }
485 
486         // Check permissions
487         if ( !bPermission )
488         {
489             String strErrorMessage = I18nService.getLocalizedString( MESSAGE_ACCESS_DENIED, getLocale( ) );
490             throw new AccessDeniedException( strErrorMessage );
491         }
492 
493         Map<String, Object> model = new HashMap<String, Object>( );
494         model.put( MARK_UNITS, listUnits );
495         model.put( MARK_USER, user );
496         model.put( MARK_UNIT, strIdUnit );
497         UnitUserAttributeManager.fillModel( request, getUser( ), model, MARK_LIST_UNIT_USER_ATTRIBUTES );
498 
499         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MODIFY_USER, getLocale( ), model );
500 
501         return getAdminPage( template.getHtml( ) );
502     }
503 
504     /**
505      * Get move user
506      * 
507      * @param request
508      *            the HTTP request
509      * @return the HTML code
510      * @throws AccessDeniedException
511      *             exception if the user does not have the permission
512      */
513     public String getMoveUser( HttpServletRequest request ) throws AccessDeniedException
514     {
515         setPageTitleProperty( PROPERTY_MOVE_USER_PAGE_TITLE );
516 
517         Unit unit = null;
518         AdminUser user = null;
519         String strIdUnit = request.getParameter( PARAMETER_ID_UNIT );
520         String strIdUser = request.getParameter( PARAMETER_ID_USER );
521         int nIdUnit = Unit.ID_NULL;
522         int nIdUser = Unit.ID_NULL;
523 
524         if ( StringUtils.isNotBlank( strIdUnit ) && StringUtils.isNumeric( strIdUnit ) && StringUtils.isNotBlank( strIdUser )
525                 && StringUtils.isNumeric( strIdUser ) )
526         {
527             nIdUnit = Integer.parseInt( strIdUnit );
528             unit = _unitService.getUnit( nIdUnit, false );
529             nIdUser = Integer.parseInt( strIdUser );
530             user = _unitUserService.getUser( nIdUser );
531         }
532 
533         if ( ( unit == null ) || ( user == null ) || !_unitUserService.isUserInUnit( nIdUser, unit.getIdUnit( ) ) )
534         {
535             throw new AppException( );
536         }
537 
538         // Check permissions
539         if ( !_unitService.isAuthorized( unit, UnitResourceIdService.PERMISSION_MOVE_USER, getUser( ) ) )
540         {
541             String strErrorMessage = I18nService.getLocalizedString( MESSAGE_ACCESS_DENIED, getLocale( ) );
542             throw new AccessDeniedException( strErrorMessage );
543         }
544 
545         List<Unit> listSubUnits = null;
546         Unit unitParent = null;
547         String strIdSelectedUnit = request.getParameter( PARAMETER_ID_SELECTED_UNIT );
548 
549         if ( StringUtils.isNotBlank( strIdSelectedUnit )
550                 && ( StringUtils.isNumeric( strIdSelectedUnit ) || Integer.toString( Unit.ID_NULL ).equals( strIdSelectedUnit ) ) )
551         {
552             int nIdSelectedUnit = Integer.parseInt( strIdSelectedUnit );
553             Unit selectedUnit = _unitService.getUnit( nIdSelectedUnit, false );
554 
555             if ( selectedUnit != null )
556             {
557                 unitParent = _unitService.getUnit( selectedUnit.getIdParent( ), false );
558             }
559 
560             listSubUnits = _unitService.getSubUnits( nIdSelectedUnit, false );
561         }
562 
563         if ( listSubUnits == null )
564         {
565             // We need to get the unit parent parent
566             unitParent = _unitService.getUnit( unit.getIdParent( ), false );
567 
568             if ( unitParent != null )
569             {
570                 unitParent = _unitService.getUnit( unitParent.getIdParent( ), false );
571             }
572 
573             listSubUnits = _unitService.getSubUnits( unit.getIdParent( ), false );
574         }
575 
576         Map<String, Object> model = new HashMap<String, Object>( );
577 
578         model.put( MARK_UNIT, unit );
579         model.put( MARK_UNIT_PARENT, unitParent );
580         model.put( MARK_USER, user );
581         model.put( MARK_LIST_SUB_UNITS, listSubUnits );
582 
583         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MOVE_USER, getLocale( ), model );
584 
585         return getAdminPage( template.getHtml( ) );
586     }
587 
588     /**
589      * Get confirm remove code mapping
590      * 
591      * @param request
592      *            the HTTP request
593      * @return the HTML code
594      */
595     public String getConfirmRemoveUser( HttpServletRequest request )
596     {
597         String strIdUnit = request.getParameter( PARAMETER_ID_UNIT );
598         String strIdUser = request.getParameter( PARAMETER_ID_USER );
599 
600         if ( StringUtils.isBlank( strIdUnit ) || !StringUtils.isNumeric( strIdUnit ) || StringUtils.isBlank( strIdUser )
601                 || !StringUtils.isNumeric( strIdUser ) )
602         {
603             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
604         }
605 
606         // Check permissions
607         if ( !_unitService.isAuthorized( strIdUnit, UnitResourceIdService.PERMISSION_REMOVE_USER, getUser( ) ) )
608         {
609             return AdminMessageService.getMessageUrl( request, Messages.USER_ACCESS_DENIED, AdminMessage.TYPE_STOP );
610         }
611 
612         UrlItem url = new UrlItem( JSP_URL_DO_REMOVE_USER );
613         url.addParameter( PARAMETER_ID_UNIT, strIdUnit );
614         url.addParameter( PARAMETER_ID_USER, strIdUser );
615 
616         return AdminMessageService.getMessageUrl( request, MESSAGE_CONFIRM_REMOVE_USER, url.getUrl( ), AdminMessage.TYPE_CONFIRMATION );
617     }
618 
619     // DO
620 
621     /**
622      * Do create an unit
623      * 
624      * @param request
625      *            the HTTP request
626      * @return the JSP return
627      */
628     public String doCreateUnit( HttpServletRequest request )
629     {
630         String strCancel = request.getParameter( PARAMETER_CANCEL );
631         String strIdParent = request.getParameter( PARAMETER_ID_PARENT );
632 
633         // The user has clicked on the cancel button => redirect to the manage
634         // page
635         if ( StringUtils.isNotBlank( strCancel ) || StringUtils.isBlank( strIdParent ) || !StringUtils.isNumeric( strIdParent ) )
636         {
637             UrlItem url = new UrlItem( JSP_MANAGE_UNITS );
638             url.addParameter( PARAMETER_ID_UNIT, strIdParent );
639 
640             return url.getUrl( );
641         }
642 
643         int nIdParent = Integer.parseInt( strIdParent );
644 
645         // Check permissions
646         if ( !_unitService.isAuthorized( strIdParent, UnitResourceIdService.PERMISSION_CREATE, getUser( ) ) || !_unitService.canCreateSubUnit( nIdParent ) )
647         {
648             return AdminMessageService.getMessageUrl( request, Messages.USER_ACCESS_DENIED, AdminMessage.TYPE_STOP );
649         }
650 
651         Unitugins/unittree/business/unit/Unit.html#Unit">Unit unit = new Unit( );
652 
653         // Populate the bean
654         ConvertUtils.register( new EnumConverter( ), TypeUnit.class );
655         populate( unit, request );
656 
657         try
658         {
659             UnitAttributeManager.populate( unit, request );
660         }
661         catch( UnitErrorException ue )
662         {
663             return AdminMessageService.getMessageUrl( request, ue.getI18nErrorMessage( ), AdminMessage.TYPE_STOP );
664         }
665 
666         // Check mandatory fields
667         Set<ConstraintViolation<Unit>> constraintViolations = BeanValidationUtil.validate( unit );
668 
669         if ( constraintViolations.size( ) > 0 )
670         {
671             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
672         }
673 
674         try
675         {
676             _unitService.createUnit( unit, request );
677         }
678         catch( Exception ex )
679         {
680             // Something wrong happened... a database check might be needed
681             AppLogService.error( ex.getMessage( ) + " when creating an unit ", ex );
682             // Revert
683             _unitService.removeUnit( unit.getIdUnit( ), request );
684 
685             return AdminMessageService.getMessageUrl( request, MESSAGE_ERROR_GENERIC_MESSAGE, AdminMessage.TYPE_ERROR );
686         }
687 
688         UrlItem url = new UrlItem( JSP_MODIFY_UNIT );
689         url.addParameter( PARAMETER_ID_UNIT, unit.getIdUnit( ) );
690 
691         return url.getUrl( );
692     }
693 
694     /**
695      * Dp modify an unit
696      * 
697      * @param request
698      *            the HTTP request
699      * @return the JSP return
700      */
701     public String doModifyUnit( HttpServletRequest request )
702     {
703         String strCancel = request.getParameter( PARAMETER_CANCEL );
704         String strIdUnit = request.getParameter( PARAMETER_ID_UNIT );
705 
706         // The user has clicked on the cancel button => redirect to the manage
707         // page
708         if ( StringUtils.isNotBlank( strCancel ) )
709         {
710             UrlItem url = new UrlItem( JSP_MANAGE_UNITS );
711             url.addParameter( PARAMETER_ID_UNIT, strIdUnit );
712 
713             return url.getUrl( );
714         }
715 
716         if ( StringUtils.isBlank( strIdUnit ) || !StringUtils.isNumeric( strIdUnit ) )
717         {
718             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
719         }
720 
721         int nIdUnit = Integer.parseInt( strIdUnit );
722 
723         // Do no get the sectors because the list will be deleted, and we store
724         // the new id sectors
725         Unit unit = _unitService.getUnit( nIdUnit, false );
726 
727         if ( unit == null )
728         {
729             return AdminMessageService.getMessageUrl( request, MESSAGE_ERROR_UNIT_NOT_FOUND, AdminMessage.TYPE_STOP );
730         }
731 
732         // Check permissions
733         if ( !_unitService.isAuthorized( unit, UnitResourceIdService.PERMISSION_MODIFY, getUser( ) ) )
734         {
735             return AdminMessageService.getMessageUrl( request, Messages.USER_ACCESS_DENIED, AdminMessage.TYPE_STOP );
736         }
737 
738         // Populate the bean
739         populate( unit, request );
740 
741         try
742         {
743             UnitAttributeManager.populate( unit, request );
744         }
745         catch( UnitErrorException ue )
746         {
747             return AdminMessageService.getMessageUrl( request, ue.getI18nErrorMessage( ), AdminMessage.TYPE_STOP );
748         }
749 
750         // Check mandatory fields
751         Set<ConstraintViolation<Unit>> constraintViolations = BeanValidationUtil.validate( unit );
752 
753         if ( constraintViolations.size( ) > 0 )
754         {
755             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
756         }
757 
758         try
759         {
760             _unitService.updateUnit( unit, request );
761         }
762         catch( Exception ex )
763         {
764             // Something wrong happened... a database check might be needed
765             AppLogService.error( ex.getMessage( ) + " when modifying an unit ", ex );
766 
767             return AdminMessageService.getMessageUrl( request, MESSAGE_ERROR_GENERIC_MESSAGE, AdminMessage.TYPE_ERROR );
768         }
769 
770         UrlItem url = new UrlItem( JSP_MODIFY_UNIT );
771         url.addParameter( PARAMETER_ID_UNIT, unit.getIdUnit( ) );
772 
773         return url.getUrl( );
774     }
775 
776     /**
777      * Do remove an unit
778      * 
779      * @param request
780      *            the HTTP request
781      * @return the JSP return
782      */
783     public String doRemoveUnit( HttpServletRequest request )
784     {
785         String strIdUnit = request.getParameter( PARAMETER_ID_UNIT );
786 
787         if ( StringUtils.isBlank( strIdUnit ) || !StringUtils.isNumeric( strIdUnit ) )
788         {
789             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
790         }
791 
792         // Check permissions
793         if ( !_unitService.isAuthorized( strIdUnit, UnitResourceIdService.PERMISSION_DELETE, getUser( ) ) )
794         {
795             return AdminMessageService.getMessageUrl( request, Messages.USER_ACCESS_DENIED, AdminMessage.TYPE_STOP );
796         }
797 
798         int nIdUnit = Integer.parseInt( strIdUnit );
799         int nIdParent = Unit.ID_ROOT;
800         Unit unit = _unitService.getUnit( nIdUnit, false );
801 
802         if ( unit != null )
803         {
804             if ( _unitService.hasSubUnits( nIdUnit ) )
805             {
806                 return AdminMessageService.getMessageUrl( request, MESSAGE_ERROR_UNIT_HAS_SUB_UNITS, AdminMessage.TYPE_STOP );
807             }
808 
809             nIdParent = unit.getIdParent( );
810 
811             try
812             {
813                 _unitService.removeUnit( nIdUnit, request );
814             }
815             catch( Exception ex )
816             {
817                 // Something wrong happened... a database check might be needed
818                 AppLogService.error( ex.getMessage( ) + " when deleting an unit ", ex );
819 
820                 return AdminMessageService.getMessageUrl( request, MESSAGE_ERROR_GENERIC_MESSAGE, AdminMessage.TYPE_ERROR );
821             }
822         }
823 
824         UrlItem url = new UrlItem( JSP_MANAGE_UNITS );
825         url.addParameter( PARAMETER_ID_UNIT, nIdParent );
826 
827         return url.getUrl( );
828     }
829 
830     /**
831      * Dp add users
832      * 
833      * @param request
834      *            the HTTP request
835      * @return the JSP return
836      */
837     public String doAddUsers( HttpServletRequest request )
838     {
839         String strCancel = request.getParameter( PARAMETER_CANCEL );
840         String strIdUnit = request.getParameter( PARAMETER_ID_UNIT );
841 
842         if ( StringUtils.isNotBlank( strCancel ) )
843         {
844             UrlItem url = new UrlItem( JSP_MANAGE_UNITS );
845             url.addParameter( PARAMETER_ID_UNIT, strIdUnit );
846 
847             return url.getUrl( );
848         }
849 
850         if ( !_unitService.isAuthorized( strIdUnit, UnitResourceIdService.PERMISSION_ADD_USER, getUser( ) ) )
851         {
852             return AdminMessageService.getMessageUrl( request, Messages.USER_ACCESS_DENIED, AdminMessage.TYPE_STOP );
853         }
854 
855         String [ ] listIdUsers = request.getParameterValues( PARAMETER_ID_USERS );
856 
857         if ( ( listIdUsers == null ) || ( listIdUsers.length == 0 ) || StringUtils.isBlank( strIdUnit ) || !StringUtils.isNumeric( strIdUnit ) )
858         {
859             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
860         }
861 
862         int nIdUnit = Integer.parseInt( strIdUnit );
863 
864         for ( String strIdUser : listIdUsers )
865         {
866             if ( StringUtils.isNotBlank( strIdUser ) && StringUtils.isNumeric( strIdUser ) )
867             {
868                 int nIdUser = Integer.parseInt( strIdUser );
869 
870                 if ( _unitUserService.isUserInUnit( nIdUser, nIdUnit ) )
871                 {
872                     return AdminMessageService.getMessageUrl( request, MESSAGE_ERROR_USER_ALREADY_IN_UNIT, AdminMessage.TYPE_STOP );
873                 }
874 
875                 _unitUserService.doProcessAddUser( nIdUser, getUser( ), request );
876                 _unitUserService.addUserToUnit( nIdUnit, nIdUser );
877             }
878         }
879 
880         UrlItem url = new UrlItem( JSP_MANAGE_UNITS );
881         url.addParameter( PARAMETER_ID_UNIT, nIdUnit );
882 
883         return url.getUrl( );
884     }
885 
886     /**
887      * Do modify user
888      * 
889      * @param request
890      *            the HTTP request
891      * @return the JSP return
892      */
893     public String doModifyUser( HttpServletRequest request )
894     {
895         String strIdUnit = request.getParameter( PARAMETER_ID_UNIT );
896         String strIdUser = request.getParameter( PARAMETER_ID_USER );
897 
898         if ( StringUtils.isBlank( strIdUnit ) || !StringUtils.isNumeric( strIdUnit ) || StringUtils.isBlank( strIdUser )
899                 || !StringUtils.isNumeric( strIdUser ) )
900         {
901             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
902         }
903 
904         // Check permissions
905         if ( !_unitService.isAuthorized( strIdUnit, UnitResourceIdService.PERMISSION_MODIFY_USER, getUser( ) ) )
906         {
907             return AdminMessageService.getMessageUrl( request, Messages.USER_ACCESS_DENIED, AdminMessage.TYPE_STOP );
908         }
909 
910         int nIdUnit = Integer.parseInt( strIdUnit );
911         int nIdUser = Integer.parseInt( strIdUser );
912         Unit unit = _unitService.getUnit( nIdUnit, false );
913         AdminUser user = _unitUserService.getUser( nIdUser );
914 
915         if ( ( unit != null ) && ( user != null ) )
916         {
917             _unitUserService.doProcessModifyUser( nIdUser, getUser( ), request );
918         }
919 
920         UrlItem url = new UrlItem( JSP_MANAGE_UNITS );
921         url.addParameter( PARAMETER_ID_UNIT, nIdUnit );
922 
923         return url.getUrl( );
924     }
925 
926     /**
927      * Do move user
928      * 
929      * @param request
930      *            the HTTP request
931      * @return the JSP return
932      */
933     public String doMoveUser( HttpServletRequest request )
934     {
935         String strCancel = request.getParameter( PARAMETER_CANCEL );
936         String strIdUnit = request.getParameter( PARAMETER_ID_UNIT );
937         String strIdSelectedUnit = request.getParameter( PARAMETER_ID_SELECTED_UNIT );
938 
939         if ( StringUtils.isNotBlank( strCancel ) )
940         {
941             UrlItem url = new UrlItem( JSP_MANAGE_UNITS );
942             url.addParameter( PARAMETER_ID_UNIT, strIdUnit );
943 
944             return url.getUrl( );
945         }
946 
947         String strIdUser = request.getParameter( PARAMETER_ID_USER );
948 
949         if ( StringUtils.isBlank( strIdUnit ) || !StringUtils.isNumeric( strIdUnit ) || StringUtils.isBlank( strIdSelectedUnit )
950                 || ( !StringUtils.isNumeric( strIdSelectedUnit ) && !Integer.toString( Unit.ID_NULL ).equals( strIdSelectedUnit ) )
951                 || StringUtils.isBlank( strIdUser ) || !StringUtils.isNumeric( strIdUser ) )
952         {
953             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
954         }
955 
956         // Check permissions
957         if ( !_unitService.isAuthorized( strIdUnit, UnitResourceIdService.PERMISSION_MOVE_USER, getUser( ) ) )
958         {
959             return AdminMessageService.getMessageUrl( request, Messages.USER_ACCESS_DENIED, AdminMessage.TYPE_STOP );
960         }
961 
962         // Check if the user has clicked on "selectSubUnits" => display the same
963         // page with the sub units
964         String strSelectSubUnits = request.getParameter( PARAMETER_SELECT_SUB_UNITS );
965         int nIdSelectedUnit = Integer.parseInt( strIdSelectedUnit );
966 
967         if ( StringUtils.isNotBlank( strSelectSubUnits ) )
968         {
969             // Check if the selected unit has sub units
970             List<Unit> listSubUnits = _unitService.getSubUnits( nIdSelectedUnit, false );
971 
972             if ( ( listSubUnits == null ) || listSubUnits.isEmpty( ) )
973             {
974                 return AdminMessageService.getMessageUrl( request, MESSAGE_ERROR_NO_SUB_UNITS, AdminMessage.TYPE_STOP );
975             }
976 
977             UrlItem url = new UrlItem( JSP_MOVE_USER );
978             url.addParameter( PARAMETER_ID_USER, strIdUser );
979             url.addParameter( PARAMETER_ID_UNIT, strIdUnit );
980             url.addParameter( PARAMETER_ID_SELECTED_UNIT, strIdSelectedUnit );
981 
982             return url.getUrl( );
983         }
984 
985         // The user must have the permission to move on both units (from and to)
986         if ( !_unitService.isAuthorized( strIdSelectedUnit, UnitResourceIdService.PERMISSION_MOVE_USER, getUser( ) ) )
987         {
988             return AdminMessageService.getMessageUrl( request, Messages.USER_ACCESS_DENIED, AdminMessage.TYPE_STOP );
989         }
990 
991         int nIdUser = Integer.parseInt( strIdUser );
992         int nIdUnit = Integer.parseInt( strIdUnit );
993         AdminUser user = _unitUserService.getUser( nIdUser );
994         Unit unit = _unitService.getUnit( nIdUnit, false );
995         Unit selectedUnit = _unitService.getUnit( nIdSelectedUnit, false );
996 
997         if ( ( user != null ) && ( unit != null ) && ( selectedUnit != null ) )
998         {
999             try
1000             {
1001                 // Remove the user from the unit
1002                 _unitUserService.removeUserFromUnit( nIdUser, nIdUnit );
1003                 _unitUserService.doProcessRemoveUser( nIdUser, getUser( ), request );
1004                 // Then add the user to the new unit
1005                 _unitUserService.addUserToUnit( nIdSelectedUnit, nIdUser );
1006                 _unitUserService.doProcessAddUser( nIdUser, getUser( ), request );
1007             }
1008             catch( Exception ex )
1009             {
1010                 // Something wrong happened... a database check might be needed
1011                 AppLogService.error( ex.getMessage( ) + " when deleting an unit ", ex );
1012 
1013                 return AdminMessageService.getMessageUrl( request, MESSAGE_ERROR_GENERIC_MESSAGE, AdminMessage.TYPE_ERROR );
1014             }
1015         }
1016 
1017         UrlItem url = new UrlItem( JSP_MANAGE_UNITS );
1018         url.addParameter( PARAMETER_ID_UNIT, nIdUnit );
1019 
1020         return url.getUrl( );
1021     }
1022 
1023     /**
1024      * Do remove user
1025      * 
1026      * @param request
1027      *            the HTTP request
1028      * @return the JSP return
1029      */
1030     public String doRemoveUser( HttpServletRequest request )
1031     {
1032         String strIdUnit = request.getParameter( PARAMETER_ID_UNIT );
1033         String strIdUser = request.getParameter( PARAMETER_ID_USER );
1034 
1035         if ( StringUtils.isBlank( strIdUnit ) || !StringUtils.isNumeric( strIdUnit ) || StringUtils.isBlank( strIdUser )
1036                 || !StringUtils.isNumeric( strIdUser ) )
1037         {
1038             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
1039         }
1040 
1041         // Check permissions
1042         if ( !_unitService.isAuthorized( strIdUnit, UnitResourceIdService.PERMISSION_REMOVE_USER, getUser( ) ) )
1043         {
1044             return AdminMessageService.getMessageUrl( request, Messages.USER_ACCESS_DENIED, AdminMessage.TYPE_STOP );
1045         }
1046 
1047         int nIdUnit = Integer.parseInt( strIdUnit );
1048         int nIdUser = Integer.parseInt( strIdUser );
1049         Unit unit = _unitService.getUnit( nIdUnit, false );
1050         AdminUser user = _unitUserService.getUser( nIdUser );
1051 
1052         if ( ( unit != null ) && ( user != null ) )
1053         {
1054             try
1055             {
1056                 _unitUserService.removeUserFromUnit( nIdUser, nIdUnit );
1057                 _unitUserService.doProcessRemoveUser( nIdUser, getUser( ), request );
1058             }
1059             catch( Exception ex )
1060             {
1061                 // Something wrong happened... a database check might be needed
1062                 AppLogService.error( ex.getMessage( ) + " when deleting an unit ", ex );
1063 
1064                 return AdminMessageService.getMessageUrl( request, MESSAGE_ERROR_GENERIC_MESSAGE, AdminMessage.TYPE_ERROR );
1065             }
1066         }
1067 
1068         UrlItem url = new UrlItem( JSP_MANAGE_UNITS );
1069         url.addParameter( PARAMETER_ID_UNIT, nIdUnit );
1070 
1071         return url.getUrl( );
1072     }
1073 
1074     /**
1075      * Get a page to change the parent of a unit
1076      * 
1077      * @param request
1078      *            The request
1079      * @return The HTML code to display
1080      * @throws AccessDeniedException
1081      *             if the user is not allowed to access the feature
1082      */
1083     public String getMoveSubTree( HttpServletRequest request ) throws AccessDeniedException
1084     {
1085         setPageTitleProperty( PROPERTY_MOVE_UNIT_PAGE_TITLE );
1086 
1087         String strIdUnitToMove = request.getParameter( PARAMETER_ID_UNIT );
1088         String strIdUnit = request.getParameter( PARAMETER_ID_UNIT_PARENT );
1089         Unit unitToMove = _unitService.getUnit( Integer.parseInt( strIdUnitToMove ), false );
1090         int nIdUnit = Unit.ID_NULL;
1091 
1092         if ( StringUtils.isNotBlank( strIdUnit ) )
1093         {
1094             nIdUnit = Integer.parseInt( strIdUnit );
1095         }
1096         else
1097         {
1098             nIdUnit = unitToMove.getIdParent( );
1099         }
1100 
1101         Unit unit = _unitService.getUnit( nIdUnit, false );
1102 
1103         // Check permissions
1104         if ( !_unitService.isAuthorized( unitToMove, UnitResourceIdService.PERMISSION_MOVE_UNIT, getUser( ) ) )
1105         {
1106             String strErrorMessage = I18nService.getLocalizedString( MESSAGE_ACCESS_DENIED, getLocale( ) );
1107             throw new AccessDeniedException( strErrorMessage );
1108         }
1109 
1110         List<Unit> listSubUnits = null;
1111 
1112         if ( unit == null )
1113         {
1114             Unit rootUnit = _unitService.getRootUnit( false );
1115             listSubUnits = new ArrayList<Unit>( );
1116 
1117             if ( unitToMove.getIdUnit( ) != rootUnit.getIdUnit( ) )
1118             {
1119                 listSubUnits.add( rootUnit );
1120             }
1121         }
1122         else
1123         {
1124             listSubUnits = _unitService.getSubUnits( unit.getIdUnit( ), false );
1125 
1126             // We check that the unit to move is not contained in the sub unit
1127             // list.
1128             for ( Unit subUnit : listSubUnits )
1129             {
1130                 if ( subUnit.getIdUnit( ) == unitToMove.getIdUnit( ) )
1131                 {
1132                     listSubUnits.remove( subUnit );
1133 
1134                     break;
1135                 }
1136             }
1137         }
1138 
1139         Map<String, Object> model = new HashMap<String, Object>( );
1140 
1141         model.put( MARK_UNIT_TO_MOVE, unitToMove );
1142         model.put( MARK_UNIT, unit );
1143         model.put( MARK_LIST_SUB_UNITS, listSubUnits );
1144 
1145         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MOVE_SUB_UNIT, getLocale( ), model );
1146 
1147         return getAdminPage( template.getHtml( ) );
1148     }
1149 
1150     /**
1151      * Do change the parent of a unit
1152      * 
1153      * @param request
1154      *            The request
1155      * @return The next URL to display
1156      */
1157     public String doMoveSubTree( HttpServletRequest request )
1158     {
1159         String strCancel = request.getParameter( PARAMETER_CANCEL );
1160         String strIdUnitToMove = request.getParameter( PARAMETER_ID_UNIT );
1161 
1162         if ( StringUtils.isNotEmpty( strCancel ) )
1163         {
1164             UrlItem urlItem = new UrlItem( AppPathService.getBaseUrl( request ) + JSP_URL_MANAGE_UNITS );
1165             urlItem.addParameter( PARAMETER_ID_UNIT, strIdUnitToMove );
1166 
1167             return urlItem.getUrl( );
1168         }
1169 
1170         String strIdUnitParent = request.getParameter( PARAMETER_ID_UNIT_PARENT );
1171 
1172         int nIdUnitToMove = Integer.parseInt( strIdUnitToMove );
1173         int nIdUnitParent = Integer.parseInt( strIdUnitParent );
1174         Unit unitToMove = _unitService.getUnit( nIdUnitToMove, false );
1175         Unit unitParent = _unitService.getUnit( nIdUnitParent, false );
1176 
1177         if ( _unitService.moveSubTree( unitToMove, unitParent ) )
1178         {
1179             UrlItem urlItem = new UrlItem( JSP_URL_MANAGE_UNITS );
1180             urlItem.addParameter( PARAMETER_ID_UNIT, unitToMove.getIdUnit( ) );
1181 
1182             return AdminMessageService.getMessageUrl( request, MESSAGE_SUB_TREE_MOVED, urlItem.getUrl( ), AdminMessage.TYPE_INFO );
1183         }
1184 
1185         return AdminMessageService.getMessageUrl( request, MESSAGE_CANT_MOVE_SUB_TREE_TO_CHILD, AdminMessage.TYPE_ERROR );
1186     }
1187 
1188     // PRIVATE METHODS
1189 
1190     /**
1191      * Reinit the search fields
1192      * 
1193      * @param request
1194      *            the HTTP request
1195      */
1196     private void reInitSearchFields( HttpServletRequest request )
1197     {
1198         _unitUserSearchFields = new UnitUserSearchFields( request );
1199     }
1200 }