use of org.broadleafcommerce.openadmin.server.dao.FieldInfo in project BroadleafCommerce by BroadleafCommerce.
the class AdvancedCollectionFieldMetadataProvider method addMetadataFromFieldType.
@Override
public MetadataProviderResponse addMetadataFromFieldType(AddMetadataFromFieldTypeRequest addMetadataFromFieldTypeRequest, Map<String, FieldMetadata> metadata) {
if (!canHandleFieldForTypeMetadata(addMetadataFromFieldTypeRequest, metadata)) {
return MetadataProviderResponse.NOT_HANDLED;
}
CollectionMetadata fieldMetadata = (CollectionMetadata) addMetadataFromFieldTypeRequest.getPresentationAttribute();
if (StringUtils.isEmpty(fieldMetadata.getCollectionCeilingEntity())) {
ParameterizedType listType = (ParameterizedType) addMetadataFromFieldTypeRequest.getRequestedField().getGenericType();
Class<?> listClass = (Class<?>) listType.getActualTypeArguments()[0];
fieldMetadata.setCollectionCeilingEntity(listClass.getName());
}
if (addMetadataFromFieldTypeRequest.getTargetClass() != null) {
if (StringUtils.isEmpty(fieldMetadata.getInheritedFromType())) {
fieldMetadata.setInheritedFromType(addMetadataFromFieldTypeRequest.getTargetClass().getName());
}
if (ArrayUtils.isEmpty(fieldMetadata.getAvailableToTypes())) {
fieldMetadata.setAvailableToTypes(new String[] { addMetadataFromFieldTypeRequest.getTargetClass().getName() });
}
}
// {@link BasicPersistenceModule#filterOutCollectionMetadata}
if (addMetadataFromFieldTypeRequest.getForeignField() != null && addMetadataFromFieldTypeRequest.isPropertyForeignKey()) {
FieldInfo info = buildFieldInfo(addMetadataFromFieldTypeRequest.getRequestedField());
BasicFieldMetadata basicMetadata = new BasicFieldMetadata();
basicMetadata.setName(info.getName());
basicMetadata.setExcluded(false);
// Don't show this anywhere on the form and ensure it's explicitly not required
basicMetadata.setVisibility(VisibilityEnum.HIDDEN_ALL);
basicMetadata.setRequired(false);
setClassOwnership(addMetadataFromFieldTypeRequest.getReturnedClass(), addMetadataFromFieldTypeRequest.getTargetClass(), metadata, info);
Map<String, FieldMetadata> fakedMd = new HashMap<String, FieldMetadata>();
fakedMd.put(addMetadataFromFieldTypeRequest.getRequestedField().getName(), basicMetadata);
// Fake out a request and some metadata to pass along as additional metadata within this metadata
AddMetadataFromFieldTypeRequest fakedRequest = new AddMetadataFromFieldTypeRequest(addMetadataFromFieldTypeRequest.getRequestedField(), addMetadataFromFieldTypeRequest.getTargetClass(), addMetadataFromFieldTypeRequest.getForeignField(), addMetadataFromFieldTypeRequest.getAdditionalForeignFields(), addMetadataFromFieldTypeRequest.getMergedPropertyType(), addMetadataFromFieldTypeRequest.getComponentProperties(), addMetadataFromFieldTypeRequest.getIdProperty(), addMetadataFromFieldTypeRequest.getPrefix(), addMetadataFromFieldTypeRequest.getRequestedPropertyName(), addMetadataFromFieldTypeRequest.getType(), addMetadataFromFieldTypeRequest.isPropertyForeignKey(), addMetadataFromFieldTypeRequest.getAdditionalForeignKeyIndexPosition(), fakedMd, basicMetadata, addMetadataFromFieldTypeRequest.getExplicitType(), addMetadataFromFieldTypeRequest.getReturnedClass(), addMetadataFromFieldTypeRequest.getDynamicEntityDao());
defaultMetadataProvider.addMetadataFromFieldType(fakedRequest, fakedMd);
fieldMetadata.getAdditionalMetadata().put(FOREIGN_KEY_ADDITIONAL_METADATA_KEY, basicMetadata);
}
metadata.put(addMetadataFromFieldTypeRequest.getRequestedPropertyName(), fieldMetadata);
return MetadataProviderResponse.HANDLED;
}
use of org.broadleafcommerce.openadmin.server.dao.FieldInfo in project BroadleafCommerce by BroadleafCommerce.
the class MapFieldPersistenceProvider method updateAssignableValue.
protected void updateAssignableValue(PopulateValueRequest populateValueRequest, Serializable instance, Object parent, Class<?> valueType, boolean persistValue, ValueAssignable assignableValue) throws IllegalAccessException, FieldNotAvailableException, InstantiationException {
if (!persistValue) {
// pre-merge (can result in a clone for enterprise)
parent = populateValueRequest.getPersistenceManager().getDynamicEntityDao().merge(parent);
assignableValue = establishAssignableValue(populateValueRequest, parent);
}
if (populateValueRequest.getProperty().getValue() == null) {
populateValueRequest.getPersistenceManager().getDynamicEntityDao().getStandardEntityManager().remove(assignableValue);
return;
}
String key = populateValueRequest.getProperty().getName().substring(populateValueRequest.getProperty().getName().indexOf(FieldManager.MAPFIELDSEPARATOR) + FieldManager.MAPFIELDSEPARATOR.length(), populateValueRequest.getProperty().getName().length());
populateValueRequest.getProperty().setOriginalValue(String.valueOf(assignableValue));
populateValueRequest.getProperty().setOriginalDisplayValue(String.valueOf(assignableValue));
assignableValue.setName(key);
assignableValue.setValue(populateValueRequest.getProperty().getValue());
String fieldName = populateValueRequest.getProperty().getName().substring(0, populateValueRequest.getProperty().getName().indexOf(FieldManager.MAPFIELDSEPARATOR));
Field field = populateValueRequest.getFieldManager().getField(instance.getClass(), fieldName);
FieldInfo fieldInfo = buildFieldInfo(field);
String manyToField = null;
if (populateValueRequest.getMetadata().getManyToField() != null) {
manyToField = populateValueRequest.getMetadata().getManyToField();
}
if (manyToField == null) {
manyToField = fieldInfo.getManyToManyMappedBy();
}
if (manyToField == null) {
manyToField = fieldInfo.getOneToManyMappedBy();
}
if (manyToField != null) {
String propertyName = populateValueRequest.getProperty().getName();
Object middleInstance = instance;
if (propertyName.contains(".")) {
propertyName = propertyName.substring(0, propertyName.lastIndexOf("."));
middleInstance = populateValueRequest.getFieldManager().getFieldValue(instance, propertyName);
}
populateValueRequest.getFieldManager().setFieldValue(assignableValue, manyToField, middleInstance);
if (!populateValueRequest.getPersistenceManager().getDynamicEntityDao().getStandardEntityManager().contains(middleInstance)) {
// if this is part of an add for the manyToField object, don't persist this map value,
// since it would result in a
// transient object exception on the manyToField object (which itself has not been saved yet)
persistValue = false;
}
}
if (persistValue) {
populateValueRequest.getPersistenceManager().getDynamicEntityDao().persist(assignableValue);
}
}
Aggregations