From 431d8d92e2f66eb448075459f5005b6258ac307d Mon Sep 17 00:00:00 2001 From: ShvaykaD Date: Tue, 9 Jan 2024 13:50:34 +0200 Subject: [PATCH 1/4] added missing WidgetTypeController APIs to RestClient --- .../controller/WidgetTypeController.java | 6 +- .../thingsboard/rest/client/RestClient.java | 133 ++++++++++++++++++ 2 files changed, 138 insertions(+), 1 deletion(-) diff --git a/application/src/main/java/org/thingsboard/server/controller/WidgetTypeController.java b/application/src/main/java/org/thingsboard/server/controller/WidgetTypeController.java index fa18fe4265..e204185fb9 100644 --- a/application/src/main/java/org/thingsboard/server/controller/WidgetTypeController.java +++ b/application/src/main/java/org/thingsboard/server/controller/WidgetTypeController.java @@ -209,6 +209,7 @@ public class WidgetTypeController extends AutoCommitController { @PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN')") @RequestMapping(value = "/widgetTypes", params = {"isSystem", "bundleAlias"}, method = RequestMethod.GET) @ResponseBody + @Deprecated public List getBundleWidgetTypesByBundleAlias( @ApiParam(value = "System or Tenant", required = true) @RequestParam boolean isSystem, @@ -241,6 +242,7 @@ public class WidgetTypeController extends AutoCommitController { @PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN')") @RequestMapping(value = "/widgetTypesDetails", params = {"isSystem", "bundleAlias"}, method = RequestMethod.GET) @ResponseBody + @Deprecated public List getBundleWidgetTypesDetailsByBundleAlias( @ApiParam(value = "System or Tenant", required = true) @RequestParam boolean isSystem, @@ -256,7 +258,7 @@ public class WidgetTypeController extends AutoCommitController { return checkNotNull(widgetTypeService.findWidgetTypesDetailsByWidgetsBundleId(getTenantId(), widgetsBundle.getId())); } - @ApiOperation(value = "Get all Widget types details for specified Bundle (getBundleWidgetTypes)", + @ApiOperation(value = "Get all Widget types details for specified Bundle (getBundleWidgetTypesDetails)", notes = "Returns an array of Widget Type Details objects that belong to specified Widget Bundle." + WIDGET_TYPE_DETAILS_DESCRIPTION + " " + SYSTEM_OR_TENANT_AUTHORITY_PARAGRAPH) @PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN', 'CUSTOMER_USER')") @RequestMapping(value = "/widgetTypesDetails", params = {"widgetsBundleId"}, method = RequestMethod.GET) @@ -292,6 +294,7 @@ public class WidgetTypeController extends AutoCommitController { @PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN', 'CUSTOMER_USER')") @RequestMapping(value = "/widgetTypesInfos", params = {"isSystem", "bundleAlias"}, method = RequestMethod.GET) @ResponseBody + @Deprecated public List getBundleWidgetTypesInfosByBundleAlias( @ApiParam(value = "System or Tenant", required = true) @RequestParam boolean isSystem, @@ -345,6 +348,7 @@ public class WidgetTypeController extends AutoCommitController { @PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN', 'CUSTOMER_USER')") @RequestMapping(value = "/widgetType", params = {"isSystem", "bundleAlias", "alias"}, method = RequestMethod.GET) @ResponseBody + @Deprecated public WidgetType getWidgetTypeByBundleAliasAndTypeAlias( @ApiParam(value = "System or Tenant", required = true) @RequestParam boolean isSystem, diff --git a/rest-client/src/main/java/org/thingsboard/rest/client/RestClient.java b/rest-client/src/main/java/org/thingsboard/rest/client/RestClient.java index 5fe2561f3f..c950febfa7 100644 --- a/rest-client/src/main/java/org/thingsboard/rest/client/RestClient.java +++ b/rest-client/src/main/java/org/thingsboard/rest/client/RestClient.java @@ -29,6 +29,7 @@ import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.http.client.support.HttpRequestWrapper; +import org.springframework.util.CollectionUtils; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; import org.springframework.web.client.HttpClientErrorException; @@ -160,6 +161,7 @@ import org.thingsboard.server.common.data.sync.vc.VersionLoadResult; import org.thingsboard.server.common.data.sync.vc.VersionedEntityInfo; import org.thingsboard.server.common.data.sync.vc.request.create.VersionCreateRequest; import org.thingsboard.server.common.data.sync.vc.request.load.VersionLoadRequest; +import org.thingsboard.server.common.data.widget.DeprecatedFilter; import org.thingsboard.server.common.data.widget.WidgetType; import org.thingsboard.server.common.data.widget.WidgetTypeDetails; import org.thingsboard.server.common.data.widget.WidgetTypeInfo; @@ -2751,6 +2753,19 @@ public class RestClient implements Closeable { } } + public Optional getWidgetTypeInfoById(WidgetTypeId widgetTypeId) { + try { + ResponseEntity widgetTypeInfo = + restTemplate.getForEntity(baseURL + "/api/widgetTypeInfo/{widgetTypeId}", WidgetTypeInfo.class, widgetTypeId.getId()); + return Optional.ofNullable(widgetTypeInfo.getBody()); + } catch (HttpClientErrorException exception) { + if (exception.getStatusCode() == HttpStatus.NOT_FOUND) { + return Optional.empty(); + } + throw exception; + } + } + public WidgetTypeDetails saveWidgetType(WidgetTypeDetails widgetTypeDetails) { return restTemplate.postForEntity(baseURL + "/api/widgetType", widgetTypeDetails, WidgetTypeDetails.class).getBody(); } @@ -2759,6 +2774,22 @@ public class RestClient implements Closeable { restTemplate.delete(baseURL + "/api/widgetType/{widgetTypeId}", widgetTypeId.getId()); } + public PageData getWidgetTypes(PageLink pageLink, Boolean tenantOnly, Boolean fullSearch, + DeprecatedFilter deprecatedFilter, List widgetTypeList) { + Map params = new HashMap<>(); + addPageLinkToParam(params, pageLink); + addWidgetInfoFiltersToParams(tenantOnly, fullSearch, deprecatedFilter, widgetTypeList, params); + return restTemplate.exchange( + baseURL + "/api/widgetTypes?" + getUrlParams(pageLink) + + getWidgetTypeInfoPageRequestUrlParams(tenantOnly, fullSearch, deprecatedFilter, widgetTypeList), + HttpMethod.GET, + HttpEntity.EMPTY, + new ParameterizedTypeReference>() { + }, + params).getBody(); + } + + @Deprecated // current name in the controller: getBundleWidgetTypesByBundleAlias public List getBundleWidgetTypes(boolean isSystem, String bundleAlias) { return restTemplate.exchange( baseURL + "/api/widgetTypes?isSystem={isSystem}&bundleAlias={bundleAlias}", @@ -2770,6 +2801,17 @@ public class RestClient implements Closeable { bundleAlias).getBody(); } + public List getBundleWidgetTypes(WidgetsBundleId widgetsBundleId) { + return restTemplate.exchange( + baseURL + "/api/widgetTypes?widgetsBundleId={widgetsBundleId}", + HttpMethod.GET, + HttpEntity.EMPTY, + new ParameterizedTypeReference>() { + }, + widgetsBundleId.getId()).getBody(); + } + + @Deprecated // current name in the controller: getBundleWidgetTypesDetailsByBundleAlias public List getBundleWidgetTypesDetails(boolean isSystem, String bundleAlias) { return restTemplate.exchange( baseURL + "/api/widgetTypesDetails?isSystem={isSystem}&bundleAlias={bundleAlias}", @@ -2781,6 +2823,28 @@ public class RestClient implements Closeable { bundleAlias).getBody(); } + public List getBundleWidgetTypesDetails(WidgetsBundleId widgetsBundleId, boolean inlineImages) { + return restTemplate.exchange( + baseURL + "/api/widgetTypesDetails?widgetsBundleId={widgetsBundleId}&inlineImages={inlineImages}", + HttpMethod.GET, + HttpEntity.EMPTY, + new ParameterizedTypeReference>() { + }, + widgetsBundleId.getId(), + inlineImages).getBody(); + } + + public List getBundleWidgetTypeFqns(WidgetsBundleId widgetsBundleId) { + return restTemplate.exchange( + baseURL + "/api/widgetTypeFqns?widgetsBundleId={widgetsBundleId}", + HttpMethod.GET, + HttpEntity.EMPTY, + new ParameterizedTypeReference>() { + }, + widgetsBundleId.getId()).getBody(); + } + + @Deprecated // current name in the controller: getBundleWidgetTypesInfosByBundleAlias public List getBundleWidgetTypesInfos(boolean isSystem, String bundleAlias) { return restTemplate.exchange( baseURL + "/api/widgetTypesInfos?isSystem={isSystem}&bundleAlias={bundleAlias}", @@ -2792,6 +2856,24 @@ public class RestClient implements Closeable { bundleAlias).getBody(); } + public PageData getBundleWidgetTypesInfos(WidgetsBundleId widgetsBundleId, PageLink pageLink, + Boolean tenantOnly, Boolean fullSearch, + DeprecatedFilter deprecatedFilter, List widgetTypeList) { + Map params = new HashMap<>(); + params.put("widgetsBundleId", widgetsBundleId.getId().toString()); + addPageLinkToParam(params, pageLink); + addWidgetInfoFiltersToParams(tenantOnly, fullSearch, deprecatedFilter, widgetTypeList, params); + return restTemplate.exchange( + baseURL + "/api/widgetTypesInfos?widgetsBundleId={widgetsBundleId}&" + getUrlParams(pageLink) + + getWidgetTypeInfoPageRequestUrlParams(tenantOnly, fullSearch, deprecatedFilter, widgetTypeList), + HttpMethod.GET, + HttpEntity.EMPTY, + new ParameterizedTypeReference>() { + }, + params).getBody(); + } + + @Deprecated // current name in the controller: getWidgetTypeByBundleAliasAndTypeAlias public Optional getWidgetType(boolean isSystem, String bundleAlias, String alias) { try { ResponseEntity widgetType = @@ -2811,6 +2893,22 @@ public class RestClient implements Closeable { } } + public Optional getWidgetType(String fqn) { + try { + ResponseEntity widgetType = + restTemplate.getForEntity( + baseURL + "/api/widgetType?fqn={fqn}", + WidgetType.class, + fqn); + return Optional.ofNullable(widgetType.getBody()); + } catch (HttpClientErrorException exception) { + if (exception.getStatusCode() == HttpStatus.NOT_FOUND) { + return Optional.empty(); + } + throw exception; + } + } + public Boolean isEdgesSupportEnabled() { return restTemplate.getForEntity(baseURL + "/api/edges/enabled", Boolean.class).getBody(); } @@ -3645,6 +3743,25 @@ public class RestClient implements Closeable { return urlParams; } + private String getWidgetTypeInfoPageRequestUrlParams(Boolean tenantOnly, Boolean fullSearch, + DeprecatedFilter deprecatedFilter, + List widgetTypeList) { + String urlParams = ""; + if (tenantOnly != null) { + urlParams = "&tenantOnly={tenantOnly}"; + } + if (fullSearch != null) { + urlParams += "&fullSearch={fullSearch}"; + } + if (deprecatedFilter != null) { + urlParams += "&deprecatedFilter={deprecatedFilter}"; + } + if (!CollectionUtils.isEmpty(widgetTypeList)) { + urlParams += "&widgetTypeList={widgetTypeList}"; + } + return urlParams; + } + private void addTimePageLinkToParam(Map params, TimePageLink pageLink) { this.addPageLinkToParam(params, pageLink); if (pageLink.getStartTime() != null) { @@ -3667,6 +3784,22 @@ public class RestClient implements Closeable { } } + private void addWidgetInfoFiltersToParams(Boolean tenantOnly, Boolean fullSearch, DeprecatedFilter deprecatedFilter, + List widgetTypeList, Map params) { + if (tenantOnly != null) { + params.put("tenantOnly", tenantOnly.toString()); + } + if (fullSearch != null) { + params.put("fullSearch", fullSearch.toString()); + } + if (deprecatedFilter != null) { + params.put("deprecatedFilter", deprecatedFilter.name()); + } + if (!CollectionUtils.isEmpty(widgetTypeList)) { + params.put("widgetTypeList", listToString(widgetTypeList)); + } + } + private String listToString(List list) { return String.join(",", list); } From 523651b71cb77eb53b049431e015d642e1384ced Mon Sep 17 00:00:00 2001 From: ShvaykaD Date: Tue, 9 Jan 2024 16:52:50 +0200 Subject: [PATCH 2/4] overload saveWidgetType, getWidgetTypes, getBundleWidgetTypesInfos methods --- .../org/thingsboard/rest/client/RestClient.java | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/rest-client/src/main/java/org/thingsboard/rest/client/RestClient.java b/rest-client/src/main/java/org/thingsboard/rest/client/RestClient.java index c950febfa7..10d3863a2a 100644 --- a/rest-client/src/main/java/org/thingsboard/rest/client/RestClient.java +++ b/rest-client/src/main/java/org/thingsboard/rest/client/RestClient.java @@ -2767,13 +2767,24 @@ public class RestClient implements Closeable { } public WidgetTypeDetails saveWidgetType(WidgetTypeDetails widgetTypeDetails) { - return restTemplate.postForEntity(baseURL + "/api/widgetType", widgetTypeDetails, WidgetTypeDetails.class).getBody(); + return saveWidgetType(widgetTypeDetails, null); + } + + public WidgetTypeDetails saveWidgetType(WidgetTypeDetails widgetTypeDetails, Boolean updateExistingByFqn) { + if (updateExistingByFqn == null) { + return restTemplate.postForEntity(baseURL + "/api/widgetType", widgetTypeDetails, WidgetTypeDetails.class).getBody(); + } + return restTemplate.postForEntity(baseURL + "/api/widgetType?updateExistingByFqn={updateExistingByFqn}", widgetTypeDetails, WidgetTypeDetails.class, updateExistingByFqn).getBody(); } public void deleteWidgetType(WidgetTypeId widgetTypeId) { restTemplate.delete(baseURL + "/api/widgetType/{widgetTypeId}", widgetTypeId.getId()); } + public PageData getWidgetTypes(PageLink pageLink) { + return getWidgetTypes(pageLink, null, null, null, null); + } + public PageData getWidgetTypes(PageLink pageLink, Boolean tenantOnly, Boolean fullSearch, DeprecatedFilter deprecatedFilter, List widgetTypeList) { Map params = new HashMap<>(); @@ -2856,6 +2867,10 @@ public class RestClient implements Closeable { bundleAlias).getBody(); } + public PageData getBundleWidgetTypesInfos(WidgetsBundleId widgetsBundleId, PageLink pageLink) { + return getBundleWidgetTypesInfos(widgetsBundleId, pageLink, null, null, null, null); + } + public PageData getBundleWidgetTypesInfos(WidgetsBundleId widgetsBundleId, PageLink pageLink, Boolean tenantOnly, Boolean fullSearch, DeprecatedFilter deprecatedFilter, List widgetTypeList) { From 29289baeb49cbe5bc48baa97baacf00c52210547 Mon Sep 17 00:00:00 2001 From: ShvaykaD Date: Wed, 10 Jan 2024 12:42:53 +0200 Subject: [PATCH 3/4] added missing WidgetsBundleController APIs to RestClient --- .../thingsboard/rest/client/RestClient.java | 47 ++++++++++++++++--- 1 file changed, 40 insertions(+), 7 deletions(-) diff --git a/rest-client/src/main/java/org/thingsboard/rest/client/RestClient.java b/rest-client/src/main/java/org/thingsboard/rest/client/RestClient.java index 10d3863a2a..0f982c3ff0 100644 --- a/rest-client/src/main/java/org/thingsboard/rest/client/RestClient.java +++ b/rest-client/src/main/java/org/thingsboard/rest/client/RestClient.java @@ -2715,15 +2715,33 @@ public class RestClient implements Closeable { return restTemplate.postForEntity(baseURL + "/api/widgetsBundle", widgetsBundle, WidgetsBundle.class).getBody(); } + public void updateWidgetsBundleWidgetTypes(WidgetsBundleId widgetsBundleId, List widgetTypeIds) { + var httpEntity = new HttpEntity<>(widgetTypeIds.stream() + .map(widgetTypeId -> widgetTypeId.getId().toString()) + .collect(Collectors.toList())); + restTemplate.exchange(baseURL + "/api/widgetsBundle/{widgetsBundleId}/widgetTypes", + HttpMethod.POST, httpEntity, Void.class, widgetsBundleId.getId()); + } + + public void updateWidgetsBundleWidgetFqns(WidgetsBundleId widgetsBundleId, List widgetTypeFqns) { + restTemplate.exchange(baseURL + "/api/widgetsBundle/{widgetsBundleId}/widgetTypeFqns", + HttpMethod.POST, new HttpEntity<>(widgetTypeFqns), Void.class, widgetsBundleId.getId()); + } + public void deleteWidgetsBundle(WidgetsBundleId widgetsBundleId) { restTemplate.delete(baseURL + "/api/widgetsBundle/{widgetsBundleId}", widgetsBundleId.getId()); } public PageData getWidgetsBundles(PageLink pageLink) { + return getWidgetsBundles(pageLink, null, null); + } + + public PageData getWidgetsBundles(PageLink pageLink, Boolean tenantOnly, Boolean fullSearch) { Map params = new HashMap<>(); addPageLinkToParam(params, pageLink); + addTenantOnlyAndFullSearchToParams(tenantOnly, fullSearch, params); return restTemplate.exchange( - baseURL + "/api/widgetsBundles?" + getUrlParams(pageLink), + baseURL + "/api/widgetsBundles?" + getUrlParams(pageLink) + getWidgetsBundlePageRequestUrlParams(tenantOnly, fullSearch), HttpMethod.GET, HttpEntity.EMPTY, new ParameterizedTypeReference>() { @@ -3758,6 +3776,17 @@ public class RestClient implements Closeable { return urlParams; } + private String getWidgetsBundlePageRequestUrlParams(Boolean tenantOnly, Boolean fullSearch) { + String urlParams = ""; + if (tenantOnly != null) { + urlParams = "&tenantOnly={tenantOnly}"; + } + if (fullSearch != null) { + urlParams += "&fullSearch={fullSearch}"; + } + return urlParams; + } + private String getWidgetTypeInfoPageRequestUrlParams(Boolean tenantOnly, Boolean fullSearch, DeprecatedFilter deprecatedFilter, List widgetTypeList) { @@ -3801,12 +3830,7 @@ public class RestClient implements Closeable { private void addWidgetInfoFiltersToParams(Boolean tenantOnly, Boolean fullSearch, DeprecatedFilter deprecatedFilter, List widgetTypeList, Map params) { - if (tenantOnly != null) { - params.put("tenantOnly", tenantOnly.toString()); - } - if (fullSearch != null) { - params.put("fullSearch", fullSearch.toString()); - } + addTenantOnlyAndFullSearchToParams(tenantOnly, fullSearch, params); if (deprecatedFilter != null) { params.put("deprecatedFilter", deprecatedFilter.name()); } @@ -3815,6 +3839,15 @@ public class RestClient implements Closeable { } } + private void addTenantOnlyAndFullSearchToParams(Boolean tenantOnly, Boolean fullSearch, Map params) { + if (tenantOnly != null) { + params.put("tenantOnly", tenantOnly.toString()); + } + if (fullSearch != null) { + params.put("fullSearch", fullSearch.toString()); + } + } + private String listToString(List list) { return String.join(",", list); } From 4155fcd03fa2d08b2d583cb464ddaa631e5f0e72 Mon Sep 17 00:00:00 2001 From: ShvaykaD Date: Wed, 10 Jan 2024 13:07:19 +0200 Subject: [PATCH 4/4] Minor refactoring of private method after self review --- .../thingsboard/rest/client/RestClient.java | 26 +++++++------------ 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/rest-client/src/main/java/org/thingsboard/rest/client/RestClient.java b/rest-client/src/main/java/org/thingsboard/rest/client/RestClient.java index 0f982c3ff0..4c03bb1847 100644 --- a/rest-client/src/main/java/org/thingsboard/rest/client/RestClient.java +++ b/rest-client/src/main/java/org/thingsboard/rest/client/RestClient.java @@ -2741,7 +2741,7 @@ public class RestClient implements Closeable { addPageLinkToParam(params, pageLink); addTenantOnlyAndFullSearchToParams(tenantOnly, fullSearch, params); return restTemplate.exchange( - baseURL + "/api/widgetsBundles?" + getUrlParams(pageLink) + getWidgetsBundlePageRequestUrlParams(tenantOnly, fullSearch), + baseURL + "/api/widgetsBundles?" + getUrlParams(pageLink) + getTenantOnlyAndFullSearchUrlParams(tenantOnly, fullSearch), HttpMethod.GET, HttpEntity.EMPTY, new ParameterizedTypeReference>() { @@ -3776,20 +3776,20 @@ public class RestClient implements Closeable { return urlParams; } - private String getWidgetsBundlePageRequestUrlParams(Boolean tenantOnly, Boolean fullSearch) { - String urlParams = ""; - if (tenantOnly != null) { - urlParams = "&tenantOnly={tenantOnly}"; + private String getWidgetTypeInfoPageRequestUrlParams(Boolean tenantOnly, Boolean fullSearch, + DeprecatedFilter deprecatedFilter, + List widgetTypeList) { + String urlParams = getTenantOnlyAndFullSearchUrlParams(tenantOnly, fullSearch); + if (deprecatedFilter != null) { + urlParams += "&deprecatedFilter={deprecatedFilter}"; } - if (fullSearch != null) { - urlParams += "&fullSearch={fullSearch}"; + if (!CollectionUtils.isEmpty(widgetTypeList)) { + urlParams += "&widgetTypeList={widgetTypeList}"; } return urlParams; } - private String getWidgetTypeInfoPageRequestUrlParams(Boolean tenantOnly, Boolean fullSearch, - DeprecatedFilter deprecatedFilter, - List widgetTypeList) { + private String getTenantOnlyAndFullSearchUrlParams(Boolean tenantOnly, Boolean fullSearch) { String urlParams = ""; if (tenantOnly != null) { urlParams = "&tenantOnly={tenantOnly}"; @@ -3797,12 +3797,6 @@ public class RestClient implements Closeable { if (fullSearch != null) { urlParams += "&fullSearch={fullSearch}"; } - if (deprecatedFilter != null) { - urlParams += "&deprecatedFilter={deprecatedFilter}"; - } - if (!CollectionUtils.isEmpty(widgetTypeList)) { - urlParams += "&widgetTypeList={widgetTypeList}"; - } return urlParams; }