Showing
5 changed files
with
40 additions
and
12 deletions
... | ... | @@ -318,7 +318,7 @@ public abstract class BaseController { |
318 | 318 | checkUserId(new UserId(entityId.getId())); |
319 | 319 | return; |
320 | 320 | case ENTITY_VIEW: |
321 | - checkEntityViewId(entityViewService.findEntityViewById(new EntityViewId(entityId.getId()))); | |
321 | + checkEntityView(entityViewService.findEntityViewById(new EntityViewId(entityId.getId()))); | |
322 | 322 | return; |
323 | 323 | default: |
324 | 324 | throw new IllegalArgumentException("Unsupported entity type: " + entityId.getEntityType()); |
... | ... | @@ -351,14 +351,14 @@ public abstract class BaseController { |
351 | 351 | try { |
352 | 352 | validateId(entityViewId, "Incorrect entityViewId " + entityViewId); |
353 | 353 | EntityView entityView = entityViewService.findEntityViewById(entityViewId); |
354 | - checkEntityViewId(entityView); | |
354 | + checkEntityView(entityView); | |
355 | 355 | return entityView; |
356 | 356 | } catch (Exception e) { |
357 | 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 | 362 | checkNotNull(entityView); |
363 | 363 | checkTenantId(entityView.getTenantId()); |
364 | 364 | if (entityView.getCustomerId() != null && !entityView.getCustomerId().getId().equals(ModelConstants.NULL_UUID)) { | ... | ... |
... | ... | @@ -204,7 +204,7 @@ public class EntityViewController extends BaseController { |
204 | 204 | List<EntityView> entityViews = checkNotNull(entityViewService.findEntityViewsByQuery(query).get()); |
205 | 205 | entityViews = entityViews.stream().filter(entityView -> { |
206 | 206 | try { |
207 | - checkEntityViewId(entityView); | |
207 | + checkEntityView(entityView); | |
208 | 208 | return true; |
209 | 209 | } catch (ThingsboardException e) { |
210 | 210 | return false; | ... | ... |
... | ... | @@ -24,7 +24,7 @@ import java.util.Arrays; |
24 | 24 | |
25 | 25 | @RunWith(ClasspathSuite.class) |
26 | 26 | @ClasspathSuite.ClassnameFilters({ |
27 | - "org.thingsboard.server.controller.sql.EntityViewControllerSqlTest", | |
27 | + "org.thingsboard.server.controller.sql.*Test", | |
28 | 28 | }) |
29 | 29 | public class ControllerSqlTestSuite { |
30 | 30 | ... | ... |
... | ... | @@ -15,6 +15,7 @@ |
15 | 15 | */ |
16 | 16 | package org.thingsboard.server.dao.entityview; |
17 | 17 | |
18 | +import com.google.common.util.concurrent.FutureCallback; | |
18 | 19 | import com.google.common.util.concurrent.Futures; |
19 | 20 | import com.google.common.util.concurrent.ListenableFuture; |
20 | 21 | import lombok.extern.slf4j.Slf4j; |
... | ... | @@ -49,6 +50,7 @@ import org.thingsboard.server.dao.service.DataValidator; |
49 | 50 | import org.thingsboard.server.dao.service.PaginatedRemover; |
50 | 51 | import org.thingsboard.server.dao.tenant.TenantDao; |
51 | 52 | |
53 | +import javax.annotation.Nullable; | |
52 | 54 | import java.util.ArrayList; |
53 | 55 | import java.util.Arrays; |
54 | 56 | import java.util.Collection; |
... | ... | @@ -57,6 +59,7 @@ import java.util.concurrent.ExecutionException; |
57 | 59 | import java.util.stream.Collectors; |
58 | 60 | |
59 | 61 | import static org.thingsboard.server.common.data.CacheConstants.ENTITY_VIEW_CACHE; |
62 | +import static org.thingsboard.server.common.data.CacheConstants.RELATIONS_CACHE; | |
60 | 63 | import static org.thingsboard.server.dao.model.ModelConstants.NULL_UUID; |
61 | 64 | import static org.thingsboard.server.dao.service.Validator.validateId; |
62 | 65 | import static org.thingsboard.server.dao.service.Validator.validatePageLink; |
... | ... | @@ -197,7 +200,30 @@ public class EntityViewServiceImpl extends AbstractEntityService implements Enti |
197 | 200 | log.trace("Executing findEntityViewsByTenantIdAndEntityIdAsync, tenantId [{}], entityId [{}]", tenantId, entityId); |
198 | 201 | validateId(tenantId, INCORRECT_TENANT_ID + tenantId); |
199 | 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 | 229 | @CacheEvict(cacheNames = ENTITY_VIEW_CACHE, key = "{#entityViewId}") |
... | ... | @@ -206,9 +232,8 @@ public class EntityViewServiceImpl extends AbstractEntityService implements Enti |
206 | 232 | log.trace("Executing deleteEntityView [{}]", entityViewId); |
207 | 233 | validateId(entityViewId, INCORRECT_ENTITY_VIEW_ID + entityViewId); |
208 | 234 | deleteEntityRelations(entityViewId); |
209 | - Cache cache = cacheManager.getCache(ENTITY_VIEW_CACHE); | |
210 | 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 | 237 | entityViewDao.removeById(entityViewId.getId()); |
213 | 238 | } |
214 | 239 | ... | ... |
... | ... | @@ -42,6 +42,7 @@ import java.util.Set; |
42 | 42 | import java.util.concurrent.ExecutionException; |
43 | 43 | import java.util.stream.Collectors; |
44 | 44 | |
45 | +import static org.thingsboard.rule.engine.api.TbRelationTypes.FAILURE; | |
45 | 46 | import static org.thingsboard.rule.engine.api.TbRelationTypes.SUCCESS; |
46 | 47 | |
47 | 48 | @Slf4j |
... | ... | @@ -49,9 +50,10 @@ import static org.thingsboard.rule.engine.api.TbRelationTypes.SUCCESS; |
49 | 50 | type = ComponentType.ACTION, |
50 | 51 | name = "copy attributes", |
51 | 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 | 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 | 57 | uiResources = {"static/rulenode/rulenode-core-config.js"}, |
56 | 58 | configDirective = "tbNodeEmptyConfig", |
57 | 59 | icon = "content_copy" |
... | ... | @@ -110,7 +112,8 @@ public class TbCopyAttributesToEntityViewNode implements TbNode { |
110 | 112 | new FutureCallback<Void>() { |
111 | 113 | @Override |
112 | 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 | 119 | @Override |
... | ... | @@ -123,7 +126,7 @@ public class TbCopyAttributesToEntityViewNode implements TbNode { |
123 | 126 | }, |
124 | 127 | t -> ctx.tellFailure(msg, t)); |
125 | 128 | } else { |
126 | - ctx.tellNext(msg, TbRelationTypes.FAILURE); | |
129 | + ctx.tellNext(msg, FAILURE); | |
127 | 130 | } |
128 | 131 | } |
129 | 132 | ... | ... |