Search in sources :

Example 11 with Property

use of org.broadleafcommerce.openadmin.dto.Property in project BroadleafCommerce by BroadleafCommerce.

the class DefaultFieldPersistenceProvider method populateValue.

@Override
public MetadataProviderResponse populateValue(PopulateValueRequest populateValueRequest, Serializable instance) throws PersistenceException {
    boolean dirty;
    try {
        Property p = populateValueRequest.getProperty();
        Object value = populateValueRequest.getFieldManager().getFieldValue(instance, p.getName());
        if (value != null) {
            p.setOriginalValue(String.valueOf(value));
            p.setOriginalDisplayValue(p.getOriginalValue());
        }
        dirty = checkDirtyState(populateValueRequest, instance, populateValueRequest.getRequestedValue());
        populateValueRequest.getFieldManager().setFieldValue(instance, populateValueRequest.getProperty().getName(), populateValueRequest.getRequestedValue());
    } catch (Exception e) {
        throw new PersistenceException(e);
    }
    populateValueRequest.getProperty().setIsDirty(dirty);
    return MetadataProviderResponse.HANDLED;
}
Also used : PersistenceException(org.broadleafcommerce.openadmin.server.service.persistence.PersistenceException) Property(org.broadleafcommerce.openadmin.dto.Property) PersistenceException(org.broadleafcommerce.openadmin.server.service.persistence.PersistenceException)

Example 12 with Property

use of org.broadleafcommerce.openadmin.dto.Property 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)

Example 13 with Property

use of org.broadleafcommerce.openadmin.dto.Property in project BroadleafCommerce by BroadleafCommerce.

the class OfferCustomPersistenceHandler method buildIsActiveProperty.

protected Property buildIsActiveProperty(boolean isActive) {
    Property property = new Property();
    property.setName(IS_ACTIVE);
    property.setValue(String.valueOf(isActive));
    return property;
}
Also used : Property(org.broadleafcommerce.openadmin.dto.Property)

Example 14 with Property

use of org.broadleafcommerce.openadmin.dto.Property in project BroadleafCommerce by BroadleafCommerce.

the class OfferCustomPersistenceHandler method buildQualifiersCanBeQualifiersProperty.

protected Property buildQualifiersCanBeQualifiersProperty(Property offerItemQualifierRuleType) {
    boolean qualifiersCanBeQualifiers = isQualifierType(offerItemQualifierRuleType) || isQualifierTargetType(offerItemQualifierRuleType);
    Property property = new Property();
    property.setName(QUALIFIERS_CAN_BE_QUALIFIERS);
    property.setValue(String.valueOf(qualifiersCanBeQualifiers));
    return property;
}
Also used : Property(org.broadleafcommerce.openadmin.dto.Property)

Example 15 with Property

use of org.broadleafcommerce.openadmin.dto.Property in project BroadleafCommerce by BroadleafCommerce.

the class OfferCustomPersistenceHandler method buildQualifiersCanBeTargetsProperty.

protected Property buildQualifiersCanBeTargetsProperty(Property offerItemQualifierRuleType) {
    boolean qualifiersCanBeTargets = isTargetType(offerItemQualifierRuleType) || isQualifierTargetType(offerItemQualifierRuleType);
    Property property = new Property();
    property.setName(QUALIFIERS_CAN_BE_TARGETS);
    property.setValue(String.valueOf(qualifiersCanBeTargets));
    return property;
}
Also used : Property(org.broadleafcommerce.openadmin.dto.Property)

Aggregations

Property (org.broadleafcommerce.openadmin.dto.Property)120 Entity (org.broadleafcommerce.openadmin.dto.Entity)62 BasicFieldMetadata (org.broadleafcommerce.openadmin.dto.BasicFieldMetadata)45 FieldMetadata (org.broadleafcommerce.openadmin.dto.FieldMetadata)38 ArrayList (java.util.ArrayList)28 PersistencePackageRequest (org.broadleafcommerce.openadmin.server.domain.PersistencePackageRequest)26 ClassMetadata (org.broadleafcommerce.openadmin.dto.ClassMetadata)25 DataWrapper (org.broadleafcommerce.openadmin.web.rulebuilder.dto.DataWrapper)21 SectionCrumb (org.broadleafcommerce.openadmin.dto.SectionCrumb)19 HashMap (java.util.HashMap)18 AdminMainEntity (org.broadleafcommerce.common.admin.domain.AdminMainEntity)18 DynamicResultSet (org.broadleafcommerce.openadmin.dto.DynamicResultSet)16 Map (java.util.Map)15 ServiceException (org.broadleafcommerce.common.exception.ServiceException)15 BasicCollectionMetadata (org.broadleafcommerce.openadmin.dto.BasicCollectionMetadata)15 Field (org.broadleafcommerce.openadmin.web.form.entity.Field)15 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)14 AdornedTargetCollectionMetadata (org.broadleafcommerce.openadmin.dto.AdornedTargetCollectionMetadata)13 RuleBuilderField (org.broadleafcommerce.openadmin.web.form.component.RuleBuilderField)12 ComboField (org.broadleafcommerce.openadmin.web.form.entity.ComboField)12