diff --git a/common/dao-api/src/main/java/org/thingsboard/server/dao/attributes/AttributesService.java b/common/dao-api/src/main/java/org/thingsboard/server/dao/attributes/AttributesService.java index cb7cbd09c6..75122aba57 100644 --- a/common/dao-api/src/main/java/org/thingsboard/server/dao/attributes/AttributesService.java +++ b/common/dao-api/src/main/java/org/thingsboard/server/dao/attributes/AttributesService.java @@ -19,6 +19,7 @@ import com.google.common.util.concurrent.ListenableFuture; import org.thingsboard.server.common.data.id.EntityId; import org.thingsboard.server.common.data.id.TenantId; import org.thingsboard.server.common.data.kv.AttributeKvEntry; +import org.thingsboard.server.common.data.kv.EntityAttributeKvEntry; import java.util.Collection; import java.util.List; @@ -35,7 +36,7 @@ public interface AttributesService { ListenableFuture> findAll(TenantId tenantId, EntityId entityId, String scope); - ListenableFuture> findAllByAttributeKey(String attributeKey); + ListenableFuture> findAllByAttributeKey(String attributeKey); ListenableFuture> save(TenantId tenantId, EntityId entityId, String scope, List attributes); diff --git a/common/data/src/main/java/org/thingsboard/server/common/data/kv/BaseEntityAttributeKvEntry.java b/common/data/src/main/java/org/thingsboard/server/common/data/kv/BaseEntityAttributeKvEntry.java new file mode 100644 index 0000000000..aac91f5613 --- /dev/null +++ b/common/data/src/main/java/org/thingsboard/server/common/data/kv/BaseEntityAttributeKvEntry.java @@ -0,0 +1,44 @@ +/** + * Copyright © 2016-2020 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.kv; + +import lombok.EqualsAndHashCode; +import lombok.ToString; +import org.thingsboard.server.common.data.EntityType; + +@EqualsAndHashCode(callSuper = true) +@ToString(callSuper = true) +public class BaseEntityAttributeKvEntry extends BaseAttributeKvEntry implements EntityAttributeKvEntry { + + private final EntityType entityType; + private final String entityId; + + public BaseEntityAttributeKvEntry(EntityType entityType, String entityId, long lastUpdateTs, KvEntry kv) { + super(kv, lastUpdateTs); + this.entityType = entityType; + this.entityId = entityId; + } + + @Override + public EntityType getEntityType() { + return entityType; + } + + @Override + public String getEntityId() { + return entityId; + } +} diff --git a/common/data/src/main/java/org/thingsboard/server/common/data/kv/EntityAttributeKvEntry.java b/common/data/src/main/java/org/thingsboard/server/common/data/kv/EntityAttributeKvEntry.java new file mode 100644 index 0000000000..b62576b8e0 --- /dev/null +++ b/common/data/src/main/java/org/thingsboard/server/common/data/kv/EntityAttributeKvEntry.java @@ -0,0 +1,24 @@ +/** + * Copyright © 2016-2020 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.kv; + +import org.thingsboard.server.common.data.EntityType; + +public interface EntityAttributeKvEntry extends AttributeKvEntry { + + EntityType getEntityType(); + String getEntityId(); +} diff --git a/dao/src/main/java/org/thingsboard/server/dao/attributes/AttributesDao.java b/dao/src/main/java/org/thingsboard/server/dao/attributes/AttributesDao.java index 6101a70b54..85f6b2de44 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/attributes/AttributesDao.java +++ b/dao/src/main/java/org/thingsboard/server/dao/attributes/AttributesDao.java @@ -19,6 +19,7 @@ import com.google.common.util.concurrent.ListenableFuture; import org.thingsboard.server.common.data.id.EntityId; import org.thingsboard.server.common.data.id.TenantId; import org.thingsboard.server.common.data.kv.AttributeKvEntry; +import org.thingsboard.server.common.data.kv.EntityAttributeKvEntry; import java.util.Collection; import java.util.List; @@ -35,7 +36,7 @@ public interface AttributesDao { ListenableFuture> findAll(TenantId tenantId, EntityId entityId, String attributeType); - ListenableFuture> findAllByAttributeKey(String attributeKey); + ListenableFuture> findAllByAttributeKey(String attributeKey); ListenableFuture save(TenantId tenantId, EntityId entityId, String attributeType, AttributeKvEntry attribute); diff --git a/dao/src/main/java/org/thingsboard/server/dao/attributes/BaseAttributesService.java b/dao/src/main/java/org/thingsboard/server/dao/attributes/BaseAttributesService.java index fbb44ac8c6..9956da347d 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/attributes/BaseAttributesService.java +++ b/dao/src/main/java/org/thingsboard/server/dao/attributes/BaseAttributesService.java @@ -23,6 +23,7 @@ import org.springframework.stereotype.Service; import org.thingsboard.server.common.data.id.EntityId; import org.thingsboard.server.common.data.id.TenantId; import org.thingsboard.server.common.data.kv.AttributeKvEntry; +import org.thingsboard.server.common.data.kv.EntityAttributeKvEntry; import org.thingsboard.server.dao.exception.IncorrectParameterException; import org.thingsboard.server.dao.service.Validator; @@ -60,7 +61,7 @@ public class BaseAttributesService implements AttributesService { } @Override - public ListenableFuture> findAllByAttributeKey(String attributeKey) { + public ListenableFuture> findAllByAttributeKey(String attributeKey) { Validator.validateString(attributeKey, "Incorrect attribute key " + attributeKey); return attributesDao.findAllByAttributeKey(attributeKey); } diff --git a/dao/src/main/java/org/thingsboard/server/dao/model/sql/AttributeKvEntity.java b/dao/src/main/java/org/thingsboard/server/dao/model/sql/AttributeKvEntity.java index f0de269cea..7787f21830 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/model/sql/AttributeKvEntity.java +++ b/dao/src/main/java/org/thingsboard/server/dao/model/sql/AttributeKvEntity.java @@ -16,20 +16,10 @@ package org.thingsboard.server.dao.model.sql; import lombok.Data; -import org.thingsboard.server.common.data.kv.AttributeKvEntry; -import org.thingsboard.server.common.data.kv.BaseAttributeKvEntry; -import org.thingsboard.server.common.data.kv.BooleanDataEntry; -import org.thingsboard.server.common.data.kv.DoubleDataEntry; -import org.thingsboard.server.common.data.kv.JsonDataEntry; -import org.thingsboard.server.common.data.kv.KvEntry; -import org.thingsboard.server.common.data.kv.LongDataEntry; -import org.thingsboard.server.common.data.kv.StringDataEntry; +import org.thingsboard.server.common.data.kv.*; import org.thingsboard.server.dao.model.ToData; -import javax.persistence.Column; -import javax.persistence.EmbeddedId; -import javax.persistence.Entity; -import javax.persistence.Table; +import javax.persistence.*; import java.io.Serializable; import static org.thingsboard.server.dao.model.ModelConstants.BOOLEAN_VALUE_COLUMN; @@ -42,6 +32,7 @@ import static org.thingsboard.server.dao.model.ModelConstants.STRING_VALUE_COLUM @Data @Entity @Table(name = "attribute_kv") +// TODO maybe move ToData to local field as well (or implement ToData differently) public class AttributeKvEntity implements ToData, Serializable { @EmbeddedId @@ -82,4 +73,22 @@ public class AttributeKvEntity implements ToData, Serializable return new BaseAttributeKvEntry(kvEntry, lastUpdateTs); } + + @Transient + public final ToData toEntityAttributeKvEntry = () -> { + KvEntry kvEntry = null; + if (strValue != null) { + kvEntry = new StringDataEntry(id.getAttributeKey(), strValue); + } else if (booleanValue != null) { + kvEntry = new BooleanDataEntry(id.getAttributeKey(), booleanValue); + } else if (doubleValue != null) { + kvEntry = new DoubleDataEntry(id.getAttributeKey(), doubleValue); + } else if (longValue != null) { + kvEntry = new LongDataEntry(id.getAttributeKey(), longValue); + } else if (jsonValue != null) { + kvEntry = new JsonDataEntry(id.getAttributeKey(), jsonValue); + } + + return new BaseEntityAttributeKvEntry(id.getEntityType(), id.getEntityId(), lastUpdateTs, kvEntry); + }; } diff --git a/dao/src/main/java/org/thingsboard/server/dao/sql/attributes/AttributeKvRepository.java b/dao/src/main/java/org/thingsboard/server/dao/sql/attributes/AttributeKvRepository.java index 37b30607ab..86616d6d8f 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/sql/attributes/AttributeKvRepository.java +++ b/dao/src/main/java/org/thingsboard/server/dao/sql/attributes/AttributeKvRepository.java @@ -37,6 +37,10 @@ public interface AttributeKvRepository extends CrudRepository findAllByAttributeKey(@Param("attributeKey") String attributeKey); + @Transactional @Modifying @Query("DELETE FROM AttributeKvEntity a WHERE a.id.entityType = :entityType " + @@ -47,7 +51,5 @@ public interface AttributeKvRepository extends CrudRepository findAllByAttributeKey(String attributeKey); } diff --git a/dao/src/main/java/org/thingsboard/server/dao/sql/attributes/JpaAttributeDao.java b/dao/src/main/java/org/thingsboard/server/dao/sql/attributes/JpaAttributeDao.java index 420ab0a42d..80119f23bf 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/sql/attributes/JpaAttributeDao.java +++ b/dao/src/main/java/org/thingsboard/server/dao/sql/attributes/JpaAttributeDao.java @@ -25,9 +25,10 @@ import org.springframework.stereotype.Component; import org.thingsboard.server.common.data.UUIDConverter; import org.thingsboard.server.common.data.id.EntityId; import org.thingsboard.server.common.data.id.TenantId; -import org.thingsboard.server.common.data.kv.AttributeKvEntry; +import org.thingsboard.server.common.data.kv.*; import org.thingsboard.server.dao.DaoUtil; import org.thingsboard.server.dao.attributes.AttributesDao; +import org.thingsboard.server.dao.model.ToData; import org.thingsboard.server.dao.model.sql.AttributeKvCompositeKey; import org.thingsboard.server.dao.model.sql.AttributeKvEntity; import org.thingsboard.server.dao.sql.JpaAbstractDaoListeningExecutorService; @@ -120,9 +121,15 @@ public class JpaAttributeDao extends JpaAbstractDaoListeningExecutorService impl } @Override - public ListenableFuture> findAllByAttributeKey(String attributeKey) { + public ListenableFuture> findAllByAttributeKey(String attributeKey) { return Futures.immediateFuture( - DaoUtil.convertDataList(attributeKvRepository.findAllByAttributeKey(attributeKey))); + DaoUtil.convertDataList( + attributeKvRepository.findAllByAttributeKey(attributeKey).stream() + .map(attributeKvEntity -> attributeKvEntity.toEntityAttributeKvEntry) + .collect(Collectors.toList()) + ) + + ); } @Override