Browse Source

Updated loading AttributeKvEntity by attribute_key

pull/3557/head
vzikratyi 6 years ago
parent
commit
ba0f8a8ea5
  1. 3
      common/dao-api/src/main/java/org/thingsboard/server/dao/attributes/AttributesService.java
  2. 44
      common/data/src/main/java/org/thingsboard/server/common/data/kv/BaseEntityAttributeKvEntry.java
  3. 24
      common/data/src/main/java/org/thingsboard/server/common/data/kv/EntityAttributeKvEntry.java
  4. 3
      dao/src/main/java/org/thingsboard/server/dao/attributes/AttributesDao.java
  5. 3
      dao/src/main/java/org/thingsboard/server/dao/attributes/BaseAttributesService.java
  6. 33
      dao/src/main/java/org/thingsboard/server/dao/model/sql/AttributeKvEntity.java
  7. 6
      dao/src/main/java/org/thingsboard/server/dao/sql/attributes/AttributeKvRepository.java
  8. 13
      dao/src/main/java/org/thingsboard/server/dao/sql/attributes/JpaAttributeDao.java

3
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.EntityId;
import org.thingsboard.server.common.data.id.TenantId; import org.thingsboard.server.common.data.id.TenantId;
import org.thingsboard.server.common.data.kv.AttributeKvEntry; import org.thingsboard.server.common.data.kv.AttributeKvEntry;
import org.thingsboard.server.common.data.kv.EntityAttributeKvEntry;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
@ -35,7 +36,7 @@ public interface AttributesService {
ListenableFuture<List<AttributeKvEntry>> findAll(TenantId tenantId, EntityId entityId, String scope); ListenableFuture<List<AttributeKvEntry>> findAll(TenantId tenantId, EntityId entityId, String scope);
ListenableFuture<List<AttributeKvEntry>> findAllByAttributeKey(String attributeKey); ListenableFuture<List<EntityAttributeKvEntry>> findAllByAttributeKey(String attributeKey);
ListenableFuture<List<Void>> save(TenantId tenantId, EntityId entityId, String scope, List<AttributeKvEntry> attributes); ListenableFuture<List<Void>> save(TenantId tenantId, EntityId entityId, String scope, List<AttributeKvEntry> attributes);

44
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;
}
}

24
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();
}

3
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.EntityId;
import org.thingsboard.server.common.data.id.TenantId; import org.thingsboard.server.common.data.id.TenantId;
import org.thingsboard.server.common.data.kv.AttributeKvEntry; import org.thingsboard.server.common.data.kv.AttributeKvEntry;
import org.thingsboard.server.common.data.kv.EntityAttributeKvEntry;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
@ -35,7 +36,7 @@ public interface AttributesDao {
ListenableFuture<List<AttributeKvEntry>> findAll(TenantId tenantId, EntityId entityId, String attributeType); ListenableFuture<List<AttributeKvEntry>> findAll(TenantId tenantId, EntityId entityId, String attributeType);
ListenableFuture<List<AttributeKvEntry>> findAllByAttributeKey(String attributeKey); ListenableFuture<List<EntityAttributeKvEntry>> findAllByAttributeKey(String attributeKey);
ListenableFuture<Void> save(TenantId tenantId, EntityId entityId, String attributeType, AttributeKvEntry attribute); ListenableFuture<Void> save(TenantId tenantId, EntityId entityId, String attributeType, AttributeKvEntry attribute);

3
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.EntityId;
import org.thingsboard.server.common.data.id.TenantId; import org.thingsboard.server.common.data.id.TenantId;
import org.thingsboard.server.common.data.kv.AttributeKvEntry; 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.exception.IncorrectParameterException;
import org.thingsboard.server.dao.service.Validator; import org.thingsboard.server.dao.service.Validator;
@ -60,7 +61,7 @@ public class BaseAttributesService implements AttributesService {
} }
@Override @Override
public ListenableFuture<List<AttributeKvEntry>> findAllByAttributeKey(String attributeKey) { public ListenableFuture<List<EntityAttributeKvEntry>> findAllByAttributeKey(String attributeKey) {
Validator.validateString(attributeKey, "Incorrect attribute key " + attributeKey); Validator.validateString(attributeKey, "Incorrect attribute key " + attributeKey);
return attributesDao.findAllByAttributeKey(attributeKey); return attributesDao.findAllByAttributeKey(attributeKey);
} }

