|
|
|
@ -15,17 +15,23 @@ |
|
|
|
*/ |
|
|
|
package org.thingsboard.server.dao.widget; |
|
|
|
|
|
|
|
import com.fasterxml.jackson.databind.JsonNode; |
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
import org.springframework.context.ApplicationEventPublisher; |
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
import org.springframework.transaction.annotation.Transactional; |
|
|
|
import org.thingsboard.common.util.JacksonUtil; |
|
|
|
import org.thingsboard.server.common.data.EntityType; |
|
|
|
import org.thingsboard.server.common.data.ResourceType; |
|
|
|
import org.thingsboard.server.common.data.id.EntityId; |
|
|
|
import org.thingsboard.server.common.data.id.HasId; |
|
|
|
import org.thingsboard.server.common.data.id.TenantId; |
|
|
|
import org.thingsboard.server.common.data.id.WidgetsBundleId; |
|
|
|
import org.thingsboard.server.common.data.page.PageData; |
|
|
|
import org.thingsboard.server.common.data.page.PageLink; |
|
|
|
import org.thingsboard.server.common.data.widget.WidgetType; |
|
|
|
import org.thingsboard.server.common.data.widget.WidgetTypeDetails; |
|
|
|
import org.thingsboard.server.common.data.widget.WidgetsBundle; |
|
|
|
import org.thingsboard.server.common.data.widget.WidgetsBundleFilter; |
|
|
|
import org.thingsboard.server.dao.entity.AbstractCachedEntityService; |
|
|
|
@ -33,6 +39,7 @@ import org.thingsboard.server.dao.eventsourcing.DeleteEntityEvent; |
|
|
|
import org.thingsboard.server.dao.eventsourcing.SaveEntityEvent; |
|
|
|
import org.thingsboard.server.dao.exception.IncorrectParameterException; |
|
|
|
import org.thingsboard.server.dao.resource.ImageService; |
|
|
|
import org.thingsboard.server.dao.resource.ResourceService; |
|
|
|
import org.thingsboard.server.dao.service.DataValidator; |
|
|
|
import org.thingsboard.server.dao.service.PaginatedRemover; |
|
|
|
import org.thingsboard.server.dao.service.Validator; |
|
|
|
@ -40,6 +47,7 @@ import org.thingsboard.server.dao.service.Validator; |
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.List; |
|
|
|
import java.util.Optional; |
|
|
|
import java.util.stream.Stream; |
|
|
|
|
|
|
|
@Service("WidgetsBundleDaoService") |
|
|
|
@Slf4j |
|
|
|
@ -64,6 +72,9 @@ public class WidgetsBundleServiceImpl implements WidgetsBundleService { |
|
|
|
@Autowired |
|
|
|
private ImageService imageService; |
|
|
|
|
|
|
|
@Autowired |
|
|
|
private ResourceService resourceService; |
|
|
|
|
|
|
|
@Override |
|
|
|
public WidgetsBundle findWidgetsBundleById(TenantId tenantId, WidgetsBundleId widgetsBundleId) { |
|
|
|
log.trace("Executing findWidgetsBundleById [{}]", widgetsBundleId); |
|
|
|
@ -196,6 +207,71 @@ public class WidgetsBundleServiceImpl implements WidgetsBundleService { |
|
|
|
deleteWidgetsBundlesByTenantId(tenantId); |
|
|
|
} |
|
|
|
|
|
|
|
@Transactional |
|
|
|
@Override |
|
|
|
public void updateSystemWidgets(Stream<String> bundles, Stream<String> widgets) { |
|
|
|
widgets.forEach(widgetTypeJson -> { |
|
|
|
try { |
|
|
|
updateSystemWidget(widgetTypeJson); |
|
|
|
} catch (Exception e) { |
|
|
|
throw new RuntimeException("Unable to load widget type from json: " + widgetTypeJson, e); |
|
|
|
} |
|
|
|
}); |
|
|
|
|
|
|
|
bundles.forEach(widgetsBundleDescriptorJson -> { |
|
|
|
JsonNode widgetsBundleDescriptor = JacksonUtil.toJsonNode(widgetsBundleDescriptorJson); |
|
|
|
if (widgetsBundleDescriptor == null || !widgetsBundleDescriptor.has("widgetsBundle")) { |
|
|
|
throw new RuntimeException("Invalid widgets bundle json: [" + widgetsBundleDescriptorJson + "]"); |
|
|
|
} |
|
|
|
|
|
|
|
JsonNode widgetsBundleJson = widgetsBundleDescriptor.get("widgetsBundle"); |
|
|
|
WidgetsBundle widgetsBundle = JacksonUtil.treeToValue(widgetsBundleJson, WidgetsBundle.class); |
|
|
|
WidgetsBundle existingWidgetsBundle = findWidgetsBundleByTenantIdAndAlias(TenantId.SYS_TENANT_ID, widgetsBundle.getAlias()); |
|
|
|
if (existingWidgetsBundle != null) { |
|
|
|
widgetsBundle.setId(existingWidgetsBundle.getId()); |
|
|
|
widgetsBundle.setCreatedTime(existingWidgetsBundle.getCreatedTime()); |
|
|
|
} |
|
|
|
widgetsBundle.setTenantId(TenantId.SYS_TENANT_ID); |
|
|
|
widgetsBundle = saveWidgetsBundle(widgetsBundle); |
|
|
|
log.debug("{} widgets bundle {}", existingWidgetsBundle == null ? "Created" : "Updated", widgetsBundle.getAlias()); |
|
|
|
|
|
|
|
List<String> widgetTypeFqns = new ArrayList<>(); |
|
|
|
if (widgetsBundleDescriptor.has("widgetTypes")) { |
|
|
|
JsonNode widgetTypesArrayJson = widgetsBundleDescriptor.get("widgetTypes"); |
|
|
|
widgetTypesArrayJson.forEach(widgetTypeJson -> { |
|
|
|
try { |
|
|
|
WidgetTypeDetails widgetTypeDetails = updateSystemWidget(widgetTypeJson.toString()); |
|
|
|
widgetTypeFqns.add(widgetTypeDetails.getFqn()); |
|
|
|
} catch (Exception e) { |
|
|
|
throw new RuntimeException("Unable to load widget type from json: " + widgetsBundleDescriptorJson, e); |
|
|
|
} |
|
|
|
}); |
|
|
|
} |
|
|
|
if (widgetsBundleDescriptor.has("widgetTypeFqns")) { |
|
|
|
JsonNode widgetFqnsArrayJson = widgetsBundleDescriptor.get("widgetTypeFqns"); |
|
|
|
widgetFqnsArrayJson.forEach(fqnJson -> { |
|
|
|
widgetTypeFqns.add(fqnJson.asText()); |
|
|
|
}); |
|
|
|
} |
|
|
|
widgetTypeService.updateWidgetsBundleWidgetFqns(TenantId.SYS_TENANT_ID, widgetsBundle.getId(), widgetTypeFqns); |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
private WidgetTypeDetails updateSystemWidget(String widgetTypeJson) { |
|
|
|
widgetTypeJson = resourceService.checkSystemResourcesUsage(widgetTypeJson, ResourceType.JS_MODULE); |
|
|
|
|
|
|
|
WidgetTypeDetails widgetTypeDetails = JacksonUtil.fromString(widgetTypeJson, WidgetTypeDetails.class); |
|
|
|
WidgetType existingWidget = widgetTypeService.findWidgetTypeByTenantIdAndFqn(TenantId.SYS_TENANT_ID, widgetTypeDetails.getFqn()); |
|
|
|
if (existingWidget != null) { |
|
|
|
widgetTypeDetails.setId(existingWidget.getId()); |
|
|
|
widgetTypeDetails.setCreatedTime(existingWidget.getCreatedTime()); |
|
|
|
} |
|
|
|
widgetTypeDetails.setTenantId(TenantId.SYS_TENANT_ID); |
|
|
|
widgetTypeDetails = widgetTypeService.saveWidgetType(widgetTypeDetails); |
|
|
|
log.debug("{} widget type {}", existingWidget == null ? "Created" : "Updated", widgetTypeDetails.getFqn()); |
|
|
|
return widgetTypeDetails; |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public Optional<HasId<?>> findEntity(TenantId tenantId, EntityId entityId) { |
|
|
|
return Optional.ofNullable(findWidgetsBundleById(tenantId, new WidgetsBundleId(entityId.getId()))); |
|
|
|
|