Skip to content

Commit

Permalink
project v5
Browse files Browse the repository at this point in the history
fix #548
added repeated values to brapi sync feature
  • Loading branch information
chaneylc committed Jul 16, 2024
1 parent b0b3185 commit 8a0df71
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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";

Expand Down Expand Up @@ -967,12 +970,60 @@ private Map<String,String> 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<BrAPIExternalReference> references = obs.getExternalReferences();
if (references != null && !references.isEmpty()) {
Expand Down Expand Up @@ -1019,6 +1070,8 @@ private List<Observation> mapObservations(List<BrAPIObservation> brapiObservatio
outputList.add(newObservation);
}

mapBrapiToLocalRep(newObservation, brapiObservation);

}
return outputList;
}
Expand Down Expand Up @@ -1132,6 +1185,8 @@ private BrAPIObservation convertToBrAPIObservation(Observation observation){

newObservation.setExternalReferences(Collections.singletonList(reference));

mapLocalRepToBrapi(observation, newObservation);

return newObservation;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -378,14 +378,14 @@ class ObservationDao {

fun insertObservation(studyId: Int, model: BrapiObservation, traitIdToTypeMap:Map<String,String>): 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
}"
)
Expand All @@ -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
Expand Down

0 comments on commit 8a0df71

Please sign in to comment.