Add support for dashboard filter backward compatibility
This commit is contained in:
parent
aa74965f01
commit
cbf4297593
@ -16,7 +16,6 @@
|
||||
package org.thingsboard.server.service.install.update;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.node.ArrayNode;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import com.google.common.util.concurrent.Futures;
|
||||
import com.google.common.util.concurrent.ListenableFuture;
|
||||
@ -84,6 +83,7 @@ import org.thingsboard.server.dao.timeseries.TimeseriesService;
|
||||
import org.thingsboard.server.service.install.InstallScripts;
|
||||
import org.thingsboard.server.service.install.SystemDataLoaderService;
|
||||
import org.thingsboard.server.service.install.TbRuleEngineQueueConfigService;
|
||||
import org.thingsboard.server.utils.MiscUtils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
@ -748,17 +748,12 @@ public class DefaultDataUpdateService implements DataUpdateService {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
JsonNode configuration = dashboard.getConfiguration();
|
||||
JsonNode entityAliases = configuration.get("entityAliases");
|
||||
List<ObjectNode> entityAliases = dashboard.getEntityAliasesConfig();
|
||||
if (entityAliases == null) {
|
||||
return null;
|
||||
}
|
||||
for (JsonNode entityAlias : entityAliases) {
|
||||
JsonNode filter = entityAlias.get("filter");
|
||||
updateFilterIfRequired(filter, "assetType", "assetType", "assetTypes");
|
||||
updateFilterIfRequired(filter, "deviceType", "deviceType", "deviceTypes");
|
||||
updateFilterIfRequired(filter, "entityViewType", "entityViewType", "entityViewTypes");
|
||||
updateFilterIfRequired(filter, "edgeType", "edgeType", "edgeTypes");
|
||||
MiscUtils.updateDashboardFilterIfRequired(entityAlias);
|
||||
}
|
||||
dashboardService.saveDashboard(dashboard);
|
||||
} catch (Exception e) {
|
||||
@ -768,17 +763,5 @@ public class DefaultDataUpdateService implements DataUpdateService {
|
||||
}, MoreExecutors.directExecutor());
|
||||
}
|
||||
|
||||
private static void updateFilterIfRequired(JsonNode filter, String filterType, String singleTypeParamName, String multipleTypesParamName) {
|
||||
if (filter == null || filter.get("type") == null) {
|
||||
return;
|
||||
}
|
||||
if (filterType.equals(filter.get("type").asText())) {
|
||||
if (filter.get(singleTypeParamName) != null) {
|
||||
ArrayNode arrayNode = JacksonUtil.OBJECT_MAPPER.createArrayNode();
|
||||
arrayNode.add(filter.get(singleTypeParamName).asText());
|
||||
((ObjectNode) filter).set(multipleTypesParamName, arrayNode);
|
||||
((ObjectNode) filter).remove(singleTypeParamName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -32,6 +32,7 @@ import org.thingsboard.server.common.data.sync.ie.EntityExportData;
|
||||
import org.thingsboard.server.dao.dashboard.DashboardService;
|
||||
import org.thingsboard.server.queue.util.TbCoreComponent;
|
||||
import org.thingsboard.server.service.sync.vc.data.EntitiesImportCtx;
|
||||
import org.thingsboard.server.utils.MiscUtils;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
@ -73,6 +74,9 @@ public class DashboardImportService extends BaseEntityImportService<DashboardId,
|
||||
for (JsonNode widgetConfig : dashboard.getWidgetsConfig()) {
|
||||
replaceIdsRecursively(ctx, idProvider, JacksonUtil.getSafely(widgetConfig, "config", "actions"), Collections.singleton("id"), HINTS);
|
||||
}
|
||||
for (JsonNode entityAlias : dashboard.getEntityAliasesConfig()) {
|
||||
MiscUtils.updateDashboardFilterIfRequired(entityAlias);
|
||||
}
|
||||
return dashboard;
|
||||
}
|
||||
|
||||
|
||||
@ -15,8 +15,13 @@
|
||||
*/
|
||||
package org.thingsboard.server.utils;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.node.ArrayNode;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import com.google.common.hash.HashFunction;
|
||||
import com.google.common.hash.Hashing;
|
||||
import org.thingsboard.common.util.JacksonUtil;
|
||||
import org.thingsboard.server.common.data.query.EntityFilterType;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.nio.charset.Charset;
|
||||
@ -106,4 +111,26 @@ public class MiscUtils {
|
||||
}
|
||||
return serverPort;
|
||||
}
|
||||
|
||||
public static void updateDashboardFilterIfRequired(JsonNode entityAlias) {
|
||||
JsonNode filter = entityAlias.get("filter");
|
||||
updateFilterByTypeIfRequired(filter, EntityFilterType.ASSET_TYPE.getLabel());
|
||||
updateFilterByTypeIfRequired(filter, EntityFilterType.DEVICE_TYPE.getLabel());
|
||||
updateFilterByTypeIfRequired(filter, EntityFilterType.ENTITY_VIEW_TYPE.getLabel());
|
||||
updateFilterByTypeIfRequired(filter, EntityFilterType.EDGE_TYPE.getLabel());
|
||||
}
|
||||
|
||||
private static void updateFilterByTypeIfRequired(JsonNode filter, String filterType) {
|
||||
if (filter == null || filter.get("type") == null) {
|
||||
return;
|
||||
}
|
||||
if (filterType.equals(filter.get("type").asText())) {
|
||||
if (filter.get(filterType) != null) {
|
||||
ArrayNode arrayNode = JacksonUtil.OBJECT_MAPPER.createArrayNode();
|
||||
arrayNode.add(filter.get(filterType).asText());
|
||||
((ObjectNode) filter).set(filterType + "s", arrayNode);
|
||||
((ObjectNode) filter).remove(filterType);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -22,13 +22,19 @@ import java.util.List;
|
||||
@Data
|
||||
public class AssetTypeFilter implements EntityFilter {
|
||||
|
||||
@Override
|
||||
public EntityFilterType getType() {
|
||||
return EntityFilterType.ASSET_TYPE;
|
||||
}
|
||||
/**
|
||||
* Replaced by {@link AssetTypeFilter#getAssetTypes()} instead.
|
||||
*/
|
||||
@Deprecated(since = "3.5", forRemoval = true)
|
||||
private String assetType;
|
||||
|
||||
private List<String> assetTypes;
|
||||
|
||||
private String assetNameFilter;
|
||||
|
||||
@Override
|
||||
public EntityFilterType getType() {
|
||||
return EntityFilterType.ASSET_TYPE;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -26,13 +26,23 @@ import java.util.List;
|
||||
@Data
|
||||
public class DeviceTypeFilter implements EntityFilter {
|
||||
|
||||
@Override
|
||||
public EntityFilterType getType() {
|
||||
return EntityFilterType.DEVICE_TYPE;
|
||||
}
|
||||
/**
|
||||
* Replaced by {@link DeviceTypeFilter#getDeviceTypes()} instead.
|
||||
*/
|
||||
@Deprecated(since = "3.5", forRemoval = true)
|
||||
private String deviceType;
|
||||
|
||||
private List<String> deviceTypes;
|
||||
|
||||
private String deviceNameFilter;
|
||||
|
||||
public DeviceTypeFilter(List<String> deviceTypes, String deviceNameFilter) {
|
||||
this.deviceTypes = deviceTypes;
|
||||
this.deviceNameFilter = deviceNameFilter;
|
||||
}
|
||||
|
||||
@Override
|
||||
public EntityFilterType getType() {
|
||||
return EntityFilterType.DEVICE_TYPE;
|
||||
}
|
||||
}
|
||||
|
||||
@ -22,13 +22,19 @@ import java.util.List;
|
||||
@Data
|
||||
public class EdgeTypeFilter implements EntityFilter {
|
||||
|
||||
@Override
|
||||
public EntityFilterType getType() {
|
||||
return EntityFilterType.EDGE_TYPE;
|
||||
}
|
||||
/**
|
||||
* Replaced by {@link EdgeTypeFilter#getEdgeTypes()} instead.
|
||||
*/
|
||||
@Deprecated(since = "3.5", forRemoval = true)
|
||||
private String edgeType;
|
||||
|
||||
private List<String> edgeTypes;
|
||||
|
||||
private String edgeNameFilter;
|
||||
|
||||
@Override
|
||||
public EntityFilterType getType() {
|
||||
return EntityFilterType.EDGE_TYPE;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -15,6 +15,8 @@
|
||||
*/
|
||||
package org.thingsboard.server.common.data.query;
|
||||
|
||||
import lombok.Getter;
|
||||
|
||||
public enum EntityFilterType {
|
||||
SINGLE_ENTITY("singleEntity"),
|
||||
ENTITY_LIST("entityList"),
|
||||
@ -31,6 +33,7 @@ public enum EntityFilterType {
|
||||
EDGE_SEARCH_QUERY("edgeSearchQuery"),
|
||||
API_USAGE_STATE("apiUsageState");
|
||||
|
||||
@Getter
|
||||
private final String label;
|
||||
|
||||
EntityFilterType(String label) {
|
||||
|
||||
@ -22,13 +22,19 @@ import java.util.List;
|
||||
@Data
|
||||
public class EntityViewTypeFilter implements EntityFilter {
|
||||
|
||||
@Override
|
||||
public EntityFilterType getType() {
|
||||
return EntityFilterType.ENTITY_VIEW_TYPE;
|
||||
}
|
||||
/**
|
||||
* Replaced by {@link EntityViewTypeFilter#getEntityViewTypes()} instead.
|
||||
*/
|
||||
@Deprecated(since = "3.5", forRemoval = true)
|
||||
private String entityViewType;
|
||||
|
||||
private List<String> entityViewTypes;
|
||||
|
||||
private String entityViewNameFilter;
|
||||
|
||||
@Override
|
||||
public EntityFilterType getType() {
|
||||
return EntityFilterType.ENTITY_VIEW_TYPE;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -53,6 +53,7 @@ import org.thingsboard.server.common.data.query.SingleEntityFilter;
|
||||
import org.thingsboard.server.common.data.relation.EntitySearchDirection;
|
||||
import org.thingsboard.server.common.data.relation.RelationEntityTypeFilter;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
@ -814,18 +815,22 @@ public class DefaultEntityQueryRepository implements EntityQueryRepository {
|
||||
switch (filter.getType()) {
|
||||
case ASSET_TYPE:
|
||||
types = ((AssetTypeFilter) filter).getAssetTypes();
|
||||
types = checkTypeListForBackwardCompatibility(types, ((AssetTypeFilter) filter).getAssetType());
|
||||
name = ((AssetTypeFilter) filter).getAssetNameFilter();
|
||||
break;
|
||||
case DEVICE_TYPE:
|
||||
types = ((DeviceTypeFilter) filter).getDeviceTypes();
|
||||
types = checkTypeListForBackwardCompatibility(types, ((DeviceTypeFilter) filter).getDeviceType());
|
||||
name = ((DeviceTypeFilter) filter).getDeviceNameFilter();
|
||||
break;
|
||||
case ENTITY_VIEW_TYPE:
|
||||
types = ((EntityViewTypeFilter) filter).getEntityViewTypes();
|
||||
types = checkTypeListForBackwardCompatibility(types, ((EntityViewTypeFilter) filter).getEntityViewType());
|
||||
name = ((EntityViewTypeFilter) filter).getEntityViewNameFilter();
|
||||
break;
|
||||
case EDGE_TYPE:
|
||||
types = ((EdgeTypeFilter) filter).getEdgeTypes();
|
||||
types = checkTypeListForBackwardCompatibility(types, ((EdgeTypeFilter) filter).getEdgeType());
|
||||
name = ((EdgeTypeFilter) filter).getEdgeNameFilter();
|
||||
break;
|
||||
default:
|
||||
@ -836,6 +841,14 @@ public class DefaultEntityQueryRepository implements EntityQueryRepository {
|
||||
return "e.type in (:entity_filter_type_query_types) and lower(e.search_text) like lower(concat(:entity_filter_type_query_name, '%%'))";
|
||||
}
|
||||
|
||||
private List<String> checkTypeListForBackwardCompatibility(List<String> types, String subType) {
|
||||
if (types == null || types.isEmpty()) {
|
||||
types = new ArrayList<>();
|
||||
types.add(subType);
|
||||
}
|
||||
return types;
|
||||
}
|
||||
|
||||
public static EntityType resolveEntityType(EntityFilter entityFilter) {
|
||||
switch (entityFilter.getType()) {
|
||||
case SINGLE_ENTITY:
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user