Commit 7bcb64958dd325e3ac401f57c4ebffb1418cc379

Authored by viktorbasanets
1 parent 2269656b

Fixed

... ... @@ -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.*Test",
  27 + "org.thingsboard.server.controller.sql.EntityViewControllerSqlTest",
28 28 })
29 29 public class ControllerSqlTestSuite {
30 30
... ...
... ... @@ -97,10 +97,19 @@ public class EntityViewServiceImpl extends AbstractEntityService implements Enti
97 97 log.trace("Executing save entity view [{}]", entityView);
98 98 entityViewValidator.validate(entityView);
99 99 EntityView savedEntityView = entityViewDao.save(entityView);
  100 + List<ListenableFuture<List<Void>>> futures = new ArrayList<>();
100 101 if (savedEntityView.getKeys() != null) {
101   - copyAttributesFromEntityToEntityView(savedEntityView, DataConstants.CLIENT_SCOPE, savedEntityView.getKeys().getAttributes().getCs());
102   - copyAttributesFromEntityToEntityView(savedEntityView, DataConstants.SERVER_SCOPE, savedEntityView.getKeys().getAttributes().getSs());
103   - copyAttributesFromEntityToEntityView(savedEntityView, DataConstants.SHARED_SCOPE, savedEntityView.getKeys().getAttributes().getSh());
  102 + futures.add(copyAttributesFromEntityToEntityView(savedEntityView, DataConstants.CLIENT_SCOPE, savedEntityView.getKeys().getAttributes().getCs()));
  103 + futures.add(copyAttributesFromEntityToEntityView(savedEntityView, DataConstants.SERVER_SCOPE, savedEntityView.getKeys().getAttributes().getSs()));
  104 + futures.add(copyAttributesFromEntityToEntityView(savedEntityView, DataConstants.SHARED_SCOPE, savedEntityView.getKeys().getAttributes().getSh()));
  105 + }
  106 + for (ListenableFuture<List<Void>> future : futures) {
  107 + try {
  108 + future.get();
  109 + } catch (InterruptedException | ExecutionException e) {
  110 + log.error("Failed to copy attributes to entity view", e);
  111 + throw new RuntimeException("Failed to copy attributes to entity view", e);
  112 + }
104 113 }
105 114 return savedEntityView;
106 115 }
... ... @@ -169,9 +178,9 @@ public class EntityViewServiceImpl extends AbstractEntityService implements Enti
169 178
170 179 @Override
171 180 public TextPageData<EntityView> findEntityViewsByTenantIdAndCustomerId(TenantId tenantId, CustomerId customerId,
172   - TextPageLink pageLink) {
  181 + TextPageLink pageLink) {
173 182 log.trace("Executing findEntityViewByTenantIdAndCustomerId, tenantId [{}], customerId [{}]," +
174   - " pageLink [{}]", tenantId, customerId, pageLink);
  183 + " pageLink [{}]", tenantId, customerId, pageLink);
175 184 validateId(tenantId, INCORRECT_TENANT_ID + tenantId);
176 185 validateId(customerId, INCORRECT_CUSTOMER_ID + customerId);
177 186 validatePageLink(pageLink, INCORRECT_PAGE_LINK + pageLink);
... ... @@ -246,7 +255,7 @@ public class EntityViewServiceImpl extends AbstractEntityService implements Enti
246 255 log.trace("Executing deleteEntityViewsByTenantId, tenantId [{}]", tenantId);
247 256 validateId(tenantId, INCORRECT_TENANT_ID + tenantId);
248 257 entityViewDao.findEntityViewsByTenantId(tenantId.getId(), new TextPageLink(DEFAULT_LIMIT)).stream()
249   - .map(view -> view.getId())
  258 + .map(view -> view.getId())
