Search in sources :

Example 6 with FieldInfo

use of org.broadleafcommerce.openadmin.server.dao.FieldInfo in project BroadleafCommerce by BroadleafCommerce.

the class MapFieldMetadataProvider method overrideViaXml.

@Override
public MetadataProviderResponse overrideViaXml(OverrideViaXmlRequest overrideViaXmlRequest, Map<String, FieldMetadata> metadata) {
    Map<String, MetadataOverride> overrides = getTargetedOverride(overrideViaXmlRequest.getDynamicEntityDao(), overrideViaXmlRequest.getRequestedConfigKey(), overrideViaXmlRequest.getRequestedCeilingEntity());
    if (overrides != null) {
        for (String propertyName : overrides.keySet()) {
            MetadataOverride localMetadata = overrides.get(propertyName);
            if (localMetadata instanceof FieldMetadataOverride) {
                FieldMetadataOverride localFieldMetadata = (FieldMetadataOverride) localMetadata;
                for (String key : metadata.keySet()) {
                    if (key.equals(propertyName)) {
                        try {
                            if (metadata.get(key) instanceof MapMetadata) {
                                MapMetadata serverMetadata = (MapMetadata) metadata.get(key);
                                if (serverMetadata.getTargetClass() != null) {
                                    Class<?> targetClass = Class.forName(serverMetadata.getTargetClass());
                                    Class<?> parentClass = null;
                                    if (serverMetadata.getOwningClass() != null) {
                                        parentClass = Class.forName(serverMetadata.getOwningClass());
                                    }
                                    String fieldName = serverMetadata.getFieldName();
                                    Field field = overrideViaXmlRequest.getDynamicEntityDao().getFieldManager().getField(targetClass, fieldName);
                                    Map<String, FieldMetadata> temp = new HashMap<String, FieldMetadata>(1);
                                    temp.put(field.getName(), serverMetadata);
                                    FieldInfo info = buildFieldInfo(field);
                                    buildMapMetadata(parentClass, targetClass, temp, info, localFieldMetadata, overrideViaXmlRequest.getDynamicEntityDao(), serverMetadata.getPrefix());
                                    serverMetadata = (MapMetadata) temp.get(field.getName());
                                    metadata.put(key, serverMetadata);
                                    if (overrideViaXmlRequest.getParentExcluded()) {
                                        if (LOG.isDebugEnabled()) {
                                            LOG.debug("applyMapMetadataOverrides:Excluding " + key + "because parent is marked as excluded.");
                                        }
                                        serverMetadata.setExcluded(true);
                                    }
                                }
                            }
                        } catch (Exception e) {
                            throw new RuntimeException(e);
                        }
                    }
                }
            }
        }
    }
    return MetadataProviderResponse.HANDLED;
}
Also used : FieldMetadata(org.broadleafcommerce.openadmin.dto.FieldMetadata) HashMap(java.util.HashMap) FieldMetadataOverride(org.broadleafcommerce.openadmin.dto.override.FieldMetadataOverride) Field(java.lang.reflect.Field) FieldMetadataOverride(org.broadleafcommerce.openadmin.dto.override.FieldMetadataOverride) MetadataOverride(org.broadleafcommerce.openadmin.dto.override.MetadataOverride) MapMetadata(org.broadleafcommerce.openadmin.dto.MapMetadata) FieldInfo(org.broadleafcommerce.openadmin.server.dao.FieldInfo) FieldMetadataOverride(org.broadleafcommerce.openadmin.dto.override.FieldMetadataOverride) AdminPresentationMergeOverride(org.broadleafcommerce.common.presentation.override.AdminPresentationMergeOverride) AdminPresentationMapOverride(org.broadleafcommerce.common.presentation.override.AdminPresentationMapOverride) MetadataOverride(org.broadleafcommerce.openadmin.dto.override.MetadataOverride)

Example 7 with FieldInfo

use of org.broadleafcommerce.openadmin.server.dao.FieldInfo in project BroadleafCommerce by BroadleafCommerce.

the class MapFieldMetadataProvider method addMetadata.

