Isn't there something provided in OpenMRS Core to do this? E.g. through the complex obs mechanism?