Downloads Documentation Community Contribute Demo






Show Sidebar
Login | Register

Ticket #714: dateAttributable2.patch

File dateAttributable2.patch, 9.2 kB (added by jacobb, 4 months ago)

Fixed empty and invalid date errors.

  • src/api/org/openmrs/PersonAttribute.java

    old new  
    1515 
    1616import java.lang.reflect.InvocationTargetException; 
    1717import java.lang.reflect.Method; 
     18import java.text.ParseException; 
     19import java.text.SimpleDateFormat; 
    1820import java.util.Date; 
    1921 
    2022import org.apache.commons.logging.Log; 
     
    412414                        } 
    413415                } 
    414416                catch (Throwable t) { 
    415                         // pass 
     417                        log.warn("Unable to hydrate value: " + getValue() + " for type: " + getAttributeType(), t); 
    416418                } 
    417419                 
    418420                log.debug("Returning value: '" + getValue() + "'"); 
  • src/api/org/openmrs/Location.java

    old new  
    354354         * @see org.openmrs.Attributable#serialize() 
    355355         */ 
    356356        public String serialize() { 
    357                 return "" + getLocationId(); 
     357                if (getLocationId() != null) 
     358                        return "" + getLocationId(); 
     359                else 
     360                        return ""; 
    358361        } 
    359362         
    360363        /** 
  • src/api/org/openmrs/util/AttributableDate.java

    old new  
     1/** 
     2 * The contents of this file are subject to the OpenMRS Public License 
     3 * Version 1.0 (the "License"); you may not use this file except in 
     4 * compliance with the License. You may obtain a copy of the License at 
     5 * http://license.openmrs.org 
     6 * 
     7 * Software distributed under the License is distributed on an "AS IS" 
     8 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the 
     9 * License for the specific language governing rights and limitations 
     10 * under the License. 
     11 * 
     12 * Copyright (C) OpenMRS, LLC.  All Rights Reserved. 
     13 */ 
     14package org.openmrs.util; 
     15 
     16import java.text.ParseException; 
     17import java.text.SimpleDateFormat; 
     18import java.util.Date; 
     19import java.util.List; 
     20 
     21import org.openmrs.Attributable; 
     22import org.openmrs.api.APIException; 
     23import org.openmrs.api.context.Context; 
     24 
     25/** 
     26 * This class is a stand in for using "java.util.Date" as a PersonAttribute format. 
     27 *  
     28 * This will allow the end user to store a date as YYYMMDD instead of storing it as 
     29 * a string in the date format of whatever user created the string 
     30 *  
     31 * @see java.util.Date 
     32 * @see org.openmrs.PersonAttribute 
     33 * @see org.openmrs.Attributable 
     34 */ 
     35public class AttributableDate extends Date implements Attributable<AttributableDate> { 
     36 
     37    private static final long serialVersionUID = 4280303636131451746L; 
     38    private final String dateFormat = "yyyy-MM-dd"; 
     39 
     40    /** 
     41     * Default empty constructor 
     42     *  
     43     * @see java.util.Date#Date() 
     44     */ 
     45    public AttributableDate() { 
     46        super(); 
     47    } 
     48     
     49    /** 
     50     * Convenience constructor allowing creation of an AttributableDate with 
     51     * the given time 
     52     *   
     53     * @param time 
     54     * @see java.util.Date#Date(long) 
     55     */ 
     56    public AttributableDate(long time) { 
     57            super(time); 
     58    } 
     59 
     60        /** 
     61         * @see org.openmrs.Attributable#findPossibleValues(java.lang.String) 
     62         */ 
     63        public List<AttributableDate> findPossibleValues(String searchText) { 
     64                // TODO Auto-generated method stub 
     65                return null; 
     66        } 
     67 
     68        /** 
     69         * @see org.openmrs.Attributable#getDisplayString() 
     70         */ 
     71        public String getDisplayString() { 
     72                return new SimpleDateFormat(dateFormat).format(this); 
     73        } 
     74 
     75        /** 
     76         * @see org.openmrs.Attributable#getPossibleValues() 
     77         */ 
     78        public List<AttributableDate> getPossibleValues() { 
     79                // TODO Auto-generated method stub 
     80                return null; 
     81        } 
     82 
     83        /** 
     84         * @see org.openmrs.Attributable#hydrate(java.lang.String) 
     85         */ 
     86        public AttributableDate hydrate(String s) { 
     87                try { 
     88                        // try to parse as the current user ( 
     89                        return new AttributableDate(((Date)Context.getDateFormat().parseObject(s)).getTime()); 
     90                } 
     91                catch (ParseException e) { 
     92                        try { 
     93                                return new AttributableDate(((Date)new SimpleDateFormat(dateFormat).parseObject(s)).getTime()); 
     94                        } 
     95                        catch (ParseException e2) { 
     96                                // if we can't parse it as the normalized string or as the current 
     97                                // user's date format, bail out 
     98                                // throw new APIException("Unable to parse the given string: '" + s + "' as a date object"); 
     99                                 
     100                                // returning null causes the field to be blanked out 
     101                                return null; 
     102                        } 
     103                } 
     104        } 
     105 
     106        /** 
     107         * @see org.openmrs.Attributable#serialize() 
     108         */ 
     109        public String serialize() { 
     110                return new SimpleDateFormat(dateFormat).format(this); 
     111        } 
     112         
     113        public String toString() { 
     114                System.out.println("toStringTest"); 
     115                return super.toString(); 
     116        } 
     117} 
  • src/api/org/openmrs/Concept.java

    old new  
    670670         * @see org.openmrs.Attributable#serialize() 
    671671         */ 
    672672        public String serialize() { 
     673                if (this.getConceptId() == null) 
     674                        return ""; 
     675                 
    673676                return "" + this.getConceptId(); 
    674677        } 
    675678         
  • src/web/org/openmrs/web/taglib/FieldGenTag.java

    old new  
    223223                try { 
    224224                        pageContext.include(this.url); 
    225225                } catch (ServletException e) { 
    226                         log.error("ServletException while trying to include a file in FieldGenTag"); 
     226                        log.error("ServletException while trying to include a file in FieldGenTag", e); 
    227227                } catch (IOException e) { 
    228                         log.error("IOException while trying to include a file in FieldGenTag"); 
     228                        log.error("IOException while trying to include a file in FieldGenTag", e); 
    229229                } 
    230230 
    231231                /* 
  • src/web/org/openmrs/web/controller/person/PersonFormController.java

    old new  
    2525 
    2626import org.apache.commons.logging.Log; 
    2727import org.apache.commons.logging.LogFactory; 
     28import org.openmrs.Attributable; 
    2829import org.openmrs.Person; 
    2930import org.openmrs.PersonAddress; 
    3031import org.openmrs.PersonAttribute; 
    3132import org.openmrs.PersonAttributeType; 
    3233import org.openmrs.PersonName; 
     34import org.openmrs.api.APIException; 
    3335import org.openmrs.api.context.Context; 
    3436import org.openmrs.propertyeditor.ConceptEditor; 
    3537import org.openmrs.util.OpenmrsConstants.PERSON_TYPE; 
     
    9395                                 
    9496                        // look for person attributes in the request and save to person 
    9597                                for (PersonAttributeType type : Context.getPersonService().getPersonAttributeTypes(PERSON_TYPE.PERSON, null)) { 
    96                                         String value = request.getParameter(type.getPersonAttributeTypeId().toString()); 
     98                                        String paramName = type.getPersonAttributeTypeId().toString(); 
     99                                        String value = request.getParameter(paramName); 
    97100                                         
    98101                                        // if there is an error displaying the attribute, the value will be null 
    99                                         if (value != null) 
    100                                                 person.addAttribute(new PersonAttribute(type, value)); 
     102                                        if (value != null) { 
     103                                                PersonAttribute attribute = new PersonAttribute(type, value); 
     104                                                try { 
     105                                                        Object hydratedObject = attribute.getHydratedObject(); 
     106                                                        if (hydratedObject == null) { 
     107                                                                // if null is returned, the value should be blanked out 
     108                                                                attribute.setValue(""); 
     109                                                        } else if (hydratedObject instanceof Attributable) { 
     110                                                                attribute.setValue(((Attributable)hydratedObject).serialize()); 
     111                                                        } 
     112                                                        else if (!hydratedObject.getClass().getName().equals(type.getFormat())) 
     113                                                                // if the classes doesn't match the format, the hydration failed somehow 
     114                                                                // TODO change the PersonAttribute.getHydratedObject() to not swallow all errors? 
     115                                                                throw new APIException(); 
     116                                                } 
     117                                                catch (APIException e) { 
     118                                                        errors.rejectValue("attributes[" + paramName + "].value", "Invalid value: " + value); 
     119                                                        log.warn("Got an invalid value: " + value + " while setting " + paramName, e); 
     120                                                         
     121                                                        // setting the value to empty so that the user can reset the value to something else 
     122                                                        attribute.setValue(""); 
     123                                                         
     124                                                } 
     125                                                person.addAttribute(attribute); 
     126                                        } 
    101127                                } 
    102128                                 
    103129                        // check patients birthdate against future dates and really old dates 
  • web/WEB-INF/openmrs-servlet.xml

    old new  
    13131313                                        org.openmrs.web.taglib.fieldgen.GenericReasonHandler 
    13141314                                </prop> 
    13151315                                <prop key="java.util.Date">org.openmrs.web.taglib.fieldgen.DateHandler</prop> 
     1316                                <prop key="org.openmrs.util.AttributableDate">org.openmrs.web.taglib.fieldgen.DateHandler</prop> 
    13161317                        </props> 
    13171318                </property> 
    13181319        </bean>