Commit 600da893858415fb0cb80111629126d845e0e0f7

Authored by Andrew Shvayka
1 parent dcba4f6b

Refactoring of Attribute keys

... ... @@ -20,14 +20,29 @@ import lombok.Data;
20 20 import lombok.NoArgsConstructor;
21 21 import org.thingsboard.server.common.data.EntityType;
22 22
  23 +import javax.persistence.Column;
  24 +import javax.persistence.Embeddable;
  25 +import javax.persistence.EnumType;
  26 +import javax.persistence.Enumerated;
23 27 import java.io.Serializable;
24 28
  29 +import static org.thingsboard.server.dao.model.ModelConstants.ATTRIBUTE_KEY_COLUMN;
  30 +import static org.thingsboard.server.dao.model.ModelConstants.ATTRIBUTE_TYPE_COLUMN;
  31 +import static org.thingsboard.server.dao.model.ModelConstants.ENTITY_ID_COLUMN;
  32 +import static org.thingsboard.server.dao.model.ModelConstants.ENTITY_TYPE_COLUMN;
  33 +
25 34 @Data
26 35 @AllArgsConstructor
27 36 @NoArgsConstructor
  37 +@Embeddable
28 38 public class AttributeKvCompositeKey implements Serializable {
  39 + @Enumerated(EnumType.STRING)
  40 + @Column(name = ENTITY_TYPE_COLUMN)
29 41 private EntityType entityType;
  42 + @Column(name = ENTITY_ID_COLUMN)
30 43 private String entityId;
  44 + @Column(name = ATTRIBUTE_TYPE_COLUMN)
31 45 private String attributeType;
  46 + @Column(name = ATTRIBUTE_KEY_COLUMN)
32 47 private String attributeKey;
33 48 }
... ...
... ... @@ -27,6 +27,7 @@ import org.thingsboard.server.common.data.kv.StringDataEntry;
27 27 import org.thingsboard.server.dao.model.ToData;
28 28
29 29 import javax.persistence.Column;
  30 +import javax.persistence.EmbeddedId;
