Skip to content
Open
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
24 changes: 24 additions & 0 deletions src/main/java/io/swagger/model/FilterBy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.swagger.model;

public class FilterBy {

private String filterOn;
private String value;

public String getFilterOn() {
return filterOn;
}

public void setFilterOn(String filterOn) {
this.filterOn = filterOn;
}

public String getValue() {
return value;
}

public void setValue(String value) {
this.value = value;
}

}
91 changes: 91 additions & 0 deletions src/main/java/io/swagger/model/SearchRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonSetter;
import io.swagger.model.sort.SortBy;
import org.brapi.test.BrAPITestServer.exceptions.BrAPIServerException;
import org.springframework.http.HttpStatus;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public abstract class SearchRequest {
@JsonIgnore
Expand All @@ -25,6 +30,20 @@ public abstract class SearchRequest {
@JsonProperty("externalReferenceSources")
protected List<String> externalReferenceSources = null;

@JsonProperty("filterBy")
protected List<FilterBy> filterBy = null;

@JsonProperty("sortBy")
protected List<SortBy> sortBy = null;

@JsonIgnore
protected Map<String, String> sortFilterEntityColumnNamesByRequestName = null;

@JsonIgnore
public List<String> getExternalReferenceIds() {
return externalReferenceIds;
}

final public SearchRequest page(Integer page) {
this.page = page;
return this;
Expand Down Expand Up @@ -119,4 +138,76 @@ public void addExternalReferenceItem(String externalReferenceId, String external
}

}

public List<FilterBy> getFilterBy() {
return filterBy;
}

public void setFilterBy(List<FilterBy> filterBy) throws BrAPIServerException {

if (filterBy == null || filterBy.isEmpty()) {
return;
}

Map<String, String> allowedSortFilterNames = getSortFilterEntityColumnNamesByRequestName();

for (FilterBy filterByItem : filterBy) {

if (filterByItem.getFilterOn() == null || filterByItem.getFilterOn().isEmpty()) {
throw new BrAPIServerException(HttpStatus.BAD_REQUEST, "filterOn attribute not provided in element of filterBy list.");
}

if (filterByItem.getValue() == null || filterByItem.getValue().isEmpty()) {
throw new BrAPIServerException(HttpStatus.BAD_REQUEST, "value attribute not provided in element of filterBy list.");
}

String filterColumnEntityName = getSortFilterEntityColumnNamesByRequestName().get(filterByItem.getFilterOn());

if (filterColumnEntityName == null) {
throw new BrAPIServerException(HttpStatus.BAD_REQUEST,
String.format("Supplied filterColumn [%s] not available in allowed names [%s]", filterByItem.getFilterOn(), allowedSortFilterNames.keySet())
);
} else {
// Remap suppliedFilterColumn to actual entity name supplied by mapper
filterByItem.setFilterOn(filterColumnEntityName);
}
}
this.filterBy = filterBy;
}

public List<SortBy> getSortByElements() {
return sortBy;
}

public void setSortBy(List<SortBy> sortBy) throws BrAPIServerException {

if (sortBy == null || sortBy.isEmpty()) {
return;
}

Map<String, String> allowedSortFilterNames = getSortFilterEntityColumnNamesByRequestName();

for (SortBy sortByItem : sortBy) {
if (sortByItem.getSortedOn() == null || sortByItem.getSortedOn().isEmpty()) {
throw new BrAPIServerException(HttpStatus.BAD_REQUEST, "sortedOn attribute not provided in element of sortBy list");
}

String sortColumnEntityName = getSortFilterEntityColumnNamesByRequestName().get(sortByItem.getSortedOn());

if (sortColumnEntityName == null) {
throw new BrAPIServerException(HttpStatus.BAD_REQUEST,
String.format("Supplied sortColumn [%s] not available in allowed names [%s]", sortByItem.getSortedOn(), allowedSortFilterNames.keySet())
);
} else {
// Remap suppliedFilterColumn to actual entity name supplied by mapper
sortByItem.setSortedOn(sortColumnEntityName);
}
}

this.sortBy = sortBy;
}

public Map<String, String> getSortFilterEntityColumnNamesByRequestName() {
throw new UnsupportedOperationException(String.format("Sort/Filtering not implemented for %s", this.getClass().getSimpleName()));
}
}
56 changes: 0 additions & 56 deletions src/main/java/io/swagger/model/core/SortBy.java

This file was deleted.

63 changes: 26 additions & 37 deletions src/main/java/io/swagger/model/core/StudySearchRequest.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,34 @@
package io.swagger.model.core;

import java.util.Map;
import java.util.Objects;

import com.fasterxml.jackson.annotation.JsonProperty;

import io.swagger.model.SearchRequest;

import java.util.ArrayList;
import java.util.List;

public class StudySearchRequest extends SearchRequest {

// Key - allowed sort or field filter name for this entity
// Value = entity field name that represents the submitted field. Used later on in query building.
private static final Map<String, String> ALLOWED_SORT_AND_FILTER_FIELDS =
Map.ofEntries(
Map.entry("germplasmDbId", "*obsunit.germplasm.id"),
Map.entry("locationDbId", "location.id"),
Map.entry("observationVariableDbId", "*observation.observationVariable.id"),
Map.entry("programDbId", "trial.program.id"),
Map.entry("programName", "trial.program.name"),
Map.entry("seasonDbId", "*season.id"),
Map.entry("studyDbId", "id"),
Map.entry("studyLocation", "location.id"),
Map.entry("trialDbId", "trial.id"),
Map.entry("studyType", "studyName"),
Map.entry("studyName", "studyName")
);

@JsonProperty("commonCropNames")
private List<String> commonCropNames = null;

Expand Down Expand Up @@ -54,12 +74,6 @@ public class StudySearchRequest extends SearchRequest {
@JsonProperty("seasonDbIds")
private List<String> seasonDbIds = null;

@JsonProperty("sortBy")
private SortBy sortBy = null;

@JsonProperty("sortOrder")
private SortOrder sortOrder = null;

@JsonProperty("studyCodes")
private List<String> studyCodes = null;

Expand Down Expand Up @@ -376,32 +390,6 @@ public void setSeasonDbIds(List<String> seasonDbIds) {
this.seasonDbIds = seasonDbIds;
}

public StudySearchRequest sortBy(SortBy sortBy) {
this.sortBy = sortBy;
return this;
}

public SortBy getSortBy() {
return sortBy;
}

public void setSortBy(SortBy sortBy) {
this.sortBy = sortBy;
}

public StudySearchRequest sortOrder(SortOrder sortOrder) {
this.sortOrder = sortOrder;
return this;
}

public SortOrder getSortOrder() {
return sortOrder;
}

public void setSortOrder(SortOrder sortOrder) {
this.sortOrder = sortOrder;
}

public StudySearchRequest studyCodes(List<String> studyCodes) {
this.studyCodes = studyCodes;
return this;
Expand Down Expand Up @@ -492,7 +480,6 @@ public boolean equals(java.lang.Object o) {
&& Objects.equals(this.active, studySearchRequest.active)
&& Objects.equals(this.seasonDbIds, studySearchRequest.seasonDbIds)
&& Objects.equals(this.sortBy, studySearchRequest.sortBy)
&& Objects.equals(this.sortOrder, studySearchRequest.sortOrder)
&& Objects.equals(this.studyCodes, studySearchRequest.studyCodes)
&& Objects.equals(this.studyPUIs, studySearchRequest.studyPUIs)
&& Objects.equals(this.studyTypes, studySearchRequest.studyTypes) && super.equals(o);
Expand All @@ -503,7 +490,7 @@ public int hashCode() {
return Objects.hash(commonCropNames, programDbIds, programNames, trialDbIds, trialNames, studyDbIds, studyNames,
locationDbIds, locationNames, germplasmDbIds, germplasmNames, observationVariableDbIds,
observationVariableNames, externalReferenceIds, externalReferenceSources, active, seasonDbIds, sortBy,
sortOrder, studyCodes, studyPUIs, studyTypes, super.hashCode());
studyCodes, studyPUIs, studyTypes, super.hashCode());
}

@Override
Expand All @@ -529,7 +516,6 @@ public String toString() {
sb.append(" active: ").append(toIndentedString(active)).append("\n");
sb.append(" seasonDbIds: ").append(toIndentedString(seasonDbIds)).append("\n");
sb.append(" sortBy: ").append(toIndentedString(sortBy)).append("\n");
sb.append(" sortOrder: ").append(toIndentedString(sortOrder)).append("\n");
sb.append(" studyCodes: ").append(toIndentedString(studyCodes)).append("\n");
sb.append(" studyPUIs: ").append(toIndentedString(studyPUIs)).append("\n");
sb.append(" studyTypes: ").append(toIndentedString(studyTypes)).append("\n");
Expand Down Expand Up @@ -583,8 +569,6 @@ public Integer getTotalParameterCount() {
count += this.seasonDbIds.size();
if (this.sortBy != null)
count += 1;
if (this.sortOrder != null)
count += 1;
if (this.studyCodes != null)
count += this.studyCodes.size();
if (this.studyPUIs != null)
Expand All @@ -593,4 +577,9 @@ public Integer getTotalParameterCount() {
count += this.studyTypes.size();
return count;
}

@Override
public Map<String, String> getSortFilterEntityColumnNamesByRequestName() {
return ALLOWED_SORT_AND_FILTER_FIELDS;
}
}
48 changes: 23 additions & 25 deletions src/main/java/io/swagger/model/core/TrialSearchRequest.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,28 @@
package io.swagger.model.core;

import java.util.Objects;
import java.util.*;

import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.model.SearchRequest;
import java.util.ArrayList;
import java.util.List;

import java.time.LocalDate;

public class TrialSearchRequest extends SearchRequest {

// Key - allowed sort or field filter name for this entity
// Value = entity field name that represents the submitted field. Used later on in query building.
private static final Map<String, String> ALLOWED_SORT_AND_FILTER_FIELDS =
Map.of(
"trialName", "trialName",
"createdDate", "createdDate",
"createdBy", "createdBy",
"trialDbId", "id",
"programDbId","program.id",
"startDate", "startDate",
"endDate", "endDate",
"active", "active"
);

@JsonProperty("commonCropNames")
private List<String> commonCropNames = null;

Expand Down Expand Up @@ -50,28 +65,6 @@ public class TrialSearchRequest extends SearchRequest {
@JsonProperty("trialPUIs")
private List<String> trialPUIs = null;

@JsonProperty("sortBy")
private SortBy sortBy = null;

@JsonProperty("sortOrder")
private SortOrder sortOrder = null;

public SortBy getSortBy() {
return sortBy;
}

public void setSortBy(SortBy sortBy) {
this.sortBy = sortBy;
}

public SortOrder getSortOrder() {
return sortOrder;
}

public void setSortOrder(SortOrder sortOrder) {
this.sortOrder = sortOrder;
}

public TrialSearchRequest commonCropNames(List<String> commonCropNames) {
this.commonCropNames = commonCropNames;
return this;
Expand Down Expand Up @@ -445,4 +438,9 @@ public Integer getTotalParameterCount() {
count += this.trialPUIs.size();
return count;
}

@Override
public Map<String, String> getSortFilterEntityColumnNamesByRequestName() {
return ALLOWED_SORT_AND_FILTER_FIELDS;
}
}
Loading