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

Add sorting and validation on Type tableSchema #263

Merged
merged 2 commits into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

### Changed
- Use Java models to represent semantics [#239](https://github.com/IN-CORE/incore-services/issues/239)
- Sort Semantic Definition Alphabetically [#238](https://github.com/IN-CORE/incore-services/issues/238)

## [1.23.0] - 2023-12-13

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

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

import static com.mongodb.client.model.Filters.and;
import static com.mongodb.client.model.Filters.eq;
Expand Down Expand Up @@ -82,19 +83,36 @@ public List<Type> searchType(String typeName) {
return matchTypeList;
}

private Boolean checkNewType(Document newType) {
private Boolean validateType(Document newType) {
return newType.get("@context") != null
&& newType.get("dc:license") != null
&& newType.get("dc:title") != null
&& newType.get("dc:description") != null
&& newType.get("url") != null
&& newType.get("openvocab:versionnumber") != null
&& newType.get("tableSchema") != null;
&& newType.get("tableSchema") != null
&& validateSchema((Map<String, List>) newType.get("tableSchema"));
}

private Boolean validateSchema(Map<String, List> tableSchema) {
List<Map<String, String>> columns = tableSchema.get("columns");
for (Map<String, String> column : columns) {
if (column.get("dc:description") != null
&& column.get("datatype") != null
&& column.get("name") != null
&& column.get("titles") != null
&& column.get("qudt:unit") != null
&& column.get("required") != null) {
} else {
return false;
}
}
return true;
}

@Override
public Document postType(Document newType) {
if (newType != null && checkNewType(newType)) {
if (newType != null && validateType(newType)) {
String name = newType.get("dc:title").toString();
if (this.hasType(name))
throw new IncoreHTTPException(Response.Status.UNAUTHORIZED, name + " already exists.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import dev.morphia.annotations.Embedded;
import java.util.List;
import java.util.stream.Collectors;
import static edu.illinois.ncsa.incore.service.semantics.utils.CommonUtil.columnComparator;

@Embedded()
public class Columns {
Expand All @@ -17,4 +19,10 @@ public Columns(List<Column> columns) {
public List<Column> getColumns() {
return columns;
}

public List<Column> getSortedColumns(String sortBy, String order) {
return this.columns.stream()
.sorted(columnComparator(sortBy, order))
.collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ public Map<String, Object> constructOutput() {
map.put("openvocab:versionnumber", this.getVersion());

} else if (field.getName().equals("tableSchema") && this.getTableSchema().getColumns() != null) {
List<Map<String, Object>> updatedTableSchema = this.getTableSchema().getColumns().stream()
List<Map<String, Object>> updatedTableSchema = this.getTableSchema()
.getSortedColumns("name", "asc").stream()
.map(column -> column.constructOutput())
.collect(Collectors.toList());
Map<String, Object> columns = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package edu.illinois.ncsa.incore.service.semantics.utils;

import edu.illinois.ncsa.incore.service.semantics.model.Column;
import edu.illinois.ncsa.incore.service.semantics.model.Type;
import java.util.Comparator;

Expand All @@ -17,4 +18,18 @@ public static Comparator<Type> typeComparator(String sortBy, String order){
if (order.equals("desc")) comparator = comparator.reversed();
return comparator;
}

public static Comparator<Column> columnComparator(String sortBy, String order){
// construct comparator
Comparator<Column> comparator;
if (sortBy.equals("title")) {
comparator = Comparator.comparing(Column::getTitles);
}
else{
// default to name
comparator = Comparator.comparing(Column::getName);
}
if (order.equals("desc")) comparator = comparator.reversed();
return comparator;
}
}
Loading