Downloads Documentation Community Contribute Demo






Show Sidebar
Login | Register

Ticket #866: PrivilegeListController_delete.patch

File PrivilegeListController_delete.patch, 3.4 kB (added by dkayiwa, 7 months ago)

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

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

    old new  
    3232import org.openmrs.web.WebConstants; 
    3333import org.springframework.beans.propertyeditors.CustomNumberEditor; 
    3434import org.springframework.context.support.MessageSourceAccessor; 
     35import org.springframework.dao.DataIntegrityViolationException; 
    3536import org.springframework.validation.BindException; 
    3637import org.springframework.web.bind.ServletRequestDataBinder; 
    3738import org.springframework.web.servlet.ModelAndView; 
     
    7071                //Locale locale = request.getLocale(); 
    7172                String view = getFormView(); 
    7273                if (Context.isAuthenticated()) { 
    73                         String[] privilegeList = request.getParameterValues("privilegeId"); 
    74                         AdministrationService as = Context.getAdministrationService(); 
    75                         UserService us = Context.getUserService(); 
    7674                        String success = ""; 
    7775                        String error = ""; 
    7876 
    7977                        MessageSourceAccessor msa = getMessageSourceAccessor(); 
    80                         String deleted = msa.getMessage("general.deleted"); 
    81                         String notDeleted = msa.getMessage("general.cannot.delete"); 
    82                         for (String p : privilegeList) { 
    83                                 //TODO convenience method deletePrivilege(String) ?? 
    84                                 try { 
    85                                         as.deletePrivilege(us.getPrivilege(p)); 
    86                                         if (!success.equals("")) success += "<br/>"; 
    87                                         success += p + " " + deleted; 
     78 
     79                        String[] privilegeList = request.getParameterValues("privilegeId"); 
     80                        if(privilegeList != null){ 
     81                                AdministrationService as = Context.getAdministrationService(); 
     82                                UserService us = Context.getUserService(); 
     83                                String deleted = msa.getMessage("general.deleted"); 
     84                                String notDeleted = msa.getMessage("Privilege.cannot.delete"); 
     85                                for (String p : privilegeList) { 
     86                                        //TODO convenience method deletePrivilege(String) ?? 
     87                                        try { 
     88                                                as.deletePrivilege(us.getPrivilege(p)); 
     89                                                if (!success.equals("")) success += "<br/>"; 
     90                                                success += p + " " + deleted; 
     91                                        } 
     92                                        catch(DataIntegrityViolationException e){ 
     93                                                error = handlePrivilegeIntegrityException(e,error,notDeleted); 
     94                                        } 
     95                                        catch (APIException e) { 
     96                                                error = handlePrivilegeIntegrityException(e,error,notDeleted); 
     97                                        } 
    8898                                } 
    89                                 catch (APIException e) { 
    90                                         log.warn("Error deleting privielge", e); 
    91                                         if (!error.equals("")) error += "<br/>"; 
    92                                         error += p + " " + notDeleted; 
    93                                 } 
    9499                        } 
     100                        else 
     101                                error = msa.getMessage("Privilege.select"); 
    95102                         
    96103                        view = getSuccessView(); 
    97104                        if (!success.equals("")) 
     
    102109                         
    103110                return new ModelAndView(new RedirectView(view)); 
    104111        } 
     112         
     113        /** 
     114         *  
     115         * Logs a privielge delete data integrity violation exception and  
     116         * returns a user friedly message of the problem that occured. 
     117         *  
     118         * @param e the exception. 
     119         * @param error the error message. 
     120         * @param notDeleted the not deleted error message. 
     121         * @return the formatted error message. 
     122         */ 
     123        private String handlePrivilegeIntegrityException(Exception e,String error,String notDeleted){ 
     124                log.warn("Error deleting privilege", e); 
     125                if (!error.equals("")) error += "<br/>"; 
     126                error += notDeleted; 
     127                return error; 
     128        } 
    105129 
    106130        /** 
    107131         *