Search in sources :

Example 6 with EntityView

use of org.thingsboard.server.common.data.EntityView in project thingsboard by thingsboard.

the class EntityViewController method saveEntityView.

@ApiOperation(value = "Save or update entity view (saveEntityView)", notes = ENTITY_VIEW_DESCRIPTION + MODEL_DESCRIPTION, produces = MediaType.APPLICATION_JSON_VALUE)
@PreAuthorize("hasAnyAuthority('TENANT_ADMIN', 'CUSTOMER_USER')")
@RequestMapping(value = "/entityView", method = RequestMethod.POST)
@ResponseBody
public EntityView saveEntityView(@ApiParam(value = "A JSON object representing the entity view.") @RequestBody EntityView entityView) throws ThingsboardException {
    try {
        entityView.setTenantId(getCurrentUser().getTenantId());
        List<ListenableFuture<?>> futures = new ArrayList<>();
        if (entityView.getId() == null) {
            accessControlService.checkPermission(getCurrentUser(), Resource.ENTITY_VIEW, Operation.CREATE, null, entityView);
        } else {
            EntityView existingEntityView = checkEntityViewId(entityView.getId(), Operation.WRITE);
            if (existingEntityView.getKeys() != null) {
                if (existingEntityView.getKeys().getAttributes() != null) {
                    futures.add(deleteAttributesFromEntityView(existingEntityView, DataConstants.CLIENT_SCOPE, existingEntityView.getKeys().getAttributes().getCs(), getCurrentUser()));
                    futures.add(deleteAttributesFromEntityView(existingEntityView, DataConstants.SERVER_SCOPE, existingEntityView.getKeys().getAttributes().getCs(), getCurrentUser()));
                    futures.add(deleteAttributesFromEntityView(existingEntityView, DataConstants.SHARED_SCOPE, existingEntityView.getKeys().getAttributes().getCs(), getCurrentUser()));
                }
            }
            List<String> tsKeys = existingEntityView.getKeys() != null && existingEntityView.getKeys().getTimeseries() != null ? existingEntityView.getKeys().getTimeseries() : Collections.emptyList();
            futures.add(deleteLatestFromEntityView(existingEntityView, tsKeys, getCurrentUser()));
        }
        EntityView savedEntityView = checkNotNull(entityViewService.saveEntityView(entityView));
        if (savedEntityView.getKeys() != null) {
            if (savedEntityView.getKeys().getAttributes() != null) {
                futures.add(copyAttributesFromEntityToEntityView(savedEntityView, DataConstants.CLIENT_SCOPE, savedEntityView.getKeys().getAttributes().getCs(), getCurrentUser()));
                futures.add(copyAttributesFromEntityToEntityView(savedEntityView, DataConstants.SERVER_SCOPE, savedEntityView.getKeys().getAttributes().getSs(), getCurrentUser()));
                futures.add(copyAttributesFromEntityToEntityView(savedEntityView, DataConstants.SHARED_SCOPE, savedEntityView.getKeys().getAttributes().getSh(), getCurrentUser()));
            }
            futures.add(copyLatestFromEntityToEntityView(savedEntityView, getCurrentUser()));
        }
        for (ListenableFuture<?> future : futures) {
            try {
                future.get();
            } catch (InterruptedException | ExecutionException e) {
                throw new RuntimeException("Failed to copy attributes to entity view", e);
            }
        }
        logEntityAction(savedEntityView.getId(), savedEntityView, null, entityView.getId() == null ? ActionType.ADDED : ActionType.UPDATED, null);
        if (entityView.getId() != null) {
            sendEntityNotificationMsg(savedEntityView.getTenantId(), savedEntityView.getId(), EdgeEventActionType.UPDATED);
        }
        return savedEntityView;
    } catch (Exception e) {
        logEntityAction(emptyId(EntityType.ENTITY_VIEW), entityView, null, entityView.getId() == null ? ActionType.ADDED : ActionType.UPDATED, e);
        throw handleException(e);
    }
}
Also used : EntityView(org.thingsboard.server.common.data.EntityView) ArrayList(java.util.ArrayList) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) ExecutionException(java.util.concurrent.ExecutionException) IncorrectParameterException(org.thingsboard.server.dao.exception.IncorrectParameterException) ThingsboardException(org.thingsboard.server.common.data.exception.ThingsboardException) ExecutionException(java.util.concurrent.ExecutionException) ApiOperation(io.swagger.annotations.ApiOperation) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 7 with EntityView