33
dao/src/main/java/org/thingsboard/server/dao/model/sql/AttributeKvEntity.java

@ -16,20 +16,10 @@
package org.thingsboard.server.dao.model.sql; package org.thingsboard.server.dao.model.sql;
import lombok.Data; import lombok.Data;
import org.thingsboard.server.common.data.kv.AttributeKvEntry; import org.thingsboard.server.common.data.kv.*;
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.dao.model.ToData; import org.thingsboard.server.dao.model.ToData;
import javax.persistence.Column; import javax.persistence.*;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.Table;
import java.io.Serializable; import java.io.Serializable;
import static org.thingsboard.server.dao.model.ModelConstants.BOOLEAN_VALUE_COLUMN; 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 @Data
@Entity @Entity
@Table(name = "attribute_kv") @Table(name = "attribute_kv")
// TODO maybe move ToData<AttributeKvEntry> to local field as well (or implement ToData<EntityAttributeKvEntry> differently)
public class AttributeKvEntity implements ToData<AttributeKvEntry>, Serializable { public class AttributeKvEntity implements ToData<AttributeKvEntry>, Serializable {
@EmbeddedId @EmbeddedId
@ -82,4 +73,22 @@ public class AttributeKvEntity implements ToData<AttributeKvEntry>, Serializable
return new BaseAttributeKvEntry(kvEntry, lastUpdateTs); return new BaseAttributeKvEntry(kvEntry, lastUpdateTs);
} }
@Transient
public final ToData<EntityAttributeKvEntry> 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);
};
} }

6
dao/src/main/java/org/thingsboard/server/dao/sql/attributes/AttributeKvRepository.java

@ -37,6 +37,10 @@ public interface AttributeKvRepository extends CrudRepository<AttributeKvEntity,
@Param("entityId") String entityId, @Param("entityId") String entityId,
@Param("attributeType") String attributeType); @Param("attributeType") String attributeType);
@Query("SELECT a FROM AttributeKvEntity a WHERE a.id.attributeKey = :attributeKey")
List<AttributeKvEntity> findAllByAttributeKey(@Param("attributeKey") String attributeKey);
@Transactional @Transactional
@Modifying @Modifying
@Query("DELETE FROM AttributeKvEntity a WHERE a.id.entityType = :entityType " + @Query("DELETE FROM AttributeKvEntity a WHERE a.id.entityType = :entityType " +
@ -47,7 +51,5 @@ public interface AttributeKvRepository extends CrudRepository<AttributeKvEntity,
@Param("entityId") String entityId, @Param("entityId") String entityId,
@Param("attributeType") String attributeType, @Param("attributeType") String attributeType,
@Param("attributeKey") String attributeKey); @Param("attributeKey") String attributeKey);
List<AttributeKvEntity> findAllByAttributeKey(String attributeKey);
} }

13
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.UUIDConverter;
import org.thingsboard.server.common.data.id.EntityId; import org.thingsboard.server.common.data.id.EntityId;
import org.thingsboard.server.common.data.id.TenantId; 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.DaoUtil;
import org.thingsboard.server.dao.attributes.AttributesDao; 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.AttributeKvCompositeKey;
import org.thingsboard.server.dao.model.sql.AttributeKvEntity; import org.thingsboard.server.dao.model.sql.AttributeKvEntity;
import org.thingsboard.server.dao.sql.JpaAbstractDaoListeningExecutorService; import org.thingsboard.server.dao.sql.JpaAbstractDaoListeningExecutorService;
@ -120,9 +121,15 @@ public class JpaAttributeDao extends JpaAbstractDaoListeningExecutorService impl
} }
@Override @Override
public ListenableFuture<List<AttributeKvEntry>> findAllByAttributeKey(String attributeKey) { public ListenableFuture<List<EntityAttributeKvEntry>> findAllByAttributeKey(String attributeKey) {
return Futures.immediateFuture( return Futures.immediateFuture(
DaoUtil.convertDataList(attributeKvRepository.findAllByAttributeKey(attributeKey))); DaoUtil.convertDataList(
attributeKvRepository.findAllByAttributeKey(attributeKey).stream()
.map(attributeKvEntity -> attributeKvEntity.toEntityAttributeKvEntry)
.collect(Collectors.toList())
)
);
} }
@Override @Override

Loading…
Cancel
Save