From 9a93a279435d2b3b50cacdbe682f238221e321b1 Mon Sep 17 00:00:00 2001 From: Andrii Landiak Date: Mon, 24 Feb 2025 14:06:04 +0200 Subject: [PATCH] Refactoring after review --- .../common/data/widget/WidgetBundleInfo.java | 39 ++++++++++ .../common/data/widget/WidgetTypeInfo.java | 25 ++----- .../dao/model/sql/WidgetTypeInfoEntity.java | 13 ++-- .../mapping/AbstractEntityInfosConverter.java | 73 ------------------- .../WidgetBundleEntityInfosConverter.java | 29 -------- 5 files changed, 51 insertions(+), 128 deletions(-) create mode 100644 common/data/src/main/java/org/thingsboard/server/common/data/widget/WidgetBundleInfo.java delete mode 100644 dao/src/main/java/org/thingsboard/server/dao/util/mapping/AbstractEntityInfosConverter.java delete mode 100644 dao/src/main/java/org/thingsboard/server/dao/util/mapping/WidgetBundleEntityInfosConverter.java diff --git a/common/data/src/main/java/org/thingsboard/server/common/data/widget/WidgetBundleInfo.java b/common/data/src/main/java/org/thingsboard/server/common/data/widget/WidgetBundleInfo.java new file mode 100644 index 0000000000..f7e43fdad0 --- /dev/null +++ b/common/data/src/main/java/org/thingsboard/server/common/data/widget/WidgetBundleInfo.java @@ -0,0 +1,39 @@ +/** + * Copyright © 2016-2024 The Thingsboard Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.thingsboard.server.common.data.widget; + +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.EqualsAndHashCode; +import lombok.Value; +import org.thingsboard.server.common.data.EntityInfo; +import org.thingsboard.server.common.data.EntityType; +import org.thingsboard.server.common.data.id.EntityIdFactory; + +import java.io.Serial; +import java.util.UUID; + +@Value +@EqualsAndHashCode(callSuper = true) +public class WidgetBundleInfo extends EntityInfo { + + @Serial + private static final long serialVersionUID = 2132305394634509820L; + + public WidgetBundleInfo(@JsonProperty("id") UUID uuid, @JsonProperty("name") String name) { + super(EntityIdFactory.getByTypeAndUuid(EntityType.WIDGETS_BUNDLE, uuid), name); + } + +} diff --git a/common/data/src/main/java/org/thingsboard/server/common/data/widget/WidgetTypeInfo.java b/common/data/src/main/java/org/thingsboard/server/common/data/widget/WidgetTypeInfo.java index 803dcbb945..f7260052a9 100644 --- a/common/data/src/main/java/org/thingsboard/server/common/data/widget/WidgetTypeInfo.java +++ b/common/data/src/main/java/org/thingsboard/server/common/data/widget/WidgetTypeInfo.java @@ -18,7 +18,6 @@ package org.thingsboard.server.common.data.widget; import io.swagger.v3.oas.annotations.media.Schema; import jakarta.validation.Valid; import lombok.Data; -import org.thingsboard.server.common.data.EntityInfo; import org.thingsboard.server.common.data.id.WidgetTypeId; import org.thingsboard.server.common.data.validation.NoXss; @@ -45,7 +44,7 @@ public class WidgetTypeInfo extends BaseWidgetType { private String widgetType; @Valid @Schema(description = "Bundles", accessMode = Schema.AccessMode.READ_ONLY) - private List bundles; + private List bundles; public WidgetTypeInfo() { super(); @@ -60,15 +59,10 @@ public class WidgetTypeInfo extends BaseWidgetType { } public WidgetTypeInfo(WidgetTypeInfo widgetTypeInfo) { - super(widgetTypeInfo); - this.image = widgetTypeInfo.getImage(); - this.description = widgetTypeInfo.getDescription(); - this.tags = widgetTypeInfo.getTags(); - this.widgetType = widgetTypeInfo.getWidgetType(); - this.bundles = Collections.emptyList(); + this(widgetTypeInfo, Collections.emptyList()); } - public WidgetTypeInfo(WidgetTypeInfo widgetTypeInfo, List bundles) { + public WidgetTypeInfo(WidgetTypeInfo widgetTypeInfo, List bundles) { super(widgetTypeInfo); this.image = widgetTypeInfo.getImage(); this.description = widgetTypeInfo.getDescription(); @@ -78,19 +72,10 @@ public class WidgetTypeInfo extends BaseWidgetType { } public WidgetTypeInfo(WidgetTypeDetails widgetTypeDetails) { - super(widgetTypeDetails); - this.image = widgetTypeDetails.getImage(); - this.description = widgetTypeDetails.getDescription(); - this.tags = widgetTypeDetails.getTags(); - if (widgetTypeDetails.getDescriptor() != null && widgetTypeDetails.getDescriptor().has("type")) { - this.widgetType = widgetTypeDetails.getDescriptor().get("type").asText(); - } else { - this.widgetType = ""; - } - this.bundles = Collections.emptyList(); + this(widgetTypeDetails, Collections.emptyList()); } - public WidgetTypeInfo(WidgetTypeDetails widgetTypeDetails, List bundles) { + public WidgetTypeInfo(WidgetTypeDetails widgetTypeDetails, List bundles) { super(widgetTypeDetails); this.image = widgetTypeDetails.getImage(); this.description = widgetTypeDetails.getDescription(); diff --git a/dao/src/main/java/org/thingsboard/server/dao/model/sql/WidgetTypeInfoEntity.java b/dao/src/main/java/org/thingsboard/server/dao/model/sql/WidgetTypeInfoEntity.java index c503632b91..d718e5613d 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/model/sql/WidgetTypeInfoEntity.java +++ b/dao/src/main/java/org/thingsboard/server/dao/model/sql/WidgetTypeInfoEntity.java @@ -15,6 +15,8 @@ */ package org.thingsboard.server.dao.model.sql; +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.JsonNode; import io.hypersistence.utils.hibernate.type.array.StringArrayType; import jakarta.persistence.Column; import jakarta.persistence.Convert; @@ -24,14 +26,13 @@ import lombok.Data; import lombok.EqualsAndHashCode; import org.hibernate.annotations.Immutable; import org.hibernate.annotations.Type; -import org.thingsboard.server.common.data.EntityInfo; +import org.thingsboard.common.util.JacksonUtil; import org.thingsboard.server.common.data.widget.BaseWidgetType; import org.thingsboard.server.common.data.widget.WidgetTypeInfo; import org.thingsboard.server.dao.model.ModelConstants; -import org.thingsboard.server.dao.util.mapping.WidgetBundleEntityInfosConverter; +import org.thingsboard.server.dao.util.mapping.JsonConverter; import java.util.HashMap; -import java.util.List; import java.util.Map; @Data @@ -62,9 +63,9 @@ public class WidgetTypeInfoEntity extends AbstractWidgetTypeEntity bundles; + private JsonNode bundles; public WidgetTypeInfoEntity() { super(); @@ -78,7 +79,7 @@ public class WidgetTypeInfoEntity extends AbstractWidgetTypeEntity() {})); return widgetTypeInfo; } diff --git a/dao/src/main/java/org/thingsboard/server/dao/util/mapping/AbstractEntityInfosConverter.java b/dao/src/main/java/org/thingsboard/server/dao/util/mapping/AbstractEntityInfosConverter.java deleted file mode 100644 index 98b94d17e2..0000000000 --- a/dao/src/main/java/org/thingsboard/server/dao/util/mapping/AbstractEntityInfosConverter.java +++ /dev/null @@ -1,73 +0,0 @@ -/** - * Copyright © 2016-2024 The Thingsboard Authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.thingsboard.server.dao.util.mapping; - -import com.fasterxml.jackson.databind.JsonNode; -import jakarta.persistence.AttributeConverter; -import jakarta.persistence.Converter; -import org.thingsboard.common.util.JacksonUtil; -import org.thingsboard.server.common.data.EntityInfo; -import org.thingsboard.server.common.data.EntityType; - -import java.nio.charset.StandardCharsets; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.UUID; - -@Converter -public abstract class AbstractEntityInfosConverter implements AttributeConverter, String> { - - protected abstract EntityType getEntityType(); - - @Override - public String convertToDatabaseColumn(List attribute) { - throw new IllegalArgumentException("Not implemented!"); - } - - @Override - public List convertToEntityAttribute(String s) { - try { - JsonNode node = JacksonUtil.fromBytes(s.getBytes(StandardCharsets.UTF_8)); - if (node.isArray()) { - List entities = new ArrayList<>(); - for (int i = 0; i < node.size(); i++) { - JsonNode row = node.get(i); - UUID id = null; - String name = null; - JsonNode idNode = row.get("id"); - JsonNode nameNode = row.get("name"); - if (idNode != null && nameNode != null) { - try { - id = UUID.fromString(idNode.asText()); - } catch (Exception ignored) {} - name = nameNode.asText(); - } - if (id != null && name != null) { - entities.add(new EntityInfo(id, getEntityType().name(), name)); - } - } - return entities; - } else { - return Collections.emptyList(); - } - } catch (Exception ex) { - String exception = String.format("Failed to convert String to %s list: %s", getEntityType(), ex.getMessage()); - throw new RuntimeException(exception, ex); - } - } - -} diff --git a/dao/src/main/java/org/thingsboard/server/dao/util/mapping/WidgetBundleEntityInfosConverter.java b/dao/src/main/java/org/thingsboard/server/dao/util/mapping/WidgetBundleEntityInfosConverter.java deleted file mode 100644 index 143de09818..0000000000 --- a/dao/src/main/java/org/thingsboard/server/dao/util/mapping/WidgetBundleEntityInfosConverter.java +++ /dev/null @@ -1,29 +0,0 @@ -/** - * Copyright © 2016-2024 The Thingsboard Authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.thingsboard.server.dao.util.mapping; - -import jakarta.persistence.Converter; -import org.thingsboard.server.common.data.EntityType; - -@Converter -public class WidgetBundleEntityInfosConverter extends AbstractEntityInfosConverter { - - @Override - protected EntityType getEntityType() { - return EntityType.WIDGETS_BUNDLE; - } - -}