use of org.thingsboard.server.common.data.EntityView in project thingsboard by thingsboard.

the class EntityViewController method assignEntityViewToPublicCustomer.

@ApiOperation(value = "Make entity view publicly available (assignEntityViewToPublicCustomer)", notes = "Entity View will be available for non-authorized (not logged-in) users. " + "This is useful to create dashboards that you plan to share/embed on a publicly available website. " + "However, users that are logged-in and belong to different tenant will not be able to access the entity view." + TENANT_AUTHORITY_PARAGRAPH)
@PreAuthorize("hasAuthority('TENANT_ADMIN')")
@RequestMapping(value = "/customer/public/entityView/{entityViewId}", method = RequestMethod.POST)
@ResponseBody
public EntityView assignEntityViewToPublicCustomer(@ApiParam(value = ENTITY_VIEW_ID_PARAM_DESCRIPTION) @PathVariable(ENTITY_VIEW_ID) String strEntityViewId) throws ThingsboardException {
    checkParameter(ENTITY_VIEW_ID, strEntityViewId);
    try {
        EntityViewId entityViewId = new EntityViewId(toUUID(strEntityViewId));
        EntityView entityView = checkEntityViewId(entityViewId, Operation.ASSIGN_TO_CUSTOMER);
        Customer publicCustomer = customerService.findOrCreatePublicCustomer(entityView.getTenantId());
        EntityView savedEntityView = checkNotNull(entityViewService.assignEntityViewToCustomer(getCurrentUser().getTenantId(), entityViewId, publicCustomer.getId()));
        logEntityAction(entityViewId, savedEntityView, savedEntityView.getCustomerId(), ActionType.ASSIGNED_TO_CUSTOMER, null, strEntityViewId, publicCustomer.getId().toString(), publicCustomer.getName());
        return savedEntityView;
    } catch (Exception e) {
        logEntityAction(emptyId(EntityType.ENTITY_VIEW), null, null, ActionType.ASSIGNED_TO_CUSTOMER, e, strEntityViewId);
        throw handleException(e);
    }
}
Also used : EntityViewId(org.thingsboard.server.common.data.id.EntityViewId) EntityView(org.thingsboard.server.common.data.EntityView) Customer(org.thingsboard.server.common.data.Customer) IncorrectParameterException(org.thingsboard.server.dao.exception.IncorrectParameterException) ThingsboardException(org.thingsboard.server.common.data.exception.ThingsboardException) ExecutionException(java.util.concurrent.ExecutionException) ApiOperation(io.swagger.annotations.ApiOperation) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 8 with EntityView

use of org.thingsboard.server.common.data.EntityView in project thingsboard by thingsboard.

the class EntityViewController method unassignEntityViewFromCustomer.

