Skip to content

Commit

Permalink
AC-1102: Migrate ObservationDAO to Kotlin (#1021)
Browse files Browse the repository at this point in the history
* AC-1102: Migrate ObservationDAO to Kotlin

* AC-1102: Error fix
  • Loading branch information
shubhamsgit authored Aug 27, 2023
1 parent 2b48736 commit 5140990
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,28 @@
*
* Copyright (C) OpenMRS, LLC. All Rights Reserved.
*/
package com.openmrs.android_sdk.library.dao

package com.openmrs.android_sdk.library.dao;

import com.openmrs.android_sdk.library.OpenmrsAndroid;
import com.openmrs.android_sdk.library.databases.AppDatabase;
import com.openmrs.android_sdk.library.databases.AppDatabaseHelper;
import com.openmrs.android_sdk.library.databases.entities.ObservationEntity;
import com.openmrs.android_sdk.library.databases.entities.StandaloneObservationEntity;
import com.openmrs.android_sdk.library.models.Observation;

import java.util.ArrayList;
import java.util.List;
import javax.inject.Inject;
import com.openmrs.android_sdk.library.OpenmrsAndroid
import com.openmrs.android_sdk.library.databases.AppDatabase
import com.openmrs.android_sdk.library.databases.AppDatabaseHelper.convert
import com.openmrs.android_sdk.library.databases.AppDatabaseHelper.convertToStandalone
import com.openmrs.android_sdk.library.databases.entities.ObservationEntity
import com.openmrs.android_sdk.library.databases.entities.StandaloneObservationEntity
import com.openmrs.android_sdk.library.models.Observation
import javax.inject.Inject

/**
* The type Observation dao.
*/
public class ObservationDAO {
class ObservationDAO @Inject constructor() {
/**
* The Observation room dao.
*/
ObservationRoomDAO observationRoomDAO = AppDatabase.getDatabase(OpenmrsAndroid.getInstance().getApplicationContext()).observationRoomDAO();
var observationRoomDAO = AppDatabase.getDatabase(
OpenmrsAndroid.getInstance()!!.applicationContext
).observationRoomDAO()

@Inject
public ObservationDAO() { }
/**
* Saves an observation entity to db
*
Expand All @@ -44,9 +41,9 @@ public ObservationDAO() { }
*
* @return id (primary key)
*/
public Long saveObservation(Observation obs, long encounterID) {
ObservationEntity observationEntity = AppDatabaseHelper.convert(obs, encounterID);
return observationRoomDAO.addObservation(observationEntity);
fun saveObservation(obs: Observation, encounterID: Long): Long {
val observationEntity = convert(obs, encounterID)
return observationRoomDAO.addObservation(observationEntity)
}

/**
Expand All @@ -57,10 +54,10 @@ public Long saveObservation(Observation obs, long encounterID) {
*
* @return count of updated values
*/
public int updateObservation(Observation obs, long encounterId) {
ObservationEntity observationEntity = AppDatabaseHelper.convert(obs, encounterId);
observationEntity.setId(encounterId);
return observationRoomDAO.updateObservation(observationEntity);
fun updateObservation(obs: Observation, encounterId: Long): Int {
val observationEntity = convert(obs, encounterId)
observationEntity.id = encounterId
return observationRoomDAO.updateObservation(observationEntity)
}

/**
Expand All @@ -69,15 +66,16 @@ public int updateObservation(Observation obs, long encounterId) {
* @param encounterID the encounter id
* @return the list
*/
public List<Observation> findObservationByEncounterID(Long encounterID) {
List<Observation> observationList;
List<ObservationEntity> observationEntityList;
try {
observationEntityList = observationRoomDAO.findObservationByEncounterID(encounterID).blockingGet();
observationList = AppDatabaseHelper.convert(observationEntityList);
return observationList;
} catch (Exception e) {
return new ArrayList<>();
fun findObservationByEncounterID(encounterID: Long): List<Observation> {
val observationList: List<Observation>
val observationEntityList: List<ObservationEntity>
return try {
observationEntityList = observationRoomDAO.findObservationByEncounterID(encounterID)
.blockingGet()
observationList = convert(observationEntityList)
observationList
} catch (e: Exception) {
ArrayList<Observation>()
}
}

Expand All @@ -87,22 +85,21 @@ public List<Observation> findObservationByEncounterID(Long encounterID) {
* @param observationList the observation list to be saved
* @return the list of primary keys
*/
public List<Long> saveStandaloneObservations(List<Observation> observationList) {
List<StandaloneObservationEntity> standaloneObservationEntityList = new ArrayList<>();
for(Observation observation: observationList){
StandaloneObservationEntity standaloneObservationEntity = AppDatabaseHelper.convertToStandalone(observation);
standaloneObservationEntityList.add(standaloneObservationEntity);
fun saveStandaloneObservations(observationList: List<Observation>): List<Long> {
val standaloneObservationEntityList: MutableList<StandaloneObservationEntity> = ArrayList()
for (observation in observationList) {
val standaloneObservationEntity = convertToStandalone(observation)
standaloneObservationEntityList.add(standaloneObservationEntity)
}
return observationRoomDAO.addStandaloneObservationList(standaloneObservationEntityList);
return observationRoomDAO.addStandaloneObservationList(standaloneObservationEntityList)
}

/**
* Delete all standalone observations in the database
*
* @param patientUuid the patient uuid for which the standalone encounters should be deleted
*/
public void deleteAllStandaloneObservations(String patientUuid) {
observationRoomDAO.deleteAllStandaloneObservationsByPatientUuid(patientUuid);
fun deleteAllStandaloneObservations(patientUuid: String) {
observationRoomDAO.deleteAllStandaloneObservationsByPatientUuid(patientUuid)
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ interface ObservationRoomDAO {
* @return the single
*/
@Query("SELECT * FROM observations WHERE encounter_id = :encounterID")
fun findObservationByEncounterID(encounterID: Long): Single<List<ObservationEntity?>?>?
fun findObservationByEncounterID(encounterID: Long): Single<List<ObservationEntity>>

/**
* Gets observation by uuid.
Expand All @@ -118,7 +118,7 @@ interface ObservationRoomDAO {
* @return the observation by uuid
*/
@Query("SELECT * FROM observations WHERE uuid = :observationUUID")
fun getObservationByUUID(observationUUID: String): Single<ObservationEntity?>?
fun getObservationByUUID(observationUUID: String): Single<ObservationEntity>

/**
* Gets observation by patient uuid
Expand All @@ -127,7 +127,7 @@ interface ObservationRoomDAO {
* @return the observation
*/
@Query("SELECT * FROM observations WHERE patient_uuid = :patient_uuid")
fun getObservationForPatientByPatientUuid(patient_uuid: String): Single<ObservationEntity?>?
fun getObservationForPatientByPatientUuid(patient_uuid: String): Single<ObservationEntity>

/**
* Gets observation by patient uuid and concept uuid.
Expand All @@ -137,23 +137,23 @@ interface ObservationRoomDAO {
* @return the observation
*/
@Query("SELECT * FROM observations WHERE patient_uuid = :patient_uuid AND conceptUuid = :concept_uuid")
fun getObservationForPatientByConceptUuid(patient_uuid: String, concept_uuid: String): Single<ObservationEntity?>?
fun getObservationForPatientByConceptUuid(patient_uuid: String, concept_uuid: String): Single<ObservationEntity>

/**
* Gets all observations.
*
* @return the all observations
*/
@Query("SELECT * FROM observations")
fun getAllObservations(): Single<List<ObservationEntity?>?>?
fun getAllObservations(): Single<List<ObservationEntity>>

/**
* Gets all Standalone Observations
*
* @return all Standalone observations
*/
@Query("SELECT * FROM standaloneObservations")
fun getAllStandaloneObservations(): Single<List<StandaloneObservationEntity?>?>?
fun getAllStandaloneObservations(): Single<List<StandaloneObservationEntity>>

/**
* Gets all Standalone Observations by uuid
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ object AppDatabaseHelper {
encounter.display = entity.display
val dateTime = entity.encounterDateTime.toLong()
encounter.setEncounterDatetime(convertTime(dateTime, DateUtils.OPEN_MRS_REQUEST_FORMAT))
encounter.observations = ObservationDAO().findObservationByEncounterID(entity.id)
encounter.observations = ObservationDAO().findObservationByEncounterID(entity.id!!)
encounter.patient = PatientDAO().findPatientByUUID(entity.patientUuid)
val location: LocationEntity? = try {
AppDatabase
Expand Down

0 comments on commit 5140990

Please sign in to comment.