Adding a resource into OpenMRS
The OpenMRS data model doesn't include everything we need for Raxa-JSS. We will add many different resources into our main OpenMRS module https://github.com/Raxa/raxacore
There are four steps involved in adding a new resource to OpenMRS:
- Create tables and Java Objects for each new resource
- Create DAO interface and implementation
- Create service interface and implementation
- Create controller and resource layer for REST
Create tables and Java Objects
Add in required columns into the liquibase.xml
Your columns should come as a changeset such as shown below:
<changeSet id="2012-07-15_create_raxacore_drug_group" author="yan"> <preConditions onFail="MARK_RAN"> <not> <tableExists tableName="raxacore_drug_group"/> </not> </preConditions> <comment> Schema for raxacore_drug_group table </comment> <createTable tableName="raxacore_drug_group"> <column name="drug_group_id" type="int" autoIncrement="true"> <constraints primaryKey="true" nullable="false"/> </column> <column name="drug_group_name" type="varchar(255)"/> <column name="date_created" type="DATETIME"> <constraints nullable="false"/> </column> <column name="uuid" type="char(38)"> <constraints unique="true" nullable="false"/> </column> </createTable> </changeSet>
- Create Java Object with fields matching the columns you just created
- your class extends:
- BaseOpenmrsMetaData if you are creating clinical data
- BaseOpenmrsData if you are creating patient-specific data
- your class implements Serializable
- should have equals(), hashCode(), and setId() functions
- your class extends:
Create hibernate mapping file to link your table and Java objectÂ
- Call the file YourClass.hbm.xml
- Add your file under <MappingResources> in config.xml
Create Data Access Object Layer
Add interface YourClassDAO.java with all the functions you require on the object (create, read, update, delete)
- Your DAO layer shouldn't access any services for other objects – if you need another service, instead move that function into the service layer
Add HibernateYourClassDAO.java that implements the interface
Add HbernateYourClassDAOTest.java and add in JUnit tests
Create Service Layer
Add YourClassService.java interface
- As per Openmrs standard, this should extend BaseOpenmrsService
- Should be \@Transactional
- Add \@Authorized({"Privilege to Use YourClass"}) privileges for each function – your new privileges should go in config.xml
Add YourClassServiceImpl.java
Add YourClassServiceImplTest.java
Configure ModuleApplicationContext.xml and TestApplicationContext.xml as per https://wiki.openmrs.org/display/docs/Module+Application+Context+File
Create Controller and Resource Layer
Add YourClassController.java
- extends BaseRestController
- Your controller should not define your service outside of a function – make a function initController() that defines your service=Context.getService(YourClassService.class)
- call initController() at the beginning of every function
Add YourClassResource.java
- extends MetadataDelegatingCrudResource or DataDelegatingCrudResource depending if clinical or patient-specific data
Â