use of eu.bcvsolutions.idm.core.api.dto.IdmGenerateValueDto in project CzechIdMng by bcvsolutions.
the class IdentityAnonymousUsernameGenerator method generate.
@Override
public IdmIdentityDto generate(IdmIdentityDto dto, IdmGenerateValueDto valueGenerator) {
Assert.notNull(dto, "DTO is required.");
Assert.notNull(valueGenerator, "Value generator is required.");
// if exists username and configuration doesn't allow regenerate return dto
if (!valueGenerator.isRegenerateValue() && StringUtils.isNotEmpty(dto.getUsername())) {
return dto;
}
// generator configuration
int numPartLen = getNumberPartLength(valueGenerator);
int numPartMax = calcMaxValueForLen(numPartLen);
String prefix = getPrefixString(valueGenerator);
// try to generate the new unique username within several attempts
int attepts = GENERATE_ATTEMPTS;
do {
int randomPartVal = generateNumberPart(numPartMax);
String username = createUsername(prefix, randomPartVal, numPartLen);
// find at the first attempt OK
if (!usernameInUse(username)) {
dto.setUsername(username);
return dto;
}
} while (--attepts != 0);
// unsuccessful with random generation -> search for empty number slots
String usernameRoot = createUsername(prefix, null, null);
IdmIdentityFilter identityFilt = new IdmIdentityFilter();
identityFilt.setText(usernameRoot);
Page<IdmIdentityDto> page = null;
int pageSize = SEARCH_PAGE_SIZE;
int pageNum = 0;
do {
page = identityService.find(identityFilt, PageRequest.of(pageNum, pageSize, Sort.by(IdmIdentity_.username.getName()).ascending()));
List<IdmIdentityDto> dtos = page.getContent();
List<String> usernameNumbers = dtos.stream().map(IdmIdentityDto::getUsername).filter(usernameFilterFactory(usernameRoot, numPartLen)).map(name -> name.replace(usernameRoot, "")).collect(Collectors.toList());
Integer newRandomPartVal = findEmptySlotFromRange(usernameNumbers, 0, usernameNumbers.size() - 1);
if (newRandomPartVal != null) {
String username = createUsername(prefix, newRandomPartVal, Integer.valueOf(numPartLen));
dto.setUsername(username);
return dto;
}
pageNum++;
} while (pageNum < page.getTotalPages());
// unable to find empty space by bisect? try min and max values which may still be missing
// first index
String username = createUsername(prefix, 0, numPartLen);
if (!usernameInUse(username)) {
dto.setUsername(username);
return dto;
}
// last index
username = createUsername(prefix, numPartMax, numPartLen);
if (!usernameInUse(username)) {
dto.setUsername(username);
return dto;
}
// it's over nothing remains to try
LOG.warn("The anonymous username generator reached the limit of the count of available numbers. Increase nuemric part length in the generator setting.");
throw new ResultCodeException(CoreResultCode.IDENTITY_UNABLE_GENERATE_UNIQUE_USERNAME);
}
use of eu.bcvsolutions.idm.core.api.dto.IdmGenerateValueDto in project CzechIdMng by bcvsolutions.
the class DefaultValueGeneratorManager method getEnabledGenerateValues.
private List<IdmGenerateValueDto> getEnabledGenerateValues(Class<? extends AbstractDto> dtoType) {
Assert.notNull(dtoType, "DTO type is required.");
//
IdmGenerateValueFilter filter = new IdmGenerateValueFilter();
filter.setDisabled(Boolean.FALSE);
filter.setDtoType(dtoType.getCanonicalName());
// we must create new instance of array list, given list is unmodifiable
List<IdmGenerateValueDto> generateValues = new ArrayList<>(service.find(filter, null).getContent());
// sort by order
Collections.sort(generateValues, new Comparator<IdmGenerateValueDto>() {
@Override
public int compare(IdmGenerateValueDto o1, IdmGenerateValueDto o2) {
return Shorts.compare(o1.getSeq(), o2.getSeq());
}
});
return generateValues;
}
use of eu.bcvsolutions.idm.core.api.dto.IdmGenerateValueDto in project CzechIdMng by bcvsolutions.
the class DefaultValueGeneratorManager method generate.
@Override
public <DTO extends AbstractDto> DTO generate(DTO dto) {
Assert.notNull(dto, "DTO is required.");
//
List<IdmGenerateValueDto> enabledGenerateValues = getEnabledGenerateValues(dto.getClass());
// iterate over saved and enabled generators in DB for given entity type
for (IdmGenerateValueDto generateValue : enabledGenerateValues) {
ValueGenerator<DTO> generator = getGenerator(generateValue);
if (generator == null) {
LOG.warn("Generator for type [{}] doesn't exist. Or more generators found for type.", generateValue.getGeneratorType());
continue;
}
if (!generator.supports(dto.getClass())) {
LOG.warn("Generator for type [{}] doesn't support given dto type [{}]. Fix your configuration [{}].", generateValue.getGeneratorType(), dto.getClass(), generateValue.getId());
continue;
}
//
DTO generatedDto = generator.generate(dto, generateValue);
if (generatedDto == null) {
LOG.debug("Generator [{}] returned null, original dto will be returned", generator.getName());
} else {
dto = generatedDto;
}
}
//
return dto;
}
use of eu.bcvsolutions.idm.core.api.dto.IdmGenerateValueDto in project CzechIdMng by bcvsolutions.
the class InitGeneratorProcessor method process.
@Override
public EventResult<ModuleDescriptorDto> process(EntityEvent<ModuleDescriptorDto> event) {
// register default value generators
if (generateValueService.get(DEFAULT_FORM_GENERATE_VALUE_ID) == null) {
IdmGenerateValueDto generateValue = new IdmGenerateValueDto(DEFAULT_FORM_GENERATE_VALUE_ID);
generateValue.setDtoType(IdmIdentityDto.class.getCanonicalName());
generateValue.setGeneratorType(IdentityFormDefaultValueGenerator.class.getCanonicalName());
generateValue.setSeq((short) 100);
generateValue.setUnmodifiable(true);
generateValueService.save(generateValue);
}
if (generateValueService.get(DEFAULT_CONCEPT_ROLE_REQUEST_FORM_GENERATE_VALUE_ID) == null) {
IdmGenerateValueDto generateValue = new IdmGenerateValueDto(DEFAULT_CONCEPT_ROLE_REQUEST_FORM_GENERATE_VALUE_ID);
generateValue.setDtoType(IdmConceptRoleRequestDto.class.getCanonicalName());
generateValue.setGeneratorType(ConceptRoleRequestFormDefaultValueGenerator.class.getCanonicalName());
generateValue.setSeq((short) 100);
generateValue.setUnmodifiable(true);
generateValueService.save(generateValue);
}
if (generateValueService.get(DEFAULT_IDENTITY_ROLE_FORM_GENERATE_VALUE_ID) == null) {
IdmGenerateValueDto generateValue = new IdmGenerateValueDto(DEFAULT_IDENTITY_ROLE_FORM_GENERATE_VALUE_ID);
generateValue.setDtoType(IdmIdentityRoleDto.class.getCanonicalName());
generateValue.setGeneratorType(IdentityRoleFormDefaultValueGenerator.class.getCanonicalName());
generateValue.setSeq((short) 100);
generateValue.setUnmodifiable(true);
generateValueService.save(generateValue);
}
//
return new DefaultEventResult<>(event, this);
}
use of eu.bcvsolutions.idm.core.api.dto.IdmGenerateValueDto in project CzechIdMng by bcvsolutions.
the class ConceptRoleRequestFormDefaultValueGeneratorTest method createGenerator.
/**
* Method create generator for this test and remove all another generators.
*
* @return
*/
private IdmGenerateValueDto createGenerator() {
IdmGenerateValueDto generateValue = new IdmGenerateValueDto();
generateValue.setDtoType(getDtoType().getCanonicalName());
generateValue.setGeneratorType(getGeneratorType());
generateValue.setSeq((short) 100);
generateValue.setUnmodifiable(true);
return generateValueService.save(generateValue);
}
Aggregations