use of eu.bcvsolutions.idm.core.scheduler.api.dto.filter.IdmLongRunningTaskFilter in project CzechIdMng by bcvsolutions.
the class RemoveAutomaticRoleTaskExecutor method validate.
/**
* Automatic role removal can be start, if previously LRT ended.
*/
@Override
public void validate(IdmLongRunningTaskDto task) {
super.validate(task);
//
UUID automaticRoleId = getAutomaticRoleId();
boolean byTree = true;
AbstractIdmAutomaticRoleDto automaticRole = roleTreeNodeService.get(automaticRoleId);
if (automaticRole == null) {
// get from automatic role attribute service
byTree = false;
automaticRole = automaticRoleAttributeService.get(automaticRoleId);
}
if (automaticRole == null) {
throw new EntityNotFoundException(AbstractIdmAutomaticRoleDto.class, automaticRoleId);
}
//
IdmLongRunningTaskFilter filter = new IdmLongRunningTaskFilter();
filter.setTaskType(AutowireHelper.getTargetType(this));
filter.setOperationState(OperationState.RUNNING);
// ignore waiting tasks
filter.setRunning(Boolean.TRUE);
//
if (byTree) {
for (UUID longRunningTaskId : getLongRunningTaskService().findIds(filter, PageRequest.of(0, 1))) {
if (longRunningTaskId.equals(getLongRunningTaskId())) {
continue;
}
throw new AcceptedException(CoreResultCode.AUTOMATIC_ROLE_TASK_RUNNING, ImmutableMap.of("taskId", longRunningTaskId.toString()));
}
//
filter.setTaskType(AutowireHelper.getTargetType(ProcessAutomaticRoleByTreeTaskExecutor.class));
for (UUID longRunningTaskId : getLongRunningTaskService().findIds(filter, PageRequest.of(0, 1))) {
if (longRunningTaskId.equals(getLongRunningTaskId())) {
continue;
}
throw new AcceptedException(CoreResultCode.AUTOMATIC_ROLE_TASK_RUNNING, ImmutableMap.of("taskId", longRunningTaskId.toString()));
}
} else {
// by attribute - prevent currently removed role only
for (IdmLongRunningTaskDto longRunningTask : getLongRunningTaskService().find(filter, null)) {
if (longRunningTask.getId().equals(getLongRunningTaskId())) {
continue;
}
if (longRunningTask.getTaskProperties().get(AbstractAutomaticRoleTaskExecutor.PARAMETER_ROLE_TREE_NODE).equals(automaticRole.getId())) {
throw new ResultCodeException(CoreResultCode.AUTOMATIC_ROLE_REMOVE_TASK_RUN_CONCURRENTLY, ImmutableMap.of("roleTreeNode", automaticRole.getId().toString(), "taskId", longRunningTask.getId().toString()));
}
}
//
filter.setTaskType(AutowireHelper.getTargetType(ProcessAutomaticRoleByAttributeTaskExecutor.class));
for (IdmLongRunningTaskDto longRunningTask : getLongRunningTaskService().find(filter, null)) {
if (longRunningTask.getId().equals(getLongRunningTaskId())) {
continue;
}
if (longRunningTask.getTaskProperties().get(AbstractAutomaticRoleTaskExecutor.PARAMETER_ROLE_TREE_NODE).equals(automaticRole.getId())) {
throw new ResultCodeException(CoreResultCode.AUTOMATIC_ROLE_REMOVE_TASK_ADD_RUNNING, ImmutableMap.of("automaticRoleId", automaticRole.getId().toString(), "taskId", longRunningTask.getId().toString()));
}
}
}
}
use of eu.bcvsolutions.idm.core.scheduler.api.dto.filter.IdmLongRunningTaskFilter in project CzechIdMng by bcvsolutions.
the class RemoveRoleCompositionTaskExecutor method validate.
/**
* Automatic role removal can be start, if previously LRT ended.
*/
@Override
public void validate(IdmLongRunningTaskDto task) {
super.validate(task);
//
// composition is already deleted
IdmRoleCompositionDto roleComposition = roleCompositionService.get(roleCompositionId);
if (roleComposition == null) {
throw new EntityNotFoundException(IdmRoleComposition.class, roleCompositionId);
}
//
IdmLongRunningTaskFilter filter = new IdmLongRunningTaskFilter();
filter.setOperationState(OperationState.RUNNING);
// ignore waiting tasks
filter.setRunning(Boolean.TRUE);
filter.setTaskType(AddNewRoleCompositionTaskExecutor.class.getCanonicalName());
for (UUID longRunningTaskId : getLongRunningTaskService().findIds(filter, PageRequest.of(0, 1))) {
throw new AcceptedException(CoreResultCode.ROLE_COMPOSITION_RUN_CONCURRENTLY, ImmutableMap.of("taskId", longRunningTaskId.toString(), "roleCompositionId", roleCompositionId.toString()));
}
}
use of eu.bcvsolutions.idm.core.scheduler.api.dto.filter.IdmLongRunningTaskFilter in project CzechIdMng by bcvsolutions.
the class ProcessAutomaticRoleByAttributeTaskExecutor method validate.
/**
* Automatic role removal can be start, if previously LRT ended.
*/
@Override
public void validate(IdmLongRunningTaskDto task) {
super.validate(task);
//
UUID automaticRoleId = getAutomaticRoleId();
AbstractIdmAutomaticRoleDto automaticRole = automaticRoleAttributeService.get(automaticRoleId);
if (automaticRole == null) {
throw new EntityNotFoundException(AbstractIdmAutomaticRoleDto.class, automaticRoleId);
}
//
IdmLongRunningTaskFilter filter = new IdmLongRunningTaskFilter();
filter.setTaskType(AutowireHelper.getTargetType(this));
filter.setOperationState(OperationState.RUNNING);
// by attribute - prevent currently processed role only
for (IdmLongRunningTaskDto longRunningTask : getLongRunningTaskService().find(filter, null)) {
if (longRunningTask.getId().equals(getLongRunningTaskId())) {
continue;
}
if (longRunningTask.getTaskProperties().get(AbstractAutomaticRoleTaskExecutor.PARAMETER_ROLE_TREE_NODE).equals(automaticRole.getId())) {
throw new AcceptedException(CoreResultCode.AUTOMATIC_ROLE_TASK_RUNNING, ImmutableMap.of("taskId", longRunningTask.getId().toString()));
}
}
//
filter.setTaskType(AutowireHelper.getTargetType(RemoveAutomaticRoleTaskExecutor.class));
for (IdmLongRunningTaskDto longRunningTask : getLongRunningTaskService().find(filter, null)) {
if (longRunningTask.getId().equals(getLongRunningTaskId())) {
continue;
}
if (longRunningTask.getTaskProperties().get(AbstractAutomaticRoleTaskExecutor.PARAMETER_ROLE_TREE_NODE).equals(automaticRole.getId())) {
throw new AcceptedException(CoreResultCode.AUTOMATIC_ROLE_TASK_RUNNING, ImmutableMap.of("taskId", longRunningTask.getId().toString()));
}
}
}
use of eu.bcvsolutions.idm.core.scheduler.api.dto.filter.IdmLongRunningTaskFilter in project CzechIdMng by bcvsolutions.
the class DefaultIdmIdentityContractServiceIntegrationTest method testAssignAutomaticRoleToExistIdentityAsync.
@Test
public void testAssignAutomaticRoleToExistIdentityAsync() {
IdmIdentityDto identityOne = getHelper().createIdentityOnly();
IdmIdentityDto identityTwo = getHelper().createIdentityOnly();
IdmIdentityDto identityThree = getHelper().createIdentityOnly();
IdmTreeNodeDto treeNode = getHelper().createTreeNode();
IdmIdentityContractDto contractOne = getHelper().createContract(identityOne, treeNode);
IdmIdentityContractDto contractTwo = getHelper().createContract(identityTwo, treeNode);
IdmIdentityContractDto contractThree = getHelper().createContract(identityThree, treeNode);
IdmRoleDto role = getHelper().createRole();
//
List<IdmLongRunningTaskDto> lrts = null;
getHelper().setConfigurationValue(EventConfiguration.PROPERTY_EVENT_ASYNCHRONOUS_ENABLED, true);
getHelper().setConfigurationValue(SchedulerConfiguration.PROPERTY_TASK_ASYNCHRONOUS_ENABLED, true);
try {
IdmRoleTreeNodeDto automaticRole = getHelper().createAutomaticRole(role, treeNode);
//
getHelper().waitForResult(res -> {
return identityRoleService.findByAutomaticRole(automaticRole.getId(), null).getTotalElements() != 3;
}, 500, 30);
getHelper().waitForResult(res -> {
IdmLongRunningTaskFilter filter = new IdmLongRunningTaskFilter();
filter.setRunning(Boolean.TRUE);
//
return taskManager.findLongRunningTasks(filter, null).getTotalElements() != 0;
});
//
List<IdmIdentityRoleDto> assignedRoles = identityRoleService.findByAutomaticRole(automaticRole.getId(), null).getContent();
Assert.assertEquals(3, assignedRoles.size());
Assert.assertTrue(assignedRoles.stream().anyMatch(ir -> ir.getIdentityContract().equals(contractOne.getId())));
Assert.assertTrue(assignedRoles.stream().anyMatch(ir -> ir.getIdentityContract().equals(contractTwo.getId())));
Assert.assertTrue(assignedRoles.stream().anyMatch(ir -> ir.getIdentityContract().equals(contractThree.getId())));
//
// check asynchronous LRT
IdmLongRunningTaskFilter filter = new IdmLongRunningTaskFilter();
filter.setTransactionId(assignedRoles.get(0).getTransactionId());
lrts = taskManager.findLongRunningTasks(filter, null).getContent();
Assert.assertFalse(lrts.isEmpty());
Assert.assertTrue(lrts.stream().allMatch(lrt -> lrt.getResultState() == OperationState.EXECUTED));
//
// and check lrt start event is properly created and ended
IdmEntityEventFilter eventFilter = new IdmEntityEventFilter();
eventFilter.setOwnerType(entityEventManager.getOwnerType(IdmLongRunningTaskDto.class));
eventFilter.setTransactionId(assignedRoles.get(0).getTransactionId());
List<IdmEntityEventDto> events = entityEventManager.findEvents(eventFilter, null).getContent();
Assert.assertFalse(events.isEmpty());
Assert.assertTrue(events.stream().allMatch(event -> event.getResult().getState() == OperationState.EXECUTED));
Assert.assertTrue(events.stream().allMatch(event -> event.getEventStarted() != null));
Assert.assertTrue(events.stream().allMatch(event -> event.getEventEnded() != null));
Assert.assertTrue(events.stream().anyMatch(event -> event.getEventType().equals(LongRunningTaskEventType.START.name())));
} finally {
getHelper().setConfigurationValue(EventConfiguration.PROPERTY_EVENT_ASYNCHRONOUS_ENABLED, false);
getHelper().setConfigurationValue(SchedulerConfiguration.PROPERTY_TASK_ASYNCHRONOUS_ENABLED, false);
//
if (lrts != null) {
lrts.forEach(lrt -> {
if (lrt.isRunning() || lrt.getResultState() == OperationState.RUNNING) {
taskManager.cancel(lrt.getId());
}
});
}
}
}
use of eu.bcvsolutions.idm.core.scheduler.api.dto.filter.IdmLongRunningTaskFilter in project CzechIdMng by bcvsolutions.
the class DefaultIdmIdentityContractServiceIntegrationTest method testCRUDContractWithAutomaticRolesAsync.
@Test
public void testCRUDContractWithAutomaticRolesAsync() {
prepareAutomaticRoles();
//
try {
getHelper().setConfigurationValue(EventConfiguration.PROPERTY_EVENT_ASYNCHRONOUS_ENABLED, true);
TransactionContextHolder.setContext(TransactionContextHolder.createEmptyContext());
UUID transactionId = TransactionContextHolder.getContext().getTransactionId();
//
// prepare identity and contract
IdmIdentityDto identity = getHelper().createIdentity((GuardedString) null);
IdmIdentityContractDto contractToCreate = service.getPrimeContract(identity.getId());
contractToCreate.setIdentity(identity.getId());
contractToCreate.setValidFrom(LocalDate.now().minusDays(1));
contractToCreate.setValidTill(LocalDate.now().plusMonths(1));
contractToCreate.setWorkPosition(nodeD.getId());
contractToCreate.setMain(true);
contractToCreate.setDescription("test-node-d");
service.save(contractToCreate);
//
IdmIdentityContractDto contract = service.getPrimeContract(identity.getId());
//
// test after create
Assert.assertEquals(nodeD.getId(), contract.getWorkPosition());
Assert.assertEquals("test-node-d", contract.getDescription());
Assert.assertEquals(transactionId, contract.getTransactionId());
//
getHelper().waitForResult(res -> {
return identityRoleService.findAllByContract(contract.getId()).isEmpty();
}, 500, Integer.MAX_VALUE);
getHelper().waitForResult(res -> {
IdmLongRunningTaskFilter filter = new IdmLongRunningTaskFilter();
filter.setRunning(Boolean.TRUE);
//
return taskManager.findLongRunningTasks(filter, null).getTotalElements() != 0;
});
//
List<IdmIdentityRoleDto> identityRoles = identityRoleService.findAllByContract(contract.getId());
Assert.assertEquals(3, identityRoles.size());
Assert.assertTrue(identityRoles.stream().allMatch(ir -> contract.getValidFrom().equals(ir.getValidFrom())));
Assert.assertTrue(identityRoles.stream().allMatch(ir -> contract.getValidTill().equals(ir.getValidTill())));
Assert.assertTrue(identityRoles.stream().allMatch(ir -> ir.getTransactionId().equals(transactionId)));
Assert.assertTrue(identityRoles.stream().anyMatch(ir -> {
return roleA.getId().equals(ir.getRole());
}));
Assert.assertTrue(identityRoles.stream().anyMatch(ir -> {
return roleB.getId().equals(ir.getRole());
}));
Assert.assertTrue(identityRoles.stream().anyMatch(ir -> {
return roleC.getId().equals(ir.getRole());
}));
//
// find by transactionId
IdmIdentityRoleFilter filter = new IdmIdentityRoleFilter();
filter.setTransactionId(transactionId);
identityRoles = identityRoleService.find(filter, null).getContent();
Assert.assertEquals(3, identityRoles.size());
Assert.assertTrue(identityRoles.stream().anyMatch(ir -> {
return roleA.getId().equals(ir.getRole());
}));
Assert.assertTrue(identityRoles.stream().anyMatch(ir -> {
return roleB.getId().equals(ir.getRole());
}));
Assert.assertTrue(identityRoles.stream().anyMatch(ir -> {
return roleC.getId().equals(ir.getRole());
}));
//
service.delete(contract);
} finally {
getHelper().setConfigurationValue(EventConfiguration.PROPERTY_EVENT_ASYNCHRONOUS_ENABLED, false);
}
}
Aggregations