Search in sources :

Example 1 with IdmGenerateValueDto

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);
}
Also used : IdmFormAttributeDto(eu.bcvsolutions.idm.core.eav.api.dto.IdmFormAttributeDto) Autowired(org.springframework.beans.factory.annotation.Autowired) Random(java.util.Random) StringUtils(org.apache.commons.lang3.StringUtils) PersistentType(eu.bcvsolutions.idm.core.eav.api.domain.PersistentType) BigDecimal(java.math.BigDecimal) IdmIdentityFilter(eu.bcvsolutions.idm.core.api.dto.filter.IdmIdentityFilter) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) Math(java.lang.Math) Sort(org.springframework.data.domain.Sort) AbstractValueGenerator(eu.bcvsolutions.idm.core.api.generator.AbstractValueGenerator) Description(org.springframework.context.annotation.Description) Predicate(java.util.function.Predicate) IdmIdentityDto(eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto) PageRequest(org.springframework.data.domain.PageRequest) Page(org.springframework.data.domain.Page) Collectors(java.util.stream.Collectors) List(java.util.List) Component(org.springframework.stereotype.Component) CoreResultCode(eu.bcvsolutions.idm.core.api.domain.CoreResultCode) IdmIdentity_(eu.bcvsolutions.idm.core.model.entity.IdmIdentity_) Pattern(java.util.regex.Pattern) IdmIdentityService(eu.bcvsolutions.idm.core.api.service.IdmIdentityService) IdmGenerateValueDto(eu.bcvsolutions.idm.core.api.dto.IdmGenerateValueDto) Assert(org.springframework.util.Assert) IdmIdentityFilter(eu.bcvsolutions.idm.core.api.dto.filter.IdmIdentityFilter) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) IdmIdentityDto(eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto)

Example 2 with IdmGenerateValueDto

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;
}
Also used : IdmGenerateValueDto(eu.bcvsolutions.idm.core.api.dto.IdmGenerateValueDto) IdmGenerateValueFilter(eu.bcvsolutions.idm.core.api.dto.filter.IdmGenerateValueFilter) ArrayList(java.util.ArrayList)

Example 3 with IdmGenerateValueDto

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;
}
Also used : IdmGenerateValueDto(eu.bcvsolutions.idm.core.api.dto.IdmGenerateValueDto)

Example 4 with IdmGenerateValueDto

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);
}
Also used : IdentityRoleFormDefaultValueGenerator(eu.bcvsolutions.idm.core.generator.role.IdentityRoleFormDefaultValueGenerator) IdmGenerateValueDto(eu.bcvsolutions.idm.core.api.dto.IdmGenerateValueDto) IdentityFormDefaultValueGenerator(eu.bcvsolutions.idm.core.generator.identity.IdentityFormDefaultValueGenerator) ConceptRoleRequestFormDefaultValueGenerator(eu.bcvsolutions.idm.core.generator.role.ConceptRoleRequestFormDefaultValueGenerator) DefaultEventResult(eu.bcvsolutions.idm.core.api.event.DefaultEventResult) IdmConceptRoleRequestDto(eu.bcvsolutions.idm.core.api.dto.IdmConceptRoleRequestDto) IdmIdentityDto(eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto) IdmIdentityRoleDto(eu.bcvsolutions.idm.core.api.dto.IdmIdentityRoleDto)

Example 5 with IdmGenerateValueDto

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);
}
Also used : IdmGenerateValueDto(eu.bcvsolutions.idm.core.api.dto.IdmGenerateValueDto)

Aggregations

IdmGenerateValueDto (eu.bcvsolutions.idm.core.api.dto.IdmGenerateValueDto)20 AbstractGeneratorTest (eu.bcvsolutions.idm.core.api.generator.AbstractGeneratorTest)10 Test (org.junit.Test)10 IdmTreeNodeDto (eu.bcvsolutions.idm.core.api.dto.IdmTreeNodeDto)8 IdmTreeTypeDto (eu.bcvsolutions.idm.core.api.dto.IdmTreeTypeDto)8 IdmIdentityDto (eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto)6 ValueGeneratorDto (eu.bcvsolutions.idm.core.api.dto.ValueGeneratorDto)2 AbstractValueGenerator (eu.bcvsolutions.idm.core.api.generator.AbstractValueGenerator)2 IdmFormAttributeDto (eu.bcvsolutions.idm.core.eav.api.dto.IdmFormAttributeDto)2 IdentityFormDefaultValueGenerator (eu.bcvsolutions.idm.core.generator.identity.IdentityFormDefaultValueGenerator)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Collectors (java.util.stream.Collectors)2 StringUtils (org.apache.commons.lang3.StringUtils)2 Autowired (org.springframework.beans.factory.annotation.Autowired)2 Assert (org.springframework.util.Assert)2 ConfigurationMap (eu.bcvsolutions.idm.core.api.domain.ConfigurationMap)1 CoreResultCode (eu.bcvsolutions.idm.core.api.domain.CoreResultCode)1 AbstractDto (eu.bcvsolutions.idm.core.api.dto.AbstractDto)1 IdmConceptRoleRequestDto (eu.bcvsolutions.idm.core.api.dto.IdmConceptRoleRequestDto)1