use of eu.bcvsolutions.idm.core.eav.api.dto.InvalidFormAttributeDto in project CzechIdMng by bcvsolutions.
the class DefaultFormService method validateAttribute.
private InvalidFormAttributeDto validateAttribute(IdmFormDefinitionDto formDefinition, IdmFormAttributeDto formAttribute, List<IdmFormValueDto> formValues, boolean validateOnlySameOwnerType) {
Assert.notNull(formAttribute, "Form attribute is required.");
//
InvalidFormAttributeDto result = new InvalidFormAttributeDto(formAttribute);
result.setDefinitionCode(formDefinition.getCode());
//
if (formAttribute.isRequired()) {
if (CollectionUtils.isEmpty(formValues) || !formValues.stream().filter(formValue -> !formValue.isEmpty()).findFirst().isPresent()) {
LOG.debug("Form attribute [{}] validation failed - value is required.", formAttribute.getCode());
//
result.setMissingValue(true);
}
}
if (CollectionUtils.isEmpty(formValues)) {
// values are not filled => other validations is not needed.
return result;
}
// TODO: redesign to registrable validators
// TODO: multiple values -> the last validation error is returned. Return invalid attribute for the all values ...
formValues.stream().filter(formValue -> !formValue.isEmpty()).forEach(formValue -> {
// minimum value validation
if (formAttribute.getMin() != null) {
if (formAttribute.getPersistentType() == PersistentType.SHORTTEXT) {
String value = formValue.getShortTextValue();
if (value.length() < formAttribute.getMin().intValue()) {
LOG.debug("Form attribute [{}] validation failed - given value [{}] is shorter than min [{}].", formAttribute.getCode(), value.length(), formAttribute.getMin());
//
result.setMinValue(formAttribute.getMin());
}
} else if (formAttribute.getPersistentType() == PersistentType.TEXT) {
String value = formValue.getStringValue();
if (value.length() < formAttribute.getMin().intValue()) {
LOG.debug("Form attribute [{}] validation failed - given value [{}] is shorter than min [{}].", formAttribute.getCode(), value.length(), formAttribute.getMin());
//
result.setMinValue(formAttribute.getMin());
}
} else if (formAttribute.getPersistentType() == PersistentType.DATE) {
LocalDate value = formValue.getDateValue().toLocalDate();
if (value.isBefore(LocalDate.now().plusDays(formAttribute.getMin().longValue()))) {
LOG.debug("Form attribute [{}] validation failed - given value [{}] is before than [{}] days.", formAttribute.getCode(), value, formAttribute.getMin());
//
result.setMinValue(formAttribute.getMin());
}
} else if (formAttribute.getPersistentType() == PersistentType.DATETIME) {
ZonedDateTime value = formValue.getDateValue();
if (value.isBefore(ZonedDateTime.now().plusDays(formAttribute.getMin().longValue()))) {
LOG.debug("Form attribute [{}] validation failed - given value [{}] is before than [{}] days.", formAttribute.getCode(), value, formAttribute.getMin());
//
result.setMinValue(formAttribute.getMin());
}
} else {
if (formValue.getLongValue() != null && formAttribute.getMin().compareTo(BigDecimal.valueOf(formValue.getLongValue())) > 0) {
LOG.debug("Form attribute [{}] validation failed - given value [{}] is lesser than min [{}].", formAttribute.getCode(), formValue.getLongValue(), formAttribute.getMin());
//
result.setMinValue(formAttribute.getMin());
}
if (formValue.getDoubleValue() != null && formAttribute.getMin().compareTo(formValue.getDoubleValue()) > 0) {
LOG.debug("Form attribute [{}] validation failed - given value [{}] is lesser than min [{}].", formAttribute.getCode(), formValue.getDoubleValue(), formAttribute.getMin());
//
result.setMinValue(formAttribute.getMin());
}
}
}
// maximum value validation
if (formAttribute.getMax() != null) {
if (formAttribute.getPersistentType() == PersistentType.SHORTTEXT) {
String value = formValue.getShortTextValue();
if (value.length() > formAttribute.getMax().intValue()) {
LOG.debug("Form attribute [{}] validation failed - given value [{}] is shorter than min [{}].", formAttribute.getCode(), value.length(), formAttribute.getMax());
//
result.setMaxValue(formAttribute.getMax());
}
} else if (formAttribute.getPersistentType() == PersistentType.TEXT) {
String value = formValue.getStringValue();
if (value.length() > formAttribute.getMax().intValue()) {
LOG.debug("Form attribute [{}] validation failed - given value [{}] is greater than min [{}].", formAttribute.getCode(), value.length(), formAttribute.getMax());
//
result.setMaxValue(formAttribute.getMax());
}
} else if (formAttribute.getPersistentType() == PersistentType.DATE) {
LocalDate value = formValue.getDateValue().toLocalDate();
if (value.isAfter(LocalDate.now().plusDays(formAttribute.getMax().longValue()))) {
LOG.debug("Form attribute [{}] validation failed - given value [{}] is after than [{}] days.", formAttribute.getCode(), value, formAttribute.getMax());
//
result.setMaxValue(formAttribute.getMax());
}
} else if (formAttribute.getPersistentType() == PersistentType.DATETIME) {
ZonedDateTime value = formValue.getDateValue();
if (value.isAfter(ZonedDateTime.now().plusDays(formAttribute.getMax().longValue()))) {
LOG.debug("Form attribute [{}] validation failed - given value [{}] is after than [{}] days.", formAttribute.getCode(), value, formAttribute.getMax());
//
result.setMaxValue(formAttribute.getMax());
}
} else {
if (formValue.getLongValue() != null && formAttribute.getMax().compareTo(BigDecimal.valueOf(formValue.getLongValue())) < 0) {
LOG.debug("Form attribute [{}] validation failed - given value [{}] is greater than max [{}].", formAttribute.getCode(), formValue.getLongValue(), formAttribute.getMax());
//
result.setMaxValue(formAttribute.getMax());
}
if (formValue.getDoubleValue() != null && formAttribute.getMax().compareTo(formValue.getDoubleValue()) < 0) {
LOG.debug("Form attribute [{}] validation failed - given value [{}] is greater than max [{}].", formAttribute.getCode(), formValue.getDoubleValue(), formAttribute.getMax());
//
result.setMaxValue(formAttribute.getMax());
}
}
}
String regex = formAttribute.getRegex();
if (StringUtils.isNotEmpty(regex)) {
Pattern p = Pattern.compile(regex);
String stringValue = formValue.getValue().toString();
// all persistent types are supported on BE, but string values makes the good sense.
Matcher m = p.matcher(stringValue);
if (!m.matches()) {
LOG.debug("Form attribute [{}] validation failed - given value [{}] does not match regex [{}].", formAttribute.getCode(), stringValue, regex);
//
result.setRegexValue(regex);
}
}
if (formAttribute.isUnique()) {
IdmFormValueFilter<FormableEntity> valueFilter = new IdmFormValueFilter<>();
valueFilter.setAttributeId(formValue.getFormAttribute());
valueFilter.setPersistentType(formValue.getPersistentType());
valueFilter.setStringValue(formValue.getStringValue());
valueFilter.setShortTextValue(formValue.getShortTextValue());
valueFilter.setBooleanValue(formValue.getBooleanValue());
valueFilter.setLongValue(formValue.getLongValue());
valueFilter.setDoubleValue(formValue.getDoubleValue());
valueFilter.setDateValue(formValue.getDateValue());
valueFilter.setUuidValue(formValue.getUuidValue());
//
Identifiable owner = getEmptyOwner(formDefinition);
Assert.notNull(owner, "Filter - attribute owner is required. Is possible to filter form values by given owner only");
//
FormValueService<FormableEntity> formValueService = getFormValueService(owner.getClass());
//
List<IdmFormValueDto> existValues = formValueService.find(valueFilter, PageRequest.of(0, 2)).getContent();
//
if (existValues.stream().filter(v -> {
if (validateOnlySameOwnerType) {
return v.getOwnerType().equals(formValue.getOwnerType());
}
return true;
}).anyMatch(v -> formValue.getId() == null || !formValue.getId().equals(v.getId()))) {
LOG.debug("Form attribute [{}] validation failed - given value [{}] is not unigue.", formAttribute.getCode(), formValue.getValue());
//
result.setUniqueValue(formValue.getValue().toString());
}
}
});
//
return result;
}
use of eu.bcvsolutions.idm.core.eav.api.dto.InvalidFormAttributeDto in project CzechIdMng by bcvsolutions.
the class FormInstanceValidateProcessor method process.
@Override
public EventResult<IdmFormInstanceDto> process(EntityEvent<IdmFormInstanceDto> event) {
IdmFormInstanceDto formInstance = event.getContent();
Assert.notNull(formInstance.getFormDefinition(), "Form definition is required for form instance validation.");
//
// resolve given / configured / overridden / default form definition
// configured definition is loaded only once and applied twice (optimization)
IdmFormDefinitionDto configuredFormDefinition = formProjectionManager.getConfiguredFormDefinition(getOwner(formInstance), formInstance.getFormDefinition());
IdmFormDefinitionDto formDefinition = formProjectionManager.overrideFormDefinition(formService.getDefinition(formInstance.getFormDefinition().getId()), configuredFormDefinition);
Assert.notNull(formDefinition, "Form definition is required for form instance validation.");
IdmFormDefinitionDto formInstanceDefinition = formProjectionManager.overrideFormDefinition(formInstance.getFormDefinition(), configuredFormDefinition);
Assert.notNull(formInstanceDefinition, "Form definition is required for form instance validation.");
//
Map<String, Serializable> properties = event.getProperties();
//
// get distinct attributes from the sent values
// PATCH is used - only sent attributes are validated
Set<IdmFormAttributeDto> sentAttributes = formInstance.getValues().stream().map(IdmFormValueDto::getFormAttribute).map(attributeId -> {
IdmFormAttributeDto mappedAttribute = formInstanceDefinition.getMappedAttribute(attributeId);
if (mappedAttribute != null) {
return mappedAttribute;
}
return formDefinition.getMappedAttribute(attributeId);
}).collect(Collectors.toSet());
// only sent attributes in definition and instance
formDefinition.setFormAttributes(Lists.newArrayList(sentAttributes));
formInstance.setFormDefinition(formDefinition);
// validate
List<InvalidFormAttributeDto> errors = formService.validate(formInstance);
// skip <required> validation if contract update is performed from time slice
if (getBooleanProperty(ContractSliceManager.SKIP_CHECK_FOR_SLICES, properties)) {
errors = errors.stream().filter(error -> {
return !error.isMissingValue();
}).collect(Collectors.toList());
}
if (!errors.isEmpty()) {
throw new InvalidFormException(errors);
}
//
return new DefaultEventResult<>(event, this);
}
use of eu.bcvsolutions.idm.core.eav.api.dto.InvalidFormAttributeDto in project CzechIdMng by bcvsolutions.
the class DefaultFormServiceIntegrationTest method testRegexValidation.
@Test
public void testRegexValidation() {
// prepare form definition a test saving form values
IdmFormAttributeDto attribute = new IdmFormAttributeDto();
String attributeName = getHelper().createName();
// ip address regex (copied from https://www.mkyong.com/regular-expressions/how-to-validate-ip-address-with-regular-expression/)
String ipRegex = "^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])$";
attribute.setCode(attributeName);
attribute.setName(attributeName);
attribute.setPersistentType(PersistentType.SHORTTEXT);
attribute.setRegex(ipRegex);
String validationMessage = "invalid...";
attribute.setValidationMessage(validationMessage);
IdmFormDefinitionDto formDefinitionOne = formService.createDefinition(IdmIdentity.class.getCanonicalName(), getHelper().createName(), Lists.newArrayList(attribute));
attribute = formDefinitionOne.getMappedAttributeByCode(attribute.getCode());
//
IdmFormValueDto value = new IdmFormValueDto(attribute);
value.setValue("one");
//
IdmFormInstanceDto formInstance = new IdmFormInstanceDto();
formInstance.setFormDefinition(formDefinitionOne);
formInstance.setValues(Lists.newArrayList(value));
//
List<InvalidFormAttributeDto> validationErrors = formService.validate(formInstance);
//
Assert.assertEquals(1, validationErrors.size());
Assert.assertTrue(validationErrors.stream().allMatch(e -> e.getRegexValue().equals(ipRegex)));
//
value.setValue("125.123.255.111");
validationErrors = formService.validate(formInstance);
Assert.assertTrue(validationErrors.isEmpty());
//
value.setValue("1.1.1.1");
validationErrors = formService.validate(formInstance);
Assert.assertTrue(validationErrors.isEmpty());
//
value.setValue("1.1.1.1.");
validationErrors = formService.validate(formInstance);
Assert.assertEquals(1, validationErrors.size());
Assert.assertTrue(validationErrors.stream().allMatch(e -> e.getRegexValue().equals(ipRegex)));
Assert.assertTrue(validationErrors.stream().allMatch(e -> e.getMessage().equals(validationMessage)));
}
use of eu.bcvsolutions.idm.core.eav.api.dto.InvalidFormAttributeDto in project CzechIdMng by bcvsolutions.
the class DefaultFormServiceIntegrationTest method testUniqueValidation.
@Test
public void testUniqueValidation() {
// 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.TEXT);
attribute.setUnique(Boolean.TRUE);
IdmFormDefinitionDto formDefinitionOne = formService.createDefinition(IdmIdentity.class.getCanonicalName(), getHelper().createName(), Lists.newArrayList(attribute));
attribute = formDefinitionOne.getMappedAttributeByCode(attribute.getCode());
//
IdmIdentityDto owner = getHelper().createIdentity((GuardedString) null);
IdmFormValueDto value = new IdmFormValueDto(attribute);
value.setValue("one");
//
IdmFormInstanceDto formInstance = new IdmFormInstanceDto(owner, formDefinitionOne, Lists.newArrayList(value));
//
List<InvalidFormAttributeDto> validationErrors = formService.validate(formInstance);
Assert.assertTrue(validationErrors.isEmpty());
//
IdmFormInstanceDto savedFormInstance = formService.saveFormInstance(owner, formDefinitionOne, Lists.newArrayList(value));
IdmFormValueDto savedValue = savedFormInstance.toValueMap().get(attributeName).get(0);
//
validationErrors = formService.validate(formInstance);
Assert.assertEquals(1, validationErrors.size());
Assert.assertTrue(validationErrors.stream().allMatch(e -> e.getUniqueValue().equals("one")));
//
value.setId(savedValue.getId());
validationErrors = formService.validate(formInstance);
Assert.assertTrue(validationErrors.isEmpty());
//
value.setId(UUID.randomUUID());
validationErrors = formService.validate(formInstance);
Assert.assertEquals(1, validationErrors.size());
Assert.assertTrue(validationErrors.stream().allMatch(e -> e.getUniqueValue().equals("one")));
}
use of eu.bcvsolutions.idm.core.eav.api.dto.InvalidFormAttributeDto in project CzechIdMng by bcvsolutions.
the class DefaultFormServiceIntegrationTest method testMinMaxDateValidation.
@Test
public void testMinMaxDateValidation() {
ZonedDateTime now = ZonedDateTime.now();
// prepare form definition a test saving form values
IdmFormAttributeDto attributeDate = new IdmFormAttributeDto();
String attributeDateCode = getHelper().createName();
attributeDate.setCode(attributeDateCode);
attributeDate.setName(attributeDateCode);
attributeDate.setPersistentType(PersistentType.DATE);
attributeDate.setMin(new BigDecimal("3"));
attributeDate.setMax(new BigDecimal("5"));
IdmFormAttributeDto attributeDateTime = new IdmFormAttributeDto();
String attributeDateTimeCode = getHelper().createName();
attributeDateTime.setCode(attributeDateTimeCode);
attributeDateTime.setName(attributeDateTimeCode);
attributeDateTime.setPersistentType(PersistentType.DATETIME);
attributeDateTime.setMin(new BigDecimal("6"));
attributeDateTime.setMax(new BigDecimal("8"));
IdmFormDefinitionDto formDefinitionOne = formService.createDefinition(IdmIdentity.class.getCanonicalName(), getHelper().createName(), Lists.newArrayList(attributeDate, attributeDateTime));
attributeDate = formDefinitionOne.getMappedAttributeByCode(attributeDate.getCode());
attributeDateTime = formDefinitionOne.getMappedAttributeByCode(attributeDateTime.getCode());
//
IdmFormValueDto valueDate = new IdmFormValueDto(attributeDate);
IdmFormValueDto valueDateTime = new IdmFormValueDto(attributeDateTime);
//
IdmFormInstanceDto formInstance = new IdmFormInstanceDto();
formInstance.setFormDefinition(formDefinitionOne);
formInstance.setValues(Lists.newArrayList(valueDate, valueDateTime));
//
List<InvalidFormAttributeDto> validationErrors = formService.validate(formInstance);
//
Assert.assertTrue(validationErrors.isEmpty());
//
valueDate.setDateValue(now.plusDays(2));
valueDateTime.setDateValue(now.plusDays(5));
//
validationErrors = formService.validate(formInstance);
Assert.assertEquals(2, validationErrors.size());
Assert.assertTrue(validationErrors.stream().allMatch(e -> e.getMinValue() != null));
Assert.assertTrue(validationErrors.stream().anyMatch(e -> e.getMinValue().equals(new BigDecimal("3")) && e.getAttributeCode().equals(attributeDateCode)));
Assert.assertTrue(validationErrors.stream().anyMatch(e -> e.getMinValue().equals(new BigDecimal("6")) && e.getAttributeCode().equals(attributeDateTimeCode)));
//
valueDate.setDateValue(now.plusDays(6));
valueDateTime.setDateValue(now.plusDays(9));
//
validationErrors = formService.validate(formInstance);
Assert.assertEquals(2, validationErrors.size());
Assert.assertTrue(validationErrors.stream().allMatch(e -> e.getMaxValue() != null));
Assert.assertTrue(validationErrors.stream().anyMatch(e -> e.getMaxValue().equals(new BigDecimal("5")) && e.getAttributeCode().equals(attributeDateCode)));
Assert.assertTrue(validationErrors.stream().anyMatch(e -> e.getMaxValue().equals(new BigDecimal("8")) && e.getAttributeCode().equals(attributeDateTimeCode)));
//
valueDate.setDateValue(now.plusDays(5));
valueDateTime.setDateValue(now.plusDays(8));
//
validationErrors = formService.validate(formInstance);
Assert.assertTrue(validationErrors.isEmpty());
//
valueDate.setDateValue(now.plusDays(3));
valueDateTime.setDateValue(now.plusDays(6).plusMinutes(2));
//
validationErrors = formService.validate(formInstance);
Assert.assertTrue(validationErrors.isEmpty());
}
Aggregations