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:

  1. Create tables and Java Objects for each new resource
  2. Create DAO interface and implementation
  3. Create service interface and implementation
  4. Create controller and resource layer for REST

Create tables and Java Objects

  1. 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>	
  2. Create Java Object with fields matching the columns you just created
    1. your class extends:
      1. BaseOpenmrsMetaData if you are creating clinical data
      2. BaseOpenmrsData if you are creating patient-specific data
    2. your class implements Serializable
      1. should have equals(), hashCode(), and setId() functions
  3. Create hibernate mapping file to link your table and Java object 
    1. Call the file YourClass.hbm.xml
    2. Add your file under <MappingResources> in config.xml

Create Data Access Object Layer

  1. Add interface YourClassDAO.java with all the functions you require on the object (create, read, update, delete)
    1. Your DAO layer shouldn't access any services for other objects – if you need another service, instead move that function into the service layer
  2. Add HibernateYourClassDAO.java that implements the interface
  3. Add HbernateYourClassDAOTest.java and add in JUnit tests

Create Service Layer

  1. Add YourClassService.java interface
    1. As per Openmrs standard, this should extend BaseOpenmrsService
    2. Should be \@Transactional
    3. Add \@Authorized({"Privilege to Use YourClass"}) privileges for each function – your new privileges should go in config.xml
  2. Add YourClassServiceImpl.java
  3. Add YourClassServiceImplTest.java
  4. Configure ModuleApplicationContext.xml and TestApplicationContext.xml as per https://wiki.openmrs.org/display/docs/Module+Application+Context+File

Create Controller and Resource Layer

  1. Add YourClassController.java
    1. extends BaseRestController
    2. Your controller should not define your service outside of a function – make a function initController() that defines your service=Context.getService(YourClassService.class)
      1. call initController() at the beginning of every function
  2. Add YourClassResource.java
    1. extends MetadataDelegatingCrudResource or DataDelegatingCrudResource depending if clinical or patient-specific data


Â