Search in sources :

Example 21 with FieldInfo

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;
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) CollectionMetadata(org.broadleafcommerce.openadmin.dto.CollectionMetadata) FieldMetadata(org.broadleafcommerce.openadmin.dto.FieldMetadata) BasicFieldMetadata(org.broadleafcommerce.openadmin.dto.BasicFieldMetadata) HashMap(java.util.HashMap) BasicFieldMetadata(org.broadleafcommerce.openadmin.dto.BasicFieldMetadata) FieldInfo(org.broadleafcommerce.openadmin.server.dao.FieldInfo) AddMetadataFromFieldTypeRequest(org.broadleafcommerce.openadmin.server.dao.provider.metadata.request.AddMetadataFromFieldTypeRequest)

Example 22 with FieldInfo

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);
    }
}
Also used : Field(java.lang.reflect.Field) FieldInfo(org.broadleafcommerce.openadmin.server.dao.FieldInfo)

Aggregations

FieldInfo (org.broadleafcommerce.openadmin.server.dao.FieldInfo)22 FieldMetadataOverride (org.broadleafcommerce.openadmin.dto.override.FieldMetadataOverride)18 Field (java.lang.reflect.Field)13 HashMap (java.util.HashMap)13 FieldMetadata (org.broadleafcommerce.openadmin.dto.FieldMetadata)13 MetadataOverride (org.broadleafcommerce.openadmin.dto.override.MetadataOverride)13 AdminPresentationMergeOverride (org.broadleafcommerce.common.presentation.override.AdminPresentationMergeOverride)12 BasicFieldMetadata (org.broadleafcommerce.openadmin.dto.BasicFieldMetadata)6 NoSuchBeanDefinitionException (org.springframework.beans.factory.NoSuchBeanDefinitionException)6 Map (java.util.Map)4 AdminPresentationMap (org.broadleafcommerce.common.presentation.AdminPresentationMap)4 AdminPresentationAdornedTargetCollectionOverride (org.broadleafcommerce.common.presentation.override.AdminPresentationAdornedTargetCollectionOverride)4 AdminPresentationCollectionOverride (org.broadleafcommerce.common.presentation.override.AdminPresentationCollectionOverride)4 AdminPresentationMapOverride (org.broadleafcommerce.common.presentation.override.AdminPresentationMapOverride)4 AdminPresentationMergeOverrides (org.broadleafcommerce.common.presentation.override.AdminPresentationMergeOverrides)4 AdminPresentationOverride (org.broadleafcommerce.common.presentation.override.AdminPresentationOverride)4 AdminPresentationOverrides (org.broadleafcommerce.common.presentation.override.AdminPresentationOverrides)4 LinkedHashMap (java.util.LinkedHashMap)3 RequiredOverride (org.broadleafcommerce.common.presentation.RequiredOverride)3 AdminPresentationDataDrivenEnumerationOverride (org.broadleafcommerce.common.presentation.override.AdminPresentationDataDrivenEnumerationOverride)3