250 259 .collect(Collectors.toList())
251 260 .forEach(id -> cacheEvict(id, cacheManager.getCache(ENTITY_VIEW_CACHE)));
252 261 tenantEntityViewRemover.removeEntities(tenantId);
... ... @@ -258,38 +267,37 @@ public class EntityViewServiceImpl extends AbstractEntityService implements Enti
258 267 cache.evict(Arrays.asList(entityView.getTenantId(), entityView.getEntityId()));
259 268 }
260 269
261   - private void copyAttributesFromEntityToEntityView(EntityView entityView, String scope, Collection<String> keys) {
  270 + private ListenableFuture<List<Void>> copyAttributesFromEntityToEntityView(EntityView entityView, String scope, Collection<String> keys) {
262 271 if (keys != null && !keys.isEmpty()) {
263 272 ListenableFuture<List<AttributeKvEntry>> getAttrFuture = attributesService.find(entityView.getEntityId(), scope, keys);
264   - Futures.addCallback(getAttrFuture, new FutureCallback<List<AttributeKvEntry>>() {
265   - @Override
266   - public void onSuccess(@Nullable List<AttributeKvEntry> attributeKvEntries) {
267   - if (attributeKvEntries != null && !attributeKvEntries.isEmpty()) {
268   - List<AttributeKvEntry> filteredAttributes =
269   - attributeKvEntries.stream()
270   - .filter(attributeKvEntry -> {
271   - if (entityView.getStartTimeMs() == 0 && entityView.getEndTimeMs() == 0) {
272   - return true;
273   - }
274   - if (entityView.getEndTimeMs() == 0 && entityView.getStartTimeMs() < attributeKvEntry.getLastUpdateTs()) {
275   - return true;
276   - }
277   - if (entityView.getStartTimeMs() == 0 && entityView.getEndTimeMs() > attributeKvEntry.getLastUpdateTs()) {
278   - return true;
279   - }
280   - return entityView.getStartTimeMs() < attributeKvEntry.getLastUpdateTs()
281   - && entityView.getEndTimeMs() > attributeKvEntry.getLastUpdateTs();
282   - }).collect(Collectors.toList());
283   - attributesService.save(entityView.getId(), scope, filteredAttributes);
284   - }
  273 + return Futures.transform(getAttrFuture, attributeKvEntries -> {
  274 + List<AttributeKvEntry> filteredAttributes = new ArrayList<>();
  275 + if (attributeKvEntries != null && !attributeKvEntries.isEmpty()) {
  276 + filteredAttributes =
  277 + attributeKvEntries.stream()
  278 + .filter(attributeKvEntry -> {
  279 + if (entityView.getStartTimeMs() == 0 && entityView.getEndTimeMs() == 0) {
  280 + return true;
  281 + }
  282 + if (entityView.getEndTimeMs() == 0 && entityView.getStartTimeMs() < attributeKvEntry.getLastUpdateTs()) {
  283 + return true;
  284 + }
  285 + if (entityView.getStartTimeMs() == 0 && entityView.getEndTimeMs() > attributeKvEntry.getLastUpdateTs()) {
  286 + return true;
  287 + }
  288 + return entityView.getStartTimeMs() < attributeKvEntry.getLastUpdateTs()
  289 + && entityView.getEndTimeMs() > attributeKvEntry.getLastUpdateTs();
  290 + }).collect(Collectors.toList());
285 291 }
286   -
287   - @Override
288   - public void onFailure(Throwable throwable) {
289   - log.error("Failed to fetch [{}] attributes [{}] for [{}] entity [{}]",
290   - scope, keys, entityView.getEntityId().getEntityType().name(), entityView.getEntityId().getId().toString(), throwable);
  292 + try {
  293 + return attributesService.save(entityView.getId(), scope, filteredAttributes).get();
  294 + } catch (InterruptedException | ExecutionException e) {
  295 + log.error("Failed to copy attributes to entity view", e);
  296 + throw new RuntimeException("Failed to copy attributes to entity view", e);
291 297 }
292 298 });
  299 + } else {
  300 + return Futures.immediateFuture(null);
293 301 }
294 302 }
295 303
... ... @@ -299,7 +307,7 @@ public class EntityViewServiceImpl extends AbstractEntityService implements Enti
299 307 @Override
300 308 protected void validateCreate(EntityView entityView) {
301 309 entityViewDao.findEntityViewByTenantIdAndName(entityView.getTenantId().getId(), entityView.getName())
302   - .ifPresent( e -> {
  310 + .ifPresent(e -> {
303 311 throw new DataValidationException("Entity view with such name already exists!");
304 312 });
305 313 }
... ... @@ -307,7 +315,7 @@ public class EntityViewServiceImpl extends AbstractEntityService implements Enti
307 315 @Override
308 316 protected void validateUpdate(EntityView entityView) {
309 317 entityViewDao.findEntityViewByTenantIdAndName(entityView.getTenantId().getId(), entityView.getName())
310   - .ifPresent( e -> {
  318 + .ifPresent(e -> {
311 319 if (!e.getUuidId().equals(entityView.getUuidId())) {
312 320 throw new DataValidationException("Entity view with such name already exists!");
313 321 }
... ...