Downloads Documentation Community Contribute Demo






Show Sidebar
Login | Register

Ticket #866: RoleListController_delete.patch

File RoleListController_delete.patch, 3.3 kB (added by dkayiwa, 7 months ago)

patch for displaying a user friendly messages for exceptions thro.wn when attempting to delete roles

  • openmrs/trunk/src/web/org/openmrs/web/controller/user/RoleListController.java

    old new  
    3434import org.openmrs.web.WebConstants; 
    3535import org.springframework.beans.propertyeditors.CustomNumberEditor; 
    3636import org.springframework.context.support.MessageSourceAccessor; 
     37import org.springframework.dao.DataIntegrityViolationException; 
    3738import org.springframework.validation.BindException; 
    3839import org.springframework.web.bind.ServletRequestDataBinder; 
    3940import org.springframework.web.bind.ServletRequestUtils; 
     
    7374                Locale locale = request.getLocale(); 
    7475                String view = getFormView(); 
    7576                if (Context.isAuthenticated()) { 
    76                         String[] roleList = ServletRequestUtils.getStringParameters(request, "roleId"); 
    77                         AdministrationService as = Context.getAdministrationService(); 
    78                         UserService us = Context.getUserService(); 
    79                          
    8077                        String success = ""; 
    8178                        String error = ""; 
    8279                         
    8380                        MessageSourceAccessor msa = getMessageSourceAccessor(); 
    84                         String deleted = msa.getMessage("general.deleted"); 
    85                         String notDeleted = msa.getMessage("general.cannot.delete"); 
    86                         for (String p : roleList) { 
    87                                 //TODO convenience method deleteRole(String) ?? 
    88                                 try { 
    89                                         as.deleteRole(us.getRole(p)); 
    90                                         if (!success.equals("")) success += "<br/>"; 
    91                                         success += p + " " + deleted; 
     81 
     82                        String[] roleList = ServletRequestUtils.getStringParameters(request, "roleId"); 
     83                        if(roleList != null){ 
     84                                AdministrationService as = Context.getAdministrationService(); 
     85                                UserService us = Context.getUserService(); 
     86                                 
     87                                String deleted = msa.getMessage("general.deleted"); 
     88                                String notDeleted = msa.getMessage("Role.cannot.delete"); 
     89                                for (String p : roleList) { 
     90                                        //TODO convenience method deleteRole(String) ?? 
     91                                        try { 
     92                                                as.deleteRole(us.getRole(p)); 
     93                                                if (!success.equals("")) success += "<br/>"; 
     94                                                success += p + " " + deleted; 
     95                                        } 
     96                                        catch(DataIntegrityViolationException e){ 
     97                                                error = handleRoleIntegrityException(e,error,notDeleted); 
     98                                        } 
     99                                        catch (APIException e) { 
     100                                                error = handleRoleIntegrityException(e,error,notDeleted); 
     101                                        } 
    92102                                } 
    93                                 catch (APIException e) { 
    94                                         log.warn(e); 
    95                                         if (!error.equals("")) error += "<br/>"; 
    96                                         error += p + " " + notDeleted; 
    97                                 } 
    98103                        } 
     104                        else 
     105                                error = msa.getMessage("Role.select"); 
    99106                         
    100107                        view = getSuccessView(); 
    101108                        if (!success.equals("")) 
     
    106113                         
    107114                return new ModelAndView(new RedirectView(view)); 
    108115        } 
     116         
     117        /** 
     118         *  
     119         * Logs a role delete data integrity violation exception and  
     120         * returns a user friedly message of the problem that occured. 
     121         *  
     122         * @param e the exception. 
     123         * @param error the error message. 
     124         * @param notDeleted the role not deleted error message. 
     125         * @return the formatted error message. 
     126         */ 
     127        private String handleRoleIntegrityException(Exception e,String error,String notDeleted){ 
     128                log.warn("Error deleting role", e); 
     129                if (!error.equals("")) error += "<br/>"; 
     130                error += notDeleted; 
     131                return error; 
     132        } 
    109133 
    110134        /** 
    111135         *