Hi,
We are having the requirement to configure drug to a concept answer and fetch them using custom representation.
What we tried,
Approach 1
We are using the following to get the concepts information:
/openmrs/ws/rest/v1/concept?name=SampleTemplate&v=custom:(uuid,name,answers:(uuid,name,displayString,names))
Issue: The above request fails when the concept answer has a drug. Reason: Drug does not have the attribute names so the request fails
Approach 2
We created Named representations as below
public class BahmniConceptResource extends ConceptResource1_9 {
@RepHandler(value=NamedRepresentation.class, name="bahmni")
public SimpleObject asBahmni(Concept delegate) throws ConversionException {
DelegatingResourceDescription description = new DelegatingResourceDescription();
description.addProperty("uuid");
description.addProperty("name", Representation.DEFAULT);
description.addProperty("answers", new NamedRepresentation("bahmniAnswer"));
return convertDelegateToRepresentation(delegate, description);
}
@RepHandler(value = NamedRepresentation.class, name="bahmniAnswer")
public SimpleObject asBahmniAnswerRepresentation(Concept delegate) throws ConversionException {
DelegatingResourceDescription description = new DelegatingResourceDescription();
description.addProperty("uuid", Representation.DEFAULT);
description.addProperty("name", Representation.DEFAULT);
description.addProperty("names", Representation.DEFAULT);
description.addProperty("displayString");
return convertDelegateToRepresentation(delegate, description);
}
}
public class BahmniDrugResource extends DrugResource1_10 {
@RepHandler(value=NamedRepresentation.class, name="bahmniAnswer")
public SimpleObject asBahmniAnswerRepresentation(Drug delegate) throws ConversionException {
DelegatingResourceDescription description = new DelegatingResourceDescription();
description.addProperty("uuid", Representation.DEFAULT);
description.addProperty("name", Representation.DEFAULT);
description.addProperty("displayString");
return convertDelegateToRepresentation(delegate, description);
}
}
And we were trying to fetch concepts like below: /openmrs/ws/rest/v1/concept?name=SampleTemplate&v=bahmni
Issue: bahmniAnswer RepHandler is always picked Reason: We tracked back this behaviour to the below code snippet in BaseDelegatingResource.java : line 669
private Method findAnnotatedMethodForRepresentation(Class<?> clazz, Representation rep) {
for (Method method : clazz.getMethods()) {
RepHandler ann = method.getAnnotation(RepHandler.class);
if (ann != null) {
if (ann.name().equals(rep.getRepresentation()))
return method;
if (ann.value().isAssignableFrom(rep.getClass())) //Line 669
return method;
}
}
return null;
}
Can you please suggest us a way forward?