From 8a0df7198321b06de5d49704cd38dc4f49ec7c63 Mon Sep 17 00:00:00 2001 From: chaneylc Date: Tue, 16 Jul 2024 14:16:40 -0500 Subject: [PATCH] project v5 fix #548 added repeated values to brapi sync feature --- .../tracker/brapi/service/BrAPIServiceV2.java | 55 +++++++++++++++++++ .../tracker/database/dao/ObservationDao.kt | 6 +- 2 files changed, 58 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/com/fieldbook/tracker/brapi/service/BrAPIServiceV2.java b/app/src/main/java/com/fieldbook/tracker/brapi/service/BrAPIServiceV2.java index c19c7838e..49f222b4d 100644 --- a/app/src/main/java/com/fieldbook/tracker/brapi/service/BrAPIServiceV2.java +++ b/app/src/main/java/com/fieldbook/tracker/brapi/service/BrAPIServiceV2.java @@ -32,6 +32,7 @@ import com.fieldbook.tracker.utilities.SuccessFunction; import com.google.gson.Gson; import com.google.gson.JsonArray; +import com.google.gson.JsonObject; import com.google.gson.reflect.TypeToken; import org.brapi.client.v2.ApiResponse; import org.brapi.client.v2.BrAPIClient; @@ -107,6 +108,8 @@ public class BrAPIServiceV2 extends AbstractBrAPIService implements BrAPIService private static final String ADDITIONAL_INFO_OBSERVATION_LEVEL_NAMES = "observationLevelNames"; + private final String REPEATED_VALUE_INDEX_KEY = "rep"; + //used to identify field book db id in external references private final String fieldBookReferenceSource = "Field Book Upload"; @@ -967,12 +970,60 @@ private Map getExtUnitDbIdMapping(){ return externalIdToInternalMap; } + private void mapLocalRepToBrapi(Observation observation, BrAPIObservation brapiObservation) { + + try { + + JsonObject info = brapiObservation.getAdditionalInfo(); + + if (info == null) { + + info = new JsonObject(); + + } + + if (observation.getRep() != null) { + + info.addProperty(REPEATED_VALUE_INDEX_KEY, observation.getRep()); + + } + + brapiObservation.additionalInfo(info); + + } catch (Exception e) { + + e.printStackTrace(); + + } + } + + private void mapBrapiToLocalRep(Observation observation, BrAPIObservation brapiObservation) { + + try { + + JsonObject info = brapiObservation.getAdditionalInfo(); + + if (info != null && info.has(REPEATED_VALUE_INDEX_KEY)) { + + observation.setRep(info.get(REPEATED_VALUE_INDEX_KEY).getAsString()); + + } + + } catch (Exception e) { + + e.printStackTrace(); + + } + } + private Observation mapToObservation(BrAPIObservation obs){ Observation newObservation = new Observation(); newObservation.setDbId(obs.getObservationDbId()); newObservation.setUnitDbId(obs.getObservationUnitDbId()); newObservation.setVariableDbId(obs.getObservationVariableDbId()); + mapBrapiToLocalRep(newObservation, obs); + //search imported obs references for first field book id List references = obs.getExternalReferences(); if (references != null && !references.isEmpty()) { @@ -1019,6 +1070,8 @@ private List mapObservations(List brapiObservatio outputList.add(newObservation); } + mapBrapiToLocalRep(newObservation, brapiObservation); + } return outputList; } @@ -1132,6 +1185,8 @@ private BrAPIObservation convertToBrAPIObservation(Observation observation){ newObservation.setExternalReferences(Collections.singletonList(reference)); + mapLocalRepToBrapi(observation, newObservation); + return newObservation; } diff --git a/app/src/main/java/com/fieldbook/tracker/database/dao/ObservationDao.kt b/app/src/main/java/com/fieldbook/tracker/database/dao/ObservationDao.kt index b1bd7cb2f..89b54add4 100644 --- a/app/src/main/java/com/fieldbook/tracker/database/dao/ObservationDao.kt +++ b/app/src/main/java/com/fieldbook/tracker/database/dao/ObservationDao.kt @@ -378,14 +378,14 @@ class ObservationDao { fun insertObservation(studyId: Int, model: BrapiObservation, traitIdToTypeMap:Map): Int = withDatabase { db -> - if (getObservation("$studyId", model.unitDbId, model.variableDbId, "1")?.dbId != null) { + if (getObservation("$studyId", model.unitDbId, model.variableDbId, model.rep ?: "1")?.dbId != null) { println( "DbId: ${ getObservation( "$studyId", model.unitDbId, model.variableDbId, - "1" + model.rep ?: "1" )?.dbId }" ) @@ -406,7 +406,7 @@ class ObservationDao { // "additional_info" to model.additional_info, "additional_info" to null, "observation_db_id" to model.dbId, - "rep" to "1", + "rep" to model.rep, Study.FK to studyId, ObservationUnit.FK to model.unitDbId, ObservationVariable.FK to model.variableDbId