use of eu.bcvsolutions.idm.core.model.entity.IdmTreeNode in project CzechIdMng by bcvsolutions.
the class DefaultIdmContractPositionService method toPredicates.
@Override
protected List<Predicate> toPredicates(Root<IdmContractPosition> root, CriteriaQuery<?> query, CriteriaBuilder builder, IdmContractPositionFilter filter) {
List<Predicate> predicates = super.toPredicates(root, query, builder, filter);
// contract id
UUID contractId = filter.getIdentityContractId();
if (contractId != null) {
predicates.add(builder.equal(root.get(IdmContractPosition_.identityContract).get(IdmIdentityContract_.id), contractId));
}
// tree node id
UUID workPosition = filter.getWorkPosition();
if (workPosition != null) {
RecursionType recursionType = filter.getRecursionType();
if (recursionType == RecursionType.NO) {
// NO recursion => equals on work position only.
predicates.add(builder.equal(root.get(IdmContractPosition_.workPosition).get(IdmTreeNode_.id), workPosition));
} else {
// prepare subquery for tree nodes and index
Subquery<IdmTreeNode> subqueryTreeNode = query.subquery(IdmTreeNode.class);
Root<IdmTreeNode> subqueryTreeNodeRoot = subqueryTreeNode.from(IdmTreeNode.class);
subqueryTreeNode.select(subqueryTreeNodeRoot);
//
if (recursionType == RecursionType.DOWN) {
subqueryTreeNode.where(builder.and(builder.equal(subqueryTreeNodeRoot.get(IdmTreeNode_.id), workPosition), builder.equal(root.get(IdmContractPosition_.workPosition).get(IdmTreeNode_.treeType), subqueryTreeNodeRoot.get(IdmTreeNode_.treeType)), builder.between(root.get(IdmContractPosition_.workPosition).get(IdmTreeNode_.forestIndex).get(IdmForestIndexEntity_.lft), subqueryTreeNodeRoot.get(IdmTreeNode_.forestIndex).get(IdmForestIndexEntity_.lft), subqueryTreeNodeRoot.get(IdmTreeNode_.forestIndex).get(IdmForestIndexEntity_.rgt))));
} else {
// UP
subqueryTreeNode.where(builder.and(builder.equal(subqueryTreeNodeRoot.get(IdmTreeNode_.id), workPosition), builder.equal(root.get(IdmContractPosition_.workPosition).get(IdmTreeNode_.treeType), subqueryTreeNodeRoot.get(IdmTreeNode_.treeType)), builder.between(subqueryTreeNodeRoot.get(IdmTreeNode_.forestIndex).get(IdmForestIndexEntity_.lft), root.get(IdmContractPosition_.workPosition).get(IdmTreeNode_.forestIndex).get(IdmForestIndexEntity_.lft), root.get(IdmContractPosition_.workPosition).get(IdmTreeNode_.forestIndex).get(IdmForestIndexEntity_.rgt))));
}
//
predicates.add(builder.exists(subqueryTreeNode));
}
}
UUID identity = filter.getIdentity();
if (identity != null) {
predicates.add(builder.equal(root.get(IdmContractPosition_.identityContract).get(IdmIdentityContract_.identity).get(IdmIdentity_.id), identity));
}
Boolean validNowOrInFuture = filter.getValidNowOrInFuture();
if (validNowOrInFuture != null) {
Path<IdmIdentityContract> pathContract = root.get(IdmContractPosition_.identityContract);
//
if (validNowOrInFuture) {
predicates.add(builder.and(builder.or(builder.greaterThanOrEqualTo(pathContract.get(IdmIdentityContract_.validTill), LocalDate.now()), builder.isNull(pathContract.get(IdmIdentityContract_.validTill))), builder.equal(pathContract.get(IdmIdentityContract_.disabled), Boolean.FALSE)));
} else {
predicates.add(builder.lessThan(pathContract.get(IdmIdentityContract_.validTill), LocalDate.now()));
}
}
//
return predicates;
}
use of eu.bcvsolutions.idm.core.model.entity.IdmTreeNode in project CzechIdMng by bcvsolutions.
the class RebuildTreeNodeIndexTaskExecutor method process.
@Override
public Boolean process() {
if (!configurationService.getBooleanValue(DefaultForestIndexService.PROPERTY_INDEX_ENABLED, true)) {
throw new ResultCodeException(CoreResultCode.FOREST_INDEX_DISABLED, ImmutableMap.of("property", DefaultForestIndexService.PROPERTY_INDEX_ENABLED));
}
IdmTreeTypeDto treeType = getTreeType();
String longRunningTaskId = configurationService.getValue(treeTypeService.getConfigurationPropertyName(treeTypeCode, IdmTreeTypeService.CONFIGURATION_PROPERTY_REBUILD));
if (StringUtils.hasLength(longRunningTaskId) && !longRunningTaskId.equals(getLongRunningTaskId().toString())) {
throw new ResultCodeException(CoreResultCode.FOREST_INDEX_RUNNING, ImmutableMap.of("type", IdmTreeNode.toForestTreeType(treeType.getId())));
}
//
LOG.info("Starting rebuilding tree node index for tree type code [{}].", treeTypeCode);
// clear all rgt, lft
try {
forestIndexService.dropIndexes(IdmTreeNode.toForestTreeType(treeType.getId()));
} finally {
configurationService.setBooleanValue(treeTypeService.getConfigurationPropertyName(treeTypeCode, IdmTreeTypeService.CONFIGURATION_PROPERTY_VALID), false);
}
try {
configurationService.setValue(treeTypeService.getConfigurationPropertyName(treeTypeCode, IdmTreeTypeService.CONFIGURATION_PROPERTY_REBUILD), getLongRunningTaskId().toString());
//
count = treeNodeRepository.findByTreeType_Id(treeType.getId(), PageRequest.of(0, 1)).getTotalElements();
counter = 0L;
boolean canContinue = true;
Page<IdmTreeNode> roots = treeNodeRepository.findRoots(treeType.getId(), PageRequest.of(0, 100, new Sort(Direction.ASC, AbstractEntity_.id.getName())));
while (canContinue) {
canContinue = processChildren(roots.getContent());
if (!canContinue) {
break;
}
if (!roots.hasNext()) {
break;
}
roots = treeNodeRepository.findRoots(treeType.getId(), roots.nextPageable());
}
//
if (count.equals(counter)) {
configurationService.deleteValue(treeTypeService.getConfigurationPropertyName(treeTypeCode, IdmTreeTypeService.CONFIGURATION_PROPERTY_VALID));
LOG.info("Tree node index for tree type code [{}] was successfully rebuilt (index size [{}]).", treeTypeCode, counter);
return Boolean.TRUE;
}
//
LOG.warn("Tree node index for tree type code [{}] rebuild was canceled (index size [{}]).", treeTypeCode, counter);
return Boolean.FALSE;
} finally {
configurationService.deleteValue(treeTypeService.getConfigurationPropertyName(treeTypeCode, IdmTreeTypeService.CONFIGURATION_PROPERTY_REBUILD));
}
}
use of eu.bcvsolutions.idm.core.model.entity.IdmTreeNode in project CzechIdMng by bcvsolutions.
the class IdentityByTreeNodeEvaluator method getPredicate.
@Override
public Predicate getPredicate(Root<IdmIdentity> root, CriteriaQuery<?> query, CriteriaBuilder builder, AuthorizationPolicy policy, BasePermission... permission) {
// check before apply evaluator
UUID treeNodeId = getUuid(policy);
if (treeNodeId == null) {
return null;
}
// subquery to treenode (contract position -> treenode)
Subquery<IdmIdentityContract> subquery = query.subquery(IdmIdentityContract.class);
Root<IdmIdentityContract> subRoot = subquery.from(IdmIdentityContract.class);
subquery.select(subRoot);
Subquery<IdmTreeNode> subqueryTreeNode = query.subquery(IdmTreeNode.class);
Root<IdmTreeNode> subqueryTreeNodeRoot = subqueryTreeNode.from(IdmTreeNode.class);
subqueryTreeNode.select(subqueryTreeNodeRoot);
subqueryTreeNode.where(builder.and(builder.equal(subqueryTreeNodeRoot.get(IdmTreeNode_.id), treeNodeId), builder.equal(subRoot.get(IdmIdentityContract_.workPosition).get(IdmTreeNode_.treeType), subqueryTreeNodeRoot.get(IdmTreeNode_.treeType)), builder.between(subRoot.get(IdmIdentityContract_.workPosition).get(IdmTreeNode_.forestIndex).get(IdmForestIndexEntity_.lft), subqueryTreeNodeRoot.get(IdmTreeNode_.forestIndex).get(IdmForestIndexEntity_.lft), subqueryTreeNodeRoot.get(IdmTreeNode_.forestIndex).get(IdmForestIndexEntity_.rgt))));
subquery.where(builder.and(// correlation attr
builder.equal(subRoot.get(IdmIdentityContract_.identity), root), builder.exists(subqueryTreeNode)));
return builder.exists(subquery);
}
use of eu.bcvsolutions.idm.core.model.entity.IdmTreeNode in project CzechIdMng by bcvsolutions.
the class DefaultIdmIdentityContractServiceUnitTest method testPrimeContractWithDefaultTreeType.
@Test
public void testPrimeContractWithDefaultTreeType() {
List<IdmIdentityContract> contracts = new ArrayList<>();
IdmIdentityContract contractWithDefaultPosition = new IdmIdentityContract(UUID.randomUUID());
contractWithDefaultPosition.setMain(false);
IdmTreeNode defaultWorkPosition = new IdmTreeNode();
defaultWorkPosition.setId(UUID.randomUUID());
IdmTreeType defaultTreeType = new IdmTreeType(UUID.randomUUID());
defaultWorkPosition.setTreeType(defaultTreeType);
contractWithDefaultPosition.setWorkPosition(defaultWorkPosition);
//
IdmIdentityContract otherContract = new IdmIdentityContract(UUID.randomUUID());
otherContract.setMain(false);
IdmTreeNode workPosition = new IdmTreeNode();
workPosition.setId(UUID.randomUUID());
workPosition.setTreeType(new IdmTreeType(UUID.randomUUID()));
otherContract.setWorkPosition(workPosition);
//
contracts.add(contractWithDefaultPosition);
contracts.add(otherContract);
//
when(applicationContext.getBean(LookupService.class)).thenReturn(lookupService);
when(lookupService.toDto(any(), any(), any())).then(new LookupAnswer());
when(repository.findAllByIdentity_Id(any(UUID.class), any())).thenReturn(contracts);
when(treeConfiguration.getDefaultType()).thenReturn(new IdmTreeTypeDto(defaultTreeType.getId()));
when(contractSliceService.count(any(IdmContractSliceFilter.class))).thenReturn(0L);
//
Assert.assertEquals(contractWithDefaultPosition.getId(), service.getPrimeContract(UUID.randomUUID()).getId());
}
Aggregations