Saturday, June 7, 2014

GSOC Week 3 : Adding new tables for system indicators

In this week i worked on creating Hibernate mappings and adding the newly designed tables system_metric_types and system_metric_values into OpenMRS database. I first created HibernateDAO objects for MetricType object then called that in PerformanceMonitoringServiceImpl service to add/remove/modify MetricType values. 

However when i then added MetricType.hbm.xml file and name in config.xml it gave me this error.

org.hibernate.MappingException: Unknown entity: org.openmrs.module.systemmetrics.MetricType at org.hibernate.impl.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:693) at org.hibernate.impl.SessionImpl.getEntityPersister(SessionImpl.java:1485) 

 It seemed that hibernate didn't pick up my mapping file entry. With the help of my mentor i finally figured out a way to get rid of it as below. Instead of mapping, i used JPA annoatations. 

 First create MetricType object and annotate it with Table and column annotations as below. As we define table name, primary key, column names here there is no need to refer a .hbm.xml file Also the class needed to implement Serializable in order to define Id, which is primary key. 

  @Entity @Table(name = "systemmetrics_metric_type") 
public class MetricType implements Serializable { 

 @Id @Column(name = "metric_id") 
 private int metricId; 

 @Id @Column(name = "metric_name", length = 255) 
 private String metricName; 

 @Id @Column(name = "metric_type", length = 255) 
 private String metricType; 


 After that modify config.xml file to use annotated classes. You can comment out mapping-files entry there. 

 <!-- Maps hibernate file's, if present -->
    <!--<mappingFiles></mappingFiles>-->

 <!-- Packages that will be scanned for JPA annotations -->
<packagesWithMappedClasses>
        org.openmrs.module.systemmetrics
</packagesWithMappedClasses>


Then it is needed to change liquibase.xml file and add the changeset entry there with entries for each our tables as below. A list of supported elements and attributed as given in http://www.liquibase.org/manual/home#available_database_refactorings 

<changeSet id="system-metrics-1" author="milinda-ruk">
        <createTable tableName="systemmetrics_metric_type">
            <column name="metric_id" type="int" autoIncrement="true">
                <constraints primaryKey="true" nullable="false"/>
            </column>
            <column name="metric_name" type="varchar(255)">
                <constraints nullable="false"/>
            </column>
            <column name="metric_type" type="varchar(255)">
                <constraints nullable="false"/>
            </column>
        </createTable>
  </changeSet>

When the module is loaded the new table is created now and you can add data there using DAO objects. 

For the next week i hope to complete adding used memory data periodically into metric_value table and moving them into used_memory_minute table.

No comments:

Post a Comment