use of eu.bcvsolutions.idm.core.api.dto.IdmContractSliceDto in project CzechIdMng by bcvsolutions.
the class DefaultContractSliceManager method copyGuarantees.
@Transactional
@Override
public void copyGuarantees(IdmContractSliceDto slice, IdmIdentityContractDto contract) {
Assert.notNull(slice, "Contract slice is required.");
Assert.notNull(slice.getId(), "Contract slice identifier is required.");
Assert.notNull(contract, "Contract is required.");
Assert.notNull(contract.getId(), "Contract identifier is required.");
IdmContractSliceGuaranteeFilter guaranteeFilter = new IdmContractSliceGuaranteeFilter();
guaranteeFilter.setContractSliceId(slice.getId());
List<IdmContractSliceGuaranteeDto> guarantees = contractSliceGuaranteeService.find(guaranteeFilter, null).getContent();
List<IdmContractGuaranteeDto> resultGuarantees = new ArrayList<>();
guarantees.forEach(guarantee -> {
IdmContractGuaranteeDto result = this.cloneGuarante(guarantee);
result.setIdentityContract(contract.getId());
resultGuarantees.add(result);
});
IdmContractGuaranteeFilter contractGuaranteeFilter = new IdmContractGuaranteeFilter();
contractGuaranteeFilter.setIdentityContractId(contract.getId());
List<IdmContractGuaranteeDto> currentGuarantees = contractGuaranteeService.find(contractGuaranteeFilter, null).getContent();
// Find and create new guarantees
resultGuarantees.stream().filter(guarantee -> {
//
return !//
currentGuarantees.stream().filter(//
cg -> guarantee.getGuarantee().equals(cg.getGuarantee())).findFirst().isPresent();
}).forEach(guaranteeToAdd -> {
ContractGuaranteeEvent event = new ContractGuaranteeEvent(contractGuaranteeService.isNew(guaranteeToAdd) ? ContractGuaranteeEventType.CREATE : ContractGuaranteeEventType.UPDATE, guaranteeToAdd, ImmutableMap.of(ContractSliceManager.SKIP_CHECK_FOR_SLICES, Boolean.TRUE));
contractGuaranteeService.publish(event);
});
// Find and remove guarantees which missing in the current result set
currentGuarantees.stream().filter(guarantee -> {
//
return !//
resultGuarantees.stream().filter(//
cg -> guarantee.getGuarantee().equals(cg.getGuarantee())).findFirst().isPresent();
}).forEach(guaranteeToRemove -> {
ContractGuaranteeEvent event = new ContractGuaranteeEvent(ContractGuaranteeEventType.DELETE, guaranteeToRemove, ImmutableMap.of(ContractSliceManager.SKIP_CHECK_FOR_SLICES, Boolean.TRUE));
contractGuaranteeService.publish(event);
});
}
use of eu.bcvsolutions.idm.core.api.dto.IdmContractSliceDto in project CzechIdMng by bcvsolutions.
the class DefaultContractSliceManager method resolveProtectionInterval.
/**
* Protection mode is activate. Check if the next slice has valid from of
* contract lover then contract valid till (plus protection interval) on given
* slice, then contract will does not terminated (his valid till will be sets by
* valid from on next slice)
*
* @param slice
* @param contract
* @param protectionInterval
*/
private void resolveProtectionInterval(IdmContractSliceDto slice, IdmIdentityContractDto contract, int protectionInterval) {
Assert.notNull(contract, "Contract is required.");
Assert.notNull(contract.getId(), "Contract identifier is required.");
List<IdmContractSliceDto> slices = this.findAllSlices(contract.getId());
IdmContractSliceDto nextSlice = this.findNextSlice(slice, slices);
if (nextSlice == null) {
// None next slice exists ... contract was not changed
return;
}
if (nextSlice.getContractValidFrom() == null) {
LOG.info(MessageFormat.format("Update contract by slice [{0}] - Resloving of the protection interval - next slice has contract valid from sets to 'null' ... contract [{1}] was changed!", slice, contract));
contract.setValidTill(null);
return;
}
long diffInDays = //
ChronoUnit.DAYS.between(//
java.time.LocalDate.parse(slice.getContractValidTill().toString()), java.time.LocalDate.parse(nextSlice.getContractValidFrom().toString()));
// till of contract in current slice. We will do nothing.
if (diffInDays <= 0) {
return;
}
if (diffInDays <= protectionInterval) {
LOG.info(MessageFormat.format("Update contract by slice [{0}] - Resloving of the protection interval - next slice has contract valid from sets to [{2}] ... contract [{1}] was changed!", slice, contract, nextSlice.getContractValidFrom()));
contract.setValidTill(nextSlice.getContractValidFrom());
}
}
use of eu.bcvsolutions.idm.core.api.dto.IdmContractSliceDto in project CzechIdMng by bcvsolutions.
the class ContractSliceManagerTest method identityFilterTest.
@Test
public void identityFilterTest() {
IdmIdentityDto identity = helper.createIdentity();
IdmTreeNodeDto node = helper.createTreeNode();
IdmTreeNodeDto node2 = helper.createTreeNode();
IdmContractSliceDto slice = helper.createContractSlice(identity, node, null, null, null);
IdmContractSliceDto slice2 = helper.createContractSlice(identity, node2, null, null, null);
IdmContractSliceFilter filter = new IdmContractSliceFilter();
filter.setIdentity(identity.getId());
Page<IdmContractSliceDto> result = contractSliceService.find(filter, null);
assertEquals("Wrong Identity", 2, result.getTotalElements());
assertTrue(result.getContent().contains(slice));
assertTrue(result.getContent().contains(slice2));
}
use of eu.bcvsolutions.idm.core.api.dto.IdmContractSliceDto in project CzechIdMng by bcvsolutions.
the class ContractSliceManagerTest method testCreateSliceWithEAVs.
@Test
public void testCreateSliceWithEAVs() {
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());
}
use of eu.bcvsolutions.idm.core.api.dto.IdmContractSliceDto 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());
}
Aggregations