Skip to content

Commit

Permalink
Merge pull request #711 from abatac/bms_integration_fix
Browse files Browse the repository at this point in the history
Bms integration fix
  • Loading branch information
trife authored Aug 23, 2023
2 parents d4d6a9c + ca106cb commit 9b95171
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,12 @@ public Void apply(final BrapiStudyDetails study) {
@Override
public void run() {
BrapiStudyDetails.merge(studyDetails, study);

// This is BMS specific. Remove the traits that are not part of the selected Observation Level.
// To ensure that only relevant traits are included in the imported study/field.
studyDetails.getTraits().removeIf(t -> t.getObservationLevelNames() != null &&
t.getObservationLevelNames().stream().noneMatch(s -> s.equalsIgnoreCase(selectedObservationLevel.getObservationLevelName())));

loadStudy();
// Check if user should save yet
traitLoadStatus = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
import com.fieldbook.tracker.utilities.CategoryJsonUtil;
import com.fieldbook.tracker.utilities.FailureFunction;
import com.fieldbook.tracker.utilities.SuccessFunction;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.reflect.TypeToken;

import org.brapi.client.v2.BrAPIClient;
import org.brapi.client.v2.model.exceptions.ApiException;
Expand Down Expand Up @@ -68,6 +71,7 @@
import org.json.JSONException;
import org.json.JSONObject;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
Expand All @@ -79,6 +83,8 @@

public class BrAPIServiceV2 extends AbstractBrAPIService implements BrAPIService {

private static final String ADDITIONAL_INFO_OBSERVATION_LEVEL_NAMES = "observationLevelNames";

//used to identify field book db id in external references
private final String fieldBookReferenceSource = "Field Book Upload";

Expand Down Expand Up @@ -1017,6 +1023,16 @@ private Pair<List<TraitObject>, Integer> mapTraits(List<BrAPIObservationVariable

}

// The BMS implementation of BrAPI 2.x Variables includes an Observation Variable with observationLevelNames metadata in the additionalInfo field.
// This metadata helps identify the level(s) at which a variable is utilized within a study/field. The information will be utilized to filter the variables
// based on the selected Observation Level during BrAPI Field Import, ensuring that only relevant variables will be processed.
if (var.getAdditionalInfo() != null && var.getAdditionalInfo().has(ADDITIONAL_INFO_OBSERVATION_LEVEL_NAMES)) {
JsonArray observationVariableNames = var.getAdditionalInfo().getAsJsonArray(ADDITIONAL_INFO_OBSERVATION_LEVEL_NAMES);
// Convert the JsonArray to a List<String>
Type listType = new TypeToken<List<String>>() {}.getType();
trait.setObservationLevelNames(new Gson().fromJson(observationVariableNames, listType));
}

// Set some config variables in fieldbook
trait.setVisible(true);
trait.setRealPosition(0);
Expand Down
16 changes: 16 additions & 0 deletions app/src/main/java/com/fieldbook/tracker/objects/TraitObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

import androidx.annotation.NonNull;

import java.util.List;

/**
* Simple wrapper class for trait data
*/
Expand All @@ -22,6 +24,12 @@ public class TraitObject {
private String traitDataSource;
private String additionalInfo;

/**
* This is a BMS specific field. This will be populated when traits are imported from
* the BMS implementation of Brapi 2.0 GET /variables.
*/
private List<String> observationLevelNames;

public String getTrait() {
return trait;
}
Expand Down Expand Up @@ -126,6 +134,14 @@ public void setAdditionalInfo(String additionalInfo) {
this.additionalInfo = additionalInfo;
}

public List<String> getObservationLevelNames() {
return observationLevelNames;
}

public void setObservationLevelNames(List<String> observationLevelNames) {
this.observationLevelNames = observationLevelNames;
}

public boolean isValidValue(final String s) {
// this code is not perfect.
// I think that it is necessary to check
Expand Down

0 comments on commit 9b95171

Please sign in to comment.