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