Search in sources :

Example 1 with EntityAttributeValidationDto

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;
}
Also used : EnumeratorAttribute(com.agiletec.aps.system.common.entity.model.attribute.EnumeratorAttribute) CompositeAttribute(com.agiletec.aps.system.common.entity.model.attribute.CompositeAttribute) AttributeRoleDto(org.entando.entando.aps.system.services.entity.model.AttributeRoleDto) EntityAttributeFullDto(org.entando.entando.aps.system.services.entity.model.EntityAttributeFullDto) IAttributeValidationRules(com.agiletec.aps.system.common.entity.model.attribute.util.IAttributeValidationRules) ArrayList(java.util.ArrayList) List(java.util.List) IndexableAttributeInterface(com.agiletec.aps.system.common.searchengine.IndexableAttributeInterface) AttributeInterface(com.agiletec.aps.system.common.entity.model.attribute.AttributeInterface) AbstractListAttribute(com.agiletec.aps.system.common.entity.model.attribute.AbstractListAttribute) EntityAttributeValidationDto(org.entando.entando.aps.system.services.entity.model.EntityAttributeValidationDto)

Aggregations

AbstractListAttribute (com.agiletec.aps.system.common.entity.model.attribute.AbstractListAttribute)1 AttributeInterface (com.agiletec.aps.system.common.entity.model.attribute.AttributeInterface)1 CompositeAttribute (com.agiletec.aps.system.common.entity.model.attribute.CompositeAttribute)1 EnumeratorAttribute (com.agiletec.aps.system.common.entity.model.attribute.EnumeratorAttribute)1 IAttributeValidationRules (com.agiletec.aps.system.common.entity.model.attribute.util.IAttributeValidationRules)1 IndexableAttributeInterface (com.agiletec.aps.system.common.searchengine.IndexableAttributeInterface)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 AttributeRoleDto (org.entando.entando.aps.system.services.entity.model.AttributeRoleDto)1 EntityAttributeFullDto (org.entando.entando.aps.system.services.entity.model.EntityAttributeFullDto)1 EntityAttributeValidationDto (org.entando.entando.aps.system.services.entity.model.EntityAttributeValidationDto)1