Improve load system widgets method.
This commit is contained in:
		
							parent
							
								
									b0bfdc535a
								
							
						
					
					
						commit
						3d182a736b
					
				@ -108,7 +108,7 @@ public class ThingsboardInstallService {
 | 
			
		||||
                    log.info("Migrating ThingsBoard entities data from cassandra to SQL database ...");
 | 
			
		||||
                    entitiesMigrateService.migrate();
 | 
			
		||||
                    log.info("Updating system data...");
 | 
			
		||||
                    systemDataLoaderService.updateSystemWidgets();
 | 
			
		||||
                    systemDataLoaderService.loadSystemWidgets();
 | 
			
		||||
                } else if ("3.0.1-cassandra".equals(upgradeFromVersion)) {
 | 
			
		||||
                    log.info("Migrating ThingsBoard latest timeseries data from cassandra to SQL database ...");
 | 
			
		||||
                    latestMigrateService.migrate();
 | 
			
		||||
@ -275,7 +275,7 @@ public class ThingsboardInstallService {
 | 
			
		||||
                    entityDatabaseSchemaService.createOrUpdateDeviceInfoView(persistToTelemetry);
 | 
			
		||||
                    log.info("Updating system data...");
 | 
			
		||||
                    dataUpdateService.upgradeRuleNodes();
 | 
			
		||||
                    systemDataLoaderService.updateSystemWidgets();
 | 
			
		||||
                    systemDataLoaderService.loadSystemWidgets();
 | 
			
		||||
                    installScripts.loadSystemLwm2mResources();
 | 
			
		||||
                }
 | 
			
		||||
                log.info("Upgrade finished successfully!");
 | 
			
		||||
 | 
			
		||||
@ -63,7 +63,6 @@ import org.thingsboard.server.common.data.kv.BasicTsKvEntry;
 | 
			
		||||
import org.thingsboard.server.common.data.kv.BooleanDataEntry;
 | 
			
		||||
import org.thingsboard.server.common.data.kv.DoubleDataEntry;
 | 
			
		||||
import org.thingsboard.server.common.data.kv.LongDataEntry;
 | 
			
		||||
import org.thingsboard.server.common.data.page.PageData;
 | 
			
		||||
import org.thingsboard.server.common.data.page.PageDataIterable;
 | 
			
		||||
import org.thingsboard.server.common.data.page.PageLink;
 | 
			
		||||
import org.thingsboard.server.common.data.query.BooleanFilterPredicate;
 | 
			
		||||
@ -84,9 +83,6 @@ import org.thingsboard.server.common.data.security.UserCredentials;
 | 
			
		||||
import org.thingsboard.server.common.data.tenant.profile.DefaultTenantProfileConfiguration;
 | 
			
		||||
import org.thingsboard.server.common.data.tenant.profile.TenantProfileData;
 | 
			
		||||
import org.thingsboard.server.common.data.tenant.profile.TenantProfileQueueConfiguration;
 | 
			
		||||
import org.thingsboard.server.common.data.widget.DeprecatedFilter;
 | 
			
		||||
import org.thingsboard.server.common.data.widget.WidgetTypeInfo;
 | 
			
		||||
import org.thingsboard.server.common.data.widget.WidgetsBundle;
 | 
			
		||||
import org.thingsboard.server.dao.attributes.AttributesService;
 | 
			
		||||
import org.thingsboard.server.dao.customer.CustomerService;
 | 
			
		||||
import org.thingsboard.server.dao.device.DeviceCredentialsService;
 | 
			
		||||
@ -536,58 +532,11 @@ public class DefaultSystemDataLoaderService implements SystemDataLoaderService {
 | 
			
		||||
        installScripts.loadDashboards(demoTenant.getId(), null);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void deleteSystemWidgetBundle(String bundleAlias) throws Exception {
 | 
			
		||||
        WidgetsBundle widgetsBundle = widgetsBundleService.findWidgetsBundleByTenantIdAndAlias(TenantId.SYS_TENANT_ID, bundleAlias);
 | 
			
		||||
        if (widgetsBundle != null) {
 | 
			
		||||
            PageData<WidgetTypeInfo> widgetTypes;
 | 
			
		||||
            var pageLink = new PageLink(1024);
 | 
			
		||||
            do {
 | 
			
		||||
                widgetTypes = widgetTypeService.findWidgetTypesInfosByWidgetsBundleId(TenantId.SYS_TENANT_ID, widgetsBundle.getId(), false, DeprecatedFilter.ALL, null, pageLink);
 | 
			
		||||
                for (var widgetType : widgetTypes.getData()) {
 | 
			
		||||
                    widgetTypeService.deleteWidgetType(TenantId.SYS_TENANT_ID, widgetType.getId());
 | 
			
		||||
                }
 | 
			
		||||
                pageLink.nextPageLink();
 | 
			
		||||
            } while (widgetTypes.hasNext());
 | 
			
		||||
            widgetsBundleService.deleteWidgetsBundle(TenantId.SYS_TENANT_ID, widgetsBundle.getId());
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void loadSystemWidgets() throws Exception {
 | 
			
		||||
        installScripts.loadSystemWidgets();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void updateSystemWidgets() throws Exception {
 | 
			
		||||
        this.deleteSystemWidgetBundle("charts");
 | 
			
		||||
        this.deleteSystemWidgetBundle("cards");
 | 
			
		||||
        this.deleteSystemWidgetBundle("maps");
 | 
			
		||||
        this.deleteSystemWidgetBundle("analogue_gauges");
 | 
			
		||||
        this.deleteSystemWidgetBundle("digital_gauges");
 | 
			
		||||
        this.deleteSystemWidgetBundle("gpio_widgets");
 | 
			
		||||
        this.deleteSystemWidgetBundle("alarm_widgets");
 | 
			
		||||
        this.deleteSystemWidgetBundle("control_widgets");
 | 
			
		||||
        this.deleteSystemWidgetBundle("maps_v2");
 | 
			
		||||
        this.deleteSystemWidgetBundle("gateway_widgets");
 | 
			
		||||
        this.deleteSystemWidgetBundle("input_widgets");
 | 
			
		||||
        this.deleteSystemWidgetBundle("date");
 | 
			
		||||
        this.deleteSystemWidgetBundle("entity_admin_widgets");
 | 
			
		||||
        this.deleteSystemWidgetBundle("navigation_widgets");
 | 
			
		||||
        this.deleteSystemWidgetBundle("edge_widgets");
 | 
			
		||||
        this.deleteSystemWidgetBundle("home_page_widgets");
 | 
			
		||||
        this.deleteSystemWidgetBundle("entity_widgets");
 | 
			
		||||
        this.deleteSystemWidgetBundle("html_widgets");
 | 
			
		||||
        this.deleteSystemWidgetBundle("tables");
 | 
			
		||||
        this.deleteSystemWidgetBundle("count_widgets");
 | 
			
		||||
        this.deleteSystemWidgetBundle("status_indicators");
 | 
			
		||||
        this.deleteSystemWidgetBundle("outdoor_environment");
 | 
			
		||||
        this.deleteSystemWidgetBundle("indoor_environment");
 | 
			
		||||
        this.deleteSystemWidgetBundle("air_quality");
 | 
			
		||||
        this.deleteSystemWidgetBundle("liquid_level_tanks");
 | 
			
		||||
        installScripts.loadSystemWidgets();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private User createUser(Authority authority,
 | 
			
		||||
                            TenantId tenantId,
 | 
			
		||||
                            CustomerId customerId,
 | 
			
		||||
 | 
			
		||||
@ -29,9 +29,13 @@ import org.thingsboard.server.common.data.exception.ThingsboardException;
 | 
			
		||||
import org.thingsboard.server.common.data.id.CustomerId;
 | 
			
		||||
import org.thingsboard.server.common.data.id.TenantId;
 | 
			
		||||
import org.thingsboard.server.common.data.oauth2.OAuth2ClientRegistrationTemplate;
 | 
			
		||||
import org.thingsboard.server.common.data.page.PageData;
 | 
			
		||||
import org.thingsboard.server.common.data.page.PageLink;
 | 
			
		||||
import org.thingsboard.server.common.data.rule.RuleChain;
 | 
			
		||||
import org.thingsboard.server.common.data.rule.RuleChainMetaData;
 | 
			
		||||
import org.thingsboard.server.common.data.widget.DeprecatedFilter;
 | 
			
		||||
import org.thingsboard.server.common.data.widget.WidgetTypeDetails;
 | 
			
		||||
import org.thingsboard.server.common.data.widget.WidgetTypeInfo;
 | 
			
		||||
import org.thingsboard.server.common.data.widget.WidgetsBundle;
 | 
			
		||||
import org.thingsboard.server.dao.dashboard.DashboardService;
 | 
			
		||||
import org.thingsboard.server.dao.exception.DataValidationException;
 | 
			
		||||
@ -48,7 +52,9 @@ import java.nio.file.Path;
 | 
			
		||||
import java.nio.file.Paths;
 | 
			
		||||
import java.util.ArrayList;
 | 
			
		||||
import java.util.Base64;
 | 
			
		||||
import java.util.HashMap;
 | 
			
		||||
import java.util.List;
 | 
			
		||||
import java.util.Map;
 | 
			
		||||
import java.util.Optional;
 | 
			
		||||
import java.util.function.Function;
 | 
			
		||||
 | 
			
		||||
@ -189,6 +195,38 @@ public class InstallScripts {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void loadSystemWidgets() throws Exception {
 | 
			
		||||
        Map<Path, JsonNode> widgetsBundlesMap = new HashMap<>();
 | 
			
		||||
        Path widgetBundlesDir = Paths.get(getDataDir(), JSON_DIR, SYSTEM_DIR, WIDGET_BUNDLES_DIR);
 | 
			
		||||
        try (DirectoryStream<Path> dirStream = Files.newDirectoryStream(widgetBundlesDir, path -> path.toString().endsWith(JSON_EXT))) {
 | 
			
		||||
            dirStream.forEach(
 | 
			
		||||
                    path -> {
 | 
			
		||||
                        JsonNode widgetsBundleDescriptorJson;
 | 
			
		||||
                        try {
 | 
			
		||||
                            widgetsBundleDescriptorJson = JacksonUtil.toJsonNode(path.toFile());
 | 
			
		||||
                        } catch (Exception e) {
 | 
			
		||||
                            log.error("Unable to parse widgets bundle from json: [{}]", path);
 | 
			
		||||
                            throw new RuntimeException("Unable to parse widgets bundle from json", e);
 | 
			
		||||
                        }
 | 
			
		||||
                        if (widgetsBundleDescriptorJson == null || !widgetsBundleDescriptorJson.has("widgetsBundle")) {
 | 
			
		||||
                            log.error("Invalid widgets bundle json: [{}]", path);
 | 
			
		||||
                            throw new RuntimeException("Invalid widgets bundle json: [" + path + "]");
 | 
			
		||||
                        }
 | 
			
		||||
                        widgetsBundlesMap.put(path, widgetsBundleDescriptorJson);
 | 
			
		||||
                        JsonNode bundleAliasNode = widgetsBundleDescriptorJson.get("widgetsBundle").get("alias");
 | 
			
		||||
                        if (bundleAliasNode == null || !bundleAliasNode.isTextual()) {
 | 
			
		||||
                            log.error("Invalid widgets bundle json: [{}]", path);
 | 
			
		||||
                            throw new RuntimeException("Invalid widgets bundle json: [" + path + "]");
 | 
			
		||||
                        }
 | 
			
		||||
                        String bundleAlias = bundleAliasNode.asText();
 | 
			
		||||
                        try {
 | 
			
		||||
                            this.deleteSystemWidgetBundle(bundleAlias);
 | 
			
		||||
                        } catch (Exception e) {
 | 
			
		||||
                            log.error("Failed to delete system widgets bundle: [{}]", bundleAlias);
 | 
			
		||||
                            throw new RuntimeException("Failed to delete system widgets bundle: [" + bundleAlias + "]", e);
 | 
			
		||||
                        }
 | 
			
		||||
                    }
 | 
			
		||||
            );
 | 
			
		||||
        }
 | 
			
		||||
        Path widgetTypesDir = Paths.get(getDataDir(), JSON_DIR, SYSTEM_DIR, WIDGET_TYPES_DIR);
 | 
			
		||||
        if (Files.exists(widgetTypesDir)) {
 | 
			
		||||
            try (DirectoryStream<Path> dirStream = Files.newDirectoryStream(widgetTypesDir, path -> path.toString().endsWith(JSON_EXT))) {
 | 
			
		||||
@ -206,44 +244,56 @@ public class InstallScripts {
 | 
			
		||||
                );
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        Path widgetBundlesDir = Paths.get(getDataDir(), JSON_DIR, SYSTEM_DIR, WIDGET_BUNDLES_DIR);
 | 
			
		||||
        try (DirectoryStream<Path> dirStream = Files.newDirectoryStream(widgetBundlesDir, path -> path.toString().endsWith(JSON_EXT))) {
 | 
			
		||||
            dirStream.forEach(
 | 
			
		||||
                    path -> {
 | 
			
		||||
                        try {
 | 
			
		||||
                            JsonNode widgetsBundleDescriptorJson = JacksonUtil.toJsonNode(path.toFile());
 | 
			
		||||
                            JsonNode widgetsBundleJson = widgetsBundleDescriptorJson.get("widgetsBundle");
 | 
			
		||||
                            WidgetsBundle widgetsBundle = JacksonUtil.treeToValue(widgetsBundleJson, WidgetsBundle.class);
 | 
			
		||||
                            WidgetsBundle savedWidgetsBundle = widgetsBundleService.saveWidgetsBundle(widgetsBundle);
 | 
			
		||||
                            List<String> widgetTypeFqns = new ArrayList<>();
 | 
			
		||||
                            if (widgetsBundleDescriptorJson.has("widgetTypes")) {
 | 
			
		||||
                                JsonNode widgetTypesArrayJson = widgetsBundleDescriptorJson.get("widgetTypes");
 | 
			
		||||
                                widgetTypesArrayJson.forEach(
 | 
			
		||||
                                        widgetTypeJson -> {
 | 
			
		||||
                                            try {
 | 
			
		||||
                                                WidgetTypeDetails widgetTypeDetails = JacksonUtil.treeToValue(widgetTypeJson, WidgetTypeDetails.class);
 | 
			
		||||
                                                var savedWidgetType = widgetTypeService.saveWidgetType(widgetTypeDetails);
 | 
			
		||||
                                                widgetTypeFqns.add(savedWidgetType.getFqn());
 | 
			
		||||
                                            } catch (Exception e) {
 | 
			
		||||
                                                log.error("Unable to load widget type from json: [{}]", path.toString());
 | 
			
		||||
                                                throw new RuntimeException("Unable to load widget type from json", e);
 | 
			
		||||
                                            }
 | 
			
		||||
                                        }
 | 
			
		||||
                                );
 | 
			
		||||
        for (var widgetsBundleDescriptorEntry : widgetsBundlesMap.entrySet()) {
 | 
			
		||||
            Path path = widgetsBundleDescriptorEntry.getKey();
 | 
			
		||||
            try {
 | 
			
		||||
                JsonNode widgetsBundleDescriptorJson = widgetsBundleDescriptorEntry.getValue();
 | 
			
		||||
                JsonNode widgetsBundleJson = widgetsBundleDescriptorJson.get("widgetsBundle");
 | 
			
		||||
                WidgetsBundle widgetsBundle = JacksonUtil.treeToValue(widgetsBundleJson, WidgetsBundle.class);
 | 
			
		||||
                WidgetsBundle savedWidgetsBundle = widgetsBundleService.saveWidgetsBundle(widgetsBundle);
 | 
			
		||||
                List<String> widgetTypeFqns = new ArrayList<>();
 | 
			
		||||
                if (widgetsBundleDescriptorJson.has("widgetTypes")) {
 | 
			
		||||
                    JsonNode widgetTypesArrayJson = widgetsBundleDescriptorJson.get("widgetTypes");
 | 
			
		||||
                    widgetTypesArrayJson.forEach(
 | 
			
		||||
                            widgetTypeJson -> {
 | 
			
		||||
                                try {
 | 
			
		||||
                                    WidgetTypeDetails widgetTypeDetails = JacksonUtil.treeToValue(widgetTypeJson, WidgetTypeDetails.class);
 | 
			
		||||
                                    var savedWidgetType = widgetTypeService.saveWidgetType(widgetTypeDetails);
 | 
			
		||||
                                    widgetTypeFqns.add(savedWidgetType.getFqn());
 | 
			
		||||
                                } catch (Exception e) {
 | 
			
		||||
                                    log.error("Unable to load widget type from json: [{}]", path.toString());
 | 
			
		||||
                                    throw new RuntimeException("Unable to load widget type from json", e);
 | 
			
		||||
                                }
 | 
			
		||||
                            }
 | 
			
		||||
                            if (widgetsBundleDescriptorJson.has("widgetTypeFqns")) {
 | 
			
		||||
                                JsonNode widgetFqnsArrayJson = widgetsBundleDescriptorJson.get("widgetTypeFqns");
 | 
			
		||||
                                widgetFqnsArrayJson.forEach(fqnJson -> {
 | 
			
		||||
                                    widgetTypeFqns.add(fqnJson.asText());
 | 
			
		||||
                                });
 | 
			
		||||
                            }
 | 
			
		||||
                            widgetTypeService.updateWidgetsBundleWidgetFqns(TenantId.SYS_TENANT_ID, savedWidgetsBundle.getId(), widgetTypeFqns);
 | 
			
		||||
                        } catch (Exception e) {
 | 
			
		||||
                            log.error("Unable to load widgets bundle from json: [{}]", path.toString());
 | 
			
		||||
                            throw new RuntimeException("Unable to load widgets bundle from json", e);
 | 
			
		||||
                        }
 | 
			
		||||
                    }
 | 
			
		||||
            );
 | 
			
		||||
                    );
 | 
			
		||||
                }
 | 
			
		||||
                if (widgetsBundleDescriptorJson.has("widgetTypeFqns")) {
 | 
			
		||||
                    JsonNode widgetFqnsArrayJson = widgetsBundleDescriptorJson.get("widgetTypeFqns");
 | 
			
		||||
                    widgetFqnsArrayJson.forEach(fqnJson -> {
 | 
			
		||||
                        widgetTypeFqns.add(fqnJson.asText());
 | 
			
		||||
                    });
 | 
			
		||||
                }
 | 
			
		||||
                widgetTypeService.updateWidgetsBundleWidgetFqns(TenantId.SYS_TENANT_ID, savedWidgetsBundle.getId(), widgetTypeFqns);
 | 
			
		||||
            } catch (Exception e) {
 | 
			
		||||
                log.error("Unable to load widgets bundle from json: [{}]", path.toString());
 | 
			
		||||
                throw new RuntimeException("Unable to load widgets bundle from json", e);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void deleteSystemWidgetBundle(String bundleAlias) {
 | 
			
		||||
        WidgetsBundle widgetsBundle = widgetsBundleService.findWidgetsBundleByTenantIdAndAlias(TenantId.SYS_TENANT_ID, bundleAlias);
 | 
			
		||||
        if (widgetsBundle != null) {
 | 
			
		||||
            PageData<WidgetTypeInfo> widgetTypes;
 | 
			
		||||
            var pageLink = new PageLink(1024);
 | 
			
		||||
            do {
 | 
			
		||||
                widgetTypes = widgetTypeService.findWidgetTypesInfosByWidgetsBundleId(TenantId.SYS_TENANT_ID, widgetsBundle.getId(), false, DeprecatedFilter.ALL, null, pageLink);
 | 
			
		||||
                for (var widgetType : widgetTypes.getData()) {
 | 
			
		||||
                    widgetTypeService.deleteWidgetType(TenantId.SYS_TENANT_ID, widgetType.getId());
 | 
			
		||||
                }
 | 
			
		||||
                pageLink.nextPageLink();
 | 
			
		||||
            } while (widgetTypes.hasNext());
 | 
			
		||||
            widgetsBundleService.deleteWidgetsBundle(TenantId.SYS_TENANT_ID, widgetsBundle.getId());
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -31,12 +31,8 @@ public interface SystemDataLoaderService {
 | 
			
		||||
 | 
			
		||||
    void loadSystemWidgets() throws Exception;
 | 
			
		||||
 | 
			
		||||
    void updateSystemWidgets() throws Exception;
 | 
			
		||||
 | 
			
		||||
    void loadDemoData() throws Exception;
 | 
			
		||||
 | 
			
		||||
    void deleteSystemWidgetBundle(String bundleAlias) throws Exception;
 | 
			
		||||
 | 
			
		||||
    void createQueues();
 | 
			
		||||
 | 
			
		||||
    void createDefaultNotificationConfigs();
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user