use of eu.bcvsolutions.idm.core.eav.api.dto.IdmFormDefinitionDto in project CzechIdMng by bcvsolutions.
the class DefaultFormService method saveFormInstance.
@Override
@Transactional
public IdmFormInstanceDto saveFormInstance(EntityEvent<IdmFormInstanceDto> event) {
IdmFormInstanceDto formInstance = event.getContent();
FormableEntity ownerEntity = getOwnerEntity(formInstance.getOwnerId(), formInstance.getOwnerType());
Assert.notNull(ownerEntity, "Form values owner is required.");
IdmFormDefinitionDto formDefinition = checkDefaultDefinition(formInstance.getOwnerType(), formInstance.getFormDefinition());
//
FormValueService<FormableEntity> formValueService = getFormValueService(formInstance.getOwnerType());
//
// values by attributes
Map<UUID, Map<UUID, IdmFormValueDto>> previousValues = new HashMap<>();
formValueService.getValues(ownerEntity, formDefinition).forEach(formValue -> {
if (!previousValues.containsKey(formValue.getFormAttribute())) {
// sort by seq
previousValues.put(formValue.getFormAttribute(), new LinkedHashMap<>());
}
previousValues.get(formValue.getFormAttribute()).put(formValue.getId(), formValue);
});
//
List<IdmFormValueDto> results = new ArrayList<>();
for (Entry<String, List<IdmFormValueDto>> attributeEntry : formInstance.toValueMap().entrySet()) {
IdmFormAttributeDto attribute = formInstance.getMappedAttributeByCode(attributeEntry.getKey());
List<IdmFormValueDto> attributePreviousValues = new ArrayList<>();
if (previousValues.containsKey(attribute.getId())) {
attributePreviousValues.addAll(previousValues.get(attribute.getId()).values());
}
results.addAll(saveAttributeValues(ownerEntity, attribute, attributePreviousValues, attributeEntry.getValue(), event.getPermission()));
}
//
return new IdmFormInstanceDto(ownerEntity, formDefinition, results);
}
use of eu.bcvsolutions.idm.core.eav.api.dto.IdmFormDefinitionDto 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.IdmFormDefinitionDto in project CzechIdMng by bcvsolutions.
the class DefaultFormService method refillDeletedAttributeValue.
/**
* Form instance must contains delete attribute (with empty value). Without it
* could be form values not deleted.
*
* @param formInstance
*/
private void refillDeletedAttributeValue(IdmFormInstanceDto formInstance) {
Assert.notNull(formInstance, "Form instance is required.");
IdmFormDefinitionDto formDefinition = formInstance.getFormDefinition();
Assert.notNull(formDefinition, "Form definition is required.");
//
formDefinition.getFormAttributes().stream().forEach(formAttribute -> {
//
List<IdmFormValueDto> values = formInstance.getValues();
boolean valueExists = //
values.stream().filter(//
formValue -> formAttribute.getId().equals(formValue.getFormAttribute())).findFirst().isPresent();
if (!valueExists) {
ArrayList<IdmFormValueDto> newValues = Lists.newArrayList(values);
IdmFormValueDto deletedValue = new IdmFormValueDto(formAttribute);
newValues.add(deletedValue);
formInstance.setValues(newValues);
}
});
}
use of eu.bcvsolutions.idm.core.eav.api.dto.IdmFormDefinitionDto in project CzechIdMng by bcvsolutions.
the class DefaultFormService method findValues.
@Override
@SuppressWarnings({ "rawtypes", "unchecked" })
@Transactional(readOnly = true)
public Page<IdmFormValueDto> findValues(IdmFormValueFilter filter, Pageable pageable, BasePermission... permission) {
Assert.notNull(filter, "Filter is required.");
// resolve owner by definition
if (filter.getOwner() == null) {
IdmFormDefinitionDto formDefinition = null;
UUID definitionId = filter.getDefinitionId();
if (definitionId != null) {
formDefinition = getCachedDefinition(definitionId);
}
//
UUID attributeId = filter.getAttributeId();
if (formDefinition == null && attributeId != null) {
IdmFormAttributeDto formAttribute = formAttributeService.get(attributeId);
if (formAttribute != null) {
formDefinition = DtoUtils.getEmbedded(formAttribute, IdmFormAttribute_.formDefinition);
}
}
if (formDefinition == null) {
throw new ResultCodeException(CoreResultCode.NOT_FOUND, ImmutableMap.of("entity", "formDefinition"));
}
filter.setOwner(getEmptyOwner(formDefinition));
}
Identifiable owner = (Identifiable) filter.getOwner();
Assert.notNull(owner, "Filter - attribute owner is required. Is possible to filter form values by given owner only");
//
FormValueService<FormableEntity> formValueService = getFormValueService(owner.getClass());
//
return formValueService.find(filter, pageable, permission);
}
use of eu.bcvsolutions.idm.core.eav.api.dto.IdmFormDefinitionDto in project CzechIdMng by bcvsolutions.
the class DefaultFormService method getCachedDefinitions.
private FormDefinitionCache getCachedDefinitions(String type) {
String cacheKey = getCacheKey(type);
ValueWrapper value = cacheManager.getValue(FORM_DEFINITION_CACHE_NAME, cacheKey);
if (value != null) {
// never null
return (FormDefinitionCache) value.get();
}
//
IdmFormDefinitionFilter filter = new IdmFormDefinitionFilter();
filter.setType(type);
List<IdmFormDefinitionDto> definitions = formDefinitionService.find(filter, PageRequest.of(0, Integer.MAX_VALUE, Sort.by(IdmFormDefinition_.code.getName()))).getContent();
//
// cache definition by id
definitions.forEach(definition -> {
// set mapped attributes - required to cache form definition with attributes
IdmFormAttributeFilter attributeFilter = new IdmFormAttributeFilter();
attributeFilter.setDefinitionId(definition.getId());
definition.setTrimmed(false);
definition.setFormAttributes(formAttributeService.find(attributeFilter, PageRequest.of(0, Integer.MAX_VALUE, Sort.by(IdmFormAttribute_.seq.getName(), IdmFormAttribute_.name.getName()))).getContent());
//
FormDefinitionCache cachedDefinition = new FormDefinitionCache();
cachedDefinition.putDefinition(definition);
//
cacheManager.cacheValue(FORM_DEFINITION_CACHE_NAME, getCacheKey(definition.getId()), cachedDefinition);
});
//
// cache by type
FormDefinitionCache cachedDefinitions = new FormDefinitionCache();
cachedDefinitions.putDefinitions(definitions);
cacheManager.cacheValue(FORM_DEFINITION_CACHE_NAME, cacheKey, cachedDefinitions);
//
return cachedDefinitions;
}
Aggregations