30 31 import javax.persistence.Entity;
31 32 import javax.persistence.EnumType;
32 33 import javax.persistence.Enumerated;
... ... @@ -48,25 +49,10 @@ import static org.thingsboard.server.dao.model.ModelConstants.STRING_VALUE_COLUM
48 49 @Data
49 50 @Entity
50 51 @Table(name = "attribute_kv")
51   -@IdClass(AttributeKvCompositeKey.class)
52 52 public class AttributeKvEntity implements ToData<AttributeKvEntry>, Serializable {
53 53
54   - @Id
55   - @Enumerated(EnumType.STRING)
56   - @Column(name = ENTITY_TYPE_COLUMN)
57   - private EntityType entityType;
58   -
59   - @Id
60   - @Column(name = ENTITY_ID_COLUMN)
61   - private String entityId;
62   -
63   - @Id
64   - @Column(name = ATTRIBUTE_TYPE_COLUMN)
65   - private String attributeType;
66   -
67   - @Id
68   - @Column(name = ATTRIBUTE_KEY_COLUMN)
69   - private String attributeKey;
  54 + @EmbeddedId
  55 + private AttributeKvCompositeKey id;
70 56
71 57 @Column(name = BOOLEAN_VALUE_COLUMN)
72 58 private Boolean booleanValue;
... ... @@ -87,13 +73,13 @@ public class AttributeKvEntity implements ToData<AttributeKvEntry>, Serializable
87 73 public AttributeKvEntry toData() {
88 74 KvEntry kvEntry = null;
89 75 if (strValue != null) {
90   - kvEntry = new StringDataEntry(attributeKey, strValue);
  76 + kvEntry = new StringDataEntry(id.getAttributeKey(), strValue);
91 77 } else if (booleanValue != null) {
92   - kvEntry = new BooleanDataEntry(attributeKey, booleanValue);
  78 + kvEntry = new BooleanDataEntry(id.getAttributeKey(), booleanValue);
93 79 } else if (doubleValue != null) {
94   - kvEntry = new DoubleDataEntry(attributeKey, doubleValue);
  80 + kvEntry = new DoubleDataEntry(id.getAttributeKey(), doubleValue);
95 81 } else if (longValue != null) {
96   - kvEntry = new LongDataEntry(attributeKey, longValue);
  82 + kvEntry = new LongDataEntry(id.getAttributeKey(), longValue);
97 83 }
98 84 return new BaseAttributeKvEntry(kvEntry, lastUpdateTs);
99 85 }
... ...
... ... @@ -15,7 +15,9 @@
15 15 */
16 16 package org.thingsboard.server.dao.sql.attributes;
17 17
  18 +import org.springframework.data.jpa.repository.Query;
18 19 import org.springframework.data.repository.CrudRepository;
  20 +import org.springframework.data.repository.query.Param;
19 21 import org.thingsboard.server.common.data.EntityType;
20 22 import org.thingsboard.server.dao.model.sql.AttributeKvCompositeKey;
21 23 import org.thingsboard.server.dao.model.sql.AttributeKvEntity;
... ... @@ -26,8 +28,11 @@ import java.util.List;
26 28 @SqlDao
27 29 public interface AttributeKvRepository extends CrudRepository<AttributeKvEntity, AttributeKvCompositeKey> {
28 30
29   - List<AttributeKvEntity> findAllByEntityTypeAndEntityIdAndAttributeType(EntityType entityType,
30   - String entityId,
31   - String attributeType);
  31 + @Query("SELECT a FROM AttributeKvEntity a WHERE a.id.entityType = :entityType " +
  32 + "AND a.id.entityId = :entityId " +
  33 + "AND a.id.attributeType = :attributeType")
  34 + List<AttributeKvEntity> findAllByEntityTypeAndEntityIdAndAttributeType(@Param("entityType") EntityType entityType,
  35 + @Param("entityId") String entityId,
  36 + @Param("attributeType") String attributeType);
32 37 }
33 38
... ...
... ... @@ -79,10 +79,7 @@ public class JpaAttributeDao extends JpaAbstractDaoListeningExecutorService impl
79 79 @Override
80 80 public ListenableFuture<Void> save(EntityId entityId, String attributeType, AttributeKvEntry attribute) {
81 81 AttributeKvEntity entity = new AttributeKvEntity();
82   - entity.setEntityType(entityId.getEntityType());
83   - entity.setEntityId(fromTimeUUID(entityId.getId()));
84   - entity.setAttributeType(attributeType);
85   - entity.setAttributeKey(attribute.getKey());
  82 + entity.setId(new AttributeKvCompositeKey(entityId.getEntityType(), fromTimeUUID(entityId.getId()), attributeType, attribute.getKey()));
86 83 entity.setLastUpdateTs(attribute.getLastUpdateTs());
87 84 entity.setStrValue(attribute.getStrValue().orElse(null));
88 85 entity.setDoubleValue(attribute.getDoubleValue().orElse(null));
... ... @@ -100,10 +97,7 @@ public class JpaAttributeDao extends JpaAbstractDaoListeningExecutorService impl
100 97 .stream()
101 98 .map(key -> {
102 99 AttributeKvEntity entityToDelete = new AttributeKvEntity();
103   - entityToDelete.setEntityType(entityId.getEntityType());
104   - entityToDelete.setEntityId(fromTimeUUID(entityId.getId()));
105   - entityToDelete.setAttributeType(attributeType);
106   - entityToDelete.setAttributeKey(key);
  100 + entityToDelete.setId(new AttributeKvCompositeKey(entityId.getEntityType(), fromTimeUUID(entityId.getId()), attributeType, key));
107 101 return entityToDelete;
108 102 }).collect(Collectors.toList());
109 103
... ...