Downloads Documentation Community Contribute Demo






Show Sidebar
Login | Register
Show
Ignore:
Timestamp:
08/18/08 12:05:28 (5 months ago)
Author:
machosry
Message:

address hierarchy: done with newPatientForm and patientDashboard yet to fix the drop down in patient.form

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • openmrs-modules/addresshierarchy/web/src/org/openmrs/module/addresshierarchy/web/controller/AddressLayoutPortletController.java

    r5288 r5302  
    11package org.openmrs.module.addresshierarchy.web.controller; 
    22 
    3 import java.util.HashMap; 
    4 import java.util.Map; 
     3import java.util.Calendar; 
     4import java.util.Date; 
     5import java.util.HashSet; 
     6import java.util.List; 
     7import java.util.Set; 
    58 
    69import javax.servlet.http.HttpServletRequest; 
    710import javax.servlet.http.HttpServletResponse; 
    811 
    9 import org.apache.commons.logging.Log; 
    10 import org.apache.commons.logging.LogFactory; 
     12import org.openmrs.Location; 
     13import org.openmrs.PatientIdentifier; 
     14import org.openmrs.PatientIdentifierType; 
    1115import org.openmrs.PersonAddress; 
     16import org.openmrs.api.PatientService; 
    1217import org.openmrs.api.context.Context; 
    1318import org.openmrs.module.addresshierarchy.AddressHierarchyService; 
     19import org.openmrs.web.controller.patient.NewPatientFormController; 
     20import org.openmrs.web.controller.patient.ShortPatientModel; 
     21import org.springframework.context.support.MessageSourceAccessor; 
    1422import org.springframework.validation.BindException; 
    15 import org.springframework.validation.Errors; 
     23import org.springframework.validation.ValidationUtils; 
    1624import org.springframework.web.servlet.ModelAndView; 
    17 import org.springframework.web.servlet.mvc.SimpleFormController; 
    18 import org.springframework.web.servlet.view.RedirectView; 
    1925 
    20 public class AddressLayoutPortletController extends SimpleFormController { 
     26public class AddressLayoutPortletController extends NewPatientFormController { 
    2127         
    22         private final static PersonAddress personAddress = null; 
     28        Set<PatientIdentifier> newIdentifiers = new HashSet<PatientIdentifier>(); 
     29    String pref = ""; 
    2330         
    24         protected final Log log = LogFactory.getLog(getClass()); 
    2531     
    26     /** 
    27      * Returns any extra data in a key-->value pair kind of way 
    28      *  
    29      * @see org.springframework.web.servlet.mvc.SimpleFormController#referenceData(javax.servlet.http.HttpServletRequest, java.lang.Object, org.springframework.validation.Errors) 
    30      */ 
    31     @Override 
    32     protected Map<String, Object> referenceData(HttpServletRequest request, Object obj, Errors err) throws Exception { 
    33         System.out.println("coem oneajsdasjh"); 
    34         // this method doesn't return any extra data right now, just an empty map 
    35         return new HashMap<String,Object>(); 
     32    private AddressHierarchyService getService(){ 
     33        return (AddressHierarchyService)Context.getService(AddressHierarchyService.class); 
    3634    } 
     35         
    3736     
     37protected ModelAndView processFormSubmission(HttpServletRequest request, HttpServletResponse response, Object obj, BindException errors) throws Exception { 
     38         
     39        super.processFormSubmission(request, response, obj, errors); 
     40         
     41        System.out.println("we are here"); 
     42                 
     43                newIdentifiers = new HashSet<PatientIdentifier>(); 
     44                 
     45                ShortPatientModel shortPatient = (ShortPatientModel)obj; 
     46                 
     47                log.debug("\nNOW GOING THROUGH PROCESSFORMSUBMISSION METHOD.......................................\n\n"); 
     48                 
     49                if (Context.isAuthenticated()) { 
     50                        PatientService ps = Context.getPatientService(); 
     51                        MessageSourceAccessor msa = getMessageSourceAccessor(); 
     52                         
     53                        String action = request.getParameter("action"); 
     54                        if (action == null || action.equals(msa.getMessage("general.save"))) { 
     55                                ValidationUtils.rejectIfEmptyOrWhitespace(errors, "name.familyName", "error.name"); 
     56                                 
     57                                String[] identifiers = request.getParameterValues("identifier"); 
     58                                String[] types = request.getParameterValues("identifierType"); 
     59                                String[] locs = request.getParameterValues("location"); 
     60                                pref = request.getParameter("preferred"); 
     61                                if (pref == null) 
     62                                        pref = ""; 
     63                                 
     64                                if (log.isDebugEnabled()) { 
     65                                        log.debug("identifiers: " + identifiers); 
     66                                        for (String s : identifiers) 
     67                                                log.debug(s); 
     68                                        log.debug("types: " + types); 
     69                                        for (String s : types) 
     70                                                log.debug(s); 
     71                                        log.debug("locations: " + locs); 
     72                                        for (String s : locs) 
     73                                                log.debug(s); 
     74                                        log.debug("preferred: " + pref); 
     75                                } 
     76                                 
     77                                // loop over the identifiers to create the patient.identifiers set 
     78                                if (identifiers != null) { 
     79                                        for (int i=0; i<identifiers.length;i++) { 
     80                                                // arguments for the spring error messages 
     81                                                String id = identifiers[i].trim(); 
     82                                                String[] args = {id}; 
     83                                                 
     84                                                // add the new identifier only if they put in some identifier string 
     85                                                if (id.length() > 0) { 
     86                                                         
     87                                                        // set up the actual identifier java object 
     88                                                        PatientIdentifierType pit = null; 
     89                                                        if (types[i] == null || types[i].equals("")) { 
     90                                                                String msg = getMessageSourceAccessor().getMessage("PatientIdentifier.identifierType.null", args); 
     91                                                                errors.reject(msg); 
     92                                                        } 
     93                                                        else 
     94                                                                pit = ps.getPatientIdentifierType(Integer.valueOf(types[i])); 
     95                                                         
     96                                                        Location loc = null; 
     97                                                        if (locs[i] == null || locs[i].equals("")) { 
     98                                                                String msg = getMessageSourceAccessor().getMessage("PatientIdentifier.location.null", args); 
     99                                                                errors.reject(msg); 
     100                                                        } 
     101                                                        else 
     102                                                                loc = null; 
     103                                                         
     104                                                        PatientIdentifier pi = new PatientIdentifier(id, pit, loc); 
     105                                                        pi.setPreferred(pref.equals(id+types[i])); 
     106                                                        if (newIdentifiers.contains(pi)) 
     107                                                                newIdentifiers.remove(pi); 
     108                                                         
     109                                                        newIdentifiers.add(pi); 
     110                                                         
     111                                                        if (log.isDebugEnabled()) { 
     112                                                                log.debug("Creating patient identifier with identifier: " + id); 
     113                                                                log.debug("and type: " + types[i]); 
     114                                                                log.debug("and location: " + locs[i]); 
     115                                                        } 
     116 
     117                                                } 
     118                                        } 
     119                                } 
     120                        } 
     121                         
     122                        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "gender", "error.null"); 
     123                         
     124                        // check patients birthdate against future dates and really old dates 
     125                        if (shortPatient.getBirthdate() != null) { 
     126                                if (shortPatient.getBirthdate().after(new Date())) 
     127                                        errors.rejectValue("birthdate", "error.date.future"); 
     128                                else { 
     129                                        Calendar c = Calendar.getInstance(); 
     130                                        c.setTime(new Date()); 
     131                                        c.add(Calendar.YEAR, -120); // patient cannot be older than 120 years old  
     132                                        if (shortPatient.getBirthdate().before(c.getTime())){ 
     133                                                errors.rejectValue("birthdate", "error.date.nonsensical"); 
     134                                        } 
     135                                } 
     136                        } 
     137                         
     138                } 
     139                 
     140                // skip calling super.processFormSubmission so that setting up the page is done 
     141                // again in the onSubmit method 
     142                String[] loclistarr = {"address.address1","address.address2","address.country","address.stateProvince","address.countyDistrict","address.subRegion","address.region","address.townshipDivision","address.cityVillage","address.neighborhoodCell","address.postalCode","address.longitude","address.latitude"}; 
     143                 
     144                String dumm= ""; 
     145                String str = ""; 
     146                Integer count = Integer.valueOf(request.getParameter("count")); 
     147                if(count != getService().getSizeOfPersonTable()){ 
     148                         
     149                        for(int i=0;i<loclistarr.length;i++){ 
     150                                dumm = "address."+loclistarr[i]; 
     151                                str += "'"+request.getParameter(dumm)+"'"; 
     152                                str += ","; 
     153                                 
     154                        } 
     155                List lastRow = getService().getLastRow(); 
     156                Object[] x = (Object[]) lastRow.get(lastRow.size()-1); 
     157                 
     158                str += x[0].toString()+","; 
     159                str += x[1].toString()+","; 
     160                 
     161                if((Boolean) x[2]){ 
     162                        str += "1,"+x[3].toString()+","+x[4].toString()+","+x[5].toString(); 
     163                } 
     164                else 
     165                        str += "0,null,null,null"; 
     166                System.out.println(str); 
     167                } 
     168                ModelAndView fin = onSubmit(request, response, shortPatient, errors); 
     169                System.out.println("now here"); 
     170                 
     171                return fin;  
     172        } 
    38173     
    39174 
     
    42177     * @see org.springframework.web.servlet.mvc.SimpleFormController#onSubmit(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object, org.springframework.validation.BindException) 
    43178     */ 
    44     @Override 
    45     protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object object, BindException exceptions) throws Exception {        
    46         //HttpSession httpSession = request.getSession(); 
    47                 String s= request.getAttribute("org.openmrs.portlet.userId").toString(); 
    48                 System.out.println(s); 
    49                 System.out.println("casjdhajsdasd"); 
    50                  
    51                 PersonAddress pa = personAddress; 
    52                 String[] arr = {"Country","State/Province","City/Village","Neighborhood/Cell","County/District","Township/Division","Sub Region","Region","Postal Code","Longitude","Latitude"}; 
    53                 pa.setCountry(request.getParameter("Country")); 
    54                 pa.setStateProvince(request.getParameter("State/Province")); 
    55                 pa.setCityVillage(request.getParameter("City/Village")); 
    56                 pa.setNeighborhoodCell(request.getParameter("Neighborhood/Cell")); 
    57                 pa.setCountyDistrict(request.getParameter("County/District")); 
    58                 pa.setTownshipDivision(request.getParameter("Township/Division")); 
    59                 pa.setSubregion(request.getParameter("Sub Region")); 
    60                 pa.setRegion(request.getParameter("Region")); 
    61                 pa.setPostalCode(request.getParameter("Postal Code")); 
    62                 pa.setLongitude(request.getParameter("Longitude")); 
    63                 pa.setLatitude(request.getParameter("Latitude")); 
    64                 pa.setAddress1(request.getParameter("Address1")); 
    65                 pa.setAddress2(request.getParameter("Address2")); 
    66                  
    67                 return new ModelAndView(new RedirectView(getSuccessView())); 
    68     } 
    69      
    70      
    71      
    72      
     179 
    73180 
    74181 
     
    82189     * @see org.springframework.web.servlet.mvc.AbstractFormController#formBackingObject(javax.servlet.http.HttpServletRequest) 
    83190     */ 
     191 
     192         
    84193    
    85194