use of eu.bcvsolutions.idm.core.model.event.ContractSliceEvent in project CzechIdMng by bcvsolutions.
the class ContractSliceManagerTest method setDirtyStateAndReferentialIntegrityTest.
@Test
public void setDirtyStateAndReferentialIntegrityTest() {
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> context = contractSliceService.publish(new ContractSliceEvent(ContractSliceEventType.CREATE, slice, ImmutableMap.of(IdmContractSliceService.SET_DIRTY_STATE_CONTRACT_SLICE, Boolean.TRUE)));
IdmContractSliceDto sliceDto = context.getContent();
// slice has skip recalculation and dirty state isn't create
allByIdentity = contractService.findAllByIdentity(identity.getId());
assertTrue(allByIdentity.isEmpty());
List<IdmEntityStateDto> dirtyStates = findDirtyStatesForSlice(sliceDto.getId());
assertFalse(dirtyStates.isEmpty());
assertEquals(1, dirtyStates.size());
contractSliceService.delete(sliceDto);
dirtyStates = findDirtyStatesForSlice(sliceDto.getId());
assertTrue(dirtyStates.isEmpty());
}
use of eu.bcvsolutions.idm.core.model.event.ContractSliceEvent in project CzechIdMng by bcvsolutions.
the class ContractSliceDeleteProcessor method process.
@Override
public EventResult<IdmContractSliceDto> process(EntityEvent<IdmContractSliceDto> event) {
IdmContractSliceDto slice = event.getContent();
// delete. Deleting is provided by ClearDirtyStateForContractSliceTaskExecutor!
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());
// Creates new dirty states, dirty states must be process by executor
createDeleteDirtyState(slice, properties);
LOG.info("Property SET_DIRTY_STATE_CONTRACT_SLICE is true -> Slice [{}] is not deleted but only marked as DIRTY." + " Deleting will be processed in ClearDirtyStateForContractSliceTaskExecutor!", slice.toString());
return new DefaultEventResult<>(event, this);
}
// delete dirty states for contract slice
findAllDirtyStatesForSlices(slice.getId()).forEach(dirtyState -> {
entityStateManager.deleteState(dirtyState);
});
// delete contract slice guarantees
IdmContractSliceGuaranteeFilter filter = new IdmContractSliceGuaranteeFilter();
filter.setContractSliceId(slice.getId());
contractGuaranteeService.find(filter, null).forEach(guarantee -> {
contractGuaranteeService.delete(guarantee);
});
UUID contractId = slice.getParentContract();
if (contractId != null) {
List<IdmContractSliceDto> slices = contractSliceManager.findAllSlices(contractId);
if (slices.size() == 1) {
// Internal delete of slice
service.deleteInternal(slice);
// This slice is last for parent contract. We will also deleted the parent
// contract;
contractService.deleteById(contractId);
return new DefaultEventResult<>(event, this);
}
// Find next slice
IdmContractSliceDto nextSlice = contractSliceManager.findNextSlice(slice, slices);
// Internal delete of slice
service.deleteInternal(slice);
// If exists next slice, then update valid till on previous slice
if (nextSlice != null) {
contractSliceManager.updateValidTillOnPreviousSlice(nextSlice, contractSliceManager.findAllSlices(contractId));
} else {
// If next slice doesn't exists, then we need to find previous slice (last after deleting) and set valid till to infinity.
IdmContractSliceDto previousSlice = contractSliceManager.findPreviousSlice(slice, contractSliceManager.findAllSlices(contractId));
if (previousSlice != null) {
// Previous slice will be valid till infinity
previousSlice.setValidTill(null);
contractSliceService.publish(new ContractSliceEvent(ContractSliceEventType.UPDATE, previousSlice, ImmutableMap.of(IdmContractSliceService.SKIP_RECALCULATE_CONTRACT_SLICE, Boolean.TRUE)));
}
}
IdmContractSliceDto validSlice = contractSliceManager.findValidSlice(contractId);
if (validSlice != null) {
// Set next slice as is currently using in contract
contractSliceManager.setSliceAsCurrentlyUsing(validSlice, event.getProperties());
}
} else {
service.deleteInternal(slice);
}
return new DefaultEventResult<>(event, this);
}
use of eu.bcvsolutions.idm.core.model.event.ContractSliceEvent in project CzechIdMng by bcvsolutions.
the class ContractSliceManagerTest method selectCurrentSliceAsContractLrtTest.
@Test
public void selectCurrentSliceAsContractLrtTest() {
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());
// None invalid slices
List<IdmContractSliceDto> unvalidSlices = contractSliceManager.findUnvalidSlices(null).getContent();
assertEquals(0, unvalidSlices.size());
// Set current slice as not currently using
sliceCurrent = contractSliceService.get(sliceCurrent.getId());
sliceCurrent.setUsingAsContract(false);
// Save without recalculation
contractSliceService.publish(new ContractSliceEvent(ContractSliceEventType.UPDATE, sliceCurrent, ImmutableMap.of(IdmContractSliceService.SKIP_RECALCULATE_CONTRACT_SLICE, Boolean.TRUE)));
// One invalid slice
unvalidSlices = contractSliceManager.findUnvalidSlices(null).getContent();
assertEquals(1, unvalidSlices.size());
SelectCurrentContractSliceTaskExecutor lrt = new SelectCurrentContractSliceTaskExecutor();
AutowireHelper.autowire(lrt);
OperationResult result = lrt.process();
assertEquals(OperationState.EXECUTED, result.getState());
}
use of eu.bcvsolutions.idm.core.model.event.ContractSliceEvent in project CzechIdMng by bcvsolutions.
the class ContractSliceManagerTest method setDirtyStateTest.
@Test
public void setDirtyStateTest() {
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.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(1, dirtyStates.size());
// delete the states
entityStateManager.deleteState(dirtyStates.get(0));
// Delete unused slice
contractSliceService.delete(eventContext.getContent());
}
use of eu.bcvsolutions.idm.core.model.event.ContractSliceEvent in project CzechIdMng by bcvsolutions.
the class DefaultContractSliceManager method recalculateContractSlice.
@Override
public void recalculateContractSlice(IdmContractSliceDto slice, IdmContractSliceDto originalSlice, Map<String, Serializable> eventProperties) {
boolean forceRecalculateCurrentUsingSlice = false;
Object forceRecalculateCurrentUsingSliceAsObject = eventProperties.get(IdmContractSliceService.FORCE_RECALCULATE_CURRENT_USING_SLICE);
if (forceRecalculateCurrentUsingSliceAsObject == null) {
forceRecalculateCurrentUsingSlice = false;
} else if (forceRecalculateCurrentUsingSliceAsObject instanceof Boolean) {
forceRecalculateCurrentUsingSlice = (Boolean) forceRecalculateCurrentUsingSliceAsObject;
} else {
forceRecalculateCurrentUsingSlice = false;
}
boolean recalculateUsingAsContract = false;
if (slice.getIdentity() != null) {
UUID parentContract = slice.getParentContract();
// null (will be recalculated)
if (originalSlice != null && !Objects.equal(originalSlice.getContractCode(), slice.getContractCode())) {
slice.setParentContract(null);
// When external code changed, link or create new contract is required
parentContract = null;
}
if (originalSlice != null && !Objects.equal(originalSlice.getParentContract(), slice.getParentContract())) {
slice.setParentContract(null);
slice.setUsingAsContract(false);
}
if (parentContract == null) {
slice = linkOrCreateContract(slice, eventProperties);
} else {
// Update contract by that slice
if (slice.isUsingAsContract()) {
// Validity of slice was changed, slice cannot be using for update the contract
if (originalSlice != null && !Objects.equal(originalSlice.getValidFrom(), slice.getValidFrom())) {
recalculateUsingAsContract = true;
} else {
IdmIdentityContractDto contract = contractService.get(parentContract);
this.getBean().updateContractBySlice(contract, slice, eventProperties);
}
}
}
}
UUID parentContract = slice.getParentContract();
if (originalSlice == null) {
// Slice is new, we want to recalculate "Is using as contract" field.
recalculateUsingAsContract = true;
}
// Recalculate on change of 'parentContract' field
boolean parentContractChanged = false;
if (originalSlice != null && !Objects.equal(originalSlice.getParentContract(), slice.getParentContract())) {
UUID originalParentContract = originalSlice.getParentContract();
// Parent contract was changed ... we need recalculate parent contract for
// original slice
parentContractChanged = true;
if (originalParentContract != null) {
IdmIdentityContractDto originalContract = contractService.get(originalParentContract);
// Find other slices for original contract
IdmContractSliceFilter sliceFilter = new IdmContractSliceFilter();
sliceFilter.setParentContract(originalParentContract);
List<IdmContractSliceDto> originalSlices = contractSliceService.find(sliceFilter, null).getContent();
if (!originalSlices.isEmpty()) {
IdmContractSliceDto originalNextSlice = this.getBean().findNextSlice(originalSlice, originalSlices);
IdmContractSliceDto originalSliceToUpdate = originalNextSlice;
if (originalNextSlice != null) {
// Next slice exists, update valid-till on previous slice by that slice
IdmContractSliceDto originalPreviousSlice = this.getBean().findPreviousSlice(originalNextSlice, originalSlices);
if (originalPreviousSlice != null) {
originalPreviousSlice.setValidTill(originalNextSlice.getValidFrom().minusDays(1));
originalSliceToUpdate = originalPreviousSlice;
}
} else {
// Next slice does not exists. I means original slice was last. Set valid-till
// on previous slice to null.
IdmContractSliceDto originalPreviousSlice = this.getBean().findPreviousSlice(originalSlice, originalSlices);
if (originalPreviousSlice != null && this.getBean().findNextSlice(originalPreviousSlice, originalSlices) == null) {
originalPreviousSlice.setValidTill(null);
originalSliceToUpdate = originalPreviousSlice;
}
}
// Save with force recalculation
contractSliceService.publish(new ContractSliceEvent(ContractSliceEventType.UPDATE, originalSliceToUpdate, ImmutableMap.of(IdmContractSliceService.FORCE_RECALCULATE_CURRENT_USING_SLICE, Boolean.TRUE)));
} else {
// Parent contract was changed and old contract does not have next slice, we
// have to delete him.
// Delete contract
contractService.publish(new IdentityContractEvent(IdentityContractEventType.DELETE, originalContract, ImmutableMap.copyOf(eventProperties))).getContent();
}
}
// Parent contract was changed, want to recalculate "Is using as contract"
// field.
recalculateUsingAsContract = true;
}
// contract
if (originalSlice == null || parentContractChanged || (originalSlice != null && !Objects.equal(originalSlice.getValidFrom(), slice.getValidFrom()))) {
// slice
if (parentContract != null) {
// Find other slices for parent contract
List<IdmContractSliceDto> slices = this.getBean().findAllSlices(parentContract);
if (!slices.isEmpty()) {
// Update validity till on this slice and on previous slice
recalculateValidTill(slice, slices);
// original slice)
if (originalSlice != null) {
IdmContractSliceDto nextSliceForOriginalSlice = this.getBean().findNextSlice(originalSlice, slices);
if (nextSliceForOriginalSlice == null) {
// Next slice not exists, it means original slice was last
IdmContractSliceDto previousSliceForOriginalSlice = this.getBean().findPreviousSlice(originalSlice, slices);
if (previousSliceForOriginalSlice != null && this.getBean().findNextSlice(previousSliceForOriginalSlice, slices) == null) {
previousSliceForOriginalSlice.setValidTill(null);
// Save with skip this processor
saveWithoutRecalculate(previousSliceForOriginalSlice);
}
}
}
}
}
// Validity from was changed, want to recalculate "Is using as contract" field.
recalculateUsingAsContract = true;
}
if (recalculateUsingAsContract || forceRecalculateCurrentUsingSlice) {
IdmContractSliceFilter sliceFilter = new IdmContractSliceFilter();
sliceFilter.setParentContract(parentContract);
sliceFilter.setUsingAsContract(Boolean.TRUE);
IdmContractSliceDto shouldBeSetAsUsing = this.getBean().findValidSlice(parentContract);
if (shouldBeSetAsUsing != null) {
Map<String, Serializable> clonedProperties = new HashMap<>(eventProperties);
if (clonedProperties.containsKey(IdmContractSliceService.FORCE_RECALCULATE_CURRENT_USING_SLICE)) {
clonedProperties.remove(IdmContractSliceService.FORCE_RECALCULATE_CURRENT_USING_SLICE);
}
shouldBeSetAsUsing = this.getBean().setSliceAsCurrentlyUsing(shouldBeSetAsUsing, clonedProperties);
if (slice.equals(shouldBeSetAsUsing)) {
// If that slice should be using as contract, then we using returned instance
// (instead the reload slice from DB)
slice = shouldBeSetAsUsing;
}
}
}
// Check if is slice new or contract valid till field was changed.
if (originalSlice == null || (!Objects.equal(originalSlice.getContractValidTill(), slice.getContractValidTill()))) {
// If is slice last, then will be to slice valid till copy date of contract
// valid till
boolean isSliceLast = this.getBean().findNextSlice(slice, this.getBean().findAllSlices(parentContract)) == null ? true : false;
if (isSliceLast) {
slice.setValidTill(null);
saveWithoutRecalculate(slice);
}
}
}
Aggregations