@ApiOperation(value = "Unassign Entity View from customer (unassignEntityViewFromCustomer)", notes = "Clears assignment of the Entity View to customer. Customer will not be able to query Entity View afterwards." + TENANT_AUTHORITY_PARAGRAPH)
@PreAuthorize("hasAuthority('TENANT_ADMIN')")
@RequestMapping(value = "/customer/entityView/{entityViewId}", method = RequestMethod.DELETE)
@ResponseBody
public EntityView unassignEntityViewFromCustomer(@ApiParam(value = ENTITY_VIEW_ID_PARAM_DESCRIPTION) @PathVariable(ENTITY_VIEW_ID) String strEntityViewId) throws ThingsboardException {
    checkParameter(ENTITY_VIEW_ID, strEntityViewId);
    try {
        EntityViewId entityViewId = new EntityViewId(toUUID(strEntityViewId));
        EntityView entityView = checkEntityViewId(entityViewId, Operation.UNASSIGN_FROM_CUSTOMER);
        if (entityView.getCustomerId() == null || entityView.getCustomerId().getId().equals(ModelConstants.NULL_UUID)) {
            throw new IncorrectParameterException("Entity View isn't assigned to any customer!");
        }
        Customer customer = checkCustomerId(entityView.getCustomerId(), Operation.READ);
        EntityView savedEntityView = checkNotNull(entityViewService.unassignEntityViewFromCustomer(getTenantId(), entityViewId));
        logEntityAction(entityViewId, entityView, entityView.getCustomerId(), ActionType.UNASSIGNED_FROM_CUSTOMER, null, strEntityViewId, customer.getId().toString(), customer.getName());
        sendEntityAssignToCustomerNotificationMsg(savedEntityView.getTenantId(), savedEntityView.getId(), customer.getId(), EdgeEventActionType.UNASSIGNED_FROM_CUSTOMER);
        return savedEntityView;
    } catch (Exception e) {
        logEntityAction(emptyId(EntityType.ENTITY_VIEW), null, null, ActionType.UNASSIGNED_FROM_CUSTOMER, e, strEntityViewId);
        throw handleException(e);
    }
}
Also used : EntityViewId(org.thingsboard.server.common.data.id.EntityViewId) EntityView(org.thingsboard.server.common.data.EntityView) Customer(org.thingsboard.server.common.data.Customer) IncorrectParameterException(org.thingsboard.server.dao.exception.IncorrectParameterException) IncorrectParameterException(org.thingsboard.server.dao.exception.IncorrectParameterException) ThingsboardException(org.thingsboard.server.common.data.exception.ThingsboardException) ExecutionException(java.util.concurrent.ExecutionException) ApiOperation(io.swagger.annotations.ApiOperation) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 9 with EntityView

use of org.thingsboard.server.common.data.EntityView in project thingsboard by thingsboard.

the class EntityViewController method getEdgeEntityViews.

