use of eu.bcvsolutions.idm.core.eav.api.dto.InvalidFormAttributeDto in project CzechIdMng by bcvsolutions.
the class DefaultFormServiceIntegrationTest method testRequiredValidation.
@Test
public void testRequiredValidation() {
// prepare form definition a test saving form values
IdmFormAttributeDto attribute = new IdmFormAttributeDto();
String attributeName = getHelper().createName();
attribute.setCode(attributeName);
attribute.setName(attributeName);
attribute.setPersistentType(PersistentType.SHORTTEXT);
attribute.setRequired(true);
IdmFormDefinitionDto formDefinitionOne = formService.createDefinition(IdmIdentity.class.getCanonicalName(), getHelper().createName(), Lists.newArrayList(attribute));
attribute = formDefinitionOne.getMappedAttributeByCode(attribute.getCode());
//
IdmFormValueDto valueOne = new IdmFormValueDto(attribute);
IdmFormValueDto valueTwo = new IdmFormValueDto(attribute);
valueTwo.setShortTextValue("value");
//
IdmFormInstanceDto formInstance = new IdmFormInstanceDto();
formInstance.setFormDefinition(formDefinitionOne);
formInstance.setValues(Lists.newArrayList(valueOne));
//
List<InvalidFormAttributeDto> validationErrors = formService.validate(formInstance);
//
Assert.assertEquals(1, validationErrors.size());
Assert.assertTrue(validationErrors.stream().anyMatch(error -> error.isMissingValue()));
Assert.assertTrue(validationErrors.stream().anyMatch(error -> error.getAttributeCode().equals(attributeName)));
//
valueOne.setShortTextValue("value");
validationErrors = formService.validate(formInstance);
//
Assert.assertTrue(validationErrors.isEmpty());
formService.validate(formInstance);
//
Assert.assertTrue(validationErrors.isEmpty());
//
formInstance.setValues(Lists.newArrayList());
//
validationErrors = formService.validate(formInstance);
//
Assert.assertEquals(1, validationErrors.size());
Assert.assertTrue(validationErrors.stream().anyMatch(error -> error.isMissingValue()));
Assert.assertTrue(validationErrors.stream().anyMatch(error -> error.getAttributeCode().equals(attributeName)));
//
formInstance.setValues(Lists.newArrayList(new IdmFormValueDto(attribute)));
//
validationErrors = formService.validate(formInstance);
//
Assert.assertEquals(1, validationErrors.size());
Assert.assertTrue(validationErrors.stream().anyMatch(error -> error.isMissingValue()));
Assert.assertTrue(validationErrors.stream().anyMatch(error -> error.getAttributeCode().equals(attributeName)));
}
use of eu.bcvsolutions.idm.core.eav.api.dto.InvalidFormAttributeDto in project CzechIdMng by bcvsolutions.
the class DefaultIdmConceptRoleRequestService method saveInternal.
@Override
@Transactional
public IdmConceptRoleRequestDto saveInternal(IdmConceptRoleRequestDto dto) {
IdmConceptRoleRequestDto savedDto = super.saveInternal(dto);
if (dto != null && dto.getRole() != null) {
// TODO: concept role request hasn't events, after implement events for the dto, please remove this.
boolean isNew = false;
if (isNew(dto)) {
isNew = true;
dto = valueGeneratorManager.generate(dto);
}
IdmRoleDto roleDto = roleService.get(dto.getRole());
if (roleDto == null) {
throw new ResultCodeException(CoreResultCode.NOT_FOUND, ImmutableMap.of("entity", dto.getRole()));
}
List<InvalidFormAttributeDto> validationErrors = validateFormAttributes(dto);
if (validationErrors != null && !validationErrors.isEmpty()) {
throw new InvalidFormException(validationErrors);
}
List<IdmFormValueDto> attributeValues = dto.getEavs().size() == 1 && dto.getEavs().get(0) != null ? dto.getEavs().get(0).getValues() : null;
// If concept is new, then we have to clear id of EAV values (new one have to be generated for this case).
if (isNew && attributeValues != null) {
attributeValues.forEach(value -> {
DtoUtils.clearAuditFields(value);
value.setId(null);
});
}
// Load sub definition by role
IdmFormDefinitionDto formDefinitionDto = roleService.getFormAttributeSubdefinition(roleDto);
if (formDefinitionDto != null) {
// Save form values for sub-definition. Validation is skipped. Was made before in this method, because now can be id of values null.
List<IdmFormValueDto> savedValues = formService.saveFormInstance(savedDto, formDefinitionDto, attributeValues, false).getValues();
IdmFormInstanceDto formInstance = new IdmFormInstanceDto();
formInstance.setValues(savedValues);
savedDto.getEavs().clear();
savedDto.getEavs().add(formInstance);
}
}
return savedDto;
}
use of eu.bcvsolutions.idm.core.eav.api.dto.InvalidFormAttributeDto in project CzechIdMng by bcvsolutions.
the class DefaultIdmRoleRequestService method validate.
@Override
public void validate(IdmRoleRequestDto request) {
Assert.notNull(request, "Request is required.");
List<IdmConceptRoleRequestDto> conceptRoles = request.getConceptRoles();
conceptRoles.forEach(concept -> {
List<InvalidFormAttributeDto> validationResults = conceptRoleRequestService.validateFormAttributes(concept);
if (validationResults != null && !validationResults.isEmpty()) {
IdmRoleDto role = null;
if (concept.getRole() != null) {
role = DtoUtils.getEmbedded(concept, IdmConceptRoleRequest_.role, IdmRoleDto.class, null);
if (role == null) {
role = roleService.get(concept.getRole());
}
} else {
IdmIdentityRoleDto identityRole = DtoUtils.getEmbedded(concept, IdmConceptRoleRequest_.identityRole, IdmIdentityRoleDto.class, null);
if (identityRole == null) {
identityRole = identityRoleThinService.get(concept.getIdentityRole());
}
if (identityRole != null) {
role = DtoUtils.getEmbedded(concept, IdmIdentityRole_.role, IdmRoleDto.class);
}
}
throw new ResultCodeException(CoreResultCode.ROLE_REQUEST_UNVALID_CONCEPT_ATTRIBUTE, //
ImmutableMap.of(//
"concept", //
concept.getId(), "roleCode", role != null ? role.getCode() : "", //
"request", //
concept.getRoleRequest(), //
"attributeCode", //
validationResults.get(0).getAttributeCode()));
//
}
});
}
use of eu.bcvsolutions.idm.core.eav.api.dto.InvalidFormAttributeDto in project CzechIdMng by bcvsolutions.
the class DefaultFormService method validate.
@Override
public List<InvalidFormAttributeDto> validate(IdmFormInstanceDto formInstance, boolean validateOnlySameOwnerType) {
Assert.notNull(formInstance, "Form instance cannot be null!");
IdmFormDefinitionDto formDefinition = formInstance.getFormDefinition();
Assert.notNull(formDefinition, "Form definition cannot be null!");
//
List<InvalidFormAttributeDto> results = Lists.newArrayList();
formDefinition.getFormAttributes().stream().forEach(formAttribute -> {
List<IdmFormValueDto> formValueForAttributes = formInstance.getValues().stream().filter(formValue -> formAttribute.getId().equals(formValue.getFormAttribute())).peek(formValue -> {
// we don't trust value persistent type - set current from attribute
formValue.setPersistentType(formAttribute.getPersistentType());
// we need owner type to perform validations
if (formValue.getOwnerType() == null && formInstance.getOwnerType() != null) {
formValue.setOwnerType(formDefinitionService.getFormableOwnerType(formInstance.getOwnerType()));
}
}).collect(//
Collectors.toList());
InvalidFormAttributeDto result = this.validateAttribute(formDefinition, formAttribute, formValueForAttributes, validateOnlySameOwnerType);
if (!result.isValid()) {
// Invalid attributes for owner => more owners can be saved together (e.g. from projection).
// ~ identifiable
result.setOwnerId(formInstance.getOwnerId());
Class<? extends Identifiable> ownerType = formInstance.getOwnerType();
if (ownerType != null) {
result.setOwnerType(lookupService.getOwnerType(ownerType));
}
results.add(result);
}
});
return results;
}
use of eu.bcvsolutions.idm.core.eav.api.dto.InvalidFormAttributeDto in project CzechIdMng by bcvsolutions.
the class DefaultIdmConceptRoleRequestService method validateFormAttributes.
@Override
public List<InvalidFormAttributeDto> validateFormAttributes(IdmConceptRoleRequestDto concept) {
if (concept == null || ConceptRoleRequestOperation.REMOVE == concept.getOperation() || concept.getState() == null || concept.getState().isTerminatedState()) {
return null;
}
IdmFormInstanceDto formInstanceDto = this.getRoleAttributeValues(concept, false);
if (formInstanceDto != null) {
UUID identityRoleId = null;
IdmIdentityRoleDto identityRoleDto = DtoUtils.getEmbedded(concept, IdmConceptRoleRequest_.identityRole, IdmIdentityRoleDto.class, null);
if (identityRoleDto == null) {
identityRoleId = concept.getIdentityRole();
} else {
identityRoleId = identityRoleDto.getId();
}
if (identityRoleId != null && ConceptRoleRequestOperation.UPDATE == concept.getOperation()) {
// Cache for save original ID of concepts.
// Id will be replaced by identity-role id and have to be returned after
// validation back, because formInstance is not immutable.
Map<UUID, UUID> identityRoleConceptValueMap = new HashMap<>();
// Find identity role value for concept value and change ID of value (because validation have to be made via identityRole).
UUID finalIdentityRoleId = identityRoleId;
formInstanceDto.getValues().forEach(value -> {
IdmFormAttributeDto formAttributeDto = new IdmFormAttributeDto();
formAttributeDto.setId(value.getFormAttribute());
formAttributeDto.setFormDefinition(formInstanceDto.getFormDefinition().getId());
IdmFormValueDto identityRoleValueDto = formService.getValues(new IdmIdentityRoleDto(finalIdentityRoleId), formAttributeDto).stream().filter(identityRoleValue -> identityRoleValue.getSeq() == value.getSeq()).findFirst().orElse(null);
// Replace concept IDs by identity-role IDs.
if (identityRoleValueDto != null) {
identityRoleConceptValueMap.put(identityRoleValueDto.getId(), value.getId());
value.setId(identityRoleValueDto.getId());
}
});
List<InvalidFormAttributeDto> validationErrors = formService.validate(formInstanceDto, false);
// Set IDs of concept back to values (formInstance is not immutable).
formInstanceDto.getValues().forEach(value -> {
if (identityRoleConceptValueMap.containsKey(value.getId())) {
value.setId(identityRoleConceptValueMap.get(value.getId()));
}
});
return validationErrors;
}
return formService.validate(formInstanceDto, false);
}
return null;
}
Aggregations