use of org.broadleafcommerce.openadmin.dto.FieldMetadata in project BroadleafCommerce by BroadleafCommerce.
the class MapStructurePersistenceModule method remove.
@Override
public void remove(PersistencePackage persistencePackage) throws ServiceException {
String[] customCriteria = persistencePackage.getCustomCriteria();
if (customCriteria != null && customCriteria.length > 0) {
LOG.warn("custom persistence handlers and custom criteria not supported for remove types other than BASIC");
}
PersistencePerspective persistencePerspective = persistencePackage.getPersistencePerspective();
Entity entity = persistencePackage.getEntity();
MapStructure mapStructure = (MapStructure) persistencePerspective.getPersistencePerspectiveItems().get(PersistencePerspectiveItemType.MAPSTRUCTURE);
if (!mapStructure.getMutable()) {
throw new SecurityServiceException("Field not mutable");
}
try {
Map<String, FieldMetadata> ceilingMergedProperties = getSimpleMergedProperties(entity.getType()[0], persistencePerspective);
String mapKey = entity.findProperty(mapStructure.getKeyPropertyName()).getValue();
if (ceilingMergedProperties.containsKey(mapStructure.getMapProperty() + FieldManager.MAPFIELDSEPARATOR + mapKey)) {
throw new ServiceException("\"" + mapKey + "\" is a reserved property name.");
}
Serializable instance = persistenceManager.getDynamicEntityDao().retrieve(Class.forName(entity.getType()[0]), Long.valueOf(entity.findProperty("symbolicId").getValue()));
Assert.isTrue(instance != null, "Entity not found");
FieldManager fieldManager = getFieldManager();
Map map = (Map) fieldManager.getFieldValue(instance, mapStructure.getMapProperty());
Object value = map.remove(entity.findProperty("priorKey").getValue());
if (mapStructure.getDeleteValueEntity()) {
persistenceManager.getDynamicEntityDao().remove((Serializable) value);
}
} catch (Exception e) {
throw new ServiceException("Problem removing entity : " + e.getMessage(), e);
}
}
use of org.broadleafcommerce.openadmin.dto.FieldMetadata in project BroadleafCommerce by BroadleafCommerce.
the class BasicFieldPersistenceProvider method canHandleSearchMapping.
protected boolean canHandleSearchMapping(AddSearchMappingRequest addSearchMappingRequest, List<FilterMapping> filterMappings) {
FieldMetadata metadata = addSearchMappingRequest.getMergedProperties().get(addSearchMappingRequest.getPropertyName());
Property property = null;
// don't handle map fields here - we'll get them in a separate provider
boolean response = detectBasicType(metadata, property) || detectAdditionalSearchTypes(metadata, property);
return response;
}
use of org.broadleafcommerce.openadmin.dto.FieldMetadata 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.FieldMetadata in project BroadleafCommerce by BroadleafCommerce.
the class OfferCustomPersistenceHandler method inspect.
@Override
public DynamicResultSet inspect(PersistencePackage persistencePackage, DynamicEntityDao dynamicEntityDao, InspectHelper helper) throws ServiceException {
Class ceilingEntityClass = getClassForName(persistencePackage.getCeilingEntityFullyQualifiedClassname());
PersistencePerspective persistencePerspective = persistencePackage.getPersistencePerspective();
Map<MergedPropertyType, Map<String, FieldMetadata>> allMergedProperties = new HashMap<MergedPropertyType, Map<String, FieldMetadata>>();
// retrieve the default properties for WorkflowEvents
Map<String, FieldMetadata> properties = helper.getSimpleMergedProperties(ceilingEntityClass.getCanonicalName(), persistencePerspective);
properties.put(SHOW_ADVANCED_VISIBILITY_OPTIONS, buildAdvancedVisibilityOptionsFieldMetaData());
properties.put(QUALIFIERS_CAN_BE_QUALIFIERS, buildQualifiersCanBeQualifiersFieldMetaData());
properties.put(QUALIFIERS_CAN_BE_TARGETS, buildQualifiersCanBeTargetsFieldMetaData());
properties.put(STACKABLE, buildStackableFieldMetaData());
if (isActiveFilter) {
properties.put(IS_ACTIVE, buildIsActiveFieldMetaData());
}
allMergedProperties.put(MergedPropertyType.PRIMARY, properties);
Class<?>[] entityClasses = dynamicEntityDao.getAllPolymorphicEntitiesFromCeiling(ceilingEntityClass);
ClassMetadata mergedMetadata = helper.buildClassMetadata(entityClasses, persistencePackage, allMergedProperties);
return new DynamicResultSet(mergedMetadata, null, null);
}
use of org.broadleafcommerce.openadmin.dto.FieldMetadata in project BroadleafCommerce by BroadleafCommerce.
the class ProductCustomPersistenceHandler method getAdminInstance.
protected Product getAdminInstance(PersistencePackage persistencePackage, DynamicEntityDao dynamicEntityDao, RecordHelper helper, Entity entity) throws ClassNotFoundException {
PersistencePerspective persistencePerspective = persistencePackage.getPersistencePerspective();
Map<String, FieldMetadata> adminProperties = helper.getSimpleMergedProperties(Product.class.getName(), persistencePerspective);
Object primaryKey = helper.getPrimaryKey(entity, adminProperties);
String type = entity.getType()[0];
Product adminInstance = (Product) dynamicEntityDao.retrieve(Class.forName(type), primaryKey);
return adminInstance;
}
Aggregations