Refactored DAO to remove additional call to relation table - use spring query
This commit is contained in:
parent
96ed8f1691
commit
71791c6847
@ -70,6 +70,7 @@ import org.thingsboard.server.service.script.RuleNodeJsScriptEngine;
|
||||
import org.thingsboard.server.service.security.permission.Operation;
|
||||
import org.thingsboard.server.service.security.permission.Resource;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
@ -85,6 +86,8 @@ public class RuleChainController extends BaseController {
|
||||
public static final String RULE_CHAIN_ID = "ruleChainId";
|
||||
public static final String RULE_NODE_ID = "ruleNodeId";
|
||||
|
||||
private static final int DEFAULT_LIMIT = 100;
|
||||
|
||||
private static final ObjectMapper objectMapper = new ObjectMapper();
|
||||
|
||||
@Autowired
|
||||
@ -477,7 +480,7 @@ public class RuleChainController extends BaseController {
|
||||
return objectMapper.writeValueAsString(resultNode);
|
||||
}
|
||||
|
||||
private JsonNode convertMsgToOut(TbMsg msg) throws Exception{
|
||||
private JsonNode convertMsgToOut(TbMsg msg) throws Exception {
|
||||
ObjectNode msgData = objectMapper.createObjectNode();
|
||||
if (!StringUtils.isEmpty(msg.getData())) {
|
||||
msgData.set("msg", objectMapper.readTree(msg.getData()));
|
||||
@ -632,13 +635,26 @@ public class RuleChainController extends BaseController {
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: refactor this - add new config to edge rule chain to set it as auto-assign
|
||||
@PreAuthorize("hasAnyAuthority('TENANT_ADMIN')")
|
||||
@RequestMapping(value = "/ruleChain/autoAssignToEdgeRuleChains", method = RequestMethod.GET)
|
||||
@ResponseBody
|
||||
public List<RuleChain> getAutoAssignToEdgeRuleChains() throws ThingsboardException {
|
||||
try {
|
||||
TenantId tenantId = getCurrentUser().getTenantId();
|
||||
return checkNotNull(ruleChainService.findAutoAssignToEdgeRuleChainsByTenantId(tenantId)).get();
|
||||
List<RuleChain> result = new ArrayList<>();
|
||||
PageLink pageLink = new PageLink(DEFAULT_LIMIT);
|
||||
PageData<RuleChain> pageData;
|
||||
do {
|
||||
pageData = ruleChainService.findAutoAssignToEdgeRuleChainsByTenantId(tenantId, pageLink);
|
||||
if (pageData.getData().size() > 0) {
|
||||
result.addAll(pageData.getData());
|
||||
if (pageData.hasNext()) {
|
||||
pageLink = pageLink.nextPageLink();
|
||||
}
|
||||
}
|
||||
} while (pageData.hasNext());
|
||||
return checkNotNull(result);
|
||||
} catch (Exception e) {
|
||||
throw handleException(e);
|
||||
}
|
||||
|
||||
@ -87,6 +87,6 @@ public interface RuleChainService {
|
||||
|
||||
boolean unsetAutoAssignToEdgeRuleChain(TenantId tenantId, RuleChainId ruleChainId);
|
||||
|
||||
ListenableFuture<List<RuleChain>> findAutoAssignToEdgeRuleChainsByTenantId(TenantId tenantId);
|
||||
PageData<RuleChain> findAutoAssignToEdgeRuleChainsByTenantId(TenantId tenantId, PageLink pageLink);
|
||||
|
||||
}
|
||||
|
||||
@ -19,7 +19,6 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ArrayNode;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import com.google.common.base.Function;
|
||||
import com.google.common.util.concurrent.FutureCallback;
|
||||
import com.google.common.util.concurrent.Futures;
|
||||
import com.google.common.util.concurrent.ListenableFuture;
|
||||
import com.google.common.util.concurrent.MoreExecutors;
|
||||
@ -381,22 +380,19 @@ public class EdgeServiceImpl extends AbstractEntityService implements EdgeServic
|
||||
@Override
|
||||
public void assignDefaultRuleChainsToEdge(TenantId tenantId, EdgeId edgeId) {
|
||||
log.trace("Executing assignDefaultRuleChainsToEdge, tenantId [{}], edgeId [{}]", tenantId, edgeId);
|
||||
ListenableFuture<List<RuleChain>> future = ruleChainService.findAutoAssignToEdgeRuleChainsByTenantId(tenantId);
|
||||
Futures.addCallback(future, new FutureCallback<List<RuleChain>>() {
|
||||
@Override
|
||||
public void onSuccess(List<RuleChain> ruleChains) {
|
||||
if (ruleChains != null && !ruleChains.isEmpty()) {
|
||||
for (RuleChain ruleChain : ruleChains) {
|
||||
ruleChainService.assignRuleChainToEdge(tenantId, ruleChain.getId(), edgeId);
|
||||
}
|
||||
PageLink pageLink = new PageLink(DEFAULT_LIMIT);
|
||||
PageData<RuleChain> pageData;
|
||||
do {
|
||||
pageData = ruleChainService.findAutoAssignToEdgeRuleChainsByTenantId(tenantId, pageLink);
|
||||
if (pageData.getData().size() > 0) {
|
||||
for (RuleChain ruleChain : pageData.getData()) {
|
||||
ruleChainService.assignRuleChainToEdge(tenantId, ruleChain.getId(), edgeId);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(Throwable t) {
|
||||
log.warn("[{}] can't find default edge rule chains [{}]", tenantId.getId(), edgeId.getId(), t);
|
||||
if (pageData.hasNext()) {
|
||||
pageLink = pageLink.nextPageLink();
|
||||
}
|
||||
}, MoreExecutors.directExecutor());
|
||||
} while (pageData.hasNext());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@ -631,13 +631,12 @@ public class BaseRuleChainService extends AbstractEntityService implements RuleC
|
||||
}
|
||||
|
||||
@Override
|
||||
public ListenableFuture<List<RuleChain>> findAutoAssignToEdgeRuleChainsByTenantId(TenantId tenantId) {
|
||||
log.trace("Executing findAutoAssignToEdgeRuleChainsByTenantId, tenantId [{}]", tenantId);
|
||||
public PageData<RuleChain> findAutoAssignToEdgeRuleChainsByTenantId(TenantId tenantId, PageLink pageLink) {
|
||||
log.trace("Executing findAutoAssignToEdgeRuleChainsByTenantId, tenantId [{}], pageLink {}", tenantId, pageLink);
|
||||
validateId(tenantId, INCORRECT_TENANT_ID + tenantId);
|
||||
return ruleChainDao.findAutoAssignToEdgeRuleChainsByTenantId(tenantId.getId());
|
||||
return ruleChainDao.findAutoAssignToEdgeRuleChainsByTenantId(tenantId.getId(), pageLink);
|
||||
}
|
||||
|
||||
|
||||
private void checkRuleNodesAndDelete(TenantId tenantId, RuleChainId ruleChainId) {
|
||||
try{
|
||||
ruleChainDao.removeById(tenantId, ruleChainId.getId());
|
||||
|
||||
@ -15,7 +15,6 @@
|
||||
*/
|
||||
package org.thingsboard.server.dao.rule;
|
||||
|
||||
import com.google.common.util.concurrent.ListenableFuture;
|
||||
import org.thingsboard.server.common.data.page.PageData;
|
||||
import org.thingsboard.server.common.data.page.PageLink;
|
||||
import org.thingsboard.server.common.data.rule.RuleChain;
|
||||
@ -23,7 +22,6 @@ import org.thingsboard.server.common.data.rule.RuleChainType;
|
||||
import org.thingsboard.server.dao.Dao;
|
||||
import org.thingsboard.server.dao.TenantEntityDao;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
@ -75,5 +73,5 @@ public interface RuleChainDao extends Dao<RuleChain>, TenantEntityDao {
|
||||
* @param tenantId the tenantId
|
||||
* @return the list of rule chain objects
|
||||
*/
|
||||
ListenableFuture<List<RuleChain>> findAutoAssignToEdgeRuleChainsByTenantId(UUID tenantId);
|
||||
PageData<RuleChain> findAutoAssignToEdgeRuleChainsByTenantId(UUID tenantId, PageLink pageLink);
|
||||
}
|
||||
|
||||
@ -15,9 +15,6 @@
|
||||
*/
|
||||
package org.thingsboard.server.dao.sql.rule;
|
||||
|
||||
import com.google.common.util.concurrent.Futures;
|
||||
import com.google.common.util.concurrent.ListenableFuture;
|
||||
import com.google.common.util.concurrent.MoreExecutors;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
@ -25,19 +22,13 @@ import org.springframework.stereotype.Component;
|
||||
import org.thingsboard.server.common.data.id.TenantId;
|
||||
import org.thingsboard.server.common.data.page.PageData;
|
||||
import org.thingsboard.server.common.data.page.PageLink;
|
||||
import org.thingsboard.server.common.data.relation.EntityRelation;
|
||||
import org.thingsboard.server.common.data.relation.RelationTypeGroup;
|
||||
import org.thingsboard.server.common.data.rule.RuleChain;
|
||||
import org.thingsboard.server.common.data.rule.RuleChainType;
|
||||
import org.thingsboard.server.dao.DaoUtil;
|
||||
import org.thingsboard.server.dao.model.sql.RuleChainEntity;
|
||||
import org.thingsboard.server.dao.relation.RelationDao;
|
||||
import org.thingsboard.server.dao.rule.RuleChainDao;
|
||||
import org.thingsboard.server.dao.sql.JpaAbstractSearchTextDao;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.UUID;
|
||||
|
||||
@ -48,9 +39,6 @@ public class JpaRuleChainDao extends JpaAbstractSearchTextDao<RuleChainEntity, R
|
||||
@Autowired
|
||||
private RuleChainRepository ruleChainRepository;
|
||||
|
||||
@Autowired
|
||||
private RelationDao relationDao;
|
||||
|
||||
@Override
|
||||
protected Class<RuleChainEntity> getEntityClass() {
|
||||
return RuleChainEntity.class;
|
||||
@ -91,7 +79,6 @@ public class JpaRuleChainDao extends JpaAbstractSearchTextDao<RuleChainEntity, R
|
||||
@Override
|
||||
public PageData<RuleChain> findRuleChainsByTenantIdAndEdgeId(UUID tenantId, UUID edgeId, PageLink pageLink) {
|
||||
log.debug("Try to find rule chains by tenantId [{}], edgeId [{}] and pageLink [{}]", tenantId, edgeId, pageLink);
|
||||
|
||||
return DaoUtil.toPageData(ruleChainRepository
|
||||
.findByTenantIdAndEdgeId(
|
||||
tenantId,
|
||||
@ -101,21 +88,13 @@ public class JpaRuleChainDao extends JpaAbstractSearchTextDao<RuleChainEntity, R
|
||||
}
|
||||
|
||||
@Override
|
||||
public ListenableFuture<List<RuleChain>> findAutoAssignToEdgeRuleChainsByTenantId(UUID tenantId) {
|
||||
public PageData<RuleChain> findAutoAssignToEdgeRuleChainsByTenantId(UUID tenantId, PageLink pageLink) {
|
||||
log.debug("Try to find auto assign to edge rule chains by tenantId [{}]", tenantId);
|
||||
ListenableFuture<List<EntityRelation>> relations =
|
||||
relationDao.findAllByFromAndType(new TenantId(tenantId), new TenantId(tenantId), EntityRelation.CONTAINS_TYPE, RelationTypeGroup.EDGE_AUTO_ASSIGN_RULE_CHAIN);
|
||||
return Futures.transformAsync(relations, input -> {
|
||||
if (input != null && !input.isEmpty()) {
|
||||
List<ListenableFuture<RuleChain>> ruleChainsFutures = new ArrayList<>(input.size());
|
||||
for (EntityRelation relation : input) {
|
||||
ruleChainsFutures.add(findByIdAsync(new TenantId(tenantId), relation.getTo().getId()));
|
||||
}
|
||||
return Futures.successfulAsList(ruleChainsFutures);
|
||||
} else {
|
||||
return Futures.immediateFuture(Collections.emptyList());
|
||||
}
|
||||
}, MoreExecutors.directExecutor());
|
||||
return DaoUtil.toPageData(ruleChainRepository
|
||||
.findAutoAssignByTenantId(
|
||||
tenantId,
|
||||
Objects.toString(pageLink.getTextSearch(), ""),
|
||||
DaoUtil.toPageable(pageLink)));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@ -21,7 +21,6 @@ import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.PagingAndSortingRepository;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
import org.thingsboard.server.common.data.rule.RuleChainType;
|
||||
import org.thingsboard.server.dao.model.sql.DashboardInfoEntity;
|
||||
import org.thingsboard.server.dao.model.sql.RuleChainEntity;
|
||||
|
||||
import java.util.UUID;
|
||||
@ -51,6 +50,14 @@ public interface RuleChainRepository extends PagingAndSortingRepository<RuleChai
|
||||
@Param("searchText") String searchText,
|
||||
Pageable pageable);
|
||||
|
||||
@Query("SELECT rc FROM RuleChainEntity rc, RelationEntity re WHERE rc.tenantId = :tenantId " +
|
||||
"AND rc.id = re.toId AND re.toType = 'RULE_CHAIN' AND re.relationTypeGroup = 'EDGE_AUTO_ASSIGN_RULE_CHAIN' " +
|
||||
"AND re.relationType = 'Contains' AND re.fromId = :tenantId AND re.fromType = 'TENANT' " +
|
||||
"AND LOWER(rc.searchText) LIKE LOWER(CONCAT(:searchText, '%'))")
|
||||
Page<RuleChainEntity> findAutoAssignByTenantId(@Param("tenantId") UUID tenantId,
|
||||
@Param("searchText") String searchText,
|
||||
Pageable pageable);
|
||||
|
||||
RuleChainEntity findByTenantIdAndTypeAndRootIsTrue(UUID tenantId, RuleChainType ruleChainType);
|
||||
|
||||
Long countByTenantId(UUID tenantId);
|
||||
|
||||
@ -334,13 +334,13 @@ public abstract class BaseRuleChainServiceTest extends AbstractServiceTest {
|
||||
public void testGetDefaultEdgeRuleChains() throws Exception {
|
||||
RuleChainId ruleChainId = saveRuleChainAndSetAutoAssignToEdge("Default Edge Rule Chain 1");
|
||||
saveRuleChainAndSetAutoAssignToEdge("Default Edge Rule Chain 2");
|
||||
List<RuleChain> result = ruleChainService.findAutoAssignToEdgeRuleChainsByTenantId(tenantId).get();
|
||||
Assert.assertEquals(2, result.size());
|
||||
PageData<RuleChain> result = ruleChainService.findAutoAssignToEdgeRuleChainsByTenantId(tenantId, new PageLink(100));
|
||||
Assert.assertEquals(2, result.getData().size());
|
||||
|
||||
ruleChainService.unsetAutoAssignToEdgeRuleChain(tenantId, ruleChainId);
|
||||
|
||||
result = ruleChainService.findAutoAssignToEdgeRuleChainsByTenantId(tenantId).get();
|
||||
Assert.assertEquals(1, result.size());
|
||||
result = ruleChainService.findAutoAssignToEdgeRuleChainsByTenantId(tenantId, new PageLink(100));
|
||||
Assert.assertEquals(1, result.getData().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user