@Override
public MetadataProviderResponse addMetadata(AddFieldMetadataRequest addMetadataRequest, Map<String, FieldMetadata> metadata) {
    if (!canHandleFieldForConfiguredMetadata(addMetadataRequest, metadata)) {
        return MetadataProviderResponse.NOT_HANDLED;
    }
    AdminPresentationMap annot = addMetadataRequest.getRequestedField().getAnnotation(AdminPresentationMap.class);
    FieldInfo info = buildFieldInfo(addMetadataRequest.getRequestedField());
    FieldMetadataOverride override = constructMapMetadataOverride(annot);
    buildMapMetadata(addMetadataRequest.getParentClass(), addMetadataRequest.getTargetClass(), metadata, info, override, addMetadataRequest.getDynamicEntityDao(), addMetadataRequest.getPrefix());
    setClassOwnership(addMetadataRequest.getParentClass(), addMetadataRequest.getTargetClass(), metadata, info);
    return MetadataProviderResponse.HANDLED;
}
Also used : FieldInfo(org.broadleafcommerce.openadmin.server.dao.FieldInfo) FieldMetadataOverride(org.broadleafcommerce.openadmin.dto.override.FieldMetadataOverride) AdminPresentationMap(org.broadleafcommerce.common.presentation.AdminPresentationMap) FieldMetadataOverride(org.broadleafcommerce.openadmin.dto.override.FieldMetadataOverride) AdminPresentationMergeOverride(org.broadleafcommerce.common.presentation.override.AdminPresentationMergeOverride) AdminPresentationMapOverride(org.broadleafcommerce.common.presentation.override.AdminPresentationMapOverride) MetadataOverride(org.broadleafcommerce.openadmin.dto.override.MetadataOverride)

Example 8 with FieldInfo

use of org.broadleafcommerce.openadmin.server.dao.FieldInfo in project BroadleafCommerce by BroadleafCommerce.

the class MapFieldMetadataProvider method buildAdminPresentationMapOverride.

