Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include sortBy parameter in BrAPI field import #902

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public class BrapiActivity extends ThemedActivity {
private BrapiStudyDetails selectedStudy;

BrapiLoadDialog brapiLoadDialog;
private String sortBy;
private String sortOrder;

// Filter by
private String programDbId;
Expand Down Expand Up @@ -79,6 +81,10 @@ public void onCreate(Bundle savedInstanceState) {
TextView baseURLText = findViewById(R.id.brapiBaseURL);
baseURLText.setText(brapiBaseURL);

// Set the default sort options from the corresponding values array
String[] apiValues = getResources().getStringArray(R.array.brapi_study_sort_values);
if (apiValues.length > 0) setSortParameters(apiValues[0]);

loadToolbar();
loadObservationLevels();
} else {
Expand Down Expand Up @@ -107,7 +113,28 @@ private void loadToolbar() {
}
}

private void setupObservationLevelsSpinner() {
private void setupSpinners() {

Spinner sortSpinner = findViewById(R.id.sortSpinner);
ArrayAdapter<CharSequence> sortAdapter = ArrayAdapter.createFromResource(this,
R.array.brapi_study_sort_options, android.R.layout.simple_spinner_item);
sortAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sortSpinner.setAdapter(sortAdapter);
sortSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String[] apiValues = getResources().getStringArray(R.array.brapi_study_sort_values);
if (position < apiValues.length) {
String selectedValue = apiValues[position];
setSortParameters(selectedValue);
loadStudiesList();
}
}

@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});

