-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
!feat: enable user defined 'id' for reports so that the get URL can b…
…e stable. Breaking change as 'id' will be required to function. (#4520)
- Loading branch information
Showing
11 changed files
with
209 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
backend/molgenis-emx2-graphql/src/main/java/org/molgenis/emx2/settings/ReportUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package org.molgenis.emx2.settings; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import org.jooq.JSONB; | ||
import org.molgenis.emx2.MolgenisException; | ||
import org.molgenis.emx2.Row; | ||
import org.molgenis.emx2.Schema; | ||
|
||
public class ReportUtils { | ||
public static Map<String, String> getReportById(String reportId, Schema schema) { | ||
try { | ||
String reportsJson = schema.getMetadata().getSetting("reports"); | ||
List<Map<String, String>> reportList = new ObjectMapper().readValue(reportsJson, List.class); | ||
if (reportsJson != null) { | ||
Optional<Map<String, String>> reportOptional = | ||
reportList.stream().filter(r -> reportId.equals(r.get("id"))).findFirst(); | ||
if (reportOptional.isPresent()) { | ||
return reportOptional.get(); | ||
} | ||
} | ||
} catch (Exception e) { | ||
// nothing to do, error will be handled below | ||
} | ||
throw new MolgenisException("Report not found id=" + reportId); | ||
} | ||
|
||
public static String getReportAsJson(String id, Schema schema, Map<String, ?> parameters) { | ||
Map<String, String> report = getReportById(id, schema); | ||
return convertToJson(schema.retrieveSql(report.get("sql"), parameters)); | ||
} | ||
|
||
public static String getReportAsJson( | ||
String id, Schema schema, Map<String, ?> parameters, int limit, int offset) { | ||
Map<String, String> report = getReportById(id, schema); | ||
String sql = report.get("sql") + " LIMIT " + limit + " OFFSET " + offset; | ||
return convertToJson(schema.retrieveSql(sql, parameters)); | ||
} | ||
|
||
public static List<Row> getReportAsRows(String id, Schema schema, Map<String, ?> parameters) { | ||
Map<String, String> report = getReportById(id, schema); | ||
return schema.retrieveSql(report.get("sql"), parameters); | ||
} | ||
|
||
public static Integer getReportCount(String id, Schema schema, Map<String, ?> parameters) { | ||
Map<String, String> report = getReportById(id, schema); | ||
String countSql = String.format("select count(*) from (%s) as count", report.get("sql")); | ||
return schema.retrieveSql(countSql, parameters).get(0).get("count", Integer.class); | ||
} | ||
|
||
private static String convertToJson(List<Row> rows) { | ||
ObjectMapper objectMapper = new ObjectMapper(); | ||
try { | ||
List<Map<String, Object>> result = new ArrayList<>(); | ||
for (Row row : rows) { | ||
if (row.getValueMap().size() == 1) { | ||
Object value = row.getValueMap().values().iterator().next(); | ||
if (value instanceof JSONB) { | ||
return value.toString(); | ||
} | ||
} | ||
result.add(row.getValueMap()); | ||
} | ||
return objectMapper.writeValueAsString(result); | ||
} catch (Exception e) { | ||
throw new MolgenisException("Cannot convert sql result set to json", e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.