Showing
5 changed files
with
40 additions
and
12 deletions
@@ -318,7 +318,7 @@ public abstract class BaseController { | @@ -318,7 +318,7 @@ public abstract class BaseController { | ||
318 | checkUserId(new UserId(entityId.getId())); | 318 | checkUserId(new UserId(entityId.getId())); |
319 | return; | 319 | return; |
320 | case ENTITY_VIEW: | 320 | case ENTITY_VIEW: |
321 | - checkEntityViewId(entityViewService.findEntityViewById(new EntityViewId(entityId.getId()))); | 321 | + checkEntityView(entityViewService.findEntityViewById(new EntityViewId(entityId.getId()))); |
322 | return; | 322 | return; |
323 | default: | 323 | default: |
324 | throw new IllegalArgumentException("Unsupported entity type: " + entityId.getEntityType()); | 324 | throw new IllegalArgumentException("Unsupported entity type: " + entityId.getEntityType()); |
@@ -351,14 +351,14 @@ public abstract class BaseController { | @@ -351,14 +351,14 @@ public abstract class BaseController { | ||
351 | try { | 351 | try { |
352 | validateId(entityViewId, "Incorrect entityViewId " + entityViewId); | 352 | validateId(entityViewId, "Incorrect entityViewId " + entityViewId); |
353 | EntityView entityView = entityViewService.findEntityViewById(entityViewId); | 353 | EntityView entityView = entityViewService.findEntityViewById(entityViewId); |
354 | - checkEntityViewId(entityView); | 354 | + checkEntityView(entityView); |
355 | return entityView; | 355 | return entityView; |
356 | } catch (Exception e) { | 356 | } catch (Exception e) { |
357 | throw handleException(e, false); | 357 | throw handleException(e, false); |
358 | } | 358 | } |
359 | } | 359 | } |
360 | 360 | ||
361 | - protected void checkEntityViewId(EntityView entityView) throws ThingsboardException { | 361 | + protected void checkEntityView(EntityView entityView) throws ThingsboardException { |
362 | checkNotNull(entityView); | 362 | checkNotNull(entityView); |
363 | checkTenantId(entityView.getTenantId()); | 363 | checkTenantId(entityView.getTenantId()); |
364 | if (entityView.getCustomerId() != null && !entityView.getCustomerId().getId().equals(ModelConstants.NULL_UUID)) { | 364 | if (entityView.getCustomerId() != null && !entityView.getCustomerId().getId().equals(ModelConstants.NULL_UUID)) { |
@@ -204,7 +204,7 @@ public class EntityViewController extends BaseController { | @@ -204,7 +204,7 @@ public class EntityViewController extends BaseController { | ||
204 | List<EntityView> entityViews = checkNotNull(entityViewService.findEntityViewsByQuery(query).get()); | 204 | List<EntityView> entityViews = checkNotNull(entityViewService.findEntityViewsByQuery(query).get()); |
205 | entityViews = entityViews.stream().filter(entityView -> { | 205 | entityViews = entityViews.stream().filter(entityView -> { |
206 | try { | 206 | try { |
207 | - checkEntityViewId(entityView); | 207 | + checkEntityView(entityView); |
208 | return true; | 208 | return true; |
209 | } catch (ThingsboardException e) { | 209 | } catch (ThingsboardException e) { |
210 | return false; | 210 | return false; |
@@ -24,7 +24,7 @@ import java.util.Arrays; | @@ -24,7 +24,7 @@ import java.util.Arrays; | ||
24 | 24 | ||
25 | @RunWith(ClasspathSuite.class) | 25 | @RunWith(ClasspathSuite.class) |
26 | @ClasspathSuite.ClassnameFilters({ | 26 | @ClasspathSuite.ClassnameFilters({ |
27 | - "org.thingsboard.server.controller.sql.EntityViewControllerSqlTest", | 27 | + "org.thingsboard.server.controller.sql.*Test", |
28 | }) | 28 | }) |
29 | public class ControllerSqlTestSuite { | 29 | public class ControllerSqlTestSuite { |
30 | 30 |
@@ -15,6 +15,7 @@ | @@ -15,6 +15,7 @@ | ||
15 | */ | 15 | */ |
16 | package org.thingsboard.server.dao.entityview; | 16 | package org.thingsboard.server.dao.entityview; |
17 | 17 | ||
18 | +import com.google.common.util.concurrent.FutureCallback; | ||
18 | import com.google.common.util.concurrent.Futures; | 19 | import com.google.common.util.concurrent.Futures; |
19 | import com.google.common.util.concurrent.ListenableFuture; | 20 | import com.google.common.util.concurrent.ListenableFuture; |
20 | import lombok.extern.slf4j.Slf4j; | 21 | import lombok.extern.slf4j.Slf4j; |
@@ -49,6 +50,7 @@ import org.thingsboard.server.dao.service.DataValidator; | @@ -49,6 +50,7 @@ import org.thingsboard.server.dao.service.DataValidator; | ||
49 | import org.thingsboard.server.dao.service.PaginatedRemover; | 50 | import org.thingsboard.server.dao.service.PaginatedRemover; |
50 | import org.thingsboard.server.dao.tenant.TenantDao; | 51 | import org.thingsboard.server.dao.tenant.TenantDao; |
51 | 52 | ||
53 | +import javax.annotation.Nullable; | ||
52 | import java.util.ArrayList; | 54 | import java.util.ArrayList; |
53 | import java.util.Arrays; | 55 | import java.util.Arrays; |
54 | import java.util.Collection; | 56 | import java.util.Collection; |
@@ -57,6 +59,7 @@ import java.util.concurrent.ExecutionException; | @@ -57,6 +59,7 @@ import java.util.concurrent.ExecutionException; | ||
57 | import java.util.stream.Collectors; | 59 | import java.util.stream.Collectors; |
58 | 60 | ||
59 | import static org.thingsboard.server.common.data.CacheConstants.ENTITY_VIEW_CACHE; | 61 | import static org.thingsboard.server.common.data.CacheConstants.ENTITY_VIEW_CACHE; |
62 | +import static org.thingsboard.server.common.data.CacheConstants.RELATIONS_CACHE; | ||
60 | import static org.thingsboard.server.dao.model.ModelConstants.NULL_UUID; | 63 | import static org.thingsboard.server.dao.model.ModelConstants.NULL_UUID; |
61 | import static org.thingsboard.server.dao.service.Validator.validateId; | 64 | import static org.thingsboard.server.dao.service.Validator.validateId; |
62 | import static org.thingsboard.server.dao.service.Validator.validatePageLink; | 65 | import static org.thingsboard.server.dao.service.Validator.validatePageLink; |
@@ -197,7 +200,30 @@ public class EntityViewServiceImpl extends AbstractEntityService implements Enti | @@ -197,7 +200,30 @@ public class EntityViewServiceImpl extends AbstractEntityService implements Enti | ||
197 | log.trace("Executing findEntityViewsByTenantIdAndEntityIdAsync, tenantId [{}], entityId [{}]", tenantId, entityId); | 200 | log.trace("Executing findEntityViewsByTenantIdAndEntityIdAsync, tenantId [{}], entityId [{}]", tenantId, entityId); |
198 | validateId(tenantId, INCORRECT_TENANT_ID + tenantId); | 201 | validateId(tenantId, INCORRECT_TENANT_ID + tenantId); |
199 | validateId(entityId.getId(), "Incorrect entityId" + entityId); | 202 | validateId(entityId.getId(), "Incorrect entityId" + entityId); |
200 | - return entityViewDao.findEntityViewsByTenantIdAndEntityIdAsync(tenantId.getId(), entityId.getId()); | 203 | + |
204 | + List<Object> tenantIdAndEntityId = new ArrayList<>(); | ||
205 | + tenantIdAndEntityId.add(tenantId); | ||
206 | + tenantIdAndEntityId.add(entityId); | ||
207 | + | ||
208 | + Cache cache = cacheManager.getCache(ENTITY_VIEW_CACHE); | ||
209 | + List<EntityView> fromCache = cache.get(tenantIdAndEntityId, List.class); | ||
210 | + if (fromCache != null) { | ||
211 | + return Futures.immediateFuture(fromCache); | ||
212 | + } else { | ||
213 | + ListenableFuture<List<EntityView>> entityViewsFuture = entityViewDao.findEntityViewsByTenantIdAndEntityIdAsync(tenantId.getId(), entityId.getId()); | ||
214 | + Futures.addCallback(entityViewsFuture, | ||
215 | + new FutureCallback<List<EntityView>>() { | ||
216 | + @Override | ||
217 | + public void onSuccess(@Nullable List<EntityView> result) { | ||
218 | + cache.putIfAbsent(tenantIdAndEntityId, result); | ||
219 | + } | ||
220 | + @Override | ||
221 | + public void onFailure(Throwable t) { | ||
222 | + log.error("Error while finding entity views by tenantId and entityId", t); | ||
223 | + } | ||
224 | + }); | ||
225 | + return entityViewsFuture; | ||
226 | + } | ||
201 | } | 227 | } |
202 | 228 | ||
203 | @CacheEvict(cacheNames = ENTITY_VIEW_CACHE, key = "{#entityViewId}") | 229 | @CacheEvict(cacheNames = ENTITY_VIEW_CACHE, key = "{#entityViewId}") |
@@ -206,9 +232,8 @@ public class EntityViewServiceImpl extends AbstractEntityService implements Enti | @@ -206,9 +232,8 @@ public class EntityViewServiceImpl extends AbstractEntityService implements Enti | ||
206 | log.trace("Executing deleteEntityView [{}]", entityViewId); | 232 | log.trace("Executing deleteEntityView [{}]", entityViewId); |
207 | validateId(entityViewId, INCORRECT_ENTITY_VIEW_ID + entityViewId); | 233 | validateId(entityViewId, INCORRECT_ENTITY_VIEW_ID + entityViewId); |
208 | deleteEntityRelations(entityViewId); | 234 | deleteEntityRelations(entityViewId); |
209 | - Cache cache = cacheManager.getCache(ENTITY_VIEW_CACHE); | ||
210 | EntityView entityView = entityViewDao.findById(entityViewId.getId()); | 235 | EntityView entityView = entityViewDao.findById(entityViewId.getId()); |
211 | - cache.evict(Arrays.asList(entityView.getTenantId(), entityView.getEntityId())); | 236 | + cacheManager.getCache(ENTITY_VIEW_CACHE).evict(Arrays.asList(entityView.getTenantId(), entityView.getEntityId())); |
212 | entityViewDao.removeById(entityViewId.getId()); | 237 | entityViewDao.removeById(entityViewId.getId()); |
213 | } | 238 | } |
214 | 239 |
@@ -42,6 +42,7 @@ import java.util.Set; | @@ -42,6 +42,7 @@ import java.util.Set; | ||
42 | import java.util.concurrent.ExecutionException; | 42 | import java.util.concurrent.ExecutionException; |
43 | import java.util.stream.Collectors; | 43 | import java.util.stream.Collectors; |
44 | 44 | ||
45 | +import static org.thingsboard.rule.engine.api.TbRelationTypes.FAILURE; | ||
45 | import static org.thingsboard.rule.engine.api.TbRelationTypes.SUCCESS; | 46 | import static org.thingsboard.rule.engine.api.TbRelationTypes.SUCCESS; |
46 | 47 | ||
47 | @Slf4j | 48 | @Slf4j |
@@ -49,9 +50,10 @@ import static org.thingsboard.rule.engine.api.TbRelationTypes.SUCCESS; | @@ -49,9 +50,10 @@ import static org.thingsboard.rule.engine.api.TbRelationTypes.SUCCESS; | ||
49 | type = ComponentType.ACTION, | 50 | type = ComponentType.ACTION, |
50 | name = "copy attributes", | 51 | name = "copy attributes", |
51 | configClazz = EmptyNodeConfiguration.class, | 52 | configClazz = EmptyNodeConfiguration.class, |
52 | - nodeDescription = "Copy attributes from asset/device to entity view", | 53 | + nodeDescription = "Copy attributes from asset/device to entity view and changes message originator to related entity view", |
53 | nodeDetails = "Copy attributes from asset/device to related entity view according to entity view configuration. \n " + | 54 | nodeDetails = "Copy attributes from asset/device to related entity view according to entity view configuration. \n " + |
54 | - "Copy will be done only for attributes that are between start and end dates and according to attribute keys configuration", | 55 | + "Copy will be done only for attributes that are between start and end dates and according to attribute keys configuration. \n" + |
56 | + "Changes message originator to related entity view and produces new messages according to count of updated entity views", | ||
55 | uiResources = {"static/rulenode/rulenode-core-config.js"}, | 57 | uiResources = {"static/rulenode/rulenode-core-config.js"}, |
56 | configDirective = "tbNodeEmptyConfig", | 58 | configDirective = "tbNodeEmptyConfig", |
57 | icon = "content_copy" | 59 | icon = "content_copy" |
@@ -110,7 +112,8 @@ public class TbCopyAttributesToEntityViewNode implements TbNode { | @@ -110,7 +112,8 @@ public class TbCopyAttributesToEntityViewNode implements TbNode { | ||
110 | new FutureCallback<Void>() { | 112 | new FutureCallback<Void>() { |
111 | @Override | 113 | @Override |
112 | public void onSuccess(@Nullable Void result) { | 114 | public void onSuccess(@Nullable Void result) { |
113 | - ctx.tellNext(msg, SUCCESS); | 115 | + TbMsg updMsg = ctx.transformMsg(msg, msg.getType(), entityView.getId(), msg.getMetaData(), msg.getData()); |
116 | + ctx.tellNext(updMsg, SUCCESS); | ||
114 | } | 117 | } |
115 | 118 | ||
116 | @Override | 119 | @Override |
@@ -123,7 +126,7 @@ public class TbCopyAttributesToEntityViewNode implements TbNode { | @@ -123,7 +126,7 @@ public class TbCopyAttributesToEntityViewNode implements TbNode { | ||
123 | }, | 126 | }, |
124 | t -> ctx.tellFailure(msg, t)); | 127 | t -> ctx.tellFailure(msg, t)); |
125 | } else { | 128 | } else { |
126 | - ctx.tellNext(msg, TbRelationTypes.FAILURE); | 129 | + ctx.tellNext(msg, FAILURE); |
127 | } | 130 | } |
128 | } | 131 | } |
129 | 132 |