use of eu.bcvsolutions.idm.core.model.event.ContractSliceEvent in project CzechIdMng by bcvsolutions.
the class ContractSliceSaveRecalculateProcessor method process.
@Override
public EventResult<IdmContractSliceDto> process(EntityEvent<IdmContractSliceDto> event) {
IdmContractSliceDto slice = event.getContent();
IdmContractSliceDto originalSlice = event.getOriginalSource();
// There is conditional for executing this processor
if (this.getBooleanProperty(IdmContractSliceService.SET_DIRTY_STATE_CONTRACT_SLICE, event.getProperties())) {
// If is set slice as using as contract, set this flag to false
if (slice.isUsingAsContract()) {
slice.setUsingAsContract(false);
service.publish(new ContractSliceEvent(ContractSliceEventType.UPDATE, slice, ImmutableMap.of(IdmContractSliceService.SKIP_RECALCULATE_CONTRACT_SLICE, Boolean.TRUE)));
}
Map<String, Serializable> properties = new HashMap<>(event.getProperties());
// save original slice into parameters, executor ClearDirtyFlagForContractSliceTaskExecutor need him for recalculation
properties.put(ClearDirtyStateForContractSliceTaskExecutor.ORIGINAL_SLICE, originalSlice);
// Creates new dirty states, dirty states must be process by executor
createDirtyState(slice, properties);
return new DefaultEventResult<>(event, this);
}
// recalculation
Map<String, Serializable> eventProperties = event.getProperties();
sliceManager.recalculateContractSlice(slice, originalSlice, eventProperties);
event.setContent(service.get(slice.getId()));
return new DefaultEventResult<>(event, this);
}
use of eu.bcvsolutions.idm.core.model.event.ContractSliceEvent in project CzechIdMng by bcvsolutions.
the class ContractSliceSynchronizationExecutor method save.
/**
* Save entity
*
* @param entity
* @param skipProvisioning
* @return
*/
@Override
protected IdmContractSliceDto save(IdmContractSliceDto entity, boolean skipProvisioning, SynchronizationContext context) {
if (entity.getIdentity() == null) {
throw new ProvisioningException(AccResultCode.SYNCHRONIZATION_IDM_FIELD_CANNOT_BE_NULL, ImmutableMap.of("property", CONTRACT_IDENTITY_FIELD));
}
EntityEvent<IdmContractSliceDto> event = new ContractSliceEvent(sliceService.isNew(entity) ? ContractSliceEventType.CREATE : ContractSliceEventType.UPDATE, entity, ImmutableMap.of(ProvisioningService.SKIP_PROVISIONING, skipProvisioning));
// We do not want execute HR processes for every contract. We need start
// them for every identity only once.
// For this we skip them now. HR processes must be start after whole
// sync finished (by using dependent scheduled task)!
event.getProperties().put(IdmIdentityContractService.SKIP_HR_PROCESSES, Boolean.TRUE);
//
// We don't want recalculate automatic role by attribute recalculation for every
// contract.
// Recalculation will be started only once.
event.getProperties().put(AutomaticRoleManager.SKIP_RECALCULATION, Boolean.TRUE);
//
// Set dirty state during recalculation, the process of recalculation will be solved in ClearDirtyStateForContractSliceTaskExecutor
// ClearDirtyStateForContractSliceTaskExecutor will be started with HR processes.
event.getProperties().put(IdmContractSliceService.SET_DIRTY_STATE_CONTRACT_SLICE, Boolean.TRUE);
IdmContractSliceDto slice = sliceService.publish(event).getContent();
if (entity.getEmbedded().containsKey(SYNC_CONTRACT_FIELD)) {
SyncIdentityContractDto syncContract = (SyncIdentityContractDto) entity.getEmbedded().get(SYNC_CONTRACT_FIELD);
IdmContractSliceGuaranteeFilter guaranteeFilter = new IdmContractSliceGuaranteeFilter();
guaranteeFilter.setContractSliceId(slice.getId());
List<IdmContractSliceGuaranteeDto> currentGuarantees = guaranteeService.find(guaranteeFilter, null).getContent();
// Search guarantees to delete
List<IdmContractSliceGuaranteeDto> guaranteesToDelete = currentGuarantees.stream().filter(sysImplementer -> {
return sysImplementer.getGuarantee() != null && !syncContract.getGuarantees().contains(new IdmIdentityDto(sysImplementer.getGuarantee()));
}).collect(Collectors.toList());
// Search guarantees to add
List<IdmIdentityDto> guaranteesToAdd = syncContract.getGuarantees().stream().filter(identity -> {
return !currentGuarantees.stream().filter(currentGuarrantee -> {
return identity.getId().equals(currentGuarrantee.getGuarantee());
}).findFirst().isPresent();
}).collect(Collectors.toList());
// Delete guarantees
guaranteesToDelete.forEach(guarantee -> {
EntityEvent<IdmContractSliceGuaranteeDto> guaranteeEvent = new ContractSliceGuaranteeEvent(ContractSliceGuaranteeEventType.DELETE, guarantee, ImmutableMap.of(ProvisioningService.SKIP_PROVISIONING, skipProvisioning));
guaranteeService.publish(guaranteeEvent);
});
// Create new guarantees
guaranteesToAdd.forEach(identity -> {
IdmContractSliceGuaranteeDto guarantee = new IdmContractSliceGuaranteeDto();
guarantee.setContractSlice(slice.getId());
guarantee.setGuarantee(identity.getId());
//
EntityEvent<IdmContractSliceGuaranteeDto> guaranteeEvent = new ContractSliceGuaranteeEvent(ContractSliceGuaranteeEventType.CREATE, guarantee, ImmutableMap.of(ProvisioningService.SKIP_PROVISIONING, skipProvisioning));
guaranteeService.publish(guaranteeEvent);
});
}
return slice;
}
use of eu.bcvsolutions.idm.core.model.event.ContractSliceEvent in project CzechIdMng by bcvsolutions.
the class ClearDirtyStateForContractSliceTaskExecutor method processItemToDelete.
/**
* Process one dirty state for contract slice to delete
*
* @param dirtyState
*/
private void processItemToDelete(IdmEntityStateDto dirtyState) {
try {
if (dirtyState.getOwnerType() == null || !dirtyState.getOwnerType().equals(IdmContractSlice.class.getName())) {
this.logItemProcessed(dirtyState, new OperationResult.Builder(OperationState.NOT_EXECUTED).build());
return;
}
IdmContractSliceDto contractSliceDto = contractSliceService.get(dirtyState.getOwnerId());
;
if (contractSliceDto == null) {
DefaultResultModel model = new DefaultResultModel(CoreResultCode.NOT_FOUND, ImmutableMap.of("ownerId", dirtyState.getOwnerId()));
this.logItemProcessed(dirtyState, new OperationResult.Builder(OperationState.NOT_EXECUTED).setModel(model).build());
return;
}
ResultModel resultModel = dirtyState.getResult().getModel();
Map<String, Object> parameters = new HashMap<>();
if (resultModel != null) {
parameters = resultModel.getParameters();
}
// Transform saved parameters into map string and serializable value
Map<String, Serializable> transformedParameters = transformParameters(parameters);
EntityEvent<IdmContractSliceDto> event = new ContractSliceEvent(ContractSliceEventType.DELETE, contractSliceDto, transformedParameters);
// Delete slice (with recalculation)
contractSliceService.publish(event);
this.logItemProcessed(contractSliceDto, new OperationResult.Builder(OperationState.EXECUTED).build());
} catch (Exception e) {
this.logItemProcessed(dirtyState, new OperationResult.Builder(OperationState.EXCEPTION).setCause(e).build());
}
}
use of eu.bcvsolutions.idm.core.model.event.ContractSliceEvent in project CzechIdMng by bcvsolutions.
the class ContractSliceManagerTest method clearStateExecutorTest.
@Test
public void clearStateExecutorTest() {
IdmIdentityDto identity = this.getHelper().createIdentity();
// remove all contracts
List<IdmIdentityContractDto> allByIdentity = contractService.findAllByIdentity(identity.getId());
allByIdentity.forEach(contract -> {
contractService.delete(contract);
});
IdmContractSliceDto slice = new IdmContractSliceDto();
slice.setContractCode("test");
slice.setIdentity(identity.getId());
slice.setValidFrom(LocalDate.now().minusDays(1));
slice.setValidTill(LocalDate.now().plusDays(5));
slice.setContractValidFrom(LocalDate.now().minusDays(50));
slice.setMain(true);
IdmContractSliceDto sliceTwo = new IdmContractSliceDto();
sliceTwo.setContractCode("test");
sliceTwo.setIdentity(identity.getId());
sliceTwo.setValidFrom(LocalDate.now().minusDays(10));
sliceTwo.setValidTill(LocalDate.now().minusDays(2));
sliceTwo.setContractValidFrom(LocalDate.now().minusDays(50));
sliceTwo.setMain(true);
EventContext<IdmContractSliceDto> eventContextOne = contractSliceService.publish(new ContractSliceEvent(ContractSliceEventType.CREATE, slice, ImmutableMap.of(IdmContractSliceService.SET_DIRTY_STATE_CONTRACT_SLICE, Boolean.TRUE)));
EventContext<IdmContractSliceDto> eventContextTwo = contractSliceService.publish(new ContractSliceEvent(ContractSliceEventType.CREATE, sliceTwo, ImmutableMap.of(IdmContractSliceService.SET_DIRTY_STATE_CONTRACT_SLICE, Boolean.TRUE)));
// slice has skip recalculation and dirty state isn't create
allByIdentity = contractService.findAllByIdentity(identity.getId());
assertTrue(allByIdentity.isEmpty());
List<IdmEntityStateDto> dirtyStates = findDirtyStatesForSlice(null);
assertFalse(dirtyStates.isEmpty());
assertEquals(2, dirtyStates.size());
ClearDirtyStateForContractSliceTaskExecutor executor = new ClearDirtyStateForContractSliceTaskExecutor();
OperationResult result = longRunningTaskManager.executeSync(executor);
assertEquals(OperationState.EXECUTED, result.getState());
dirtyStates = findDirtyStatesForSlice(null);
assertTrue(dirtyStates.isEmpty());
assertEquals(0, dirtyStates.size());
allByIdentity = contractService.findAllByIdentity(identity.getId());
assertEquals(1, allByIdentity.size());
IdmIdentityContractDto contractDto = allByIdentity.get(0);
assertTrue(contractDto.getControlledBySlices());
assertEquals(LocalDate.now().minusDays(50), contractDto.getValidFrom());
// Delete unused slices
contractSliceService.delete(eventContextOne.getContent());
contractSliceService.delete(eventContextTwo.getContent());
}
use of eu.bcvsolutions.idm.core.model.event.ContractSliceEvent in project CzechIdMng by bcvsolutions.
the class ContractSliceManagerTest method skipRecalculationTest.
@Test
public void skipRecalculationTest() {
IdmIdentityDto identity = this.getHelper().createIdentity();
// remove all contracts
List<IdmIdentityContractDto> allByIdentity = contractService.findAllByIdentity(identity.getId());
allByIdentity.forEach(contract -> {
contractService.delete(contract);
});
IdmContractSliceDto slice = new IdmContractSliceDto();
slice.setContractCode("test");
slice.setIdentity(identity.getId());
slice.setValidFrom(LocalDate.now().minusDays(5));
slice.setValidTill(LocalDate.now().plusDays(5));
slice.setContractValidFrom(LocalDate.now().minusDays(5));
slice.setMain(true);
EventContext<IdmContractSliceDto> eventContext = contractSliceService.publish(new ContractSliceEvent(ContractSliceEventType.CREATE, slice, ImmutableMap.of(IdmContractSliceService.SKIP_RECALCULATE_CONTRACT_SLICE, Boolean.TRUE)));
// slice has skip recalculation and dirty state isn't create
allByIdentity = contractService.findAllByIdentity(identity.getId());
assertTrue(allByIdentity.isEmpty());
List<IdmEntityStateDto> dirtyStates = findDirtyStatesForSlice(null);
assertTrue(dirtyStates.isEmpty());
// Delete unused slice
contractSliceService.delete(eventContext.getContent());
}
Aggregations