Setting answers in observation to the concepts selected at runtime

0 views
Skip to first unread message

Rakshit Wadhwa

unread,
Jun 9, 2014, 8:57:17 AM6/9/14
to d...@openmrs.org
Hi

I have a  use case in which as a part of observation object which I have created I need to allow the user to choose the concept on runtime, and set its answer.

Say I create a concept object with concept id as "x" given by the user.

Concept concept = (Concept)Context.getConceptService().getConcept(x);

then I set it in observation as,       
obs.setConcept(concept);

Now I need to add answer to the concept in the observation.
say, if "x" was height or something which takes numeric value 
obs.setValueNumeric(y);

But, since I don't know what x could be at compile time I can't use any particular obs.setValue* .

Is there any generic way to handle this piece of code, or do I always need to check, e.g.

if (x instanceof Integer)
      obs.setValueNumeric(y)
else if (x intanceof Boolean)
     obs.setValueBoolean(y)


This approach looks error prone to me since I won't have any compile time checking as I will have to initiate y as Object and cast as per concept datatype.
Also, can you please guide me to the code how observation handles this when we make an observation manually from the webapp itself, as it allows us to select concepts and answers at runtime.

Thanks and regards
Rakshit




Burke Mamlin

unread,
Jun 9, 2014, 11:03:12 AM6/9/14
to d...@openmrs.org
What happens if you call Obs.setValueAsString(String)?  It looks like that's the convenience method your are looking for, although the Javadoc is confusing (it describes parameter s is "the string to coerce to a boolean" though the description sounds like it should handle other datatypes).

-Burke


--
OpenMRS Developers: http://go.openmrs.org/dev
Post: d...@openmrs.org | Unsubscribe: dev+uns...@openmrs.org
Manage your OpenMRS subscriptions at https://id.openmrs.org/

Rakshit Wadhwa

unread,
Jun 9, 2014, 1:37:32 PM6/9/14
to d...@openmrs.org
Thanks Burke

That served the purpose, apart from some checks(like it always takes as false, if i pass any xyz string for boolean concepts, other than true) this will do.

But definitely useful.

Thanks and regards
Rakshit


To unsubscribe from this group and stop receiving emails from it, send an email to dev+uns...@openmrs.org.

Burke Mamlin

unread,
Jun 9, 2014, 1:44:55 PM6/9/14
to d...@openmrs.org
Looking at the code, it looks numeric, date & times, text, and boolean values are supported; coded concepts are "not yet implemented" (makes sense, since converting a string to a concept would require the Obs object to access teh API itself – something we try to avoid – and looking up concepts by string can be ambiguous).

So, except for coded observations, you should be good.

Cheers,

-Burke
Reply all
Reply to author
Forward
0 new messages