From 6d26a918870cc7ca5bbf2afd3677251c9f36304f Mon Sep 17 00:00:00 2001 From: Dmitry Taimanov Date: Mon, 15 Jun 2020 16:51:59 +0400 Subject: [PATCH] Reports REST API improvements. Add group details endpoint and documentation #260 --- .../haulmont/reports/rest-api-swagger.yaml | 50 +++++++++++++++++++ .../web/restapi/v1/ReportRestController.java | 5 ++ .../v1/ReportRestControllerManager.java | 36 +++++++++++-- 3 files changed, 88 insertions(+), 3 deletions(-) diff --git a/modules/web/src/com/haulmont/reports/rest-api-swagger.yaml b/modules/web/src/com/haulmont/reports/rest-api-swagger.yaml index d43cf815..2af9da40 100644 --- a/modules/web/src/com/haulmont/reports/rest-api-swagger.yaml +++ b/modules/web/src/com/haulmont/reports/rest-api-swagger.yaml @@ -11,6 +11,7 @@ info: * Getting list of avaliable reports * Getting report metadata (parameters, templates) + * Getting report group details * Execution of report The Reports REST API is available at the URL `/rest/reports/v1` in the web module, e.g. `http://localhost:8080/app/rest/reports/v1/report` @@ -40,6 +41,13 @@ parameters: description: Report identifier required: true default: "" + groupIdParam: + name: groupId + in: path + type: string + description: Report Group identifier + required: true + default: "" entityJsonParam: name: entityJson description: JSON object with the entity @@ -72,6 +80,9 @@ definitions: code: type: string description: Report code + group: + type: string + description: Group id reportFull: type: object @@ -85,6 +96,9 @@ definitions: code: type: string description: Report code + group: + type: string + description: Group id inputParameters: type: array items: @@ -119,6 +133,10 @@ definitions: type: string description: | For ENUMERATION type there will be a java class for enumeration here + defaultValue: + type: string + description: | + Default value of parameter in string form templates: type: array items: @@ -160,6 +178,19 @@ definitions: items: type: string + group: + type: object + properties: + id: + type: string + description: Group id + title: + type: string + description: Group title + code: + type: string + description: Group code + ################################################################################ # Tags # ################################################################################ @@ -228,6 +259,25 @@ paths: schema: $ref: '#/definitions/error' + /group/{groupId}: + parameters: + - $ref: '#/parameters/groupIdParam' + get: + tags: + - Group + summary: Get a single group by id + description: | + Gets a single group by identifier + responses: + 200: + description: Success. The group is returned in the response body. + schema: + $ref: '#/definitions/group' + 403: + description: Forbidden. The user doesn't have permissions to read the report + schema: + $ref: '#/definitions/error' + /run/{reportId}: parameters: - $ref: '#/parameters/reportIdParam' diff --git a/modules/web/src/com/haulmont/reports/web/restapi/v1/ReportRestController.java b/modules/web/src/com/haulmont/reports/web/restapi/v1/ReportRestController.java index 89a26aa3..a129eb7f 100644 --- a/modules/web/src/com/haulmont/reports/web/restapi/v1/ReportRestController.java +++ b/modules/web/src/com/haulmont/reports/web/restapi/v1/ReportRestController.java @@ -56,6 +56,11 @@ public String loadReport(@PathVariable String entityId) { return controllerManager.loadReport(entityId); } + @GetMapping(value = "/group/{entityId}") + public String loadGroup(@PathVariable String entityId){ + return controllerManager.loadGroup(entityId); + } + @PostMapping(value = "/run/{entityId}") public void runReport(@PathVariable String entityId, @RequestBody(required = false) String body, HttpServletResponse response) { diff --git a/modules/web/src/com/haulmont/reports/web/restapi/v1/ReportRestControllerManager.java b/modules/web/src/com/haulmont/reports/web/restapi/v1/ReportRestControllerManager.java index fa4eea77..ea633886 100644 --- a/modules/web/src/com/haulmont/reports/web/restapi/v1/ReportRestControllerManager.java +++ b/modules/web/src/com/haulmont/reports/web/restapi/v1/ReportRestControllerManager.java @@ -57,6 +57,30 @@ public class ReportRestControllerManager { @Inject protected ParameterClassResolver parameterClassResolver; + public String loadGroup(String entityId) { + checkCanReadEntity(metadata.getClassNN(ReportGroup.class)); + + LoadContext loadContext = new LoadContext<>(ReportGroup.class); + loadContext.setView( + new View(ReportGroup.class) + .addProperty("id") + .addProperty("title") + .addProperty("code")) + .setQueryString("select g from report$ReportGroup g where g.id = :id") + .setParameter("id", getIdFromString(entityId, metadata.getClassNN(ReportGroup.class))); + + ReportGroup group = dataManager.load(loadContext); + checkEntityIsNotNull(metadata.getClassNN(ReportGroup.class).getName(), entityId, group); + + GroupInfo info = new GroupInfo(); + //noinspection ConstantConditions + info.id = group.getId().toString(); + info.code = group.getCode(); + info.title = group.getTitle(); + + return createGson().toJson(info); + } + public String loadReportsList() { checkCanReadEntity(metadata.getClassNN(Report.class)); @@ -204,7 +228,7 @@ protected Object prepareValue(ReportInputParameter inputParam, ParameterValueInf } protected Class resolveDatatypeActualClass(ReportInputParameter inputParam) { - switch (inputParam.getType()){ + switch (inputParam.getType()) { case DATE: return java.sql.Date.class; case TIME: @@ -263,13 +287,13 @@ protected InputParameterInfo mapInputParameterInfo(ReportInputParameter paramete } if (parameter.getDefaultValue() != null) { - inputParameterInfo.defaultValue = transformDefaultValue(parameter); + inputParameterInfo.defaultValue = transformDefaultValue(parameter); } return inputParameterInfo; } protected String transformDefaultValue(ReportInputParameter parameter) { - switch (parameter.getType()){ + switch (parameter.getType()) { case ENTITY: EntityLoadInfo info = EntityLoadInfo.parse(parameter.getDefaultValue()); if (info != null) return info.getId().toString(); @@ -375,6 +399,12 @@ protected class ReportInfo { protected List inputParameters; } + protected static class GroupInfo { + protected String id; + protected String title; + protected String code; + } + protected static class TemplateInfo { protected String code; protected String outputType;