Refactoring of Attribute keys
This commit is contained in:
		
							parent
							
								
									dcba4f6b58
								
							
						
					
					
						commit
						600da89385
					
				@ -20,14 +20,29 @@ import lombok.Data;
 | 
			
		||||
import lombok.NoArgsConstructor;
 | 
			
		||||
import org.thingsboard.server.common.data.EntityType;
 | 
			
		||||
 | 
			
		||||
import javax.persistence.Column;
 | 
			
		||||
import javax.persistence.Embeddable;
 | 
			
		||||
import javax.persistence.EnumType;
 | 
			
		||||
import javax.persistence.Enumerated;
 | 
			
		||||
import java.io.Serializable;
 | 
			
		||||
 | 
			
		||||
import static org.thingsboard.server.dao.model.ModelConstants.ATTRIBUTE_KEY_COLUMN;
 | 
			
		||||
import static org.thingsboard.server.dao.model.ModelConstants.ATTRIBUTE_TYPE_COLUMN;
 | 
			
		||||
import static org.thingsboard.server.dao.model.ModelConstants.ENTITY_ID_COLUMN;
 | 
			
		||||
import static org.thingsboard.server.dao.model.ModelConstants.ENTITY_TYPE_COLUMN;
 | 
			
		||||
 | 
			
		||||
@Data
 | 
			
		||||
@AllArgsConstructor
 | 
			
		||||
@NoArgsConstructor
 | 
			
		||||
@Embeddable
 | 
			
		||||
public class AttributeKvCompositeKey implements Serializable {
 | 
			
		||||
    @Enumerated(EnumType.STRING)
 | 
			
		||||
    @Column(name = ENTITY_TYPE_COLUMN)
 | 
			
		||||
    private EntityType entityType;
 | 
			
		||||
    @Column(name = ENTITY_ID_COLUMN)
 | 
			
		||||
    private String entityId;
 | 
			
		||||
    @Column(name = ATTRIBUTE_TYPE_COLUMN)
 | 
			
		||||
    private String attributeType;
 | 
			
		||||
    @Column(name = ATTRIBUTE_KEY_COLUMN)
 | 
			
		||||
    private String attributeKey;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -27,6 +27,7 @@ import org.thingsboard.server.common.data.kv.StringDataEntry;
 | 
			
		||||
import org.thingsboard.server.dao.model.ToData;
 | 
			
		||||
 | 
			
		||||
import javax.persistence.Column;
 | 
			
		||||
import javax.persistence.EmbeddedId;
 | 
			
		||||
import javax.persistence.Entity;
 | 
			
		||||
import javax.persistence.EnumType;
 | 
			
		||||
import javax.persistence.Enumerated;
 | 
			
		||||
@ -48,25 +49,10 @@ import static org.thingsboard.server.dao.model.ModelConstants.STRING_VALUE_COLUM
 | 
			
		||||
@Data
 | 
			
		||||
@Entity
 | 
			
		||||
@Table(name = "attribute_kv")
 | 
			
		||||
@IdClass(AttributeKvCompositeKey.class)
 | 
			
		||||
public class AttributeKvEntity implements ToData<AttributeKvEntry>, Serializable {
 | 
			
		||||
 | 
			
		||||
    @Id
 | 
			
		||||
    @Enumerated(EnumType.STRING)
 | 
			
		||||
    @Column(name = ENTITY_TYPE_COLUMN)
 | 
			
		||||
    private EntityType entityType;
 | 
			
		||||
 | 
			
		||||
    @Id
 | 
			
		||||
    @Column(name = ENTITY_ID_COLUMN)
 | 
			
		||||
    private String entityId;
 | 
			
		||||
 | 
			
		||||
    @Id
 | 
			
		||||
    @Column(name = ATTRIBUTE_TYPE_COLUMN)
 | 
			
		||||
    private String attributeType;
 | 
			
		||||
 | 
			
		||||
    @Id
 | 
			
		||||
    @Column(name = ATTRIBUTE_KEY_COLUMN)
 | 
			
		||||
    private String attributeKey;
 | 
			
		||||
    @EmbeddedId
 | 
			
		||||
    private AttributeKvCompositeKey id;
 | 
			
		||||
 | 
			
		||||
    @Column(name = BOOLEAN_VALUE_COLUMN)
 | 
			
		||||
    private Boolean booleanValue;
 | 
			
		||||
@ -87,13 +73,13 @@ public class AttributeKvEntity implements ToData<AttributeKvEntry>, Serializable
 | 
			
