Browse Source

Refactoring after review

pull/12688/head
Andrii Landiak 2 years ago
parent
commit
9a93a27943
  1. 39
      common/data/src/main/java/org/thingsboard/server/common/data/widget/WidgetBundleInfo.java
  2. 25
      common/data/src/main/java/org/thingsboard/server/common/data/widget/WidgetTypeInfo.java
  3. 13
      dao/src/main/java/org/thingsboard/server/dao/model/sql/WidgetTypeInfoEntity.java
  4. 73
      dao/src/main/java/org/thingsboard/server/dao/util/mapping/AbstractEntityInfosConverter.java
  5. 29
      dao/src/main/java/org/thingsboard/server/dao/util/mapping/WidgetBundleEntityInfosConverter.java

39
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);
}
}

25
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 io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.Valid; import jakarta.validation.Valid;
import lombok.Data; import lombok.Data;
import org.thingsboard.server.common.data.EntityInfo;
import org.thingsboard.server.common.data.id.WidgetTypeId; import org.thingsboard.server.common.data.id.WidgetTypeId;
import org.thingsboard.server.common.data.validation.NoXss; import org.thingsboard.server.common.data.validation.NoXss;
@ -45,7 +44,7 @@ public class WidgetTypeInfo extends BaseWidgetType {
private String widgetType; private String widgetType;
@Valid @Valid
@Schema(description = "Bundles", accessMode = Schema.AccessMode.READ_ONLY) @Schema(description = "Bundles", accessMode = Schema.AccessMode.READ_ONLY)
private List<EntityInfo> bundles; private List<WidgetBundleInfo> bundles;
public WidgetTypeInfo() { public WidgetTypeInfo() {
super(); super();
@ -60,15 +59,10 @@ public class WidgetTypeInfo extends BaseWidgetType {
} }
public WidgetTypeInfo(WidgetTypeInfo widgetTypeInfo) { public WidgetTypeInfo(WidgetTypeInfo widgetTypeInfo) {
super(widgetTypeInfo); this(widgetTypeInfo, Collections.emptyList());
this.image = widgetTypeInfo.getImage();
this.description = widgetTypeInfo.getDescription();
this.tags = widgetTypeInfo.getTags();
this.widgetType = widgetTypeInfo.getWidgetType();
this.bundles = Collections.emptyList();
} }
public WidgetTypeInfo(WidgetTypeInfo widgetTypeInfo, List<EntityInfo> bundles) { public WidgetTypeInfo(WidgetTypeInfo widgetTypeInfo, List<WidgetBundleInfo> bundles) {
super(widgetTypeInfo); super(widgetTypeInfo);
this.image = widgetTypeInfo.getImage(); this.image = widgetTypeInfo.getImage();
this.description = widgetTypeInfo.getDescription(); this.description = widgetTypeInfo.getDescription();
@ -78,19 +72,10 @@ public class WidgetTypeInfo extends BaseWidgetType {
} }
public WidgetTypeInfo(WidgetTypeDetails widgetTypeDetails) { public WidgetTypeInfo(WidgetTypeDetails widgetTypeDetails) {
super(widgetTypeDetails); this(widgetTypeDetails, Collections.emptyList());
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();
} }
public WidgetTypeInfo(WidgetTypeDetails widgetTypeDetails, List<EntityInfo> bundles) { public WidgetTypeInfo(WidgetTypeDetails widgetTypeDetails, List<WidgetBundleInfo> bundles) {
super(widgetTypeDetails); super(widgetTypeDetails);
this.image = widgetTypeDetails.getImage(); this.image = widgetTypeDetails.getImage();
this.description = widgetTypeDetails.getDescription(); this.description = widgetTypeDetails.getDescription();

13
dao/src/main/java/org/thingsboard/server/dao/model/sql/WidgetTypeInfoEntity.java

@ -15,6 +15,8 @@
*/ */
package org.thingsboard.server.dao.model.sql; 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 io.hypersistence.utils.hibernate.type.array.StringArrayType;
import jakarta.persistence.Column; import jakarta.persistence.Column;
import jakarta.persistence.Convert; import jakarta.persistence.Convert;
@ -24,14 +26,13 @@ import lombok.Data;
import lombok.EqualsAndHashCode; import lombok.EqualsAndHashCode;
import org.hibernate.annotations.Immutable; import org.hibernate.annotations.Immutable;
import org.hibernate.annotations.Type; 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.BaseWidgetType;
import org.thingsboard.server.common.data.widget.WidgetTypeInfo; import org.thingsboard.server.common.data.widget.WidgetTypeInfo;
import org.thingsboard.server.dao.model.ModelConstants; 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.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
@Data @Data
@ -62,9 +63,9 @@ public class WidgetTypeInfoEntity extends AbstractWidgetTypeEntity<WidgetTypeInf
@Column(name = ModelConstants.WIDGET_TYPE_WIDGET_TYPE_PROPERTY) @Column(name = ModelConstants.WIDGET_TYPE_WIDGET_TYPE_PROPERTY)
private String widgetType; private String widgetType;
@Convert(converter = WidgetBundleEntityInfosConverter.class) @Convert(converter = JsonConverter.class)
@Column(name = ModelConstants.WIDGET_BUNDLES_PROPERTY) @Column(name = ModelConstants.WIDGET_BUNDLES_PROPERTY)
private List<EntityInfo> bundles; private JsonNode bundles;
public WidgetTypeInfoEntity() { public WidgetTypeInfoEntity() {
super(); super();
@ -78,7 +79,7 @@ public class WidgetTypeInfoEntity extends AbstractWidgetTypeEntity<WidgetTypeInf
widgetTypeInfo.setDescription(description); widgetTypeInfo.setDescription(description);
widgetTypeInfo.setTags(tags); widgetTypeInfo.setTags(tags);
widgetTypeInfo.setWidgetType(widgetType); widgetTypeInfo.setWidgetType(widgetType);
widgetTypeInfo.setBundles(bundles); widgetTypeInfo.setBundles(JacksonUtil.convertValue(bundles, new TypeReference<>() {}));
return widgetTypeInfo; return widgetTypeInfo;
} }

73
dao/src/main/java/org/thingsboard/server/dao/util/mapping/AbstractEntityInfosConverter.java

@ -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<List<EntityInfo>, String> {
protected abstract EntityType getEntityType();
@Override
public String convertToDatabaseColumn(List<EntityInfo> attribute) {
throw new IllegalArgumentException("Not implemented!");
}
@Override
public List<EntityInfo> convertToEntityAttribute(String s) {
try {
JsonNode node = JacksonUtil.fromBytes(s.getBytes(StandardCharsets.UTF_8));
if (node.isArray()) {
List<EntityInfo> 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);
}
}
}

29
dao/src/main/java/org/thingsboard/server/dao/util/mapping/WidgetBundleEntityInfosConverter.java

@ -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;
}
}
Loading…
Cancel
Save