Search in sources :

Example 1 with AdminPresentationCollectionOverride

use of org.broadleafcommerce.common.presentation.override.AdminPresentationCollectionOverride in project BroadleafCommerce by BroadleafCommerce.

the class CollectionFieldMetadataProvider method buildAdminPresentationCollectionOverride.

protected void buildAdminPresentationCollectionOverride(String prefix, Boolean isParentExcluded, Map<String, FieldMetadata> mergedProperties, Map<String, AdminPresentationCollectionOverride> presentationCollectionOverrides, String propertyName, String key, DynamicEntityDao dynamicEntityDao) {
    AdminPresentationCollectionOverride override = presentationCollectionOverrides.get(propertyName);
    if (override != null) {
        AdminPresentationCollection 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("buildAdminPresentationCollectionOverride: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("buildAdminPresentationCollectionOverride:Showing " + key + "because an override annotation declared " + testKey + " to not be excluded");
                    }
                    metadata.setExcluded(false);
                }
            }
            if (!(mergedProperties.get(key) instanceof BasicCollectionMetadata)) {
                return;
            }
            BasicCollectionMetadata serverMetadata = (BasicCollectionMetadata) 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 = constructBasicCollectionMetadataOverride(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);
                    buildCollectionMetadata(parentClass, targetClass, temp, info, localMetadata, prefix);
                    BasicCollectionMetadata result = (BasicCollectionMetadata) temp.get(field.getName());
                    result.setInheritedFromType(serverMetadata.getInheritedFromType());
                    result.setAvailableToTypes(serverMetadata.getAvailableToTypes());
                    mergedProperties.put(key, result);
                    if (isParentExcluded) {
                        if (LOG.isDebugEnabled()) {
                            LOG.debug("buildAdminPresentationCollectionOverride: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) AdminPresentationCollectionOverride(org.broadleafcommerce.common.presentation.override.AdminPresentationCollectionOverride) HashMap(java.util.HashMap) AdminPresentationCollection(org.broadleafcommerce.common.presentation.AdminPresentationCollection) FieldMetadataOverride(org.broadleafcommerce.openadmin.dto.override.FieldMetadataOverride) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException) Field(java.lang.reflect.Field) BasicCollectionMetadata(org.broadleafcommerce.openadmin.dto.BasicCollectionMetadata) FieldInfo(org.broadleafcommerce.openadmin.server.dao.FieldInfo)

Example 2 with AdminPresentationCollectionOverride

use of org.broadleafcommerce.common.presentation.override.AdminPresentationCollectionOverride in project BroadleafCommerce by BroadleafCommerce.

the class CollectionFieldMetadataProvider method overrideViaAnnotation.

@Override
public MetadataProviderResponse overrideViaAnnotation(OverrideViaAnnotationRequest overrideViaAnnotationRequest, Map<String, FieldMetadata> metadata) {
    if (!canHandleAnnotationOverride(overrideViaAnnotationRequest, metadata)) {
        return MetadataProviderResponse.NOT_HANDLED;
    }
    Map<String, AdminPresentationCollectionOverride> presentationCollectionOverrides = new HashMap<String, AdminPresentationCollectionOverride>();
    AdminPresentationOverrides myOverrides = overrideViaAnnotationRequest.getRequestedEntity().getAnnotation(AdminPresentationOverrides.class);
    if (myOverrides != null) {
        for (AdminPresentationCollectionOverride myOverride : myOverrides.collections()) {
            presentationCollectionOverrides.put(myOverride.name(), myOverride);
        }
    }
    for (String propertyName : presentationCollectionOverrides.keySet()) {
        for (String key : metadata.keySet()) {
            if (key.startsWith(propertyName)) {
                buildAdminPresentationCollectionOverride(overrideViaAnnotationRequest.getPrefix(), overrideViaAnnotationRequest.getParentExcluded(), metadata, presentationCollectionOverrides, 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 BasicCollectionMetadata) {
                        BasicCollectionMetadata serverMetadata = (BasicCollectionMetadata) 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 = overrideCollectionMergeMetadata(override);
                                if (serverMetadata.getExcluded() != null && serverMetadata.getExcluded() && (fieldMetadataOverride.getExcluded() == null || fieldMetadataOverride.getExcluded())) {
                                    continue;
                                }
                                buildCollectionMetadata(parentClass, targetClass, temp, info, fieldMetadataOverride, overrideViaAnnotationRequest.getPrefix());
                                serverMetadata = (BasicCollectionMetadata) 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) AdminPresentationCollectionOverride(org.broadleafcommerce.common.presentation.override.AdminPresentationCollectionOverride) HashMap(java.util.HashMap) FieldMetadataOverride(org.broadleafcommerce.openadmin.dto.override.FieldMetadataOverride) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException) AdminPresentationMergeOverride(org.broadleafcommerce.common.presentation.override.AdminPresentationMergeOverride) Field(java.lang.reflect.Field) AdminPresentationMergeOverrides(org.broadleafcommerce.common.presentation.override.AdminPresentationMergeOverrides) BasicCollectionMetadata(org.broadleafcommerce.openadmin.dto.BasicCollectionMetadata) HashMap(java.util.HashMap) Map(java.util.Map) FieldInfo(org.broadleafcommerce.openadmin.server.dao.FieldInfo) FieldMetadataOverride(org.broadleafcommerce.openadmin.dto.override.FieldMetadataOverride) AdminPresentationMergeOverride(org.broadleafcommerce.common.presentation.override.AdminPresentationMergeOverride) MetadataOverride(org.broadleafcommerce.openadmin.dto.override.MetadataOverride) AdminPresentationCollectionOverride(org.broadleafcommerce.common.presentation.override.AdminPresentationCollectionOverride)

Aggregations

Field (java.lang.reflect.Field)2 HashMap (java.util.HashMap)2 AdminPresentationCollectionOverride (org.broadleafcommerce.common.presentation.override.AdminPresentationCollectionOverride)2 BasicCollectionMetadata (org.broadleafcommerce.openadmin.dto.BasicCollectionMetadata)2 FieldMetadata (org.broadleafcommerce.openadmin.dto.FieldMetadata)2 FieldMetadataOverride (org.broadleafcommerce.openadmin.dto.override.FieldMetadataOverride)2 FieldInfo (org.broadleafcommerce.openadmin.server.dao.FieldInfo)2 NoSuchBeanDefinitionException (org.springframework.beans.factory.NoSuchBeanDefinitionException)2 Map (java.util.Map)1 AdminPresentationCollection (org.broadleafcommerce.common.presentation.AdminPresentationCollection)1 AdminPresentationMergeOverride (org.broadleafcommerce.common.presentation.override.AdminPresentationMergeOverride)1 AdminPresentationMergeOverrides (org.broadleafcommerce.common.presentation.override.AdminPresentationMergeOverrides)1 AdminPresentationOverrides (org.broadleafcommerce.common.presentation.override.AdminPresentationOverrides)1 MetadataOverride (org.broadleafcommerce.openadmin.dto.override.MetadataOverride)1