use of eu.bcvsolutions.idm.core.eav.api.dto.filter.IdmFormValueFilter in project CzechIdMng by bcvsolutions.
the class FormAttributeDeleteProcessor method process.
@Override
@SuppressWarnings({ "rawtypes", "unchecked" })
public EventResult<IdmFormAttributeDto> process(EntityEvent<IdmFormAttributeDto> event) {
IdmFormAttributeDto formAttribute = event.getContent();
UUID formAttributeId = formAttribute.getId();
Assert.notNull(formAttributeId, "Form attribute id is required.");
//
boolean forceDelete = getBooleanProperty(PROPERTY_FORCE_DELETE, event.getProperties());
if (forceDelete) {
IdmFormValueFilter filter = new IdmFormValueFilter();
filter.setAttributeId(formAttributeId);
formValueServices.getPlugins().forEach(formValueService -> {
formValueService.find(filter, null).getContent().forEach(formValue -> {
formValueService.delete((IdmFormValueDto) formValue);
//
clearSession();
});
});
}
//
service.deleteInternal(formAttribute);
//
return new DefaultEventResult<>(event, this);
}
use of eu.bcvsolutions.idm.core.eav.api.dto.filter.IdmFormValueFilter in project CzechIdMng by bcvsolutions.
the class FormValueDeleteBulkActionIntegrationTest method processBulkActionByFilter.
@Test
public void processBulkActionByFilter() {
Identifiable owner = getHelper().createIdentity((GuardedString) null);
//
// create definition with attribute
IdmFormAttributeDto attribute = new IdmFormAttributeDto();
String attributeName = getHelper().createName();
attribute.setCode(attributeName);
attribute.setName(attribute.getCode());
attribute.setMultiple(true);
attribute.setPersistentType(PersistentType.SHORTTEXT);
IdmFormDefinitionDto formDefinitionOne = formService.createDefinition(IdmIdentity.class, getHelper().createName(), Lists.newArrayList(attribute));
attribute = formDefinitionOne.getMappedAttributeByCode(attribute.getCode());
//
// fill values
formService.saveValues(owner, attribute, Lists.newArrayList(FORM_VALUE_ONE, FORM_VALUE_TWO));
Map<String, List<IdmFormValueDto>> m = formService.getFormInstance(owner, formDefinitionOne).toValueMap();
//
// check value and persistent type
Assert.assertEquals(2, m.get(attributeName).size());
Assert.assertTrue(m.get(attributeName).stream().anyMatch(v -> v.getValue().equals(FORM_VALUE_ONE)));
Assert.assertTrue(m.get(attributeName).stream().anyMatch(v -> v.getValue().equals(FORM_VALUE_TWO)));
//
IdmBulkActionDto bulkAction = findBulkAction(IdmFormValue.class, FormValueDeleteBulkAction.NAME);
IdmFormValueFilter<?> filter = new IdmFormValueFilter<>();
filter.setDefinitionId(formDefinitionOne.getId());
filter.setShortTextValue(FORM_VALUE_ONE);
filter.setAttributeId(attribute.getId());
bulkAction.setTransformedFilter(filter);
bulkAction.setFilter(toMap(filter));
IdmBulkActionDto processAction = bulkActionManager.processAction(bulkAction);
checkResultLrt(processAction, 1l, null, null);
m = formService.getFormInstance(owner, formDefinitionOne).toValueMap();
//
Assert.assertEquals(1, m.get(attributeName).size());
Assert.assertTrue(m.get(attributeName).stream().anyMatch(v -> v.getValue().equals(FORM_VALUE_TWO)));
}
use of eu.bcvsolutions.idm.core.eav.api.dto.filter.IdmFormValueFilter 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.filter.IdmFormValueFilter in project CzechIdMng by bcvsolutions.
the class DefaultFormService method toValueFilter.
/**
* Construct value filter for find form values.
*
* @param ownerEntity owner
* @param formDefinition form definition
* @param filter only some attributes can be found only
* @return
* @since 10.3.0
*/
protected IdmFormValueFilter<FormableEntity> toValueFilter(FormableEntity ownerEntity, IdmFormDefinitionDto formDefinition, FormableFilter filter) {
IdmFormValueFilter<FormableEntity> valueFilter = new IdmFormValueFilter<>();
valueFilter.setOwner(ownerEntity);
valueFilter.setDefinitionId(formDefinition.getId());
valueFilter.setAddSecredProxyString(true);
if (filter == null) {
// filter is not given => all attributes
return valueFilter;
}
//
// apply formable filter with given parameters
List<FormDefinitionAttributes> formDefinitionAttributes = filter.getFormDefinitionAttributes();
if (CollectionUtils.isEmpty(formDefinitionAttributes)) {
// filter is given, but empty => all attributes
return valueFilter;
}
//
UUID formDefinitionId = formDefinition.getId();
FormDefinitionAttributes attributes = formDefinitionAttributes.stream().filter(a -> formDefinitionId.equals(a.getDefinition())).findFirst().orElse(null);
if (attributes == null) {
// filter is given, but without processed definition => all attributes
return valueFilter;
}
//
List<UUID> attributeIds = attributes.getAttributes();
if (CollectionUtils.isEmpty(attributeIds)) {
// filter for processed definition is given, but without attributes => all attributes
return valueFilter;
}
// set attributes by filter
valueFilter.setAttributeIds(attributes.getAttributes());
//
return valueFilter;
}
use of eu.bcvsolutions.idm.core.eav.api.dto.filter.IdmFormValueFilter in project CzechIdMng by bcvsolutions.
the class AbstractFormValueService method saveInternal.
/**
* Saves a given entity. Use the returned instance for further operations as the save operation might have changed the
* entity instance completely.
*
* @param entity
* @return the saved entity
*/
@Override
@Transactional
public IdmFormValueDto saveInternal(IdmFormValueDto dto) {
Assert.notNull(dto, "DTO is required to be saved.");
Serializable formValue = dto.getValue();
//
E persistedEntity = null;
if (dto.getId() != null) {
persistedEntity = this.getEntity(dto.getId());
}
// check, if value has to be persisted in confidential storage
if (dto.isConfidential()) {
dto.clearValues();
if (formValue != null) {
String previousFormValue;
if (dto.getId() != null) {
previousFormValue = confidentialStorage.get(dto.getId(), getEntityClass(), getConfidentialStorageKey(dto.getFormAttribute()), String.class);
} else {
previousFormValue = null;
}
if (Objects.equals(formValue, previousFormValue) && persistedEntity != null) {
dto.setStringValue(persistedEntity.getStringValue());
dto.setShortTextValue(persistedEntity.getShortTextValue());
} else {
String proxyValue = String.format("%s-%s", CONFIDENTIAL_STORAGE_VALUE_PREFIX, UUID.randomUUID());
// we need only to know, if value was filled and when (~ for audit)
dto.setStringValue(proxyValue);
dto.setShortTextValue(proxyValue);
}
}
}
// persist entity
E entity = getRepository().save(toEntity(dto, persistedEntity));
// save values to confidential storage
if (entity.isConfidential()) {
confidentialStorage.save(entity.getId(), entity.getClass(), getConfidentialStorageKey(entity.getFormAttribute().getId()), formValue);
LOG.debug("FormValue [{}] is persisted in confidential storage", entity.getId());
}
// return actual dto with proxy string
IdmFormValueFilter<O> context = new IdmFormValueFilter<>();
context.setAddSecredProxyString(true);
return toDto(entity, null, context);
}
Aggregations