use of org.openmrs.layout.address.AddressTemplate in project openmrs-module-pihcore by PIH.
the class AddressBundle method getAddressTemplate.
/**
* @return a new AddressTemplate instance for the given configuration
*/
public AddressTemplate getAddressTemplate() {
AddressTemplate addressTemplate = new AddressTemplate("");
Map<String, String> nameMappings = new HashMap<String, String>();
Map<String, String> sizeMappings = new HashMap<String, String>();
Map<String, String> elementDefaults = new HashMap<String, String>();
for (AddressComponent c : getAddressComponents()) {
log.error("adding " + c.getField().getName() + " to nameMappings");
nameMappings.put(c.getField().getName(), c.getNameMapping());
sizeMappings.put(c.getField().getName(), Integer.toString(c.getSizeMapping()));
if (c.getElementDefault() != null) {
elementDefaults.put(c.getField().getName(), c.getElementDefault());
}
}
addressTemplate.setNameMappings(nameMappings);
addressTemplate.setSizeMappings(sizeMappings);
addressTemplate.setElementDefaults(elementDefaults);
addressTemplate.setLineByLineFormat(getLineByLineFormat());
return addressTemplate;
}
use of org.openmrs.layout.address.AddressTemplate in project openmrs-core by openmrs.
the class PersonAddressValidator method validate.
/**
* @see org.springframework.validation.Validator#validate(java.lang.Object,
* org.springframework.validation.Errors)
* @should pass if all the dates are valid
* @should fail if the startDate is in the future
* @should fail if the endDate is before the startDate
* @should pass if startDate and endDate are both null
* @should pass if startDate is null
* @should pass if endDate is null
* @should fail if required fields are empty
* @should pass if required fields are not empty
* @should pass validation if field lengths are correct
* @should fail validation if field lengths are not correct
*/
@Override
public void validate(Object object, Errors errors) {
// TODO Validate other aspects of the personAddress object
if (log.isDebugEnabled()) {
log.debug(this.getClass().getName() + ".validate...");
}
if (object == null) {
throw new IllegalArgumentException("The personAddress object should not be null");
}
PersonAddress personAddress = (PersonAddress) object;
// resolve a shorter name to display along with the error message
String addressString;
if (StringUtils.isNotBlank(personAddress.getAddress1())) {
addressString = personAddress.getAddress1();
} else if (StringUtils.isNotBlank(personAddress.getAddress2())) {
addressString = personAddress.getAddress2();
} else if (StringUtils.isNotBlank(personAddress.getCityVillage())) {
addressString = personAddress.getCityVillage();
} else {
addressString = personAddress.toString();
}
if (OpenmrsUtil.compareWithNullAsEarliest(personAddress.getStartDate(), new Date()) > 0) {
errors.rejectValue("startDate", "PersonAddress.error.startDateInFuture", new Object[] { "'" + addressString + "'" }, "The Start Date for address '" + addressString + "' shouldn't be in the future");
}
if (personAddress.getStartDate() != null && OpenmrsUtil.compareWithNullAsLatest(personAddress.getStartDate(), personAddress.getEndDate()) > 0) {
errors.rejectValue("endDate", "PersonAddress.error.endDateBeforeStartDate", new Object[] { "'" + addressString + "'" }, "The End Date for address '" + addressString + "' shouldn't be earlier than the Start Date");
}
String xml = Context.getLocationService().getAddressTemplate();
List<String> requiredElements;
try {
AddressTemplate addressTemplate = Context.getSerializationService().getDefaultSerializer().deserialize(xml, AddressTemplate.class);
requiredElements = addressTemplate.getRequiredElements();
} catch (Exception e) {
errors.reject(Context.getMessageSourceService().getMessage("AddressTemplate.error"));
return;
}
if (requiredElements != null) {
for (String fieldName : requiredElements) {
try {
Object value = PropertyUtils.getProperty(personAddress, fieldName);
if (StringUtils.isBlank((String) value)) {
// required field not found
errors.reject(Context.getMessageSourceService().getMessage("AddressTemplate.error.requiredAddressFieldIsBlank", new Object[] { fieldName }, Context.getLocale()));
}
} catch (Exception e) {
// wrong field declared in template
errors.reject(Context.getMessageSourceService().getMessage("AddressTemplate.error.fieldNotDeclaredInTemplate", new Object[] { fieldName }, Context.getLocale()));
}
}
}
ValidateUtil.validateFieldLengths(errors, object.getClass(), "address1", "address2", "cityVillage", "stateProvince", "postalCode", "country", "latitude", "longitude", "voidReason", "countyDistrict", "address3", "address4", "address5", "address6", "address7", "address8", "address9", "address10", "address11", "address12", "address13", "address14", "address15");
}
Aggregations