Search in sources :

Example 1 with AdminPresentationDataDrivenEnumerationOverride

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

the class BasicFieldMetadataProvider method buildAdminPresentationDataDrivenEnumerationOverride.

protected void buildAdminPresentationDataDrivenEnumerationOverride(Map<String, FieldMetadata> mergedProperties, Map<String, AdminPresentationDataDrivenEnumerationOverride> presentationOverrides, String propertyName, String key) {
    AdminPresentationDataDrivenEnumerationOverride override = presentationOverrides.get(propertyName);
    if (override != null) {
        AdminPresentationDataDrivenEnumeration annot = override.value();
        if (annot != null) {
            if (!(mergedProperties.get(key) instanceof BasicFieldMetadata)) {
                return;
            }
            BasicFieldMetadata metadata = (BasicFieldMetadata) mergedProperties.get(key);
            metadata.setFieldType(SupportedFieldType.DATA_DRIVEN_ENUMERATION);
            metadata.setExplicitFieldType(SupportedFieldType.DATA_DRIVEN_ENUMERATION);
            metadata.setOptionListEntity(annot.optionListEntity().getName());
            metadata.setHideEnumerationIfEmpty(annot.optionHideIfEmpty());
            if (metadata.getOptionListEntity().equals(DataDrivenEnumerationValueImpl.class.getName())) {
                metadata.setOptionValueFieldName("key");
                metadata.setOptionDisplayFieldName("display");
            } else if (metadata.getOptionListEntity() == null && (StringUtils.isEmpty(metadata.getOptionValueFieldName()) || StringUtils.isEmpty(metadata.getOptionDisplayFieldName()))) {
                throw new IllegalArgumentException("Problem setting up data driven enumeration for (" + propertyName + "). The optionListEntity, optionValueFieldName and optionDisplayFieldName properties must all be included if not using DataDrivenEnumerationValueImpl as the optionListEntity.");
            } else {
                metadata.setOptionValueFieldName(annot.optionValueFieldName());
                metadata.setOptionDisplayFieldName(annot.optionDisplayFieldName());
            }
            if (!ArrayUtils.isEmpty(annot.optionFilterParams())) {
                String[][] params = new String[annot.optionFilterParams().length][3];
                for (int j = 0; j < params.length; j++) {
                    params[j][0] = annot.optionFilterParams()[j].param();
                    params[j][1] = annot.optionFilterParams()[j].value();
                    params[j][2] = String.valueOf(annot.optionFilterParams()[j].paramType());
                }
                metadata.setOptionFilterParams(params);
            } else {
                metadata.setOptionFilterParams(new String[][] {});
            }
            if (!StringUtils.isEmpty(metadata.getOptionListEntity())) {
                buildDataDrivenEnumList(metadata);
            }
        }
    }
}
Also used : BasicFieldMetadata(org.broadleafcommerce.openadmin.dto.BasicFieldMetadata) AdminPresentationDataDrivenEnumerationOverride(org.broadleafcommerce.common.presentation.override.AdminPresentationDataDrivenEnumerationOverride) AdminPresentationDataDrivenEnumeration(org.broadleafcommerce.common.presentation.AdminPresentationDataDrivenEnumeration) DataDrivenEnumerationValueImpl(org.broadleafcommerce.common.enumeration.domain.DataDrivenEnumerationValueImpl)

Example 2 with AdminPresentationDataDrivenEnumerationOverride

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

the class BasicFieldMetadataProvider method overrideViaAnnotation.

