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 15 15 16 16 import java.lang.reflect.InvocationTargetException; 17 17 import java.lang.reflect.Method; 18 import java.text.ParseException; 19 import java.text.SimpleDateFormat; 18 20 import java.util.Date; 19 21 20 22 import org.apache.commons.logging.Log; … … 412 414 } 413 415 } 414 416 catch (Throwable t) { 415 // pass417 log.warn("Unable to hydrate value: " + getValue() + " for type: " + getAttributeType(), t); 416 418 } 417 419 418 420 log.debug("Returning value: '" + getValue() + "'"); -
src/api/org/openmrs/Location.java
old new 354 354 * @see org.openmrs.Attributable#serialize() 355 355 */ 356 356 public String serialize() { 357 return "" + getLocationId(); 357 if (getLocationId() != null) 358 return "" + getLocationId(); 359 else 360 return ""; 358 361 } 359 362 360 363 /** -
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 */ 14 package org.openmrs.util; 15 16 import java.text.ParseException; 17 import java.text.SimpleDateFormat; 18 import java.util.Date; 19 import java.util.List; 20 21 import org.openmrs.Attributable; 22 import org.openmrs.api.APIException; 23 import 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 */ 35 public 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 670 670 * @see org.openmrs.Attributable#serialize() 671 671 */ 672 672 public String serialize() { 673 if (this.getConceptId() == null) 674 return ""; 675 673 676 return "" + this.getConceptId(); 674 677 } 675 678 -
src/web/org/openmrs/web/taglib/FieldGenTag.java
old new 223 223 try { 224 224 pageContext.include(this.url); 225 225 } 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); 227 227 } 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); 229 229 } 230 230 231 231 /* -
src/web/org/openmrs/web/controller/person/PersonFormController.java
old new 25 25 26 26 import org.apache.commons.logging.Log; 27 27 import org.apache.commons.logging.LogFactory; 28 import org.openmrs.Attributable; 28 29 import org.openmrs.Person; 29 30 import org.openmrs.PersonAddress; 30 31 import org.openmrs.PersonAttribute; 31 32 import org.openmrs.PersonAttributeType; 32 33 import org.openmrs.PersonName; 34 import org.openmrs.api.APIException; 33 35 import org.openmrs.api.context.Context; 34 36 import org.openmrs.propertyeditor.ConceptEditor; 35 37 import org.openmrs.util.OpenmrsConstants.PERSON_TYPE; … … 93 95 94 96 // look for person attributes in the request and save to person 95 97 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); 97 100 98 101 // 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 } 101 127 } 102 128 103 129 // check patients birthdate against future dates and really old dates -
web/WEB-INF/openmrs-servlet.xml
old new 1313 1313 org.openmrs.web.taglib.fieldgen.GenericReasonHandler 1314 1314 </prop> 1315 1315 <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> 1316 1317 </props> 1317 1318 </property> 1318 1319 </bean>
Download in other formats:
Powered by Trac 0.10.5
By Edgewall Software.
Visit the Trac open source project at
http://trac.edgewall.com/