Skip to content

Commit

Permalink
save obs timestamps during brapi sync
Browse files Browse the repository at this point in the history
  • Loading branch information
bellerbrock committed Aug 16, 2024
1 parent 7f161d3 commit 5da4825
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
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 @@ -87,6 +86,7 @@
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.threeten.bp.OffsetDateTime;

import java.lang.reflect.Type;
import java.util.ArrayList;
Expand All @@ -108,8 +108,6 @@ 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 @@ -970,59 +968,18 @@ 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());
newObservation.setTimestamp(obs.getObservationTimeStamp().toString());

mapBrapiToLocalRep(newObservation, obs);
newObservation.setTimestamp(
OffsetDateTime.parse(
obs.getObservationTimeStamp().toString()
)
);

//search imported obs references for first field book id
List<BrAPIExternalReference> references = obs.getExternalReferences();
Expand Down Expand Up @@ -1063,15 +1020,18 @@ private List<Observation> mapObservations(List<BrAPIObservation> brapiObservatio
String internalVarId = extVariableDbIdMap.get(brapiObservation.getObservationVariableDbId());
newObservation.setVariableDbId(internalVarId);
newObservation.setValue(brapiObservation.getValue());
newObservation.setTimestamp(
OffsetDateTime.parse(
brapiObservation.getObservationTimeStamp().toString()
)
);

//Make sure we are on the right experiment level.
// This will cause bugs if there have been plot and plant level traits found as the observations retrieves all of them
if(internalUnitId != null) {
outputList.add(newObservation);
}

mapBrapiToLocalRep(newObservation, brapiObservation);

}
return outputList;
}
Expand Down Expand Up @@ -1185,8 +1145,6 @@ 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 @@ -2978,9 +2978,7 @@ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

if (oldVersion <= 11 && newVersion >= 12) {
// Create the ObservationDetails view
Log.w("DataHelper", "Creating ObservationDetails View. View def is:");
Log.w("DataHelper", ObservationDao.Companion.getSObservationsDetailView());
// db.execSQL(ObservationDao.Companion.getSObservationsDetailView());
db.execSQL(ObservationDao.Companion.getSObservationsDetailView());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ class ObservationDao {
"observation_variable_name" to model.variableName,
"observation_variable_field_book_format" to variableFormat,
"value" to model.value,
"observation_time_stamp" to model.timestamp,
"observation_time_stamp" to model.timestamp.toString(),
"collector" to model.collector,
// "geoCoordinates" to model.geo_coordinates,
"geoCoordinates" to null,
Expand Down

0 comments on commit 5da4825

Please sign in to comment.