		||||
    public AttributeKvEntry toData() {
 | 
			
		||||
        KvEntry kvEntry = null;
 | 
			
		||||
        if (strValue != null) {
 | 
			
		||||
            kvEntry = new StringDataEntry(attributeKey, strValue);
 | 
			
		||||
            kvEntry = new StringDataEntry(id.getAttributeKey(), strValue);
 | 
			
		||||
        } else if (booleanValue != null) {
 | 
			
		||||
            kvEntry = new BooleanDataEntry(attributeKey, booleanValue);
 | 
			
		||||
            kvEntry = new BooleanDataEntry(id.getAttributeKey(), booleanValue);
 | 
			
		||||
        } else if (doubleValue != null) {
 | 
			
		||||
            kvEntry = new DoubleDataEntry(attributeKey, doubleValue);
 | 
			
		||||
            kvEntry = new DoubleDataEntry(id.getAttributeKey(), doubleValue);
 | 
			
		||||
        } else if (longValue != null) {
 | 
			
		||||
            kvEntry = new LongDataEntry(attributeKey, longValue);
 | 
			
		||||
            kvEntry = new LongDataEntry(id.getAttributeKey(), longValue);
 | 
			
		||||
        }
 | 
			
		||||
        return new BaseAttributeKvEntry(kvEntry, lastUpdateTs);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@ -15,7 +15,9 @@
 | 
			
		||||
 */
 | 
			
		||||
package org.thingsboard.server.dao.sql.attributes;
 | 
			
		||||
 | 
			
		||||
import org.springframework.data.jpa.repository.Query;
 | 
			
		||||
import org.springframework.data.repository.CrudRepository;
 | 
			
		||||
import org.springframework.data.repository.query.Param;
 | 
			
		||||
import org.thingsboard.server.common.data.EntityType;
 | 
			
		||||
import org.thingsboard.server.dao.model.sql.AttributeKvCompositeKey;
 | 
			
		||||
import org.thingsboard.server.dao.model.sql.AttributeKvEntity;
 | 
			
		||||
@ -26,8 +28,11 @@ import java.util.List;
 | 
			
		||||
@SqlDao
 | 
			
		||||
public interface AttributeKvRepository extends CrudRepository<AttributeKvEntity, AttributeKvCompositeKey> {
 | 
			
		||||
 | 
			
		||||
    List<AttributeKvEntity> findAllByEntityTypeAndEntityIdAndAttributeType(EntityType entityType,
 | 
			
		||||
                                                                           String entityId,
 | 
			
		||||
                                                                           String attributeType);
 | 
			
		||||
    @Query("SELECT a FROM AttributeKvEntity a WHERE a.id.entityType = :entityType " +
 | 
			
		||||
            "AND a.id.entityId = :entityId " +
 | 
			
		||||
            "AND a.id.attributeType = :attributeType")
 | 
			
		||||
    List<AttributeKvEntity> findAllByEntityTypeAndEntityIdAndAttributeType(@Param("entityType") EntityType entityType,
 | 
			
		||||
                                                                           @Param("entityId") String entityId,
 | 
			
		||||
                                                                           @Param("attributeType") String attributeType);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -79,10 +79,7 @@ public class JpaAttributeDao extends JpaAbstractDaoListeningExecutorService impl
 | 
			
		||||
    @Override
 | 
			
		||||
    public ListenableFuture<Void> save(EntityId entityId, String attributeType, AttributeKvEntry attribute) {
 | 
			
		||||
        AttributeKvEntity entity = new AttributeKvEntity();
 | 
			
		||||
        entity.setEntityType(entityId.getEntityType());
 | 
			
		||||
        entity.setEntityId(fromTimeUUID(entityId.getId()));
 | 
			
		||||
        entity.setAttributeType(attributeType);
 | 
			
		||||
        entity.setAttributeKey(attribute.getKey());
 | 
			
		||||
        entity.setId(new AttributeKvCompositeKey(entityId.getEntityType(), fromTimeUUID(entityId.getId()), attributeType, attribute.getKey()));
 | 
			
		||||
        entity.setLastUpdateTs(attribute.getLastUpdateTs());
 | 
			
		||||
        entity.setStrValue(attribute.getStrValue().orElse(null));
 | 
			
		||||
        entity.setDoubleValue(attribute.getDoubleValue().orElse(null));
 | 
			
		||||
@ -100,10 +97,7 @@ public class JpaAttributeDao extends JpaAbstractDaoListeningExecutorService impl
 | 
			
		||||
                .stream()
 | 
			
		||||
                .map(key -> {
 | 
			
		||||
                    AttributeKvEntity entityToDelete = new AttributeKvEntity();
 | 
			
		||||
                    entityToDelete.setEntityType(entityId.getEntityType());
 | 
			
		||||
                    entityToDelete.setEntityId(fromTimeUUID(entityId.getId()));
 | 
			
		||||
                    entityToDelete.setAttributeType(attributeType);
 | 
			
		||||
                    entityToDelete.setAttributeKey(key);
 | 
			
		||||
                    entityToDelete.setId(new AttributeKvCompositeKey(entityId.getEntityType(), fromTimeUUID(entityId.getId()), attributeType, key));
 | 
			
		||||
                    return entityToDelete;
 | 
			
		||||
                }).collect(Collectors.toList());
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user