@Override
public MetadataProviderResponse overrideViaAnnotation(OverrideViaAnnotationRequest overrideViaAnnotationRequest, Map<String, FieldMetadata> metadata) {
    if (!canHandleAnnotationOverride(overrideViaAnnotationRequest, metadata)) {
        return MetadataProviderResponse.NOT_HANDLED;
    }
    Map<String, AdminPresentationOverride> presentationOverrides = new LinkedHashMap<>();
    Map<String, AdminPresentationToOneLookupOverride> presentationToOneLookupOverrides = new LinkedHashMap<>();
    Map<String, AdminPresentationDataDrivenEnumerationOverride> presentationDataDrivenEnumerationOverrides = new LinkedHashMap<>();
    AdminPresentationOverrides myOverrides = overrideViaAnnotationRequest.getRequestedEntity().getAnnotation(AdminPresentationOverrides.class);
    if (myOverrides != null) {
        for (AdminPresentationOverride myOverride : myOverrides.value()) {
            presentationOverrides.put(myOverride.name(), myOverride);
        }
        for (AdminPresentationToOneLookupOverride myOverride : myOverrides.toOneLookups()) {
            presentationToOneLookupOverrides.put(myOverride.name(), myOverride);
        }
        for (AdminPresentationDataDrivenEnumerationOverride myOverride : myOverrides.dataDrivenEnums()) {
            presentationDataDrivenEnumerationOverrides.put(myOverride.name(), myOverride);
        }
    }
    for (String propertyName : presentationOverrides.keySet()) {
        for (String key : metadata.keySet()) {
            if (StringUtils.isEmpty(propertyName) || key.startsWith(propertyName)) {
                buildAdminPresentationOverride(overrideViaAnnotationRequest.getPrefix(), overrideViaAnnotationRequest.getParentExcluded(), metadata, presentationOverrides, propertyName, key, overrideViaAnnotationRequest.getDynamicEntityDao());
            }
        }
    }
    for (String propertyName : presentationToOneLookupOverrides.keySet()) {
        for (String key : metadata.keySet()) {
            if (key.startsWith(propertyName)) {
                buildAdminPresentationToOneLookupOverride(metadata, presentationToOneLookupOverrides, propertyName, key);
            }
        }
    }
    for (String propertyName : presentationDataDrivenEnumerationOverrides.keySet()) {
        for (String key : metadata.keySet()) {
            if (key.startsWith(propertyName)) {
                buildAdminPresentationDataDrivenEnumerationOverride(metadata, presentationDataDrivenEnumerationOverrides, propertyName, key);
            }
        }
    }
    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<>();
            loopMap.putAll(metadata);
            for (Map.Entry<String, FieldMetadata> entry : loopMap.entrySet()) {
                // if (entry.getKey().startsWith(propertyName) || StringUtils.isEmpty(propertyName)) {
                if (StringUtil.segmentInclusion(entry.getKey(), propertyName) || StringUtils.isEmpty(propertyName)) {
                    FieldMetadata targetMetadata = entry.getValue();
                    if (targetMetadata instanceof BasicFieldMetadata) {
                        BasicFieldMetadata serverMetadata = (BasicFieldMetadata) 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<>(1);
                                temp.put(fieldName, serverMetadata);
                                FieldInfo info;
                                if (field != null) {
                                    info = buildFieldInfo(field);
                                } else {
                                    info = new FieldInfo();
                                    info.setName(fieldName);
                                }
                                FieldMetadataOverride fieldMetadataOverride = overrideMergeMetadata(override);
                                if (serverMetadata.getExcluded() != null && serverMetadata.getExcluded() && (fieldMetadataOverride.getExcluded() == null || fieldMetadataOverride.getExcluded())) {
                                    continue;
                                }
                                buildBasicMetadata(parentClass, targetClass, temp, info, fieldMetadataOverride, overrideViaAnnotationRequest.getDynamicEntityDao());
                                serverMetadata = (BasicFieldMetadata) temp.get(fieldName);
                                metadata.put(entry.getKey(), serverMetadata);
                            } catch (Exception e) {
                                throw new RuntimeException(e);
                            }
                        }
                    }
                }
            }
        }
    }
    return MetadataProviderResponse.HANDLED;
}
Also used : AdminPresentationOverride(org.broadleafcommerce.common.presentation.override.AdminPresentationOverride) AdminPresentationOverrides(org.broadleafcommerce.common.presentation.override.AdminPresentationOverrides) FieldMetadata(org.broadleafcommerce.openadmin.dto.FieldMetadata) BasicFieldMetadata(org.broadleafcommerce.openadmin.dto.BasicFieldMetadata) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) FieldMetadataOverride(org.broadleafcommerce.openadmin.dto.override.FieldMetadataOverride) LinkedHashMap(java.util.LinkedHashMap) AdminPresentationMergeOverride(org.broadleafcommerce.common.presentation.override.AdminPresentationMergeOverride) Field(java.lang.reflect.Field) BasicFieldMetadata(org.broadleafcommerce.openadmin.dto.BasicFieldMetadata) AdminPresentationMergeOverrides(org.broadleafcommerce.common.presentation.override.AdminPresentationMergeOverrides) AdminPresentationDataDrivenEnumerationOverride(org.broadleafcommerce.common.presentation.override.AdminPresentationDataDrivenEnumerationOverride) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) AdminPresentationToOneLookupOverride(org.broadleafcommerce.common.presentation.override.AdminPresentationToOneLookupOverride) FieldInfo(org.broadleafcommerce.openadmin.server.dao.FieldInfo) AdminPresentationMergeOverride(org.broadleafcommerce.common.presentation.override.AdminPresentationMergeOverride) RequiredOverride(org.broadleafcommerce.common.presentation.RequiredOverride) AdminPresentationToOneLookupOverride(org.broadleafcommerce.common.presentation.override.AdminPresentationToOneLookupOverride) AdminPresentationOverride(org.broadleafcommerce.common.presentation.override.AdminPresentationOverride) AdminPresentationDataDrivenEnumerationOverride(org.broadleafcommerce.common.presentation.override.AdminPresentationDataDrivenEnumerationOverride) FieldMetadataOverride(org.broadleafcommerce.openadmin.dto.override.FieldMetadataOverride) MetadataOverride(org.broadleafcommerce.openadmin.dto.override.MetadataOverride)

Aggregations

AdminPresentationDataDrivenEnumerationOverride (org.broadleafcommerce.common.presentation.override.AdminPresentationDataDrivenEnumerationOverride)2 BasicFieldMetadata (org.broadleafcommerce.openadmin.dto.BasicFieldMetadata)2 Field (java.lang.reflect.Field)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 DataDrivenEnumerationValueImpl (org.broadleafcommerce.common.enumeration.domain.DataDrivenEnumerationValueImpl)1 AdminPresentationDataDrivenEnumeration (org.broadleafcommerce.common.presentation.AdminPresentationDataDrivenEnumeration)1 RequiredOverride (org.broadleafcommerce.common.presentation.RequiredOverride)1 AdminPresentationMergeOverride (org.broadleafcommerce.common.presentation.override.AdminPresentationMergeOverride)1 AdminPresentationMergeOverrides (org.broadleafcommerce.common.presentation.override.AdminPresentationMergeOverrides)1 AdminPresentationOverride (org.broadleafcommerce.common.presentation.override.AdminPresentationOverride)1 AdminPresentationOverrides (org.broadleafcommerce.common.presentation.override.AdminPresentationOverrides)1 AdminPresentationToOneLookupOverride (org.broadleafcommerce.common.presentation.override.AdminPresentationToOneLookupOverride)1 FieldMetadata (org.broadleafcommerce.openadmin.dto.FieldMetadata)1 FieldMetadataOverride (org.broadleafcommerce.openadmin.dto.override.FieldMetadataOverride)1 MetadataOverride (org.broadleafcommerce.openadmin.dto.override.MetadataOverride)1 FieldInfo (org.broadleafcommerce.openadmin.server.dao.FieldInfo)1