Merge pull request #6704 from ViacheslavKlimov/entity-view-ie

Entity views VC
This commit is contained in:
Igor Kulikov 2022-06-14 11:07:57 +03:00 committed by GitHub
commit f25dd50254
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 105 additions and 8 deletions

View File

@ -28,6 +28,8 @@ ALTER TABLE customer
ADD COLUMN IF NOT EXISTS external_id UUID;
ALTER TABLE widgets_bundle
ADD COLUMN IF NOT EXISTS external_id UUID;
ALTER TABLE entity_view
ADD COLUMN IF NOT EXISTS external_id UUID;
CREATE INDEX IF NOT EXISTS idx_device_external_id ON device(tenant_id, external_id);
CREATE INDEX IF NOT EXISTS idx_device_profile_external_id ON device_profile(tenant_id, external_id);

View File

@ -58,7 +58,7 @@ public class DefaultEntitiesExportImportService implements EntitiesExportImportS
protected static final List<EntityType> SUPPORTED_ENTITY_TYPES = List.of(
EntityType.CUSTOMER, EntityType.ASSET, EntityType.RULE_CHAIN,
EntityType.DASHBOARD, EntityType.DEVICE_PROFILE, EntityType.DEVICE,
EntityType.WIDGETS_BUNDLE
EntityType.ENTITY_VIEW, EntityType.WIDGETS_BUNDLE
);

View File

