Search in sources :

Example 1 with BasicPersistenceModule

use of org.broadleafcommerce.openadmin.server.service.persistence.module.BasicPersistenceModule in project BroadleafCommerce by BroadleafCommerce.

the class EntityValidatorServiceImpl method validate.

@Override
public void validate(Entity submittedEntity, @Nullable Serializable instance, Map<String, FieldMetadata> propertiesMetadata, RecordHelper recordHelper, boolean validateUnsubmittedProperties) {
    Object idValue = null;
    if (instance != null) {
        String idField = (String) ((BasicPersistenceModule) recordHelper.getCompatibleModule(OperationType.BASIC)).getPersistenceManager().getDynamicEntityDao().getIdMetadata(instance.getClass()).get("name");
        try {
            idValue = recordHelper.getFieldManager().getFieldValue(instance, idField);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (FieldNotAvailableException e) {
            throw new RuntimeException(e);
        }
    }
    Entity entity;
    boolean isUpdateRequest;
    if (idValue == null) {
        // This is for an add, or if the instance variable is null (e.g. PageTemplateCustomPersistenceHandler)
        entity = submittedEntity;
        isUpdateRequest = false;
    } else {
        if (validateUnsubmittedProperties) {
            // This is for an update, as the submittedEntity instance will likely only contain the dirty properties
            entity = recordHelper.getRecord(propertiesMetadata, instance, null, null);
            // would be the confirmation field for a password validation
            for (Map.Entry<String, FieldMetadata> entry : propertiesMetadata.entrySet()) {
                if (entity.findProperty(entry.getKey()) == null) {
                    Property myProperty = submittedEntity.findProperty(entry.getKey());
                    if (myProperty != null) {
                        entity.addProperty(myProperty);
                    }
                } else if (submittedEntity.findProperty(entry.getKey()) != null) {
                    entity.findProperty(entry.getKey()).setValue(submittedEntity.findProperty(entry.getKey()).getValue());
                    entity.findProperty(entry.getKey()).setIsDirty(submittedEntity.findProperty(entry.getKey()).getIsDirty());
                }
            }
        } else {
            entity = submittedEntity;
        }
        isUpdateRequest = true;
    }
    List<String> types = getTypeHierarchy(entity);
    // validate each individual property according to their validation configuration
    for (Entry<String, FieldMetadata> metadataEntry : propertiesMetadata.entrySet()) {
        FieldMetadata metadata = metadataEntry.getValue();
        // Don't test this field if it was not inherited from our polymorphic type (or supertype)
        if (instance != null && (types.contains(metadata.getInheritedFromType()) || instance.getClass().getName().equals(metadata.getInheritedFromType()))) {
            Property property = entity.getPMap().get(metadataEntry.getKey());
            // and we don't need to validate them.
            if (!validateUnsubmittedProperties && property == null) {
                continue;
            }
            // for radio buttons, it's possible that the entity property was never populated in the first place from the POST
            // and so it will be null
            String propertyName = metadataEntry.getKey();
            String propertyValue = (property == null) ? null : property.getValue();
            if (metadata instanceof BasicFieldMetadata) {
                // First execute the global field validators
                if (CollectionUtils.isNotEmpty(globalEntityValidators)) {
                    for (GlobalPropertyValidator validator : globalEntityValidators) {
                        PropertyValidationResult result = validator.validate(entity, instance, propertiesMetadata, (BasicFieldMetadata) metadata, propertyName, propertyValue);
                        if (!result.isValid()) {
                            submittedEntity.addValidationError(propertyName, result.getErrorMessage());
                        }
                    }
                }
                // Now execute the validators configured for this particular field
                Map<String, List<Map<String, String>>> validations = ((BasicFieldMetadata) metadata).getValidationConfigurations();
                for (Map.Entry<String, List<Map<String, String>>> validation : validations.entrySet()) {
                    String validationImplementation = validation.getKey();
                    for (Map<String, String> configuration : validation.getValue()) {
                        PropertyValidator validator = null;
                        // attempt bean resolution to find the validator
                        if (applicationContext.containsBean(validationImplementation)) {
                            validator = applicationContext.getBean(validationImplementation, PropertyValidator.class);
                        }
                        // not a bean, attempt to instantiate the class
                        if (validator == null) {
                            try {
                                validator = (PropertyValidator) Class.forName(validationImplementation).newInstance();
                            } catch (Exception e) {
                            // do nothing
                            }
                        }
                        if (validator == null) {
                            throw new PersistenceException("Could not find validator: " + validationImplementation + " for property: " + propertyName);
                        }
                        PropertyValidationResult result = validator.validate(entity, instance, propertiesMetadata, configuration, (BasicFieldMetadata) metadata, propertyName, propertyValue);
                        if (!result.isValid()) {
                            for (String message : result.getErrorMessages()) {
                                submittedEntity.addValidationError(propertyName, message);
                            }
                        }
                    }
                }
            }
        }
    }
}
Also used : Entity(org.broadleafcommerce.openadmin.dto.Entity) FieldMetadata(org.broadleafcommerce.openadmin.dto.FieldMetadata) BasicFieldMetadata(org.broadleafcommerce.openadmin.dto.BasicFieldMetadata) BeansException(org.springframework.beans.BeansException) FieldNotAvailableException(org.broadleafcommerce.openadmin.server.service.persistence.module.FieldNotAvailableException) PersistenceException(org.broadleafcommerce.openadmin.server.service.persistence.PersistenceException) BasicPersistenceModule(org.broadleafcommerce.openadmin.server.service.persistence.module.BasicPersistenceModule) BasicFieldMetadata(org.broadleafcommerce.openadmin.dto.BasicFieldMetadata) FieldNotAvailableException(org.broadleafcommerce.openadmin.server.service.persistence.module.FieldNotAvailableException) PersistenceException(org.broadleafcommerce.openadmin.server.service.persistence.PersistenceException) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map) Property(org.broadleafcommerce.openadmin.dto.Property)

Aggregations

ArrayList (java.util.ArrayList)1 List (java.util.List)1 Map (java.util.Map)1 BasicFieldMetadata (org.broadleafcommerce.openadmin.dto.BasicFieldMetadata)1 Entity (org.broadleafcommerce.openadmin.dto.Entity)1 FieldMetadata (org.broadleafcommerce.openadmin.dto.FieldMetadata)1 Property (org.broadleafcommerce.openadmin.dto.Property)1 PersistenceException (org.broadleafcommerce.openadmin.server.service.persistence.PersistenceException)1 BasicPersistenceModule (org.broadleafcommerce.openadmin.server.service.persistence.module.BasicPersistenceModule)1 FieldNotAvailableException (org.broadleafcommerce.openadmin.server.service.persistence.module.FieldNotAvailableException)1 BeansException (org.springframework.beans.BeansException)1