use of eu.bcvsolutions.idm.core.api.dto.filter.IdmContractSliceFilter in project CzechIdMng by bcvsolutions.
the class IdentityContractDeleteProcessor method process.
@Override
public EventResult<IdmIdentityContractDto> process(EntityEvent<IdmIdentityContractDto> event) {
IdmIdentityContractDto contract = event.getContent();
UUID contractId = contract.getId();
Assert.notNull(contractId, "Contract must have a ID!");
boolean forceDelete = getBooleanProperty(PROPERTY_FORCE_DELETE, event.getProperties());
//
// check contract can be deleted - cannot be deleted, when is controlled by slices
IdmContractSliceFilter sliceFilter = new IdmContractSliceFilter();
sliceFilter.setParentContract(contractId);
if (contractSliceService.find(sliceFilter, null).getTotalElements() > 0) {
// Cannot be enforced => contract cannot be deleted at all.
throw new ResultCodeException(CoreResultCode.CONTRACT_IS_CONTROLLED_CANNOT_BE_DELETED, ImmutableMap.of("contractId", contractId));
}
//
// Find all concepts and remove relation on contract
IdmConceptRoleRequestFilter conceptRequestFilter = new IdmConceptRoleRequestFilter();
conceptRequestFilter.setIdentityContractId(contractId);
conceptRequestService.find(conceptRequestFilter, null).getContent().forEach(concept -> {
String message = null;
if (concept.getState().isTerminatedState()) {
message = MessageFormat.format("IdentityContract [{0}] (requested in concept [{1}]) was deleted (not from this role request)!", contractId, concept.getId());
} else {
message = MessageFormat.format("Request change in concept [{0}], was not executed, because requested IdentityContract [{1}] was deleted (not from this role request)!", concept.getId(), contractId);
// Cancel concept and WF
concept = conceptRequestService.cancel(concept);
}
IdmRoleRequestDto request = roleRequestService.get(concept.getRoleRequest());
roleRequestService.addToLog(request, message);
conceptRequestService.addToLog(concept, message);
roleRequestService.save(request);
conceptRequestService.save(concept);
});
//
// delete referenced roles
List<IdmConceptRoleRequestDto> concepts = new ArrayList<>();
identityRoleService.findAllByContract(contractId).forEach(identityRole -> {
// but automatic roles has to be removed in the same request.
if (identityRole.getDirectRole() == null) {
IdmConceptRoleRequestDto conceptRoleRequest = new IdmConceptRoleRequestDto();
conceptRoleRequest.setIdentityRole(identityRole.getId());
conceptRoleRequest.setRole(identityRole.getRole());
conceptRoleRequest.setOperation(ConceptRoleRequestOperation.REMOVE);
// ignore not found
conceptRoleRequest.setIdentityContract(contractId);
//
concepts.add(conceptRoleRequest);
}
});
if (forceDelete) {
// ~ async with force
IdmRoleRequestDto roleRequest = new IdmRoleRequestDto();
roleRequest.setApplicant(contract.getIdentity());
roleRequest.setConceptRoles(concepts);
//
RoleRequestEvent requestEvent = new RoleRequestEvent(RoleRequestEventType.EXCECUTE, roleRequest);
requestEvent.setPriority(PriorityType.HIGH);
//
roleRequestService.startConcepts(requestEvent, event);
} else {
// ~ sync
roleRequestService.executeConceptsImmediate(contract.getIdentity(), concepts);
}
// delete contract guarantees
IdmContractGuaranteeFilter filter = new IdmContractGuaranteeFilter();
filter.setIdentityContractId(contractId);
contractGuaranteeService.find(filter, null).forEach(guarantee -> {
contractGuaranteeService.delete(guarantee);
});
// delete contract positions
IdmContractPositionFilter positionFilter = new IdmContractPositionFilter();
positionFilter.setIdentityContractId(contractId);
contractPositionService.find(positionFilter, null).forEach(position -> {
contractPositionService.delete(position);
});
//
// delete all contract's delegations
IdmDelegationDefinitionFilter delegationFilter = new IdmDelegationDefinitionFilter();
delegationFilter.setDelegatorContractId(contractId);
delegationDefinitionService.find(delegationFilter, null).forEach(delegation -> {
delegationDefinitionService.delete(delegation);
});
// delete identity contract
if (forceDelete) {
LOG.debug("Contract [{}] should be deleted by caller after all asynchronus processes are completed.", contractId);
//
// dirty flag only - will be processed after asynchronous events ends
IdmEntityStateDto stateDeleted = new IdmEntityStateDto();
stateDeleted.setEvent(event.getId());
stateDeleted.setResult(new OperationResultDto.Builder(OperationState.RUNNING).setModel(new DefaultResultModel(CoreResultCode.DELETED)).build());
entityStateManager.saveState(contract, stateDeleted);
//
// set disabled
contract.setState(ContractState.DISABLED);
service.saveInternal(contract);
} else {
service.deleteInternal(contract);
}
return new DefaultEventResult<>(event, this);
}
use of eu.bcvsolutions.idm.core.api.dto.filter.IdmContractSliceFilter in project CzechIdMng by bcvsolutions.
the class ContractGuaranteeDeleteProcessor method checkControlledBySlices.
/**
* Test if contract of the given contract guarantee has some slices.
*
* @param guarantee
* @return
*/
private void checkControlledBySlices(EntityEvent<IdmContractGuaranteeDto> event) {
IdmContractGuaranteeDto guarantee = event.getContent();
if (getBooleanProperty(ContractSliceManager.SKIP_CHECK_FOR_SLICES, event.getProperties())) {
return;
}
UUID contract = guarantee.getIdentityContract();
IdmContractSliceFilter sliceFilter = new IdmContractSliceFilter();
sliceFilter.setParentContract(contract);
if (contract != null && sliceService.count(sliceFilter) > 0) {
throw new ResultCodeException(CoreResultCode.CONTRACT_IS_CONTROLLED_GUARANTEE_CANNOT_BE_DELETED, ImmutableMap.of("contractId", contract));
}
}
use of eu.bcvsolutions.idm.core.api.dto.filter.IdmContractSliceFilter in project CzechIdMng by bcvsolutions.
the class IdentityContractSaveProcessor method checkControlledBySlices.
/**
* Test if contract has some slices.
*
* @param guarantee
* @return
* @since 11.0.0
*/
protected void checkControlledBySlices(EntityEvent<IdmIdentityContractDto> event) {
IdmIdentityContractDto contract = event.getContent();
if (getBooleanProperty(ContractSliceManager.SKIP_CHECK_FOR_SLICES, event.getProperties())) {
return;
}
//
UUID contractId = contract.getId();
IdmContractSliceFilter sliceFilter = new IdmContractSliceFilter();
sliceFilter.setParentContract(contractId);
if (contractId != null && contractSliceService.count(sliceFilter) > 0) {
throw new ResultCodeException(CoreResultCode.CONTRACT_IS_CONTROLLED_CANNOT_BE_MODIFIED, ImmutableMap.of("contractId", contractId));
}
}
use of eu.bcvsolutions.idm.core.api.dto.filter.IdmContractSliceFilter in project CzechIdMng by bcvsolutions.
the class ContractSliceManagerTest method deleteSliceTest.
@Test
public void deleteSliceTest() {
IdmIdentityDto identity = helper.createIdentity();
String contractCode = "contract-one";
IdmContractSliceDto sliceFuture = helper.createContractSlice(identity, null, LocalDate.now().plusDays(10), null, LocalDate.now().plusDays(100));
sliceFuture.setContractCode(contractCode);
contractSliceService.save(sliceFuture);
IdmContractSliceDto sliceCurrent = helper.createContractSlice(identity, null, LocalDate.now(), null, LocalDate.now().plusDays(50));
sliceCurrent.setContractCode(contractCode);
contractSliceService.save(sliceCurrent);
IdmContractSliceDto slicePast = helper.createContractSlice(identity, null, LocalDate.now().minusDays(10), null, LocalDate.now().plusDays(200));
slicePast.setContractCode(contractCode);
contractSliceService.save(slicePast);
IdmContractSliceFilter filter = new IdmContractSliceFilter();
filter.setIdentity(identity.getId());
List<IdmContractSliceDto> results = contractSliceService.find(filter, null).getContent();
assertEquals(3, results.size());
UUID parentContract = results.get(0).getParentContract();
List<IdmContractSliceDto> slices = contractSliceManager.findAllSlices(parentContract);
assertEquals(3, slices.size());
IdmContractSliceDto validSlice = contractSliceManager.findValidSlice(parentContract);
// Valid slice should be currentSlice
assertEquals(sliceCurrent, validSlice);
// Check created contract by that slice
IdmIdentityContractFilter contractFilter = new IdmIdentityContractFilter();
contractFilter.setIdentity(identity.getId());
List<IdmIdentityContractDto> resultsContract = //
contractService.find(filter, null).getContent().stream().filter(//
c -> contractService.get(c.getId()).getControlledBySlices()).collect(Collectors.toList());
assertEquals(1, resultsContract.size());
// Current slice should be contract
IdmIdentityContractDto contract = resultsContract.get(0);
assertTrue(contract.isValid());
assertEquals(sliceCurrent.getContractValidFrom(), contract.getValidFrom());
// Delete the current slice
contractSliceService.delete(contractSliceService.get(sliceCurrent.getId()));
slices = contractSliceManager.findAllSlices(parentContract);
assertEquals(2, slices.size());
validSlice = contractSliceManager.findValidSlice(parentContract);
// Valid slice should be slicePast now
assertEquals(slicePast, validSlice);
List<IdmContractSliceDto> unvalidSlices = contractSliceManager.findUnvalidSlices(null).getContent();
assertEquals(0, unvalidSlices.size());
// Reload the contract
contract = contractService.get(contract.getId());
assertTrue(contract.isValid());
assertEquals(slicePast.getContractValidFrom(), contract.getValidFrom());
assertEquals(slicePast.getContractValidTill(), contract.getValidTill());
}
use of eu.bcvsolutions.idm.core.api.dto.filter.IdmContractSliceFilter in project CzechIdMng by bcvsolutions.
the class ContractSliceManagerTest method testDeleteEAVOnSlice.
@Test
public void testDeleteEAVOnSlice() {
IdmIdentityDto identity = helper.createIdentity();
String contractCode = "contract-one";
IdmContractSliceDto slice = helper.createContractSlice(identity, null, null, null, null);
slice.setContractCode(contractCode);
slice = contractSliceService.save(slice);
// Init form definition for identity-contract
IdmFormDefinitionDto definition = this.initIdentityContractFormDefinition();
// Create slice with EAV values
IdmFormInstanceDto formInstanceDto = formService.getFormInstance(slice, definition);
Assert.assertNotNull(formInstanceDto);
Assert.assertNotNull(formInstanceDto.getFormDefinition());
Assert.assertEquals(0, formInstanceDto.getValues().size());
IdmFormAttributeDto attribute = formInstanceDto.getMappedAttributeByCode(NUMBER_OF_FINGERS);
formService.saveValues(slice, attribute, Lists.newArrayList(BigDecimal.TEN));
// We need to save slice for invoke save slice to the contract
slice = contractSliceService.save(slice);
formInstanceDto = formService.getFormInstance(slice, definition);
Assert.assertNotNull(formInstanceDto);
Assert.assertNotNull(formInstanceDto.getFormDefinition());
Assert.assertEquals(1, formInstanceDto.getValues().size());
Assert.assertEquals(BigDecimal.TEN.longValue(), ((BigDecimal) formInstanceDto.getValues().get(0).getValue()).longValue());
IdmContractSliceFilter filter = new IdmContractSliceFilter();
filter.setIdentity(identity.getId());
List<IdmContractSliceDto> results = contractSliceService.find(filter, null).getContent();
assertEquals(1, results.size());
IdmContractSliceDto createdSlice = results.get(0);
assertTrue(createdSlice.isValid());
assertEquals(null, createdSlice.getValidTill());
// Check created contract by that slice
IdmIdentityContractFilter contractFilter = new IdmIdentityContractFilter();
contractFilter.setIdentity(identity.getId());
List<IdmIdentityContractDto> resultsContract = //
contractService.find(filter, null).getContent().stream().filter(//
c -> contractService.get(c.getId()).getControlledBySlices()).collect(Collectors.toList());
//
assertEquals(1, resultsContract.size());
IdmIdentityContractDto contract = resultsContract.get(0);
assertEquals(slice.getContractValidFrom(), contract.getValidFrom());
assertEquals(slice.getContractValidTill(), contract.getValidTill());
assertTrue(contract.isValidNowOrInFuture());
IdmFormInstanceDto contractFormInstanceDto = formService.getFormInstance(contract);
Assert.assertNotNull(contractFormInstanceDto);
Assert.assertNotNull(contractFormInstanceDto.getFormDefinition());
Assert.assertEquals(1, contractFormInstanceDto.getValues().size());
Assert.assertEquals(BigDecimal.TEN.longValue(), ((BigDecimal) contractFormInstanceDto.getValues().get(0).getValue()).longValue());
formService.saveValues(slice, attribute, null);
// We need to save slice for invoke save slice to the contract
slice = contractSliceService.save(slice);
contractFormInstanceDto = formService.getFormInstance(contract);
Assert.assertNotNull(contractFormInstanceDto);
Assert.assertNotNull(contractFormInstanceDto.getFormDefinition());
Assert.assertEquals(0, contractFormInstanceDto.getValues().size());
}
Aggregations