use of eu.bcvsolutions.idm.core.eav.api.service.FormValueService in project CzechIdMng by bcvsolutions.
the class DefaultFormService method saveFormInstance.
/**
* {@inheritDoc}
*
* TODO: validations by given form definition? I don't think, it will not be
* useful in synchronization etc. - only FE validations will be enough ...
*/
@Override
@Transactional
public IdmFormInstanceDto saveFormInstance(Identifiable owner, IdmFormDefinitionDto formDefinition, List<IdmFormValueDto> values) {
FormableEntity ownerEntity = getOwnerEntity(owner);
Assert.notNull(values, "Form values are required!");
Assert.notNull(ownerEntity, "Form values owner is required!");
formDefinition = checkDefaultDefinition(ownerEntity.getClass(), formDefinition);
//
FormValueService<FormableEntity> formValueService = getFormValueService(ownerEntity);
//
Map<UUID, IdmFormValueDto> previousValues = new HashMap<>();
formValueService.getValues(ownerEntity, formDefinition).forEach(formValue -> {
previousValues.put(formValue.getId(), formValue);
});
//
List<IdmFormValueDto> results = new ArrayList<>();
for (IdmFormValueDto value : values) {
// value could contant attribute id only
UUID attributeId = value.getFormAttribute();
Assert.notNull(attributeId, "Form attribute is required");
IdmFormAttributeDto attribute = formDefinition.getMappedAttribute(attributeId);
Assert.notNull(attribute, "Form attribute is required");
//
value.setOwnerAndAttribute(ownerEntity, attribute);
//
IdmFormValueDto previousValue = value.getId() == null ? null : previousValues.get(value.getId());
if (previousValue != null) {
// saved values will not be removed
previousValues.remove(value.getId());
// confidential value is always updated - only new values are sent from client
if (value.isConfidential() || !value.isEquals(previousValue)) {
// update value
results.add(formValueService.save(value));
LOG.trace("FormValue [{}:{}] for owner [{}] was updated", attribute.getCode(), value.getId(), ownerEntity);
}
} else {
// create new value
results.add(formValueService.save(value));
LOG.trace("FormValue [{}:{}] for owner [{}] was created", attribute.getCode(), value.getId(), ownerEntity);
}
}
//
// remove unsaved values by attribute definition (patch method is not
// implemented now)
previousValues.values().stream().filter(formValue -> {
// they could not be sent with form (only changed values)
return !formValue.isConfidential();
}).forEach(value -> {
formValueService.delete(value);
LOG.trace("FormValue [{}:{}] for owner [{}] was deleted", value.getFormAttribute(), value.getId(), ownerEntity);
});
// publish event - eav was saved
if (lookupService.getDtoLookup(ownerEntity.getClass()) == null) {
// TODO: remove this branch after all agends will be rewritten to dto usage
entityEventManager.process(new CoreEvent<>(CoreEventType.EAV_SAVE, ownerEntity));
} else {
entityEventManager.process(new CoreEvent<>(CoreEventType.EAV_SAVE, lookupService.lookupDto(ownerEntity.getClass(), ownerEntity.getId())));
}
//
return new IdmFormInstanceDto(ownerEntity, formDefinition, results);
}
use of eu.bcvsolutions.idm.core.eav.api.service.FormValueService 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;
}
Aggregations