Downloads Documentation Community Contribute Demo






Show Sidebar
Login | Register

Ticket #866: LocationListController_delete.patch

File LocationListController_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 locations

  • openmrs/trunk/src/web/org/openmrs/web/controller/encounter/LocationListController.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                         String[] locationList = request.getParameterValues("locationId"); 
    72                         AdministrationService as = Context.getAdministrationService(); 
    73                         EncounterService es = Context.getEncounterService(); 
    74                          
    7572                        String success = ""; 
    7673                        String error = ""; 
    7774                         
    7875                        MessageSourceAccessor msa = getMessageSourceAccessor(); 
    79                         String deleted = msa.getMessage("general.deleted"); 
    80                         String notDeleted = msa.getMessage("general.cannot.delete"); 
    81                         for (String p : locationList) { 
    82                                 //TODO convenience method deleteLocation(Integer) ?? 
    83                                 try { 
    84                                         as.deleteLocation(es.getLocation(Integer.valueOf(p))); 
    85                                         if (!success.equals("")) success += "<br/>"; 
    86                                         success += p + " " + deleted; 
     76 
     77                        String[] locationList = request.getParameterValues("locationId"); 
     78                        if(locationList != null){ 
     79                                AdministrationService as = Context.getAdministrationService(); 
     80                                EncounterService es = Context.getEncounterService(); 
     81                                 
     82                                String deleted = msa.getMessage("general.deleted"); 
     83                                String notDeleted = msa.getMessage("Location.cannot.delete"); 
     84                                for (String p : locationList) { 
     85                                        //TODO convenience method deleteLocation(Integer) ?? 
     86                                        try { 
     87                                                as.deleteLocation(es.getLocation(Integer.valueOf(p))); 
     88                                                if (!success.equals("")) success += "<br/>"; 
     89                                                success += p + " " + deleted; 
     90                                        } 
     91                                        catch(DataIntegrityViolationException e){ 
     92                                                error = handleLocationIntegrityException(e,error,notDeleted); 
     93                                        } 
     94                                        catch (APIException e) { 
     95                                                error = handleLocationIntegrityException(e,error,notDeleted); 
     96                                        } 
    8797                                } 
    88                                 catch (APIException e) { 
    89                                         log.warn("Error deleting location", e); 
    90                                         if (!error.equals("")) error += "<br/>"; 
    91                                         error += p + " " + notDeleted; 
    92                                 } 
    9398                        } 
     99                        else 
     100                                error = msa.getMessage("Location.select"); 
    94101                         
    95102                        view = getSuccessView(); 
    96103                        if (!success.equals("")) 
     
    101108                         
    102109                return new ModelAndView(new RedirectView(view)); 
    103110        } 
     111         
     112        /** 
     113         *  
     114         * Logs a location delete data integrity violation exception and  
     115         * returns a user friedly message of the problem that occured. 
     116         *  
     117         * @param e the exception. 
     118         * @param error the error message. 
     119         * @param notDeleted the not deleted error message. 
     120         * @return the formatted error message. 
     121         */ 
     122        private String handleLocationIntegrityException(Exception e,String error,String notDeleted){ 
     123                log.warn("Error deleting location", e); 
     124                if (!error.equals("")) error += "<br/>"; 
     125                error += notDeleted; 
     126                return error; 
     127        } 
    104128 
    105129        /** 
    106130         *