appliance baobab linux blogging care CD4 Instrument Interface challenges database developing countries email eVCT documention hardware hct history hiv irc linux open source openmrs opera power presentation projects ruby on rails server software touchscreen touchscreen toolkit vct vct openmrs migration vct videos video video malawi volunteer

Rails, Composite Keys, OpenMRS and Cloning

openmrs software

Well, I just spent a good few hours pouring over some code and finally found the problem. Composite Keys. In OpenMRS there are 12 tables that use composite primary keys. I think we have mentioned that this is easily handled in rails using the composite_keys gem and set_primary_keys function in the respective models. What I didn’t know is that things get a little wacky when you try to clone an ActiveRecord object that has composite keys.

p = PatientIdentifer.new
p.patient_id = 2
new_patient = p.clone
new_patient.patient_id = 3
NoMethodError: undefined method `patient_id=' ...

The clone method in ActiveRecord actually deletes the primary key from the list of attributes. This way if you save it, it will act like a new record. Of course when there are multiple primary keys and they are not autoincrementing this is a problem, because you can’t set them and can’t save the clone. Ever. What is needed is an alternate clone function when the model is composite. So I hacked this together and threw it in our OpenMRS model that is the basis of all our ActiveRecord models in this application.

# cloning when there are composite primary keys
# will delete all of the key attributes, we don't want that
def composite_clone
if composite?
attrs = self.attributes_before_type_cast
self.class.new do |record|
record.send :instance_variable_set, '@attributes', attrs
end
else
clone
end
end

Now, ideally this would go into a module and I would use alias_method and map clone to orig_clone, then override the actual clone method. But let me just tell you that doesn’t work. Or didn’t work. Something to do with order of operations or some such. If you can get that working please comment.

Add comment March 20th, 2007

Baobab Voluntary Counseling and Testing System Uses OpenMRS Data Model

care database developing countries hct open source openmrs software vct vct openmrs migration

The Baobab Voluntary Counseling and Testing (VCT) System is currently used in more than ten sites throughout Malawi. It allows clients to anonymously be tested for HIV, Tuberculosis, and other diseases. Registration clerks, counselors and lab technicians enter the collected information into Baobab touchscreen systems in real time. Though counseling, referrals, and general awareness are the primary goals of any VCT centre, data collection and analysis are a close second.
Until now, Baobab has used a proprietary data model for all of the data collection. With the latest release, Baobab has developed a set of SQL scripts that allows existing VCT deployments to migrate their data to the OpenMRS data model. By registering triggers in the MySQL database the system can continue to update the OpenMRS database as the VCT data is updated. This allows site facilitators to utilize open tools for analyzing the VCT data.

Why Choose OpenMRS?

The Open Medical Record System is a common framework upon which medical informatics efforts in developing countries can be built. It is currently in use in Rwanda and implementations are being built around the world. Using a shared data model across disparate installations and regions enables wider analysis of successes, failures, and general trends. The shared data model will enable each site to improve care based on the comparable results, successes, and assessed need making a better experience for each client.

Get the Code

Baobab has published the code under the MIT license on Google Code.

Add comment August 30th, 2006


Paypal a donation

Why donate?