Improved method that replaces uuids
This commit is contained in:
parent
7a4fec93cd
commit
5cad77910f
@ -15,6 +15,8 @@
|
||||
*/
|
||||
package org.thingsboard.server.service.sync.ie.exporting.impl;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import org.thingsboard.common.util.JacksonUtil;
|
||||
import org.thingsboard.server.common.data.EntityType;
|
||||
import org.thingsboard.server.common.data.ExportableEntity;
|
||||
import org.thingsboard.server.common.data.exception.ThingsboardException;
|
||||
@ -32,12 +34,19 @@ public abstract class BaseEntityExportService<I extends EntityId, E extends Expo
|
||||
super.setAdditionalExportData(ctx, entity, exportData);
|
||||
}
|
||||
|
||||
protected void setRelatedEntities(EntitiesExportCtx<?> ctx, E mainEntity, D exportData) {}
|
||||
protected void setRelatedEntities(EntitiesExportCtx<?> ctx, E mainEntity, D exportData) {
|
||||
}
|
||||
|
||||
protected D newExportData() {
|
||||
return (D) new EntityExportData<E>();
|
||||
};
|
||||
}
|
||||
|
||||
;
|
||||
|
||||
public abstract Set<EntityType> getSupportedEntityTypes();
|
||||
|
||||
protected void replaceUuidsRecursively(EntitiesExportCtx<?> ctx, JsonNode node, Set<String> skipFieldsSet) {
|
||||
JacksonUtil.replaceUuidsRecursively(node, skipFieldsSet, uuid -> getExternalIdOrElseInternalByUuid(ctx, uuid));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -27,9 +27,10 @@ import org.thingsboard.server.common.data.id.DashboardId;
|
||||
import org.thingsboard.server.common.data.sync.ie.EntityExportData;
|
||||
import org.thingsboard.server.queue.util.TbCoreComponent;
|
||||
import org.thingsboard.server.service.sync.vc.data.EntitiesExportCtx;
|
||||
import org.thingsboard.server.utils.RegexUtils;
|
||||
import org.thingsboard.common.util.RegexUtils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
|
||||
@ -46,17 +47,12 @@ public class DashboardExportService extends BaseEntityExportService<DashboardId,
|
||||
}
|
||||
if (dashboard.getEntityAliasesConfig() != null) {
|
||||
for (JsonNode entityAlias : dashboard.getEntityAliasesConfig()) {
|
||||
ArrayList<String> fields = Lists.newArrayList(entityAlias.fieldNames());
|
||||
for (String field : fields) {
|
||||
if (field.equals("id")) continue;
|
||||
JsonNode oldFieldValue = entityAlias.get(field);
|
||||
JsonNode newFieldValue = JacksonUtil.toJsonNode(RegexUtils.replace(oldFieldValue.toString(), RegexUtils.UUID_PATTERN, uuid -> {
|
||||
return getExternalIdOrElseInternalByUuid(ctx, UUID.fromString(uuid)).toString();
|
||||
}));
|
||||
((ObjectNode) entityAlias).set(field, newFieldValue);
|
||||
}
|
||||
replaceUuidsRecursively(ctx, entityAlias, Collections.singleton("id"));
|
||||
}
|
||||
}
|
||||
for (JsonNode widgetConfig : dashboard.getWidgetsConfig()) {
|
||||
replaceUuidsRecursively(ctx, JacksonUtil.getSafely(widgetConfig, "config", "actions"), Collections.singleton("id"));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@ -27,7 +27,7 @@ import org.thingsboard.server.common.data.sync.ie.RuleChainExportData;
|
||||
import org.thingsboard.server.dao.rule.RuleChainService;
|
||||
import org.thingsboard.server.queue.util.TbCoreComponent;
|
||||
import org.thingsboard.server.service.sync.vc.data.EntitiesExportCtx;
|
||||
import org.thingsboard.server.utils.RegexUtils;
|
||||
import org.thingsboard.common.util.RegexUtils;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Optional;
|
||||
@ -52,7 +52,7 @@ public class RuleChainExportService extends BaseEntityExportService<RuleChainId,
|
||||
ruleNode.setCreatedTime(0);
|
||||
ruleNode.setExternalId(null);
|
||||
JsonNode ruleNodeConfig = ruleNode.getConfiguration();
|
||||
String newRuleNodeConfigJson = RegexUtils.replace(ruleNodeConfig.toString(), RegexUtils.UUID_PATTERN, uuid -> {
|
||||
String newRuleNodeConfigJson = RegexUtils.replace(JacksonUtil.toString(ruleNodeConfig), RegexUtils.UUID_PATTERN, uuid -> {
|
||||
return getExternalIdOrElseInternalByUuid(ctx, UUID.fromString(uuid)).toString();
|
||||
});
|
||||
ruleNodeConfig = JacksonUtil.toJsonNode(newRuleNodeConfigJson);
|
||||
|
||||
@ -34,7 +34,7 @@ import org.thingsboard.server.dao.dashboard.DashboardService;
|
||||
import org.thingsboard.server.queue.util.TbCoreComponent;
|
||||
import org.thingsboard.server.service.security.model.SecurityUser;
|
||||
import org.thingsboard.server.service.sync.vc.data.EntitiesImportCtx;
|
||||
import org.thingsboard.server.utils.RegexUtils;
|
||||
import org.thingsboard.common.util.RegexUtils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
|
||||
@ -37,7 +37,7 @@ import org.thingsboard.server.dao.rule.RuleNodeDao;
|
||||
import org.thingsboard.server.queue.util.TbCoreComponent;
|
||||
import org.thingsboard.server.service.security.model.SecurityUser;
|
||||
import org.thingsboard.server.service.sync.vc.data.EntitiesImportCtx;
|
||||
import org.thingsboard.server.utils.RegexUtils;
|
||||
import org.thingsboard.common.util.RegexUtils;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
@ -95,7 +95,7 @@ public class RuleChainImportService extends BaseEntityImportService<RuleChainId,
|
||||
|
||||
ruleNodes.forEach(ruleNode -> {
|
||||
JsonNode ruleNodeConfig = ruleNode.getConfiguration();
|
||||
String newRuleNodeConfigJson = RegexUtils.replace(ruleNodeConfig.toString(), RegexUtils.UUID_PATTERN, uuid -> {
|
||||
String newRuleNodeConfigJson = RegexUtils.replace(JacksonUtil.toString(ruleNodeConfig), RegexUtils.UUID_PATTERN, uuid -> {
|
||||
return idProvider.getInternalIdByUuid(UUID.fromString(uuid), ctx.isFinalImportAttempt(), HINTS)
|
||||
.map(entityId -> entityId.getId().toString())
|
||||
.orElse(uuid);
|
||||
|
||||
@ -17,6 +17,7 @@ package org.thingsboard.server.common.data;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.node.ArrayNode;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.EqualsAndHashCode;
|
||||
@ -24,7 +25,11 @@ import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.thingsboard.server.common.data.id.DashboardId;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.StreamSupport;
|
||||
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
public class Dashboard extends DashboardInfo implements ExportableEntity<DashboardId> {
|
||||
@ -33,7 +38,8 @@ public class Dashboard extends DashboardInfo implements ExportableEntity<Dashboa
|
||||
|
||||
private transient JsonNode configuration;
|
||||
|
||||
@Getter @Setter
|
||||
@Getter
|
||||
@Setter
|
||||
private DashboardId externalId;
|
||||
|
||||
public Dashboard() {
|
||||
@ -68,11 +74,29 @@ public class Dashboard extends DashboardInfo implements ExportableEntity<Dashboa
|
||||
|
||||
@JsonIgnore
|
||||
public ObjectNode getEntityAliasesConfig() {
|
||||
return (ObjectNode) Optional.ofNullable(getConfiguration())
|
||||
return (ObjectNode) Optional.ofNullable(configuration)
|
||||
.map(config -> config.get("entityAliases"))
|
||||
.filter(JsonNode::isObject).orElse(null);
|
||||
}
|
||||
|
||||
@JsonIgnore
|
||||
public List<ObjectNode> getWidgetsConfig() {
|
||||
return Optional.ofNullable(configuration)
|
||||
.map(config -> config.get("widgets"))
|
||||
.filter(node -> !node.isEmpty())
|
||||
.map(node -> (ObjectNode) node)
|
||||
.map(object -> {
|
||||
List<ObjectNode> widgets = new ArrayList<>(object.size());
|
||||
object.forEach(child -> {
|
||||
if (child.isObject()) {
|
||||
widgets.add((ObjectNode) child);
|
||||
}
|
||||
});
|
||||
return widgets;
|
||||
})
|
||||
.orElse(Collections.emptyList());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
|
||||
@ -22,10 +22,16 @@ import com.fasterxml.jackson.databind.MapperFeature;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.SerializationFeature;
|
||||
import com.fasterxml.jackson.databind.json.JsonMapper;
|
||||
import com.fasterxml.jackson.databind.node.ArrayNode;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import java.util.function.UnaryOperator;
|
||||
|
||||
/**
|
||||
* Created by Valerii Sosliuk on 5/12/2017.
|
||||
@ -151,4 +157,59 @@ public class JacksonUtil {
|
||||
+ value + " cannot be transformed to a String", e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static JsonNode getSafely(JsonNode node, String... path) {
|
||||
if (node == null) {
|
||||
return null;
|
||||
}
|
||||
for (String p : path) {
|
||||
if (!node.has(p)) {
|
||||
return null;
|
||||
} else {
|
||||
node = node.get(p);
|
||||
}
|
||||
}
|
||||
return node;
|
||||
}
|
||||
|
||||
public static void replaceUuidsRecursively(JsonNode node, Set<String> skipFieldsSet, UnaryOperator<UUID> replacer) {
|
||||
if (node == null) {
|
||||
return;
|
||||
}
|
||||
if (node.isObject()) {
|
||||
ObjectNode objectNode = (ObjectNode) node;
|
||||
List<String> fieldNames = new ArrayList<>(objectNode.size());
|
||||
objectNode.fieldNames().forEachRemaining(fieldNames::add);
|
||||
for (String fieldName : fieldNames) {
|
||||
if (skipFieldsSet.contains(fieldName)) {
|
||||
continue;
|
||||
}
|
||||
var child = objectNode.get(fieldName);
|
||||
if (child.isObject() || child.isArray()) {
|
||||
replaceUuidsRecursively(child, skipFieldsSet, replacer);
|
||||
} else if (child.isTextual()) {
|
||||
String text = child.asText();
|
||||
String newText = RegexUtils.replace(text, RegexUtils.UUID_PATTERN, uuid -> replacer.apply(UUID.fromString(uuid)).toString());
|
||||
if (!text.equals(newText)) {
|
||||
objectNode.put(fieldName, newText);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if (node.isArray()) {
|
||||
ArrayNode array = (ArrayNode) node;
|
||||
for (int i = 0; i < array.size(); i++) {
|
||||
JsonNode arrayElement = array.get(i);
|
||||
if (arrayElement.isObject() || arrayElement.isArray()) {
|
||||
replaceUuidsRecursively(arrayElement, skipFieldsSet, replacer);
|
||||
} else if (arrayElement.isTextual()) {
|
||||
String text = arrayElement.asText();
|
||||
String newText = RegexUtils.replace(text, RegexUtils.UUID_PATTERN, uuid -> replacer.apply(UUID.fromString(uuid)).toString());
|
||||
if (!text.equals(newText)) {
|
||||
array.set(i, newText);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -13,7 +13,7 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.thingsboard.server.utils;
|
||||
package org.thingsboard.common.util;
|
||||
|
||||
import lombok.AccessLevel;
|
||||
import lombok.NoArgsConstructor;
|
||||
Loading…
x
Reference in New Issue
Block a user