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;
}
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);
}
}
}
}
}
}
}
}
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;
}
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;
}
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;
}
Aggregations