@ -0,0 +1,62 @@
/**
* Copyright © 2016-2022 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.service.sync.ie.importing.impl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.thingsboard.server.common.data.EntityType;
import org.thingsboard.server.common.data.EntityView;
import org.thingsboard.server.common.data.audit.ActionType;
import org.thingsboard.server.common.data.exception.ThingsboardException;
import org.thingsboard.server.common.data.id.EntityViewId;
import org.thingsboard.server.common.data.id.TenantId;
import org.thingsboard.server.common.data.sync.ie.EntityExportData;
import org.thingsboard.server.common.data.sync.ie.EntityImportSettings;
import org.thingsboard.server.dao.entityview.EntityViewService;
import org.thingsboard.server.queue.util.TbCoreComponent;
import org.thingsboard.server.service.security.model.SecurityUser;
@Service
@TbCoreComponent
@RequiredArgsConstructor
public class EntityViewImportService extends BaseEntityImportService<EntityViewId, EntityView, EntityExportData<EntityView>> {
private final EntityViewService entityViewService;
@Override
protected void setOwner(TenantId tenantId, EntityView entityView, IdProvider idProvider) {
entityView.setTenantId(tenantId);
entityView.setCustomerId(idProvider.getInternalId(entityView.getCustomerId()));
}
@Override
protected EntityView prepareAndSave(TenantId tenantId, EntityView entityView, EntityExportData<EntityView> exportData, IdProvider idProvider, EntityImportSettings importSettings) {
entityView.setEntityId(idProvider.getInternalId(entityView.getEntityId()));
return entityViewService.saveEntityView(entityView);
}
@Override
protected void onEntitySaved(SecurityUser user, EntityView savedEntityView, EntityView oldEntityView) throws ThingsboardException {
entityNotificationService.notifyCreateOrUpdateEntity(user.getTenantId(), savedEntityView.getId(), savedEntityView,
null, oldEntityView == null ? ActionType.ADDED : ActionType.UPDATED, user);
}
@Override
public EntityType getEntityType() {
return EntityType.ENTITY_VIEW;
}
}

View File

@ -36,7 +36,7 @@ import org.thingsboard.server.common.data.validation.NoXss;
@AllArgsConstructor
@EqualsAndHashCode(callSuper = true)
public class EntityView extends SearchTextBasedWithAdditionalInfo<EntityViewId>
implements HasName, HasTenantId, HasCustomerId {
implements HasName, HasTenantId, HasCustomerId, ExportableEntity<EntityViewId> {
private static final long serialVersionUID = 5582010124562018986L;
@ -59,6 +59,8 @@ public class EntityView extends SearchTextBasedWithAdditionalInfo<EntityViewId>
@ApiModelProperty(position = 10, value = "Represents the end time of the interval that is used to limit access to target device telemetry. Customer will not be able to see entity telemetry that is outside the specified interval;")
private long endTimeMs;
private EntityViewId externalId;
public EntityView() {
super();
}
@ -77,6 +79,7 @@ public class EntityView extends SearchTextBasedWithAdditionalInfo<EntityViewId>
this.keys = entityView.getKeys();
this.startTimeMs = entityView.getStartTimeMs();
this.endTimeMs = entityView.getEndTimeMs();
this.externalId = entityView.getExternalId();
}
@Override

View File

@ -23,6 +23,7 @@ import org.thingsboard.server.common.data.Customer;
import org.thingsboard.server.common.data.Dashboard;
import org.thingsboard.server.common.data.Device;
import org.thingsboard.server.common.data.DeviceProfile;
import org.thingsboard.server.common.data.EntityView;
import org.thingsboard.server.common.data.asset.Asset;
import org.thingsboard.server.common.data.rule.RuleChain;
import org.thingsboard.server.common.data.widget.WidgetsBundle;
@ -43,6 +44,7 @@ import java.lang.annotation.Target;
@Type(name = "ASSET", value = Asset.class),
@Type(name = "DASHBOARD", value = Dashboard.class),
@Type(name = "CUSTOMER", value = Customer.class),
@Type(name = "ENTITY_VIEW", value = EntityView.class),
@Type(name = "WIDGETS_BUNDLE", value = WidgetsBundle.class)
})
public @interface JsonTbEntity {

View File

@ -23,6 +23,7 @@ import org.thingsboard.server.common.data.id.TenantId;
import org.thingsboard.server.common.data.page.PageData;
import org.thingsboard.server.common.data.page.PageLink;
import org.thingsboard.server.dao.Dao;
import org.thingsboard.server.dao.ExportableEntityDao;
import java.util.List;
import java.util.Optional;
@ -31,7 +32,7 @@ import java.util.UUID;
/**
* Created by Victor Basanets on 8/28/2017.
*/
public interface EntityViewDao extends Dao<EntityView> {
public interface EntityViewDao extends Dao<EntityView>, ExportableEntityDao<EntityView> {
/**
* Find entity view info by id.

View File

@ -89,6 +89,9 @@ public abstract class AbstractEntityViewEntity<T extends EntityView> extends Bas
@Column(name = ModelConstants.ENTITY_VIEW_ADDITIONAL_INFO_PROPERTY)
private JsonNode additionalInfo;
@Column(name = ModelConstants.EXTERNAL_ID_PROPERTY)
private UUID externalId;
private static final ObjectMapper mapper = new ObjectMapper();
public AbstractEntityViewEntity() {
@ -121,6 +124,9 @@ public abstract class AbstractEntityViewEntity<T extends EntityView> extends Bas
this.endTs = entityView.getEndTimeMs();
this.searchText = entityView.getSearchText();
this.additionalInfo = entityView.getAdditionalInfo();
if (entityView.getExternalId() != null) {
this.externalId = entityView.getExternalId().getId();
}
}
public AbstractEntityViewEntity(EntityViewEntity entityViewEntity) {
@ -137,6 +143,7 @@ public abstract class AbstractEntityViewEntity<T extends EntityView> extends Bas
this.endTs = entityViewEntity.getEndTs();
this.searchText = entityViewEntity.getSearchText();
this.additionalInfo = entityViewEntity.getAdditionalInfo();
this.externalId = entityViewEntity.getExternalId();
}
@Override
@ -172,6 +179,9 @@ public abstract class AbstractEntityViewEntity<T extends EntityView> extends Bas
entityView.setStartTimeMs(startTs);
entityView.setEndTimeMs(endTs);
entityView.setAdditionalInfo(additionalInfo);
if (externalId != null) {
entityView.setExternalId(new EntityViewId(externalId));
}
return entityView;
}
}

View File

@ -20,6 +20,7 @@ import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.thingsboard.server.dao.ExportableEntityRepository;
import org.thingsboard.server.dao.model.sql.EntityViewEntity;
import org.thingsboard.server.dao.model.sql.EntityViewInfoEntity;
@ -29,7 +30,7 @@ import java.util.UUID;
/**
* Created by Victor Basanets on 8/31/2017.
*/
public interface EntityViewRepository extends JpaRepository<EntityViewEntity, UUID> {
public interface EntityViewRepository extends JpaRepository<EntityViewEntity, UUID>, ExportableEntityRepository<EntityViewEntity> {
@Query("SELECT new org.thingsboard.server.dao.model.sql.EntityViewInfoEntity(e, c.title, c.additionalInfo) " +
"FROM EntityViewEntity e " +

View File

@ -201,9 +201,23 @@ public class JpaEntityViewDao extends JpaAbstractSearchTextDao<EntityViewEntity,
DaoUtil.toPageable(pageLink)));
}
@Override
public EntityView findByTenantIdAndExternalId(UUID tenantId, UUID externalId) {
return DaoUtil.getData(entityViewRepository.findByTenantIdAndExternalId(tenantId, externalId));
}
@Override
public PageData<EntityView> findByTenantId(UUID tenantId, PageLink pageLink) {
return findEntityViewsByTenantId(tenantId, pageLink);
}
@Override
public EntityView findByTenantIdAndName(UUID tenantId, String name) {
return findEntityViewByTenantIdAndName(tenantId, name).orElse(null);
}
@Override
public EntityType getEntityType() {
return EntityType.ENTITY_VIEW;
}
}

View File

@ -440,7 +440,8 @@ CREATE TABLE IF NOT EXISTS entity_view (
start_ts bigint,
end_ts bigint,
search_text varchar(255),
additional_info varchar
additional_info varchar,
external_id uuid
);
CREATE TABLE IF NOT EXISTS ts_kv_latest

View File

@ -14,7 +14,7 @@
/// limitations under the License.
///
import { BaseData } from '@shared/models/base-data';
import { BaseData, ExportableEntity } from '@shared/models/base-data';
import { TenantId } from '@shared/models/id/tenant-id';
import { CustomerId } from '@shared/models/id/customer-id';
import { EntityViewId } from '@shared/models/id/entity-view-id';
@ -32,7 +32,7 @@ export interface TelemetryEntityView {
attributes: AttributesEntityView;
}
export interface EntityView extends BaseData<EntityViewId> {
export interface EntityView extends BaseData<EntityViewId>, ExportableEntity<EntityViewId> {
tenantId: TenantId;
customerId: CustomerId;
entityId: EntityId;

View File

@ -28,6 +28,7 @@ export const exportableEntityTypes: Array<EntityType> = [
EntityType.CUSTOMER,
EntityType.DEVICE_PROFILE,
EntityType.RULE_CHAIN,
EntityType.ENTITY_VIEW,
EntityType.WIDGETS_BUNDLE
];