Custom representation fetch for concept when the answers has a drug

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?

One option would be to change the code for custom representations so that there’s a way of defining optional properties, or to generally not have it fail on requested properties that aren’t available. (I think this issue has come up before, e.g. if you’re asking for a set of orders which may be drug orders or lab orders, and thus have different fields.)

Another option would be to fix the findAnnotatedMethodForRepresentation logic so that it correctly supports named representations whose names differ.

Final option: you can override the getRepresentationDescription method (or something like that) and have it return the Bahmni representations if requested, or super otherwise.