Downloads Documentation Community Contribute Demo






Show Sidebar
Login | Register

Ticket #866: MimeTypeListController_delete.patch

File MimeTypeListController_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 mime types

  • openmrs/trunk/src/web/org/openmrs/web/controller/observation/MimeTypeListController.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[] mimeTypeList = request.getParameterValues("mimeTypeId"); 
    74                         AdministrationService as = Context.getAdministrationService(); 
    75                         ObsService os = Context.getObsService(); 
    76                          
    7774                        String success = ""; 
    7875                        String error = ""; 
    7976                         
    8077                        MessageSourceAccessor msa = getMessageSourceAccessor(); 
    81                         String deleted = msa.getMessage("general.deleted"); 
    82                         String notDeleted = msa.getMessage("general.cannot.delete"); 
    83                         for (String m : mimeTypeList) { 
    84                                 try { 
    85                                         as.deleteMimeType(os.getMimeType(Integer.valueOf(m))); 
    86                                         if (!success.equals("")) success += "<br/>"; 
    87                                         success += m + " " + deleted; 
     78 
     79                        String[] mimeTypeList = request.getParameterValues("mimeTypeId"); 
     80                        if(mimeTypeList != null){ 
     81                                AdministrationService as = Context.getAdministrationService(); 
     82                                ObsService os = Context.getObsService(); 
     83                                 
     84                                String deleted = msa.getMessage("general.deleted"); 
     85                                String notDeleted = msa.getMessage("MimeType.cannot.delete"); 
     86                                for (String m : mimeTypeList) { 
     87                                        try { 
     88                                                as.deleteMimeType(os.getMimeType(Integer.valueOf(m))); 
     89                                                if (!success.equals("")) success += "<br/>"; 
     90                                                success += m + " " + deleted; 
     91                                        } 
     92                                        catch(DataIntegrityViolationException e){ 
     93                                                error = handleMimeTypeIntegrityException(e,error,notDeleted); 
     94                                        } 
     95                                        catch (APIException e) { 
     96                                                error = handleMimeTypeIntegrityException(e,error,notDeleted); 
     97                                        } 
    8898                                } 
    89                                 catch (APIException e) { 
    90                                         log.warn("Error deleting mime type", e); 
    91                                         if (!error.equals("")) error += "<br/>"; 
    92                                         error += m + " " + notDeleted; 
    93                                 } 
    9499                        } 
     100                        else 
     101                                error = msa.getMessage("MimeType.select"); 
    95102                         
    96103                        view = getSuccessView(); 
    97104                        if (!success.equals("")) 
     
    105112 
    106113        /** 
    107114         *  
     115         * Logs a mime type 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 handleMimeTypeIntegrityException(Exception e,String error,String notDeleted){ 
     124                log.warn("Error deleting mime type", e); 
     125                if (!error.equals("")) error += "<br/>"; 
     126                error += notDeleted; 
     127                return error; 
     128        } 
     129         
     130        /** 
     131         *  
    108132         * This is called prior to displaying a form for the first time.  It tells Spring 
    109133         *   the form/command object to load into the request 
    110134         *