use of org.entando.entando.aps.system.services.entity.model.EntityAttributeValidationDto in project entando-core by entando.
the class AbstractEntityService method buildAttribute.
protected AttributeInterface buildAttribute(String typeCode, EntityAttributeFullDto attributeDto, Map<String, AttributeInterface> attributeMap, BindingResult bindingResult) {
String type = attributeDto.getType();
AttributeInterface prototype = attributeMap.get(type);
if (null == prototype) {
logger.warn("Undefined attribute of type {}", type);
this.addError(EntityTypeValidator.ERRCODE_INVALID_ATTRIBUTE_TYPE, bindingResult, new String[] { typeCode, type }, "entityType.attribute.type.invalid");
return null;
}
AttributeInterface attribute = (AttributeInterface) prototype.getAttributePrototype();
attribute.setName(attributeDto.getCode());
attribute.setDescription(attributeDto.getName());
attribute.setIndexingType(attributeDto.isIndexable() ? IndexableAttributeInterface.INDEXING_TYPE_TEXT : null);
List<AttributeRoleDto> dtoRoles = attributeDto.getRoles();
if (null != dtoRoles && !dtoRoles.isEmpty()) {
List<String> codes = dtoRoles.stream().map(AttributeRoleDto::getCode).collect(Collectors.toList());
attribute.setRoles(codes.toArray(new String[codes.size()]));
}
attribute.setRequired(attributeDto.isMandatory());
attribute.setSearchable(attributeDto.isListFilter());
if (attribute instanceof EnumeratorAttribute) {
// to check into validator
String staticItems = attributeDto.getEnumeratorStaticItems();
String extractor = attributeDto.getEnumeratorExtractorBean();
if (StringUtils.isEmpty(staticItems) && StringUtils.isEmpty(extractor)) {
this.addError(EntityTypeValidator.ERRCODE_INVALID_ENUMERATOR, bindingResult, new String[] { typeCode, attributeDto.getCode() }, "entityType.attribute.enumerator.invalid");
}
((EnumeratorAttribute) attribute).setStaticItems(staticItems);
((EnumeratorAttribute) attribute).setExtractorBeanName(extractor);
((EnumeratorAttribute) attribute).setCustomSeparator(attributeDto.getEnumeratorStaticItemsSeparator());
}
IAttributeValidationRules validationRules = attribute.getValidationRules();
validationRules.setRequired(attributeDto.isMandatory());
EntityAttributeValidationDto validationDto = attributeDto.getValidationRules();
if (null != validationDto) {
validationDto.buildAttributeValidation(typeCode, attribute, bindingResult);
}
if (attribute instanceof AbstractListAttribute) {
if (null != attributeDto.getNestedAttribute()) {
EntityAttributeFullDto nestedAttributeDto = attributeDto.getNestedAttribute();
((AbstractListAttribute) attribute).setNestedAttributeType(this.buildAttribute(typeCode, nestedAttributeDto, attributeMap, bindingResult));
} else {
this.addError(EntityTypeValidator.ERRCODE_INVALID_LIST, bindingResult, new String[] { typeCode, type }, "entityType.attribute.list.missingNestedAttribute");
}
} else if (attribute instanceof CompositeAttribute) {
List<EntityAttributeFullDto> compositeElementsDto = attributeDto.getCompositeAttributes();
if (null != compositeElementsDto && !compositeElementsDto.isEmpty()) {
for (EntityAttributeFullDto attributeElementDto : compositeElementsDto) {
AttributeInterface attributeElement = this.buildAttribute(typeCode, attributeElementDto, attributeMap, bindingResult);
((CompositeAttribute) attribute).getAttributeMap().put(attributeElement.getName(), attributeElement);
((CompositeAttribute) attribute).getAttributes().add(attributeElement);
}
} else {
this.addError(EntityTypeValidator.ERRCODE_INVALID_COMPOSITE, bindingResult, new String[] { typeCode, type }, "entityType.attribute.composite.missingElements");
}
}
return attribute;
}
Aggregations