if (!observationLevels.isEmpty()) {
selectedObservationLevel = observationLevels.get(0);
Expand Down Expand Up @@ -142,8 +169,8 @@ private void loadStudiesList() {
//init page numbers
paginationManager.refreshPageIndicator();
Integer initPage = paginationManager.getPage();

brAPIService.getStudies(this.programDbId, this.trialDbId, paginationManager, new Function<List<BrapiStudyDetails>, Void>() {
brAPIService.getStudies(this.programDbId, this.trialDbId, this.sortBy, this.sortOrder, paginationManager, new Function<List<BrapiStudyDetails>, Void>() {
@Override
public Void apply(final List<BrapiStudyDetails> studies) {

Expand Down Expand Up @@ -195,7 +222,7 @@ private void loadObservationLevels() {
brAPIService.getObservationLevels(programDbId, input -> {
this.observationLevels = input;
runOnUiThread(() -> {
setupObservationLevelsSpinner();
setupSpinners();
loadStudiesList();
});
}, failureInput -> {
Expand Down Expand Up @@ -241,6 +268,14 @@ private void saveStudy() {
}
}

private void setSortParameters(String sortValue) {
String[] parts = sortValue.split(" ");
if (parts.length == 2) {
sortBy = parts[0];
sortOrder = parts[1];
}
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
int itemId = item.getItemId();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ static void handleConnectionError(Context context, int code) {

void getTrials(String programDbId, BrapiPaginationManager paginationManager, final Function<List<BrapiTrial>, Void> function, final Function<Integer, Void> failFunction);

void getStudies(String programDbId, String trialDbId, BrapiPaginationManager paginationManager, final Function<List<BrapiStudyDetails>, Void> function, final Function<Integer, Void> failFunction);
void getStudies(String programDbId, String trialDbId, String sortBy, String sortOrder, BrapiPaginationManager paginationManager, final Function<List<BrapiStudyDetails>, Void> function, final Function<Integer, Void> failFunction);

void getStudyDetails(final String studyDbId, final Function<BrapiStudyDetails, Void> function, final Function<Integer, Void> failFunction);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ private List<BrapiTrial> mapTrials(List<TrialSummary> trialList) {
return brapiTrials;
}

public void getStudies(String programDbId, String trialDbId, BrapiPaginationManager paginationManager,
public void getStudies(String programDbId, String trialDbId, String sortBy, String sortOrder, BrapiPaginationManager paginationManager,
final Function<List<BrapiStudyDetails>, Void> function,
final Function<Integer, Void> failFunction) {
Integer initPage = paginationManager.getPage();
Expand Down Expand Up @@ -384,7 +384,7 @@ public void onFailure(ApiException error, int i, Map<String, List<String>> map)
studiesApi.studiesGetAsync(
null, null, null, programDbId,
null, null, trialDbId, null, null,
null, true, null, null,
null, true, sortBy, sortOrder,
paginationManager.getPage(), paginationManager.getPageSize(), getBrapiToken(), callback);

} catch (ApiException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ private List<BrapiTrial> mapTrials(List<BrAPITrial> trialList) {
return brapiTrials;
}

public void getStudies(String programDbId, String trialDbId, BrapiPaginationManager paginationManager,
public void getStudies(String programDbId, String trialDbId, String sortBy, String sortOrder, BrapiPaginationManager paginationManager,
final Function<List<BrapiStudyDetails>, Void> function,
final Function<Integer, Void> failFunction) {
Integer initPage = paginationManager.getPage();
Expand Down Expand Up @@ -422,7 +422,7 @@ public void onFailure(ApiException error, int i, Map<String, List<String>> map)
};

StudyQueryParams queryParams = new StudyQueryParams();
queryParams.active("true").programDbId(programDbId).trialDbId(trialDbId).page(paginationManager.getPage()).pageSize(paginationManager.getPageSize());
queryParams.active("true").programDbId(programDbId).trialDbId(trialDbId).sortBy(sortBy).sortOrder(sortOrder).page(paginationManager.getPage()).pageSize(paginationManager.getPageSize());
studiesApi.studiesGetAsync(queryParams, callback);

} catch (ApiException error) {
Expand Down
48 changes: 36 additions & 12 deletions app/src/main/res/layout/activity_brapi.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,48 +10,72 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:minHeight="?attr/actionBarSize" />
android:minHeight="?attr/actionBarSize"
android:layout_marginBottom="10dp"/>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:orientation="horizontal">
android:orientation="horizontal"
android:layout_marginBottom="16dp">

<TextView
android:id="@+id/brapiBaseURLLbl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingBottom="5dp"
android:text="@string/brapi_base_url" />

<TextView
android:id="@+id/brapiBaseURL"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
android:layout_height="wrap_content"
android:text="Base URL Here" />
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginBottom="10dp">

<TextView
android:id="@+id/sortLbl"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:text="@string/brapi_study_sort" />

<Spinner
android:id="@+id/sortSpinner"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3" />
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginBottom="10dp">

<TextView
android:id="@+id/observationLevelLbl"
android:layout_width="wrap_content"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:paddingLeft="10dp"
android:paddingBottom="5dp"
android:paddingRight="10dp"
android:text="@string/brapi_observation_level" />

<Spinner
android:id="@+id/studyObservationLevels"
android:layout_width="wrap_content"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingBottom="10dp"
android:paddingStart="10dp" />
android:layout_weight="3" />
</LinearLayout>

<LinearLayout
Expand Down
18 changes: 17 additions & 1 deletion app/src/main/res/values/array.xml
Original file line number Diff line number Diff line change
Expand Up @@ -172,12 +172,28 @@
<item>3</item>
</string-array>


<!-- degrees of precision to be used in spinner -->
<string-array name="dialog_geonav_collect_degrees_of_precision">
<item>Any</item>
<item>GPS</item>
<item>RTK</item>
<item>Float RTK</item>
</string-array>

<!-- BrAPI sort display names -->
<string-array name="brapi_study_sort_options">
<item>@string/brapi_sort_study_name_asc</item>
<item>@string/brapi_sort_study_name_desc</item>
<item>@string/brapi_sort_study_id_asc</item>
<item>@string/brapi_sort_study_id_desc</item>
</string-array>

<!-- Corresponding API values -->
<string-array name="brapi_study_sort_values">
<item>studyName ASC</item>
<item>studyName DESC</item>
<item>studyDbId ASC</item>
<item>studyDbId DESC</item>
</string-array>

</resources>
5 changes: 5 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -949,6 +949,11 @@
<string name="brapi_study_name">Name</string>
<string name="brapi_study_description">Description</string>
<string name="brapi_study_location">Location</string>
<string name="brapi_study_sort">Sort</string>
<string name="brapi_sort_study_name_asc">Study Name Ascending</string>
<string name="brapi_sort_study_name_desc">Study Name Descending</string>
<string name="brapi_sort_study_id_asc">Study ID Ascending</string>
<string name="brapi_sort_study_id_desc">Study ID Descending</string>
<string name="brapi_observation_level">Observation Level</string>
<string name="brapi_study_num_traits"> Traits</string>
<string name="brapi_not_found">ERROR: Target system does not support this operation via BrAPI.</string>
Expand Down
5 changes: 3 additions & 2 deletions app/src/test/java/BrapiServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@
public class BrapiServiceTest {

private static final String BRAPI_URL = "https://test-server.brapi.org";

private static final String SORT_BY = "studyName";
private static final String SORT_ORDER = "ASC";
private BrAPIService brAPIService;
private final String programDbId;
private final String trialDbId;
Expand Down Expand Up @@ -120,7 +121,7 @@ public void checkGetStudies() {

BrapiPaginationManager pageMan = new BrapiPaginationManager(0, 1000);
// Call our get studies endpoint with the same parsing that our classes use.
this.brAPIService.getStudies(null, this.trialDbId, pageMan, new Function<List<BrapiStudyDetails>, Void>() {
this.brAPIService.getStudies(null, this.trialDbId, SORT_BY, SORT_ORDER, pageMan, new Function<List<BrapiStudyDetails>, Void>() {
@Override
public Void apply(List<BrapiStudyDetails> input) {
// Check that there is atleast one study returned.
Expand Down