Downloads Documentation Community Contribute Demo






Show Sidebar
Login | Register

Ticket #866: PatientIdentifierTypeListController.patch

File PatientIdentifierTypeListController.patch, 3.0 kB (added by dkayiwa, 7 months ago)

patch for displaying a user friendly messages for exceptions thro.wn when attempting to delete patient identifier types

  • openmrs/trunk/src/web/org/openmrs/web/controller/patient/PatientIdentifierTypeListController.java

    old new  
    3131import org.openmrs.web.WebConstants; 
    3232import org.springframework.beans.propertyeditors.CustomNumberEditor; 
    3333import org.springframework.context.support.MessageSourceAccessor; 
     34import org.springframework.dao.DataIntegrityViolationException; 
    3435import org.springframework.validation.BindException; 
    3536import org.springframework.web.bind.ServletRequestDataBinder; 
    3637import org.springframework.web.servlet.ModelAndView; 
     
    6869                 
    6970                String view = getFormView(); 
    7071                if (Context.isAuthenticated()) { 
    71                          
     72                        String success = ""; 
     73                        String error = ""; 
     74 
     75                        MessageSourceAccessor msa = getMessageSourceAccessor(); 
     76 
    7277                        String[] identifierTypeList = request.getParameterValues("patientIdentifierTypeId"); 
     78                        if(identifierTypeList != null){ 
    7379                        AdministrationService as = Context.getAdministrationService(); 
    7480                        PatientService ps = Context.getPatientService(); 
    7581                         
    76                         String success = ""; 
    77                         String error = ""; 
    78  
    79                         MessageSourceAccessor msa = getMessageSourceAccessor(); 
    8082                        String deleted = msa.getMessage("general.deleted"); 
    81                         String notDeleted = msa.getMessage("general.cannot.delete"); 
     83                        String notDeleted = msa.getMessage("PatientIdentifierType.cannot.delete"); 
    8284                        for (String p : identifierTypeList) { 
    8385                                try { 
    8486                                        as.deletePatientIdentifierType(ps.getPatientIdentifierType(Integer.valueOf(p))); 
    8587                                        if (!success.equals("")) success += "<br/>"; 
    8688                                        success += p + " " + deleted; 
    8789                                } 
     90                                catch(DataIntegrityViolationException e){ 
     91                                        error = handleIdentifierIntegrityException(e,error,notDeleted); 
     92                                } 
    8893                                catch (APIException e) { 
    89                                         log.warn("Error deleting patient identifier type", e); 
    90                                         if (!error.equals("")) error += "<br/>"; 
    91                                         error += p + " " + notDeleted; 
     94                                        error = handleIdentifierIntegrityException(e,error,notDeleted); 
    9295                                } 
    9396                        } 
     97                        } 
     98                        else 
     99                                error = msa.getMessage("PatientIdentifierType.select"); 
    94100                         
    95101                        view = getSuccessView(); 
    96102                        if (!success.equals("")) 
     
    101107                 
    102108                return new ModelAndView(new RedirectView(view)); 
    103109        } 
     110         
     111        /** 
     112         *  
     113         * Logs a Patient Identifier Type delete data integrity violation exception and  
     114         * returns a user friedly message of the problem that occured. 
     115         *  
     116         * @param e the exception. 
     117         * @param error the error message. 
     118         * @param notDeleted the role not deleted error message. 
     119         * @return the formatted error message. 
     120         */ 
     121        private String handleIdentifierIntegrityException(Exception e,String error,String notDeleted){ 
     122                log.warn("Error deleting patient identifier type", e); 
     123                if (!error.equals("")) error += "<br/>"; 
     124                error += notDeleted; 
     125                return error; 
     126        } 
    104127 
    105128        /** 
    106129         *