Browse Source

Minor refactoring for ResourceService

pull/12174/head
ViacheslavKlimov 2 years ago
parent
commit
f50789ebf3
  1. 4
      application/src/main/java/org/thingsboard/server/service/install/update/ResourcesUpdater.java
  2. 15
      application/src/main/java/org/thingsboard/server/service/resource/DefaultTbResourceService.java
  3. 8
      common/dao-api/src/main/java/org/thingsboard/server/dao/resource/ResourceService.java
  4. 11
      dao/src/main/java/org/thingsboard/server/dao/dashboard/DashboardServiceImpl.java
  5. 63
      dao/src/main/java/org/thingsboard/server/dao/resource/BaseResourceService.java
  6. 9
      dao/src/main/java/org/thingsboard/server/dao/widget/WidgetTypeServiceImpl.java

4
application/src/main/java/org/thingsboard/server/service/install/update/ResourcesUpdater.java

@ -101,7 +101,7 @@ public class ResourcesUpdater {
for (DashboardId dashboardId : dashboards) { for (DashboardId dashboardId : dashboards) {
executor.submit(() -> { executor.submit(() -> {
Dashboard dashboard = dashboardService.findDashboardById(TenantId.SYS_TENANT_ID, dashboardId); Dashboard dashboard = dashboardService.findDashboardById(TenantId.SYS_TENANT_ID, dashboardId);
boolean updated = resourceService.updateResourcesUsage(dashboard); // will convert resources ids to new structure boolean updated = resourceService.updateResourcesUsage(dashboard.getTenantId(), dashboard); // will convert resources ids to new structure
if (updated) { if (updated) {
dashboardService.saveDashboard(dashboard); dashboardService.saveDashboard(dashboard);
updatedCount.incrementAndGet(); updatedCount.incrementAndGet();
@ -130,7 +130,7 @@ public class ResourcesUpdater {
for (WidgetTypeId widgetTypeId : widgets) { for (WidgetTypeId widgetTypeId : widgets) {
executor.submit(() -> { executor.submit(() -> {
WidgetTypeDetails widgetTypeDetails = widgetTypeService.findWidgetTypeDetailsById(TenantId.SYS_TENANT_ID, widgetTypeId); WidgetTypeDetails widgetTypeDetails = widgetTypeService.findWidgetTypeDetailsById(TenantId.SYS_TENANT_ID, widgetTypeId);
boolean updated = resourceService.updateResourcesUsage(widgetTypeDetails); boolean updated = resourceService.updateResourcesUsage(widgetTypeDetails.getTenantId(), widgetTypeDetails);
if (updated) { if (updated) {
widgetTypeService.saveWidgetType(widgetTypeDetails); widgetTypeService.saveWidgetType(widgetTypeDetails);
updatedCount.incrementAndGet(); updatedCount.incrementAndGet();

15
application/src/main/java/org/thingsboard/server/service/resource/DefaultTbResourceService.java

@ -46,7 +46,7 @@ import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Comparator; import java.util.Comparator;
import java.util.List; import java.util.List;
import java.util.function.Function; import java.util.function.Supplier;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream; import java.util.stream.Stream;
@ -132,12 +132,12 @@ public class DefaultTbResourceService extends AbstractTbEntityService implements
@Override @Override
public List<ResourceExportData> exportResources(Dashboard dashboard, SecurityUser user) throws ThingsboardException { public List<ResourceExportData> exportResources(Dashboard dashboard, SecurityUser user) throws ThingsboardException {
return exportResources(dashboard, imageService::getUsedImages, resourceService::getUsedResources, user); return exportResources(() -> imageService.getUsedImages(dashboard), () -> resourceService.getUsedResources(user.getTenantId(), dashboard), user);
} }
@Override @Override
public List<ResourceExportData> exportResources(WidgetTypeDetails widgetTypeDetails, SecurityUser user) throws ThingsboardException { public List<ResourceExportData> exportResources(WidgetTypeDetails widgetTypeDetails, SecurityUser user) throws ThingsboardException {
return exportResources(widgetTypeDetails, imageService::getUsedImages, resourceService::getUsedResources, user); return exportResources(() -> imageService.getUsedImages(widgetTypeDetails), () -> resourceService.getUsedResources(user.getTenantId(), widgetTypeDetails), user);
} }
@Override @Override
@ -153,13 +153,12 @@ public class DefaultTbResourceService extends AbstractTbEntityService implements
} }
} }
private <T> List<ResourceExportData> exportResources(T entity, private <T> List<ResourceExportData> exportResources(Supplier<Collection<TbResourceInfo>> imagesProcessor,
Function<T, Collection<TbResourceInfo>> imagesProcessor, Supplier<Collection<TbResourceInfo>> resourcesProcessor,
Function<T, Collection<TbResourceInfo>> resourcesProcessor,
SecurityUser user) throws ThingsboardException { SecurityUser user) throws ThingsboardException {
List<TbResourceInfo> resources = new ArrayList<>(); List<TbResourceInfo> resources = new ArrayList<>();
resources.addAll(imagesProcessor.apply(entity)); resources.addAll(imagesProcessor.get());
resources.addAll(resourcesProcessor.apply(entity)); resources.addAll(resourcesProcessor.get());
for (TbResourceInfo resourceInfo : resources) { for (TbResourceInfo resourceInfo : resources) {
accessControlService.checkPermission(user, Resource.TB_RESOURCE, Operation.READ, resourceInfo.getId(), resourceInfo); accessControlService.checkPermission(user, Resource.TB_RESOURCE, Operation.READ, resourceInfo.getId(), resourceInfo);
} }

8
common/dao-api/src/main/java/org/thingsboard/server/dao/resource/ResourceService.java

@ -80,13 +80,13 @@ public interface ResourceService extends EntityDaoService {
TbResourceInfo findSystemOrTenantResourceByEtag(TenantId tenantId, ResourceType resourceType, String etag); TbResourceInfo findSystemOrTenantResourceByEtag(TenantId tenantId, ResourceType resourceType, String etag);
boolean updateResourcesUsage(Dashboard dashboard); boolean updateResourcesUsage(TenantId tenantId, Dashboard dashboard);
boolean updateResourcesUsage(WidgetTypeDetails widgetTypeDetails); boolean updateResourcesUsage(TenantId tenantId, WidgetTypeDetails widgetTypeDetails);
Collection<TbResourceInfo> getUsedResources(Dashboard dashboard); Collection<TbResourceInfo> getUsedResources(TenantId tenantId, Dashboard dashboard);
Collection<TbResourceInfo> getUsedResources(WidgetTypeDetails widgetTypeDetails); Collection<TbResourceInfo> getUsedResources(TenantId tenantId, WidgetTypeDetails widgetTypeDetails);
TbResource createOrUpdateSystemResource(ResourceType resourceType, String resourceKey, byte[] data); TbResource createOrUpdateSystemResource(ResourceType resourceType, String resourceKey, byte[] data);

11
dao/src/main/java/org/thingsboard/server/dao/dashboard/DashboardServiceImpl.java

@ -162,18 +162,19 @@ public class DashboardServiceImpl extends AbstractEntityService implements Dashb
dashboardValidator.validate(dashboard, DashboardInfo::getTenantId); dashboardValidator.validate(dashboard, DashboardInfo::getTenantId);
} }
try { try {
TenantId tenantId = dashboard.getTenantId();
if (CollectionUtils.isNotEmpty(dashboard.getResources())) { if (CollectionUtils.isNotEmpty(dashboard.getResources())) {
resourceService.importResources(dashboard.getTenantId(), dashboard.getResources()); resourceService.importResources(tenantId, dashboard.getResources());
} }
imageService.updateImagesUsage(dashboard); imageService.updateImagesUsage(dashboard);
resourceService.updateResourcesUsage(dashboard); resourceService.updateResourcesUsage(tenantId, dashboard);
var saved = dashboardDao.save(dashboard.getTenantId(), dashboard); var saved = dashboardDao.save(tenantId, dashboard);
publishEvictEvent(new DashboardTitleEvictEvent(saved.getId())); publishEvictEvent(new DashboardTitleEvictEvent(saved.getId()));
eventPublisher.publishEvent(SaveEntityEvent.builder().tenantId(saved.getTenantId()) eventPublisher.publishEvent(SaveEntityEvent.builder().tenantId(tenantId)
.entityId(saved.getId()).created(dashboard.getId() == null).build()); .entityId(saved.getId()).created(dashboard.getId() == null).build());
if (dashboard.getId() == null) { if (dashboard.getId() == null) {
countService.publishCountEntityEvictEvent(saved.getTenantId(), EntityType.DASHBOARD); countService.publishCountEntityEvictEvent(tenantId, EntityType.DASHBOARD);
} }
return saved; return saved;
} catch (Exception e) { } catch (Exception e) {

63
dao/src/main/java/org/thingsboard/server/dao/resource/BaseResourceService.java

@ -402,13 +402,13 @@ public class BaseResourceService extends AbstractCachedEntityService<ResourceInf
} }
@Override @Override
public boolean updateResourcesUsage(Dashboard dashboard) { public boolean updateResourcesUsage(TenantId tenantId, Dashboard dashboard) {
Map<String, String> links = getResourcesLinks(dashboard.getResources()); Map<String, String> links = getResourcesLinks(dashboard.getResources());
return updateResourcesUsage(dashboard.getTenantId(), List.of(dashboard.getConfiguration()), List.of(DASHBOARD_RESOURCES_MAPPING), links); return updateResourcesUsage(tenantId, List.of(dashboard.getConfiguration()), List.of(DASHBOARD_RESOURCES_MAPPING), links);
} }
@Override @Override
public boolean updateResourcesUsage(WidgetTypeDetails widgetTypeDetails) { public boolean updateResourcesUsage(TenantId tenantId, WidgetTypeDetails widgetTypeDetails) {
Map<String, String> links = getResourcesLinks(widgetTypeDetails.getResources()); Map<String, String> links = getResourcesLinks(widgetTypeDetails.getResources());
List<JsonNode> jsonNodes = new ArrayList<>(2); List<JsonNode> jsonNodes = new ArrayList<>(2);
List<Map<String, String>> mappings = new ArrayList<>(2); List<Map<String, String>> mappings = new ArrayList<>(2);
@ -422,7 +422,7 @@ public class BaseResourceService extends AbstractCachedEntityService<ResourceInf
mappings.add(WIDGET_DEFAULT_CONFIG_RESOURCES_MAPPING); mappings.add(WIDGET_DEFAULT_CONFIG_RESOURCES_MAPPING);
} }
boolean updated = updateResourcesUsage(widgetTypeDetails.getTenantId(), jsonNodes, mappings, links); boolean updated = updateResourcesUsage(tenantId, jsonNodes, mappings, links);
if (defaultConfig != null) { if (defaultConfig != null) {
widgetTypeDetails.setDefaultConfig(defaultConfig); widgetTypeDetails.setDefaultConfig(defaultConfig);
} }
@ -474,12 +474,12 @@ public class BaseResourceService extends AbstractCachedEntityService<ResourceInf
} }
@Override @Override
public Collection<TbResourceInfo> getUsedResources(Dashboard dashboard) { public Collection<TbResourceInfo> getUsedResources(TenantId tenantId, Dashboard dashboard) {
return getUsedResources(dashboard.getTenantId(), List.of(dashboard.getConfiguration()), List.of(DASHBOARD_RESOURCES_MAPPING)).values(); return getUsedResources(tenantId, List.of(dashboard.getConfiguration()), List.of(DASHBOARD_RESOURCES_MAPPING)).values();
} }
@Override @Override
public Collection<TbResourceInfo> getUsedResources(WidgetTypeDetails widgetTypeDetails) { public Collection<TbResourceInfo> getUsedResources(TenantId tenantId, WidgetTypeDetails widgetTypeDetails) {
List<JsonNode> jsonNodes = new ArrayList<>(2); List<JsonNode> jsonNodes = new ArrayList<>(2);
List<Map<String, String>> mappings = new ArrayList<>(2); List<Map<String, String>> mappings = new ArrayList<>(2);
@ -492,7 +492,7 @@ public class BaseResourceService extends AbstractCachedEntityService<ResourceInf
mappings.add(WIDGET_DEFAULT_CONFIG_RESOURCES_MAPPING); mappings.add(WIDGET_DEFAULT_CONFIG_RESOURCES_MAPPING);
} }
return getUsedResources(widgetTypeDetails.getTenantId(), jsonNodes, mappings).values(); return getUsedResources(tenantId, jsonNodes, mappings).values();
} }
private Map<TbResourceId, TbResourceInfo> getUsedResources(TenantId tenantId, List<JsonNode> jsonNodes, List<Map<String, String>> mappings) { private Map<TbResourceId, TbResourceInfo> getUsedResources(TenantId tenantId, List<JsonNode> jsonNodes, List<Map<String, String>> mappings) {
@ -543,30 +543,33 @@ public class BaseResourceService extends AbstractCachedEntityService<ResourceInf
for (int i = 0; i < jsonNodes.size(); i++) { for (int i = 0; i < jsonNodes.size(); i++) {
JsonNode jsonNode = jsonNodes.get(i); JsonNode jsonNode = jsonNodes.get(i);
// processing by mappings first // processing by mappings first
JacksonUtil.replaceByMapping(jsonNode, mappings.get(i), Collections.emptyMap(), (name, urlNode) -> { if (i <= mappings.size() - 1) {
String value = null; JacksonUtil.replaceByMapping(jsonNode, mappings.get(i), Collections.emptyMap(), (name, urlNode) -> {
if (urlNode.isTextual()) { // link is in the right place String value = null;
value = urlNode.asText(); if (urlNode.isTextual()) { // link is in the right place
} else { value = urlNode.asText();
JsonNode id = urlNode.get("id"); // old structure is used } else {
if (id != null && id.isTextual()) { JsonNode id = urlNode.get("id"); // old structure is used
value = id.asText(); if (id != null && id.isTextual()) {
value = id.asText();
}
} }
}
if (StringUtils.isNotBlank(value)) { if (StringUtils.isNotBlank(value)) {
value = processor.apply(value); value = processor.apply(value);
} else { } else {
value = ""; value = "";
} }
JsonNode newValue = new TextNode(value);
if (!newValue.toString().equals(urlNode.toString())) {
updated.set(true);
log.trace("Replaced by mapping '{}' ({}) with '{}'", value, name, newValue);
}
return newValue;
});
}
JsonNode newValue = new TextNode(value);
if (!newValue.toString().equals(urlNode.toString())) {
updated.set(true);
log.trace("Replaced by mapping '{}' ({}) with '{}'", value, name, newValue);
}
return newValue;
});
// processing all // processing all
JacksonUtil.replaceAll(jsonNode, "", (name, value) -> { JacksonUtil.replaceAll(jsonNode, "", (name, value) -> {
@ -597,7 +600,7 @@ public class BaseResourceService extends AbstractCachedEntityService<ResourceInf
importResources(dashboard.getTenantId(), dashboard.getResources()); importResources(dashboard.getTenantId(), dashboard.getResources());
} }
imageService.updateImagesUsage(dashboard); imageService.updateImagesUsage(dashboard);
updateResourcesUsage(dashboard); updateResourcesUsage(dashboard.getTenantId(), dashboard);
data = JacksonUtil.writeValueAsBytes(dashboard); data = JacksonUtil.writeValueAsBytes(dashboard);
} }

9
dao/src/main/java/org/thingsboard/server/dao/widget/WidgetTypeServiceImpl.java

@ -99,14 +99,15 @@ public class WidgetTypeServiceImpl implements WidgetTypeService {
log.trace("Executing saveWidgetType [{}]", widgetTypeDetails); log.trace("Executing saveWidgetType [{}]", widgetTypeDetails);
widgetTypeValidator.validate(widgetTypeDetails, WidgetType::getTenantId); widgetTypeValidator.validate(widgetTypeDetails, WidgetType::getTenantId);
try { try {
TenantId tenantId = widgetTypeDetails.getTenantId();
if (CollectionUtils.isNotEmpty(widgetTypeDetails.getResources())) { if (CollectionUtils.isNotEmpty(widgetTypeDetails.getResources())) {
resourceService.importResources(widgetTypeDetails.getTenantId(), widgetTypeDetails.getResources()); resourceService.importResources(tenantId, widgetTypeDetails.getResources());
} }
imageService.updateImagesUsage(widgetTypeDetails); imageService.updateImagesUsage(widgetTypeDetails);
resourceService.updateResourcesUsage(widgetTypeDetails); resourceService.updateResourcesUsage(tenantId, widgetTypeDetails);
WidgetTypeDetails result = widgetTypeDao.save(widgetTypeDetails.getTenantId(), widgetTypeDetails); WidgetTypeDetails result = widgetTypeDao.save(tenantId, widgetTypeDetails);
eventPublisher.publishEvent(SaveEntityEvent.builder().tenantId(result.getTenantId()) eventPublisher.publishEvent(SaveEntityEvent.builder().tenantId(tenantId)
.entityId(result.getId()).created(widgetTypeDetails.getId() == null).build()); .entityId(result.getId()).created(widgetTypeDetails.getId() == null).build());
return result; return result;
} catch (Exception t) { } catch (Exception t) {

Loading…
Cancel
Save