protected void buildAdminPresentationMapOverride(String prefix, Boolean isParentExcluded, Map<String, FieldMetadata> mergedProperties, Map<String, AdminPresentationMapOverride> presentationMapOverrides, String propertyName, String key, DynamicEntityDao dynamicEntityDao) {
    AdminPresentationMapOverride override = presentationMapOverrides.get(propertyName);
    if (override != null) {
        AdminPresentationMap annot = override.value();
        if (annot != null) {
            String testKey = prefix + key;
            if ((testKey.startsWith(propertyName + ".") || testKey.equals(propertyName)) && annot.excluded()) {
                FieldMetadata metadata = mergedProperties.get(key);
                if (LOG.isDebugEnabled()) {
                    LOG.debug("buildAdminPresentationMapOverride:Excluding " + key + "because an override annotation declared " + testKey + "to be excluded");
                }
                metadata.setExcluded(true);
                return;
            }
            if ((testKey.startsWith(propertyName + ".") || testKey.equals(propertyName)) && !annot.excluded()) {
                FieldMetadata metadata = mergedProperties.get(key);
                if (!isParentExcluded) {
                    if (LOG.isDebugEnabled()) {
                        LOG.debug("buildAdminPresentationMapOverride:Showing " + key + "because an override annotation declared " + testKey + " to not be excluded");
                    }
                    metadata.setExcluded(false);
                }
            }
            if (!(mergedProperties.get(key) instanceof MapMetadata)) {
                return;
            }
            MapMetadata serverMetadata = (MapMetadata) mergedProperties.get(key);
            if (serverMetadata.getTargetClass() != null) {
                try {
                    Class<?> targetClass = Class.forName(serverMetadata.getTargetClass());
                    Class<?> parentClass = null;
                    if (serverMetadata.getOwningClass() != null) {
                        parentClass = Class.forName(serverMetadata.getOwningClass());
                    }
                    String fieldName = serverMetadata.getFieldName();
                    Field field = dynamicEntityDao.getFieldManager().getField(targetClass, fieldName);
                    FieldMetadataOverride localMetadata = constructMapMetadataOverride(annot);
                    // do not include the previous metadata - we want to construct a fresh metadata from the override annotation
                    Map<String, FieldMetadata> temp = new HashMap<String, FieldMetadata>(1);
                    FieldInfo info = buildFieldInfo(field);
                    buildMapMetadata(parentClass, targetClass, temp, info, localMetadata, dynamicEntityDao, serverMetadata.getPrefix());
                    MapMetadata result = (MapMetadata) temp.get(field.getName());
                    result.setInheritedFromType(serverMetadata.getInheritedFromType());
                    result.setAvailableToTypes(serverMetadata.getAvailableToTypes());
                    mergedProperties.put(key, result);
                    if (isParentExcluded) {
                        if (LOG.isDebugEnabled()) {
                            LOG.debug("buildAdminPresentationMapOverride:Excluding " + key + "because the parent was excluded");
                        }
                        serverMetadata.setExcluded(true);
                    }
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
}
Also used : FieldMetadata(org.broadleafcommerce.openadmin.dto.FieldMetadata) HashMap(java.util.HashMap) FieldMetadataOverride(org.broadleafcommerce.openadmin.dto.override.FieldMetadataOverride) AdminPresentationMap(org.broadleafcommerce.common.presentation.AdminPresentationMap) Field(java.lang.reflect.Field) AdminPresentationMapOverride(org.broadleafcommerce.common.presentation.override.AdminPresentationMapOverride) MapMetadata(org.broadleafcommerce.openadmin.dto.MapMetadata) FieldInfo(org.broadleafcommerce.openadmin.server.dao.FieldInfo)

Example 9 with FieldInfo

use of org.broadleafcommerce.openadmin.server.dao.FieldInfo in project BroadleafCommerce by BroadleafCommerce.

the class MapFieldMetadataProvider method overrideViaAnnotation.

@Override
public MetadataProviderResponse overrideViaAnnotation(OverrideViaAnnotationRequest overrideViaAnnotationRequest, Map<String, FieldMetadata> metadata) {
    if (!canHandleAnnotationOverride(overrideViaAnnotationRequest, metadata)) {
        return MetadataProviderResponse.NOT_HANDLED;
    }
    Map<String, AdminPresentationMapOverride> presentationMapOverrides = new HashMap<String, AdminPresentationMapOverride>();
    AdminPresentationOverrides myOverrides = overrideViaAnnotationRequest.getRequestedEntity().getAnnotation(AdminPresentationOverrides.class);
    if (myOverrides != null) {
        for (AdminPresentationMapOverride myOverride : myOverrides.maps()) {
            presentationMapOverrides.put(myOverride.name(), myOverride);
        }
    }
    for (String propertyName : presentationMapOverrides.keySet()) {
        for (String key : metadata.keySet()) {
            if (key.startsWith(propertyName)) {
                buildAdminPresentationMapOverride(overrideViaAnnotationRequest.getPrefix(), overrideViaAnnotationRequest.getParentExcluded(), metadata, presentationMapOverrides, propertyName, key, overrideViaAnnotationRequest.getDynamicEntityDao());
            }
        }
    }
    AdminPresentationMergeOverrides myMergeOverrides = overrideViaAnnotationRequest.getRequestedEntity().getAnnotation(AdminPresentationMergeOverrides.class);
    if (myMergeOverrides != null) {
        for (AdminPresentationMergeOverride override : myMergeOverrides.value()) {
            String propertyName = override.name();
            Map<String, FieldMetadata> loopMap = new HashMap<String, FieldMetadata>();
            loopMap.putAll(metadata);
            for (Map.Entry<String, FieldMetadata> entry : loopMap.entrySet()) {
                if (entry.getKey().startsWith(propertyName) || StringUtils.isEmpty(propertyName)) {
                    FieldMetadata targetMetadata = entry.getValue();
                    if (targetMetadata instanceof MapMetadata) {
                        MapMetadata serverMetadata = (MapMetadata) targetMetadata;
                        if (serverMetadata.getTargetClass() != null) {
                            try {
                                Class<?> targetClass = Class.forName(serverMetadata.getTargetClass());
                                Class<?> parentClass = null;
                                if (serverMetadata.getOwningClass() != null) {
                                    parentClass = Class.forName(serverMetadata.getOwningClass());
                                }
                                String fieldName = serverMetadata.getFieldName();
                                Field field = overrideViaAnnotationRequest.getDynamicEntityDao().getFieldManager().getField(targetClass, fieldName);
                                Map<String, FieldMetadata> temp = new HashMap<String, FieldMetadata>(1);
                                temp.put(field.getName(), serverMetadata);
                                FieldInfo info = buildFieldInfo(field);
                                FieldMetadataOverride fieldMetadataOverride = overrideMapMergeMetadata(override);
                                if (serverMetadata.getExcluded() != null && serverMetadata.getExcluded() && (fieldMetadataOverride.getExcluded() == null || fieldMetadataOverride.getExcluded())) {
                                    continue;
                                }
                                buildMapMetadata(parentClass, targetClass, temp, info, fieldMetadataOverride, overrideViaAnnotationRequest.getDynamicEntityDao(), serverMetadata.getPrefix());
                                serverMetadata = (MapMetadata) temp.get(field.getName());
                                metadata.put(entry.getKey(), serverMetadata);
                            } catch (Exception e) {
                                throw new RuntimeException(e);
                            }
                        }
                    }
                }
            }
        }
    }
    return MetadataProviderResponse.HANDLED;
}
Also used : AdminPresentationOverrides(org.broadleafcommerce.common.presentation.override.AdminPresentationOverrides) FieldMetadata(org.broadleafcommerce.openadmin.dto.FieldMetadata) HashMap(java.util.HashMap) FieldMetadataOverride(org.broadleafcommerce.openadmin.dto.override.FieldMetadataOverride) AdminPresentationMergeOverride(org.broadleafcommerce.common.presentation.override.AdminPresentationMergeOverride) Field(java.lang.reflect.Field) AdminPresentationMapOverride(org.broadleafcommerce.common.presentation.override.AdminPresentationMapOverride) AdminPresentationMergeOverrides(org.broadleafcommerce.common.presentation.override.AdminPresentationMergeOverrides) HashMap(java.util.HashMap) Map(java.util.Map) AdminPresentationMap(org.broadleafcommerce.common.presentation.AdminPresentationMap) MapMetadata(org.broadleafcommerce.openadmin.dto.MapMetadata) FieldInfo(org.broadleafcommerce.openadmin.server.dao.FieldInfo) FieldMetadataOverride(org.broadleafcommerce.openadmin.dto.override.FieldMetadataOverride) AdminPresentationMergeOverride(org.broadleafcommerce.common.presentation.override.AdminPresentationMergeOverride) AdminPresentationMapOverride(org.broadleafcommerce.common.presentation.override.AdminPresentationMapOverride) MetadataOverride(org.broadleafcommerce.openadmin.dto.override.MetadataOverride)

Example 10 with FieldInfo

use of org.broadleafcommerce.openadmin.server.dao.FieldInfo in project BroadleafCommerce by BroadleafCommerce.

the class AbstractFieldMetadataProvider method buildFieldInfo.

protected FieldInfo buildFieldInfo(Field field) {
    FieldInfo info = new FieldInfo();
    info.setName(field.getName());
    info.setGenericType(field.getGenericType());
    ManyToMany manyToMany = field.getAnnotation(ManyToMany.class);
    if (manyToMany != null) {
        info.setManyToManyMappedBy(manyToMany.mappedBy());
        info.setManyToManyTargetEntity(manyToMany.targetEntity().getName());
    }
    OneToMany oneToMany = field.getAnnotation(OneToMany.class);
    if (oneToMany != null) {
        info.setOneToManyMappedBy(oneToMany.mappedBy());
        info.setOneToManyTargetEntity(oneToMany.targetEntity().getName());
    }
    MapKey mapKey = field.getAnnotation(MapKey.class);
    if (mapKey != null) {
        info.setMapKey(mapKey.name());
    }
    return info;
}
Also used : MapKey(javax.persistence.MapKey) ManyToMany(javax.persistence.ManyToMany) OneToMany(javax.persistence.OneToMany) 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