@PreAuthorize("hasAnyAuthority('TENANT_ADMIN', 'CUSTOMER_USER')")
@RequestMapping(value = "/edge/{edgeId}/entityViews", params = { "pageSize", "page" }, method = RequestMethod.GET)
@ResponseBody
public PageData<EntityView> getEdgeEntityViews(@PathVariable(EDGE_ID) String strEdgeId, @RequestParam int pageSize, @RequestParam int page, @RequestParam(required = false) String type, @RequestParam(required = false) String textSearch, @RequestParam(required = false) String sortProperty, @RequestParam(required = false) String sortOrder, @RequestParam(required = false) Long startTime, @RequestParam(required = false) Long endTime) throws ThingsboardException {
    checkParameter(EDGE_ID, strEdgeId);
    try {
        TenantId tenantId = getCurrentUser().getTenantId();
        EdgeId edgeId = new EdgeId(toUUID(strEdgeId));
        checkEdgeId(edgeId, Operation.READ);
        TimePageLink pageLink = createTimePageLink(pageSize, page, textSearch, sortProperty, sortOrder, startTime, endTime);
        PageData<EntityView> nonFilteredResult;
        if (type != null && type.trim().length() > 0) {
            nonFilteredResult = entityViewService.findEntityViewsByTenantIdAndEdgeIdAndType(tenantId, edgeId, type, pageLink);
        } else {
            nonFilteredResult = entityViewService.findEntityViewsByTenantIdAndEdgeId(tenantId, edgeId, pageLink);
        }
        List<EntityView> filteredEntityViews = nonFilteredResult.getData().stream().filter(entityView -> {
            try {
                accessControlService.checkPermission(getCurrentUser(), Resource.ENTITY_VIEW, Operation.READ, entityView.getId(), entityView);
                return true;
            } catch (ThingsboardException e) {
                return false;
            }
        }).collect(Collectors.toList());
        PageData<EntityView> filteredResult = new PageData<>(filteredEntityViews, nonFilteredResult.getTotalPages(), nonFilteredResult.getTotalElements(), nonFilteredResult.hasNext());
        return checkNotNull(filteredResult);
    } catch (Exception e) {
        throw handleException(e);
    }
}
Also used : TENANT_OR_CUSTOMER_AUTHORITY_PARAGRAPH(org.thingsboard.server.controller.ControllerConstants.TENANT_OR_CUSTOMER_AUTHORITY_PARAGRAPH) PathVariable(org.springframework.web.bind.annotation.PathVariable) RequestParam(org.springframework.web.bind.annotation.RequestParam) Edge(org.thingsboard.server.common.data.edge.Edge) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) TimePageLink(org.thingsboard.server.common.data.page.TimePageLink) Customer(org.thingsboard.server.common.data.Customer) SORT_ORDER_ALLOWABLE_VALUES(org.thingsboard.server.controller.ControllerConstants.SORT_ORDER_ALLOWABLE_VALUES) ApiParam(io.swagger.annotations.ApiParam) Autowired(org.springframework.beans.factory.annotation.Autowired) ENTITY_VIEW_TYPE(org.thingsboard.server.controller.ControllerConstants.ENTITY_VIEW_TYPE) SettableFuture(com.google.common.util.concurrent.SettableFuture) CUSTOMER_ID_PARAM_DESCRIPTION(org.thingsboard.server.controller.ControllerConstants.CUSTOMER_ID_PARAM_DESCRIPTION) TenantId(org.thingsboard.server.common.data.id.TenantId) ApiOperation(io.swagger.annotations.ApiOperation) AttributeKvEntry(org.thingsboard.server.common.data.kv.AttributeKvEntry) EntityViewInfo(org.thingsboard.server.common.data.EntityViewInfo) EDGE_UNASSIGN_RECEIVE_STEP_DESCRIPTION(org.thingsboard.server.controller.ControllerConstants.EDGE_UNASSIGN_RECEIVE_STEP_DESCRIPTION) EntityType(org.thingsboard.server.common.data.EntityType) CUSTOMER_ID(org.thingsboard.server.controller.ControllerConstants.CUSTOMER_ID) BaseReadTsKvQuery(org.thingsboard.server.common.data.kv.BaseReadTsKvQuery) IncorrectParameterException(org.thingsboard.server.dao.exception.IncorrectParameterException) EdgeId(org.thingsboard.server.common.data.id.EdgeId) PageLink(org.thingsboard.server.common.data.page.PageLink) TENANT_AUTHORITY_PARAGRAPH(org.thingsboard.server.controller.ControllerConstants.TENANT_AUTHORITY_PARAGRAPH) MediaType(org.springframework.http.MediaType) SecurityUser(org.thingsboard.server.service.security.model.SecurityUser) Collection(java.util.Collection) PAGE_DATA_PARAMETERS(org.thingsboard.server.controller.ControllerConstants.PAGE_DATA_PARAMETERS) RequestMethod(org.springframework.web.bind.annotation.RequestMethod) TimeseriesService(org.thingsboard.server.dao.timeseries.TimeseriesService) RestController(org.springframework.web.bind.annotation.RestController) Collectors(java.util.stream.Collectors) EdgeEventActionType(org.thingsboard.server.common.data.edge.EdgeEventActionType) EntityView(org.thingsboard.server.common.data.EntityView) TsKvEntry(org.thingsboard.server.common.data.kv.TsKvEntry) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) EntityViewSearchQuery(org.thingsboard.server.common.data.entityview.EntityViewSearchQuery) Operation(org.thingsboard.server.service.security.permission.Operation) EntitySubtype(org.thingsboard.server.common.data.EntitySubtype) EntityViewId(org.thingsboard.server.common.data.id.EntityViewId) SORT_ORDER_DESCRIPTION(org.thingsboard.server.controller.ControllerConstants.SORT_ORDER_DESCRIPTION) ENTITY_VIEW_TEXT_SEARCH_DESCRIPTION(org.thingsboard.server.controller.ControllerConstants.ENTITY_VIEW_TEXT_SEARCH_DESCRIPTION) PAGE_NUMBER_DESCRIPTION(org.thingsboard.server.controller.ControllerConstants.PAGE_NUMBER_DESCRIPTION) CustomerId(org.thingsboard.server.common.data.id.CustomerId) ModelConstants(org.thingsboard.server.dao.model.ModelConstants) SORT_PROPERTY_DESCRIPTION(org.thingsboard.server.controller.ControllerConstants.SORT_PROPERTY_DESCRIPTION) MoreExecutors(com.google.common.util.concurrent.MoreExecutors) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) ENTITY_VIEW_INFO_DESCRIPTION(org.thingsboard.server.controller.ControllerConstants.ENTITY_VIEW_INFO_DESCRIPTION) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ENTITY_VIEW_INFO_SORT_PROPERTY_ALLOWABLE_VALUES(org.thingsboard.server.controller.ControllerConstants.ENTITY_VIEW_INFO_SORT_PROPERTY_ALLOWABLE_VALUES) ArrayList(java.util.ArrayList) RequestBody(org.springframework.web.bind.annotation.RequestBody) ENTITY_VIEW_ID_PARAM_DESCRIPTION(org.thingsboard.server.controller.ControllerConstants.ENTITY_VIEW_ID_PARAM_DESCRIPTION) ActionType(org.thingsboard.server.common.data.audit.ActionType) TbCoreComponent(org.thingsboard.server.queue.util.TbCoreComponent) PAGE_SIZE_DESCRIPTION(org.thingsboard.server.controller.ControllerConstants.PAGE_SIZE_DESCRIPTION) EntityId(org.thingsboard.server.common.data.id.EntityId) MODEL_DESCRIPTION(org.thingsboard.server.controller.ControllerConstants.MODEL_DESCRIPTION) EDGE_ID(org.thingsboard.server.controller.EdgeController.EDGE_ID) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) Nullable(javax.annotation.Nullable) DataConstants(org.thingsboard.server.common.data.DataConstants) ThingsboardException(org.thingsboard.server.common.data.exception.ThingsboardException) EDGE_ASSIGN_ASYNC_FIRST_STEP_DESCRIPTION(org.thingsboard.server.controller.ControllerConstants.EDGE_ASSIGN_ASYNC_FIRST_STEP_DESCRIPTION) ResponseBody(org.springframework.web.bind.annotation.ResponseBody) FutureCallback(com.google.common.util.concurrent.FutureCallback) ExecutionException(java.util.concurrent.ExecutionException) HttpStatus(org.springframework.http.HttpStatus) Futures(com.google.common.util.concurrent.Futures) EDGE_ASSIGN_RECEIVE_STEP_DESCRIPTION(org.thingsboard.server.controller.ControllerConstants.EDGE_ASSIGN_RECEIVE_STEP_DESCRIPTION) EDGE_UNASSIGN_ASYNC_FIRST_STEP_DESCRIPTION(org.thingsboard.server.controller.ControllerConstants.EDGE_UNASSIGN_ASYNC_FIRST_STEP_DESCRIPTION) StringUtils.isBlank(org.apache.commons.lang3.StringUtils.isBlank) PageData(org.thingsboard.server.common.data.page.PageData) Resource(org.thingsboard.server.service.security.permission.Resource) ENTITY_VIEW_DESCRIPTION(org.thingsboard.server.controller.ControllerConstants.ENTITY_VIEW_DESCRIPTION) ENTITY_VIEW_SORT_PROPERTY_ALLOWABLE_VALUES(org.thingsboard.server.controller.ControllerConstants.ENTITY_VIEW_SORT_PROPERTY_ALLOWABLE_VALUES) ReadTsKvQuery(org.thingsboard.server.common.data.kv.ReadTsKvQuery) Collections(java.util.Collections) TenantId(org.thingsboard.server.common.data.id.TenantId) PageData(org.thingsboard.server.common.data.page.PageData) EntityView(org.thingsboard.server.common.data.EntityView) EdgeId(org.thingsboard.server.common.data.id.EdgeId) ThingsboardException(org.thingsboard.server.common.data.exception.ThingsboardException) TimePageLink(org.thingsboard.server.common.data.page.TimePageLink) IncorrectParameterException(org.thingsboard.server.dao.exception.IncorrectParameterException) ThingsboardException(org.thingsboard.server.common.data.exception.ThingsboardException) ExecutionException(java.util.concurrent.ExecutionException) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 10 with EntityView

