diff --git a/dao/src/main/java/org/thingsboard/server/dao/model/nosql/EntityViewEntity.java b/dao/src/main/java/org/thingsboard/server/dao/model/nosql/EntityViewEntity.java new file mode 100644 index 0000000000..c3227e9605 --- /dev/null +++ b/dao/src/main/java/org/thingsboard/server/dao/model/nosql/EntityViewEntity.java @@ -0,0 +1,151 @@ +/** + * Copyright © 2016-2018 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.dao.model.nosql; + +import com.datastax.driver.core.utils.UUIDs; +import com.datastax.driver.mapping.annotations.PartitionKey; +import com.datastax.driver.mapping.annotations.Table; +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.ToString; +import org.hibernate.annotations.Type; +import org.thingsboard.server.common.data.EntityView; +import org.thingsboard.server.common.data.id.CustomerId; +import org.thingsboard.server.common.data.id.DeviceId; +import org.thingsboard.server.common.data.id.EntityViewId; +import org.thingsboard.server.common.data.id.TenantId; +import org.thingsboard.server.dao.model.ModelConstants; +import org.thingsboard.server.dao.model.SearchTextEntity; + +import javax.persistence.Column; + +import java.io.IOException; +import java.util.List; +import java.util.UUID; +import java.util.stream.Collectors; + +import static org.thingsboard.server.dao.model.ModelConstants.ENTITY_VIEW_TABLE_FAMILY_NAME; +import static org.thingsboard.server.dao.model.ModelConstants.ID_PROPERTY; + +/** + * Created by Victor Basanets on 8/31/2017. + */ +@Data +@Table(name = ENTITY_VIEW_TABLE_FAMILY_NAME) +@EqualsAndHashCode +@ToString +public class EntityViewEntity implements SearchTextEntity { + + @PartitionKey(value = 0) + @Column(name = ID_PROPERTY) + private UUID id; + + @PartitionKey(value = 1) + @Column(name = ModelConstants.ENTITY_VIEW_ENTITY_ID_PROPERTY) + private UUID entityId; + + @PartitionKey(value = 2) + @Column(name = ModelConstants.ENTITY_VIEW_TENANT_ID_PROPERTY) + private UUID tenantId; + + @PartitionKey(value = 3) + @Column(name = ModelConstants.ENTITY_VIEW_CUSTOMER_ID_PROPERTY) + private UUID customerId; + + @Column(name = ModelConstants.ENTITY_VIEW_NAME_PROPERTY) + private String name; + + @Type(type = "json") + @Column(name = ModelConstants.ENTITY_VIEW_KEYS_PROPERTY) + private JsonNode keys; + + @Column(name = ModelConstants.ENTITY_VIEW_TS_BEGIN_PROPERTY) + private String tsBegin; + + @Column(name = ModelConstants.ENTITY_VIEW_TS_END_PROPERTY) + private String tsEnd; + + @Column(name = ModelConstants.SEARCH_TEXT_PROPERTY) + private String searchText; + + @Type(type = "json") + @Column(name = ModelConstants.ENTITY_VIEW_ADDITIONAL_INFO_PROPERTY) + private JsonNode additionalInfo; + + public EntityViewEntity() { + super(); + } + + public EntityViewEntity(EntityView entityView) { + if (entityView.getId() != null) { + this.id = entityView.getId().getId(); + } + if (entityView.getEntityId() != null) { + this.entityId = entityView.getEntityId().getId(); + } + if (entityView.getTenantId() != null) { + this.tenantId = entityView.getTenantId().getId(); + } + if (entityView.getCustomerId() != null) { + this.customerId = entityView.getCustomerId().getId(); + } + this.name = entityView.getName(); + try { + this.keys = new ObjectMapper().readTree("{\"" + entityView.getName() + "\" : [" + + entityView.getKeys().stream() + .map(k -> "\"" + k + "\"") + .collect(Collectors.joining(", ")) + "]}"); + } catch (IOException e) { + e.printStackTrace(); + } + this.tsBegin = String.valueOf(entityView.getTsBegin()); + this.tsEnd = String.valueOf(entityView.getTsEnd()); + this.additionalInfo = entityView.getAdditionalInfo(); + } + + @Override + public String getSearchTextSource() { + return name; + } + + @Override + public EntityView toData() { + EntityView entityView = new EntityView(new EntityViewId(id)); + entityView.setCreatedTime(UUIDs.unixTimestamp(id)); + if (entityId != null) { + entityView.setEntityId(new DeviceId(entityId)); + } + if (tenantId != null) { + entityView.setTenantId(new TenantId(tenantId)); + } + if (customerId != null) { + entityView.setCustomerId(new CustomerId(customerId)); + } + entityView.setName(name); + try { + entityView.setKeys(new ObjectMapper().readValue(keys.toString(), new TypeReference>(){})); + } catch (IOException e) { + e.printStackTrace(); + } + entityView.setTsBegin(Long.parseLong(tsBegin)); + entityView.setTsEnd(Long.parseLong(tsEnd)); + entityView.setAdditionalInfo(additionalInfo); + return entityView; + } +} diff --git a/dao/src/main/java/org/thingsboard/server/dao/model/sql/EntityViewEntity.java b/dao/src/main/java/org/thingsboard/server/dao/model/sql/EntityViewEntity.java index 1444e9745e..dba075ce01 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/model/sql/EntityViewEntity.java +++ b/dao/src/main/java/org/thingsboard/server/dao/model/sql/EntityViewEntity.java @@ -23,6 +23,7 @@ import lombok.Data; import lombok.EqualsAndHashCode; import org.hibernate.annotations.Type; import org.hibernate.annotations.TypeDef; +import org.thingsboard.server.common.data.EntityType; import org.thingsboard.server.common.data.EntityView; import org.thingsboard.server.common.data.id.CustomerId; import org.thingsboard.server.common.data.id.DeviceId; @@ -38,8 +39,13 @@ import javax.persistence.Entity; import javax.persistence.Table; import java.io.IOException; import java.util.List; +import java.util.UUID; import java.util.stream.Collectors; +/** + * Created by Victor Basanets on 8/30/2017. + */ + @Data @EqualsAndHashCode(callSuper = true) @Entity @@ -123,15 +129,23 @@ public class EntityViewEntity extends BaseSqlEntity implements Searc entityView.setCreatedTime(UUIDs.unixTimestamp(getId())); /*Ned to refactor and replace DeviceId to Class<> instance*/ - entityView.setEntityId(entityId != null ? new DeviceId(toUUID(entityId)) : null); - entityView.setTenantId(tenantId != null ? new TenantId(toUUID(tenantId)) : null); - entityView.setCustomerId(customerId != null ? new CustomerId(toUUID(customerId)) : null); + if (entityId != null) { + entityView.setEntityId(new DeviceId(toUUID(entityId))); + } + if (tenantId != null) { + entityView.setTenantId(new TenantId(toUUID(tenantId))); + } + if (customerId != null) { + entityView.setCustomerId(new CustomerId(toUUID(customerId))); + } entityView.setName(name); try { entityView.setKeys(new ObjectMapper().readValue(keys.toString(), new TypeReference>(){})); } catch (IOException e) { e.printStackTrace(); } + entityView.setTsBegin(Long.parseLong(tsBegin)); + entityView.setTsEnd(Long.parseLong(tsEnd)); entityView.setAdditionalInfo(additionalInfo); return entityView; }