Adding Person Attribute Type through code.

Tags: #<Tag:0x00007f076bd7f590> #<Tag:0x00007f076bd7f450>

Hey Everyone,

One of the features of My GSoC Project is to Automating the LBAC module installation. It is of improvement in adding Person Attribute Type Automatically while Installation of LBAC module rather through doing

Goto: System Administration → Advanced Administration → Persons → Manage Person Attribute Types, and Adding person Attribute type manually.

If anyone had an idea of adding the person Attribute Type through code or through the config file while Installation of the module automatically can guide me.

Mentor: @suthagar23

cc: @dkayiwa @ruhanga

1 Like

Have you checked with available Person Service methods?

If you are developing on 2.x, the metadatadeploy will be your friend. I normally create the metadata I want in a separate class by extending the correct classe, then call the install method, then make sure those are available when the module start in the start method. Here is an example of how I do person attributes: Have your attributes in this class then insatll them as I did here install, remember that class is made a bean by using @Component and extending AbstractMetadataBundle. Note that you can install as many types of metadata as they appear here, then in your activator do this in the start method

MetadataDeployService deployService = Context.getService(MetadataDeployService.class); deployService.installBundle(Context.getRegisteredComponents(CommonMetadataBundle.class).get(0));

NOTE: The bold text is the class you installed your person attributes in, and the uuids can be created randomly for new attributes but use existing uuids for already created attributes. Educate yourself with this literature. Hope that helps, let me know if you need more

2 Likes

Thanks, @ningosi for providing very helpful information.

@vankineenitawrun Did you look in to this one : https://github.com/openmrs/openmrs-core/blob/master/api/src/main/java/org/openmrs/api/PersonService.java#L129?

Hope it will be a single service method which can create person attribute through the code :slight_smile:

Yes, @suthagar23. gone through the docs https://docs.openmrs.org/doc/org/openmrs/api/PersonService.html#savePersonAttributeType-org.openmrs.PersonAttributeType-

I think for our Issue we can use this savePersonAttributeType Method in the Activator of Module.

AFAIK, It will be the simplest solution for the question :wink:

1 Like

If we go do by this way we can use it REST API to I guess.:thinking:

REST API vs OpenMRS API, I think those are different :wink:

Why not use metadata deploy - see steps below. The beauty with this approach is that you can define the uuid which is used to reference the attribute going forward

  1. Person Attribute Type definition - https://github.com/METS-Programme/openmrs-module-aijar/blob/819ea7e393fd7cba21fbcae2b10e3927ec2a7ea5/api/src/main/java/org/openmrs/module/aijar/metadata/core/PersonAttributeTypes.java#L14-L41

  2. Bundle to deploy the attribute type - https://github.com/METS-Programme/openmrs-module-aijar/blob/819ea7e393fd7cba21fbcae2b10e3927ec2a7ea5/api/src/main/java/org/openmrs/module/aijar/api/deploy/bundle/CommonMetadataBundle.java#L37

  3. Bundle deployed in the module activator https://github.com/METS-Programme/openmrs-module-aijar/blob/819ea7e393fd7cba21fbcae2b10e3927ec2a7ea5/api/src/main/java/org/openmrs/module/aijar/AijarActivator.java#L135

Thanks @ssmusoke, For adopting this one, we need to depend on this dependency API. That’s why I thought, the OpenMRS Person Service will be easiest way to move forward. Am I correct?

My approach is never to write custom code that needs troubleshooting and testing, but to follow the approaches designed for the particular job at hand.

Metadata deploy is the recommended approach for deploying metadata why use something else?

Thanks, @ssmusoke.

as we can set UUID to the object before saving it database using personService.savePersonAttributeType(myObject).

we can use savePersonAttribute method as we have only instance to be added to the database, instead of using a dependency. suggest if I am missing something :slight_smile: