Browse Source

refactored due to comments

pull/11861/head
YevhenBondarenko 2 years ago
parent
commit
7f2895a5ce
  1. 22
      dao/src/main/java/org/thingsboard/server/dao/entity/AbstractEntityService.java
  2. 20
      dao/src/main/java/org/thingsboard/server/dao/rule/BaseRuleChainService.java

22
dao/src/main/java/org/thingsboard/server/dao/entity/AbstractEntityService.java

@ -18,9 +18,11 @@ package org.thingsboard.server.dao.entity;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.exception.ConstraintViolationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.annotation.Lazy;
import org.thingsboard.server.common.data.EntityView;
import org.thingsboard.server.common.data.HasDebugMode;
import org.thingsboard.server.common.data.StringUtils;
import org.thingsboard.server.common.data.id.EdgeId;
import org.thingsboard.server.common.data.id.EntityId;
@ -33,11 +35,13 @@ import org.thingsboard.server.dao.entityview.EntityViewService;
import org.thingsboard.server.dao.exception.DataValidationException;
import org.thingsboard.server.dao.housekeeper.CleanUpService;
import org.thingsboard.server.dao.relation.RelationService;
import org.thingsboard.server.dao.tenant.TbTenantProfileCache;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
@Slf4j
public abstract class AbstractEntityService {
@ -68,6 +72,13 @@ public abstract class AbstractEntityService {
@Lazy
protected CleanUpService cleanUpService;
@Autowired
@Lazy
private TbTenantProfileCache tbTenantProfileCache;
@Value("${debug_mode.max_duration:15}")
private int maxDebugModeDurationMinutes;
protected void createRelation(TenantId tenantId, EntityRelation relation) {
log.debug("Creating relation: {}", relation);
relationService.saveRelation(tenantId, relation);
@ -124,4 +135,15 @@ public abstract class AbstractEntityService {
}
}
}
protected void setDebugAllUntil(TenantId tenantId, HasDebugMode entity, long now) {
int debugDuration = tbTenantProfileCache.get(tenantId).getDefaultProfileConfiguration().getMaxDebugModeDurationMinutes(maxDebugModeDurationMinutes);
long debugUntil = now + TimeUnit.MINUTES.toMillis(debugDuration);
if (entity.isDebugAll()) {
entity.setDebugAllUntil(debugUntil);
} else if (entity.getDebugAllUntil() > debugUntil) {
throw new DataValidationException("Unable to update 'debugAllUntil' property. To reset the debug duration, please modify the 'debugAll' property instead.");
}
}
}

20
dao/src/main/java/org/thingsboard/server/dao/rule/BaseRuleChainService.java

@ -24,8 +24,6 @@ import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.hibernate.exception.ConstraintViolationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.thingsboard.common.util.JacksonUtil;
@ -67,7 +65,6 @@ import org.thingsboard.server.dao.service.DataValidator;
import org.thingsboard.server.dao.service.PaginatedRemover;
import org.thingsboard.server.dao.service.Validator;
import org.thingsboard.server.dao.service.validator.RuleChainDataValidator;
import org.thingsboard.server.dao.tenant.TbTenantProfileCache;
import java.util.ArrayList;
import java.util.Collection;
@ -79,7 +76,6 @@ import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import java.util.stream.Collectors;
@ -113,13 +109,6 @@ public class BaseRuleChainService extends AbstractEntityService implements RuleC
@Autowired
private DataValidator<RuleChain> ruleChainValidator;
@Autowired
@Lazy
private TbTenantProfileCache tbTenantProfileCache;
@Value("${debug_mode.max_duration:15}")
private int maxDebugModeDurationMinutes;
@Override
@Transactional
public RuleChain saveRuleChain(RuleChain ruleChain) {
@ -230,14 +219,7 @@ public class BaseRuleChainService extends AbstractEntityService implements RuleC
node.setRuleChainId(ruleChainId);
node = ruleNodeUpdater.apply(node);
int debugDuration = tbTenantProfileCache.get(tenantId).getDefaultProfileConfiguration().getMaxDebugModeDurationMinutes(maxDebugModeDurationMinutes);
long debugUntil = now + TimeUnit.MINUTES.toMillis(debugDuration);
if (node.isDebugAll()) {
node.setDebugAllUntil(debugUntil);
} else if (node.getDebugAllUntil() > debugUntil) {
throw new DataValidationException("Unable to update 'debugAllUntil' property. To reset the debug duration, please modify the 'debugAll' property instead.");
}
setDebugAllUntil(tenantId, node, now);
RuleChainDataValidator.validateRuleNode(node);
RuleNode savedNode = ruleNodeDao.save(tenantId, node);

Loading…
Cancel
Save