use of org.thingsboard.server.common.data.EntityView in project thingsboard by thingsboard.

the class AbstractEntityService method checkAssignedEntityViewsToEdge.

protected void checkAssignedEntityViewsToEdge(TenantId tenantId, EntityId entityId, EdgeId edgeId) {
    try {
        List<EntityView> entityViews = entityViewService.findEntityViewsByTenantIdAndEntityIdAsync(tenantId, entityId).get();
        if (entityViews != null && !entityViews.isEmpty()) {
            EntityView entityView = entityViews.get(0);
            // TODO: @voba - refactor this blocking operation
            Boolean relationExists = relationService.checkRelation(tenantId, edgeId, entityView.getId(), EntityRelation.CONTAINS_TYPE, RelationTypeGroup.EDGE).get();
            if (relationExists) {
                throw new DataValidationException("Can't unassign device/asset from edge that is related to entity view and entity view is assigned to edge!");
            }
        }
    } catch (Exception e) {
        log.error("[{}] Exception while finding entity views for entityId [{}]", tenantId, entityId, e);
        throw new RuntimeException("Exception while finding entity views for entityId [" + entityId + "]", e);
    }
}
Also used : DataValidationException(org.thingsboard.server.dao.exception.DataValidationException) EntityView(org.thingsboard.server.common.data.EntityView) ConstraintViolationException(org.hibernate.exception.ConstraintViolationException) DataValidationException(org.thingsboard.server.dao.exception.DataValidationException)

Aggregations

EntityView (org.thingsboard.server.common.data.EntityView)51 TelemetryEntityView (org.thingsboard.server.common.data.objects.TelemetryEntityView)21 Test (org.junit.Test)18 AttributesEntityView (org.thingsboard.server.common.data.objects.AttributesEntityView)17 ExecutionException (java.util.concurrent.ExecutionException)16 EntityViewId (org.thingsboard.server.common.data.id.EntityViewId)15 ArrayList (java.util.ArrayList)13 IncorrectParameterException (org.thingsboard.server.dao.exception.IncorrectParameterException)12 Customer (org.thingsboard.server.common.data.Customer)11 ThingsboardException (org.thingsboard.server.common.data.exception.ThingsboardException)11 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)10 ApiOperation (io.swagger.annotations.ApiOperation)9 List (java.util.List)9 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)9 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)9 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)8 Edge (org.thingsboard.server.common.data.edge.Edge)8 PageLink (org.thingsboard.server.common.data.page.PageLink)7 DataValidationException (org.thingsboard.server.dao.exception.DataValidationException)7 Matchers.containsString (org.hamcrest.Matchers.containsString)6