Skip to content

Commit

Permalink
added the parameters for excluding hazard from dataset GET (#24)
Browse files Browse the repository at this point in the history
#24 added the parameters for excluding hazard from dataset GET
  • Loading branch information
ywkim312 committed Oct 20, 2021
1 parent 6c806f5 commit 36472f9
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -141,16 +141,17 @@ public List<Dataset> getDatasets(@ApiParam(value = "DataType of IN-CORE datasets
@ApiParam(value = "Username of the creator", required = false) @QueryParam("creator") String creator,
@ApiParam(value = "Name of space") @DefaultValue("") @QueryParam("space") String spaceName,
@ApiParam(value = "Skip the first n results") @QueryParam("skip") int offset,
@ApiParam(value = "Limit no of results to return") @DefaultValue("100") @QueryParam("limit") int limit) {
@ApiParam(value = "Limit no of results to return") @DefaultValue("100") @QueryParam("limit") int limit,
@ApiParam(value = "Exclusion of the hazard dataset") @DefaultValue("true") @QueryParam("excludeHazard") boolean excludeHazard ){
List<Dataset> datasets;
if (typeStr != null && titleStr == null) { // query only for the type
datasets = repository.getDatasetByType(typeStr);
datasets = repository.getDatasetByType(typeStr, excludeHazard);
} else if (typeStr == null && titleStr != null) { // query only for the title
datasets = repository.getDatasetByTitle(titleStr);
datasets = repository.getDatasetByTitle(titleStr, excludeHazard);
} else if (typeStr != null && titleStr != null) { // query for both type and title
datasets = repository.getDatasetByTypeAndTitle(typeStr, titleStr);
datasets = repository.getDatasetByTypeAndTitle(typeStr, titleStr, excludeHazard);
} else {
datasets = repository.getAllDatasets();
datasets = repository.getAllDatasets(excludeHazard);
}

if (datasets == null) {
Expand Down Expand Up @@ -858,7 +859,8 @@ public Object updateObject(@ApiParam(value = "Dataset Id from data service", req
})
public List<Dataset> findDatasets(@ApiParam(value = "Text to search by", example = "building") @QueryParam("text") String text,
@ApiParam(value = "Skip the first n results") @QueryParam("skip") int offset,
@ApiParam(value = "Limit no of results to return") @DefaultValue("100") @QueryParam("limit") int limit) {
@ApiParam(value = "Limit no of results to return") @DefaultValue("100") @QueryParam("limit") int limit,
@ApiParam(value = "Exclusion of the hazard dataset") @DefaultValue("true") @QueryParam("excludeHazard") boolean excludeHazard) {
List<Dataset> datasets;

Dataset ds = repository.getDatasetById(text);
Expand All @@ -867,7 +869,7 @@ public List<Dataset> findDatasets(@ApiParam(value = "Text to search by", example
add(ds);
}};
} else {
datasets = this.repository.searchDatasets(text);
datasets = this.repository.searchDatasets(text, excludeHazard);
}

Set<String> membersSet = authorizer.getAllMembersUserHasReadAccessTo(this.username, spaceRepository.getAllSpaces());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,19 @@
public interface IRepository {
void initialize();

List<Dataset> getAllDatasets();
List<Dataset> getAllDatasets(Boolean excludeHazard);

Dataset getDatasetById(String id);

List<Dataset> getDatasetByType(String type);
List<Dataset> getDatasetByType(String type, Boolean excludeHazard);

List<Dataset> getDatasetByTitle(String title);
List<Dataset> getDatasetByTitle(String title, Boolean excludeHazard);

List<Dataset> getDatasetByCreator(String creator, Boolean withHazard);

List<Dataset> getDatasetByTypeAndTitle(String type, String title);
List<Dataset> getDatasetByTypeAndTitle(String type, String title, Boolean excludeHazard);

List<Dataset> searchDatasets(String text);
List<Dataset> searchDatasets(String text, Boolean excludeHazard);

Dataset addDataset(Dataset dataset);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,14 @@ public void initialize() {
this.initializeDataStore();
}

public List<Dataset> getAllDatasets() {
return this.dataStore.find(Dataset.class).iterator().toList();
public List<Dataset> getAllDatasets(Boolean excludeHazard) {
if (excludeHazard) {
Query<Dataset> datasetQuery = this.dataStore.find(Dataset.class).filter(
Filters.nin(DATASET_FIELD_TYPE, HazardConstants.DATA_TYPE_HAZARD));
return datasetQuery.iterator().toList();
} else {
return this.dataStore.find(Dataset.class).iterator().toList();
}
}

public List<MvzDataset> getAllMvzDatasets() {
Expand All @@ -83,16 +89,32 @@ public Dataset getDatasetById(String id) {
return this.dataStore.find(Dataset.class).filter(Filters.eq("_id", new ObjectId(id))).first();
}

public List<Dataset> getDatasetByType(String type) {
Query<Dataset> datasetQuery = this.dataStore.find(Dataset.class)
.filter(Filters.regex(DATASET_FIELD_TYPE).pattern(type).caseInsensitive());
return datasetQuery.iterator().toList();
public List<Dataset> getDatasetByType(String type, Boolean excludeHazard) {
if (excludeHazard) {
Query<Dataset> datasetQuery = this.dataStore.find(Dataset.class).filter(Filters.and(
Filters.regex(DATASET_FIELD_TYPE).pattern(type).caseInsensitive(),
Filters.nin(DATASET_FIELD_TYPE, HazardConstants.DATA_TYPE_HAZARD)
));
return datasetQuery.iterator().toList();
} else {
Query<Dataset> datasetQuery = this.dataStore.find(Dataset.class)
.filter(Filters.regex(DATASET_FIELD_TYPE).pattern(type).caseInsensitive());
return datasetQuery.iterator().toList();
}
}

public List<Dataset> getDatasetByTitle(String title) {
Query<Dataset> datasetQuery = this.dataStore.find(Dataset.class)
.filter(Filters.regex(DATASET_FIELD_TITLE).pattern(title).caseInsensitive());
return datasetQuery.iterator().toList();
public List<Dataset> getDatasetByTitle(String title, Boolean excludeHazard) {
if (excludeHazard) {
Query<Dataset> datasetQuery = this.dataStore.find(Dataset.class).filter(Filters.and(
Filters.regex(DATASET_FIELD_TITLE).pattern(title).caseInsensitive(),
Filters.nin(DATASET_FIELD_TYPE, HazardConstants.DATA_TYPE_HAZARD)
));
return datasetQuery.iterator().toList();
} else {
Query<Dataset> datasetQuery = this.dataStore.find(Dataset.class)
.filter(Filters.regex(DATASET_FIELD_TITLE).pattern(title).caseInsensitive());
return datasetQuery.iterator().toList();
}
}

public List<Dataset> getDatasetByCreator(String creator, Boolean withHazard) {
Expand All @@ -111,12 +133,21 @@ public List<Dataset> getDatasetByCreator(String creator, Boolean withHazard) {
return datasetQuery.iterator().toList();
}

public List<Dataset> getDatasetByTypeAndTitle(String type, String title) {
Query<Dataset> datasetQuery = this.dataStore.find(Dataset.class).filter(Filters.and(
Filters.regex(DATASET_FIELD_TYPE).pattern(type).caseInsensitive(),
Filters.regex(DATASET_FIELD_TITLE).pattern(title).caseInsensitive()
));
return datasetQuery.iterator().toList();
public List<Dataset> getDatasetByTypeAndTitle(String type, String title, Boolean excludeHazard) {
if (excludeHazard) {
Query<Dataset> datasetQuery = this.dataStore.find(Dataset.class).filter(Filters.and(
Filters.regex(DATASET_FIELD_TYPE).pattern(type).caseInsensitive(),
Filters.regex(DATASET_FIELD_TITLE).pattern(title).caseInsensitive(),
Filters.nin(DATASET_FIELD_TYPE, HazardConstants.DATA_TYPE_HAZARD)
));
return datasetQuery.iterator().toList();
} else {
Query<Dataset> datasetQuery = this.dataStore.find(Dataset.class).filter(Filters.and(
Filters.regex(DATASET_FIELD_TYPE).pattern(type).caseInsensitive(),
Filters.regex(DATASET_FIELD_TITLE).pattern(title).caseInsensitive()
));
return datasetQuery.iterator().toList();
}
}

public Dataset getDatasetByFileDescriptorId(String id) {
Expand Down Expand Up @@ -146,7 +177,9 @@ public MvzDataset addMvzDataset(MvzDataset mvzDataset) {

public List<FileDescriptor> getAllFileDescriptors() {
List<FileDescriptor> fileDescriptors = new ArrayList<FileDescriptor>();
List<Dataset> datasets = getAllDatasets();
// in here, it will include all the hazards dataset as well
// TODO: if hazard dataset needs to be excluded, modify the method
List<Dataset> datasets = getAllDatasets(false);
for (Dataset dataset : datasets) {
List<FileDescriptor> fds = dataset.getFileDescriptors();
fileDescriptors.addAll(fds);
Expand Down Expand Up @@ -190,16 +223,30 @@ public Dataset updateDataset(String datasetId, String propName, String propValue
}

@Override
public List<Dataset> searchDatasets(String text) {
Query<Dataset> query = this.dataStore.find(Dataset.class).filter(
Filters.or(
Filters.regex("title").pattern(text).caseInsensitive(),
Filters.regex("description").pattern(text).caseInsensitive(),
Filters.regex("creator").pattern(text).caseInsensitive(),
Filters.regex("fileDescriptors.filename").pattern(text).caseInsensitive(),
Filters.regex("dataType").pattern(text).caseInsensitive()
public List<Dataset> searchDatasets(String text, Boolean excludeHazard) {
if (excludeHazard) {
Query<Dataset> datasetQuery = this.dataStore.find(Dataset.class).filter(Filters.and(
Filters.or(
Filters.regex("title").pattern(text).caseInsensitive(),
Filters.regex("description").pattern(text).caseInsensitive(),
Filters.regex("creator").pattern(text).caseInsensitive(),
Filters.regex("fileDescriptors.filename").pattern(text).caseInsensitive(),
Filters.regex("dataType").pattern(text).caseInsensitive()
),
Filters.nin(DATASET_FIELD_TYPE, HazardConstants.DATA_TYPE_HAZARD)
));
return query.iterator().toList();
return datasetQuery.iterator().toList();
} else {
Query<Dataset> query = this.dataStore.find(Dataset.class).filter(
Filters.or(
Filters.regex("title").pattern(text).caseInsensitive(),
Filters.regex("description").pattern(text).caseInsensitive(),
Filters.regex("creator").pattern(text).caseInsensitive(),
Filters.regex("fileDescriptors.filename").pattern(text).caseInsensitive(),
Filters.regex("dataType").pattern(text).caseInsensitive()
));
return query.iterator().toList();
}
}

@Override
Expand Down
10 changes: 5 additions & 5 deletions server/data-service/src/test/java/mocks/MockDataRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ public void initialize() {
}

@Override
public List<Dataset> getAllDatasets() {
public List<Dataset> getAllDatasets(Boolean excludeHazard) {
return this.datasets;
}

@Override
public List<Dataset> searchDatasets(String text) {
public List<Dataset> searchDatasets(String text, Boolean excludeHazard) {
List<Dataset> outList = new ArrayList<>();
for (int i = 0; i < this.datasets.size(); i++) {
if (this.datasets.get(i).getDescription().contains(text)) {
Expand Down Expand Up @@ -93,7 +93,7 @@ public Dataset getDatasetById(String id) {
}

@Override
public List<Dataset> getDatasetByTitle(String title) {
public List<Dataset> getDatasetByTitle(String title, Boolean excludeHazard) {
List<Dataset> outlist = new ArrayList<>();
for (int i = 0; i < this.datasets.size(); i++) {
if (this.datasets.get(i).getTitle().equalsIgnoreCase(title)) {
Expand All @@ -115,7 +115,7 @@ public List<Dataset> getDatasetByCreator(String creator, Boolean withHazard) {
}

@Override
public List<Dataset> getDatasetByType(String type) {
public List<Dataset> getDatasetByType(String type, Boolean excludeHazard) {
List<Dataset> outlist = new ArrayList<>();
for (int i = 0; i < this.datasets.size(); i++) {
if (this.datasets.get(i).getDataType().equalsIgnoreCase(type)) {
Expand All @@ -126,7 +126,7 @@ public List<Dataset> getDatasetByType(String type) {
}

@Override
public List<Dataset> getDatasetByTypeAndTitle(String type, String title) {
public List<Dataset> getDatasetByTypeAndTitle(String type, String title, Boolean excludeHazard) {
// this will not be tested in here
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,8 @@ public class HazardConstants {
public static final String SHAPEFILE_FORMAT = "shapefile";
public static final String HAZARD_TIF = "hazard.tif";

private static String PROBABILISTIC_EARTHQUAKE_HAZARD_SCHEM;

public static final List<String> DATA_TYPE_HAZARD = new ArrayList<>(Arrays.asList(
PROBABILISTIC_EARTHQUAKE_HAZARD_SCHEM,
PROBABILISTIC_EARTHQUAKE_HAZARD_SCHEMA,
DETERMINISTIC_EARTHQUAKE_HAZARD_SCHEMA,
PROBABILISTIC_TSUNAMI_HAZARD_SCHEMA,
DETERMINISTIC_TSUNAMI_HAZARD_SCHEMA,
Expand Down

0 comments on commit 36472f9

Please sign in to comment.