Search in sources :

Example 51 with BasicFieldMetadata

use of org.broadleafcommerce.openadmin.dto.BasicFieldMetadata 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)

Example 52 with BasicFieldMetadata

use of org.broadleafcommerce.openadmin.dto.BasicFieldMetadata in project BroadleafCommerce by BroadleafCommerce.

the class BasicFieldMetadataProvider method buildAdminPresentationOverride.

protected void buildAdminPresentationOverride(String prefix, Boolean isParentExcluded, Map<String, FieldMetadata> mergedProperties, Map<String, AdminPresentationOverride> presentationOverrides, String propertyName, String key, DynamicEntityDao dynamicEntityDao) {
    AdminPresentationOverride override = presentationOverrides.get(propertyName);
    if (override != null) {
        AdminPresentation 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("buildAdminPresentationOverride: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("buildAdminPresentationOverride:Showing " + key + "because an override annotation declared " + testKey + " to not be excluded");
                    }
                    metadata.setExcluded(false);
                }
            }
            if (!(mergedProperties.get(key) instanceof BasicFieldMetadata)) {
                return;
            }
            BasicFieldMetadata serverMetadata = (BasicFieldMetadata) 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 = constructBasicMetadataOverride(annot, null, null);
                    // do not include the previous metadata - we want to construct a fresh metadata from the override annotation
                    Map<String, FieldMetadata> temp = new HashMap<>(1);
                    FieldInfo info = buildFieldInfo(field);
                    buildBasicMetadata(parentClass, targetClass, temp, info, localMetadata, dynamicEntityDao);
                    BasicFieldMetadata result = (BasicFieldMetadata) temp.get(field.getName());
                    result.setInheritedFromType(serverMetadata.getInheritedFromType());
                    result.setAvailableToTypes(serverMetadata.getAvailableToTypes());
                    result.setFieldType(serverMetadata.getFieldType());
                    result.setSecondaryType(serverMetadata.getSecondaryType());
                    result.setLength(serverMetadata.getLength());
                    result.setScale(serverMetadata.getScale());
                    result.setPrecision(serverMetadata.getPrecision());
                    result.setRequired(serverMetadata.getRequired());
                    result.setUnique(serverMetadata.getUnique());
                    result.setForeignKeyCollection(serverMetadata.getForeignKeyCollection());
                    result.setMutable(serverMetadata.getMutable());
                    result.setMergedPropertyType(serverMetadata.getMergedPropertyType());
                    mergedProperties.put(key, result);
                    if (isParentExcluded) {
                        if (LOG.isDebugEnabled()) {
                            LOG.debug("buildAdminPresentationOverride:Excluding " + key + "because the parent was excluded");
                        }
                        serverMetadata.setExcluded(true);
                    }
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
}
Also used : AdminPresentationOverride(org.broadleafcommerce.common.presentation.override.AdminPresentationOverride) FieldMetadata(org.broadleafcommerce.openadmin.dto.FieldMetadata) BasicFieldMetadata(org.broadleafcommerce.openadmin.dto.BasicFieldMetadata) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) AdminPresentation(org.broadleafcommerce.common.presentation.AdminPresentation) FieldMetadataOverride(org.broadleafcommerce.openadmin.dto.override.FieldMetadataOverride) Field(java.lang.reflect.Field) BasicFieldMetadata(org.broadleafcommerce.openadmin.dto.BasicFieldMetadata) FieldInfo(org.broadleafcommerce.openadmin.server.dao.FieldInfo)

Example 53 with BasicFieldMetadata

use of org.broadleafcommerce.openadmin.dto.BasicFieldMetadata in project BroadleafCommerce by BroadleafCommerce.

the class DefaultFieldMetadataProvider method addMetadataFromMappingData.

@Override
public MetadataProviderResponse addMetadataFromMappingData(AddMetadataFromMappingDataRequest addMetadataFromMappingDataRequest, FieldMetadata metadata) {
    BasicFieldMetadata fieldMetadata = (BasicFieldMetadata) metadata;
    fieldMetadata.setFieldType(addMetadataFromMappingDataRequest.getType());
    fieldMetadata.setSecondaryType(addMetadataFromMappingDataRequest.getSecondaryType());
    if (addMetadataFromMappingDataRequest.getRequestedEntityType() != null && !addMetadataFromMappingDataRequest.getRequestedEntityType().isCollectionType()) {
        Column column = null;
        for (Property property : addMetadataFromMappingDataRequest.getComponentProperties()) {
            if (property.getName().equals(addMetadataFromMappingDataRequest.getPropertyName())) {
                Object columnObject = property.getColumnIterator().next();
                if (columnObject instanceof Column) {
                    column = (Column) columnObject;
                }
                break;
            }
        }
        if (column != null) {
            fieldMetadata.setLength(column.getLength());
            fieldMetadata.setScale(column.getScale());
            fieldMetadata.setPrecision(column.getPrecision());
            fieldMetadata.setRequired(!column.isNullable());
            fieldMetadata.setUnique(column.isUnique());
        }
        fieldMetadata.setForeignKeyCollection(false);
    } else {
        fieldMetadata.setForeignKeyCollection(true);
    }
    fieldMetadata.setMutable(true);
    fieldMetadata.setMergedPropertyType(addMetadataFromMappingDataRequest.getMergedPropertyType());
    if (SupportedFieldType.BROADLEAF_ENUMERATION.equals(addMetadataFromMappingDataRequest.getType())) {
        try {
            setupBroadleafEnumeration(fieldMetadata.getBroadleafEnumeration(), fieldMetadata, addMetadataFromMappingDataRequest.getDynamicEntityDao());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    return MetadataProviderResponse.HANDLED;
}
Also used : Column(org.hibernate.mapping.Column) BasicFieldMetadata(org.broadleafcommerce.openadmin.dto.BasicFieldMetadata) Property(org.hibernate.mapping.Property) FieldMetadataOverride(org.broadleafcommerce.openadmin.dto.override.FieldMetadataOverride) MetadataOverride(org.broadleafcommerce.openadmin.dto.override.MetadataOverride)

Example 54 with BasicFieldMetadata

use of org.broadleafcommerce.openadmin.dto.BasicFieldMetadata in project BroadleafCommerce by BroadleafCommerce.

the class DefaultFieldMetadataProvider method addMetadataFromFieldType.

@Override
public MetadataProviderResponse addMetadataFromFieldType(AddMetadataFromFieldTypeRequest addMetadataFromFieldTypeRequest, Map<String, FieldMetadata> metadata) {
    if (addMetadataFromFieldTypeRequest.getPresentationAttribute() != null) {
        if (addMetadataFromFieldTypeRequest.getExplicitType() != null && addMetadataFromFieldTypeRequest.getExplicitType() != SupportedFieldType.UNKNOWN && addMetadataFromFieldTypeRequest.getExplicitType() != SupportedFieldType.BOOLEAN && addMetadataFromFieldTypeRequest.getExplicitType() != SupportedFieldType.INTEGER && addMetadataFromFieldTypeRequest.getExplicitType() != SupportedFieldType.DATE && addMetadataFromFieldTypeRequest.getExplicitType() != SupportedFieldType.STRING && addMetadataFromFieldTypeRequest.getExplicitType() != SupportedFieldType.MONEY && addMetadataFromFieldTypeRequest.getExplicitType() != SupportedFieldType.DECIMAL && addMetadataFromFieldTypeRequest.getExplicitType() != SupportedFieldType.FOREIGN_KEY && addMetadataFromFieldTypeRequest.getExplicitType() != SupportedFieldType.ADDITIONAL_FOREIGN_KEY) {
            metadata.put(addMetadataFromFieldTypeRequest.getRequestedPropertyName(), addMetadataFromFieldTypeRequest.getDynamicEntityDao().getMetadata().getFieldMetadata(addMetadataFromFieldTypeRequest.getPrefix(), addMetadataFromFieldTypeRequest.getRequestedPropertyName(), addMetadataFromFieldTypeRequest.getComponentProperties(), addMetadataFromFieldTypeRequest.getExplicitType(), addMetadataFromFieldTypeRequest.getType(), addMetadataFromFieldTypeRequest.getTargetClass(), addMetadataFromFieldTypeRequest.getPresentationAttribute(), addMetadataFromFieldTypeRequest.getMergedPropertyType(), addMetadataFromFieldTypeRequest.getDynamicEntityDao()));
        } else if (addMetadataFromFieldTypeRequest.getExplicitType() != null && addMetadataFromFieldTypeRequest.getExplicitType() == SupportedFieldType.BOOLEAN || addMetadataFromFieldTypeRequest.getReturnedClass().equals(Boolean.class) || addMetadataFromFieldTypeRequest.getReturnedClass().equals(Character.class)) {
            metadata.put(addMetadataFromFieldTypeRequest.getRequestedPropertyName(), addMetadataFromFieldTypeRequest.getDynamicEntityDao().getMetadata().getFieldMetadata(addMetadataFromFieldTypeRequest.getPrefix(), addMetadataFromFieldTypeRequest.getRequestedPropertyName(), addMetadataFromFieldTypeRequest.getComponentProperties(), SupportedFieldType.BOOLEAN, addMetadataFromFieldTypeRequest.getType(), addMetadataFromFieldTypeRequest.getTargetClass(), addMetadataFromFieldTypeRequest.getPresentationAttribute(), addMetadataFromFieldTypeRequest.getMergedPropertyType(), addMetadataFromFieldTypeRequest.getDynamicEntityDao()));
        } else if (addMetadataFromFieldTypeRequest.getExplicitType() != null && addMetadataFromFieldTypeRequest.getExplicitType() == SupportedFieldType.INTEGER || addMetadataFromFieldTypeRequest.getReturnedClass().equals(Byte.class) || addMetadataFromFieldTypeRequest.getReturnedClass().equals(Short.class) || addMetadataFromFieldTypeRequest.getReturnedClass().equals(Integer.class) || addMetadataFromFieldTypeRequest.getReturnedClass().equals(Long.class)) {
            if (addMetadataFromFieldTypeRequest.getRequestedPropertyName().equals(addMetadataFromFieldTypeRequest.getIdProperty())) {
                metadata.put(addMetadataFromFieldTypeRequest.getRequestedPropertyName(), addMetadataFromFieldTypeRequest.getDynamicEntityDao().getMetadata().getFieldMetadata(addMetadataFromFieldTypeRequest.getPrefix(), addMetadataFromFieldTypeRequest.getRequestedPropertyName(), addMetadataFromFieldTypeRequest.getComponentProperties(), SupportedFieldType.ID, SupportedFieldType.INTEGER, addMetadataFromFieldTypeRequest.getType(), addMetadataFromFieldTypeRequest.getTargetClass(), addMetadataFromFieldTypeRequest.getPresentationAttribute(), addMetadataFromFieldTypeRequest.getMergedPropertyType(), addMetadataFromFieldTypeRequest.getDynamicEntityDao()));
            } else {
                metadata.put(addMetadataFromFieldTypeRequest.getRequestedPropertyName(), addMetadataFromFieldTypeRequest.getDynamicEntityDao().getMetadata().getFieldMetadata(addMetadataFromFieldTypeRequest.getPrefix(), addMetadataFromFieldTypeRequest.getRequestedPropertyName(), addMetadataFromFieldTypeRequest.getComponentProperties(), SupportedFieldType.INTEGER, addMetadataFromFieldTypeRequest.getType(), addMetadataFromFieldTypeRequest.getTargetClass(), addMetadataFromFieldTypeRequest.getPresentationAttribute(), addMetadataFromFieldTypeRequest.getMergedPropertyType(), addMetadataFromFieldTypeRequest.getDynamicEntityDao()));
            }
        } else if (addMetadataFromFieldTypeRequest.getExplicitType() != null && addMetadataFromFieldTypeRequest.getExplicitType() == SupportedFieldType.DATE || addMetadataFromFieldTypeRequest.getReturnedClass().equals(Calendar.class) || addMetadataFromFieldTypeRequest.getReturnedClass().equals(Date.class) || addMetadataFromFieldTypeRequest.getReturnedClass().equals(Timestamp.class)) {
            metadata.put(addMetadataFromFieldTypeRequest.getRequestedPropertyName(), addMetadataFromFieldTypeRequest.getDynamicEntityDao().getMetadata().getFieldMetadata(addMetadataFromFieldTypeRequest.getPrefix(), addMetadataFromFieldTypeRequest.getRequestedPropertyName(), addMetadataFromFieldTypeRequest.getComponentProperties(), SupportedFieldType.DATE, addMetadataFromFieldTypeRequest.getType(), addMetadataFromFieldTypeRequest.getTargetClass(), addMetadataFromFieldTypeRequest.getPresentationAttribute(), addMetadataFromFieldTypeRequest.getMergedPropertyType(), addMetadataFromFieldTypeRequest.getDynamicEntityDao()));
        } else if (addMetadataFromFieldTypeRequest.getExplicitType() != null && addMetadataFromFieldTypeRequest.getExplicitType() == SupportedFieldType.STRING || addMetadataFromFieldTypeRequest.getReturnedClass().equals(String.class)) {
            if (addMetadataFromFieldTypeRequest.getRequestedPropertyName().equals(addMetadataFromFieldTypeRequest.getIdProperty())) {
                metadata.put(addMetadataFromFieldTypeRequest.getRequestedPropertyName(), addMetadataFromFieldTypeRequest.getDynamicEntityDao().getMetadata().getFieldMetadata(addMetadataFromFieldTypeRequest.getPrefix(), addMetadataFromFieldTypeRequest.getRequestedPropertyName(), addMetadataFromFieldTypeRequest.getComponentProperties(), SupportedFieldType.ID, SupportedFieldType.STRING, addMetadataFromFieldTypeRequest.getType(), addMetadataFromFieldTypeRequest.getTargetClass(), addMetadataFromFieldTypeRequest.getPresentationAttribute(), addMetadataFromFieldTypeRequest.getMergedPropertyType(), addMetadataFromFieldTypeRequest.getDynamicEntityDao()));
            } else {
                metadata.put(addMetadataFromFieldTypeRequest.getRequestedPropertyName(), addMetadataFromFieldTypeRequest.getDynamicEntityDao().getMetadata().getFieldMetadata(addMetadataFromFieldTypeRequest.getPrefix(), addMetadataFromFieldTypeRequest.getRequestedPropertyName(), addMetadataFromFieldTypeRequest.getComponentProperties(), SupportedFieldType.STRING, addMetadataFromFieldTypeRequest.getType(), addMetadataFromFieldTypeRequest.getTargetClass(), addMetadataFromFieldTypeRequest.getPresentationAttribute(), addMetadataFromFieldTypeRequest.getMergedPropertyType(), addMetadataFromFieldTypeRequest.getDynamicEntityDao()));
            }
        } else if (addMetadataFromFieldTypeRequest.getExplicitType() != null && addMetadataFromFieldTypeRequest.getExplicitType() == SupportedFieldType.MONEY || addMetadataFromFieldTypeRequest.getReturnedClass().equals(Money.class)) {
            metadata.put(addMetadataFromFieldTypeRequest.getRequestedPropertyName(), addMetadataFromFieldTypeRequest.getDynamicEntityDao().getMetadata().getFieldMetadata(addMetadataFromFieldTypeRequest.getPrefix(), addMetadataFromFieldTypeRequest.getRequestedPropertyName(), addMetadataFromFieldTypeRequest.getComponentProperties(), SupportedFieldType.MONEY, addMetadataFromFieldTypeRequest.getType(), addMetadataFromFieldTypeRequest.getTargetClass(), addMetadataFromFieldTypeRequest.getPresentationAttribute(), addMetadataFromFieldTypeRequest.getMergedPropertyType(), addMetadataFromFieldTypeRequest.getDynamicEntityDao()));
        } else if (addMetadataFromFieldTypeRequest.getExplicitType() != null && addMetadataFromFieldTypeRequest.getExplicitType() == SupportedFieldType.DECIMAL || addMetadataFromFieldTypeRequest.getReturnedClass().equals(Double.class) || addMetadataFromFieldTypeRequest.getReturnedClass().equals(BigDecimal.class)) {
            metadata.put(addMetadataFromFieldTypeRequest.getRequestedPropertyName(), addMetadataFromFieldTypeRequest.getDynamicEntityDao().getMetadata().getFieldMetadata(addMetadataFromFieldTypeRequest.getPrefix(), addMetadataFromFieldTypeRequest.getRequestedPropertyName(), addMetadataFromFieldTypeRequest.getComponentProperties(), SupportedFieldType.DECIMAL, addMetadataFromFieldTypeRequest.getType(), addMetadataFromFieldTypeRequest.getTargetClass(), addMetadataFromFieldTypeRequest.getPresentationAttribute(), addMetadataFromFieldTypeRequest.getMergedPropertyType(), addMetadataFromFieldTypeRequest.getDynamicEntityDao()));
        } else if (addMetadataFromFieldTypeRequest.getExplicitType() != null && addMetadataFromFieldTypeRequest.getExplicitType() == SupportedFieldType.FOREIGN_KEY || addMetadataFromFieldTypeRequest.getForeignField() != null && addMetadataFromFieldTypeRequest.isPropertyForeignKey()) {
            ClassMetadata foreignMetadata;
            String foreignKeyClass;
            String lookupDisplayProperty;
            if (addMetadataFromFieldTypeRequest.getForeignField() == null) {
                Class<?>[] entities = addMetadataFromFieldTypeRequest.getDynamicEntityDao().getAllPolymorphicEntitiesFromCeiling(addMetadataFromFieldTypeRequest.getType().getReturnedClass());
                foreignMetadata = addMetadataFromFieldTypeRequest.getDynamicEntityDao().getSessionFactory().getClassMetadata(entities[entities.length - 1]);
                foreignKeyClass = entities[entities.length - 1].getName();
                lookupDisplayProperty = ((BasicFieldMetadata) addMetadataFromFieldTypeRequest.getPresentationAttribute()).getLookupDisplayProperty();
                if (StringUtils.isEmpty(lookupDisplayProperty) && AdminMainEntity.class.isAssignableFrom(entities[entities.length - 1])) {
                    lookupDisplayProperty = AdminMainEntity.MAIN_ENTITY_NAME_PROPERTY;
                }
                if (StringUtils.isEmpty(lookupDisplayProperty)) {
                    lookupDisplayProperty = "name";
                }
            } else {
                try {
                    foreignMetadata = addMetadataFromFieldTypeRequest.getDynamicEntityDao().getSessionFactory().getClassMetadata(Class.forName(addMetadataFromFieldTypeRequest.getForeignField().getForeignKeyClass()));
                    foreignKeyClass = addMetadataFromFieldTypeRequest.getForeignField().getForeignKeyClass();
                    lookupDisplayProperty = addMetadataFromFieldTypeRequest.getForeignField().getDisplayValueProperty();
                    if (StringUtils.isEmpty(lookupDisplayProperty) && AdminMainEntity.class.isAssignableFrom(Class.forName(foreignKeyClass))) {
                        lookupDisplayProperty = AdminMainEntity.MAIN_ENTITY_NAME_PROPERTY;
                    }
                    if (StringUtils.isEmpty(lookupDisplayProperty)) {
                        lookupDisplayProperty = "name";
                    }
                } catch (ClassNotFoundException e) {
                    throw new RuntimeException(e);
                }
            }
            Class<?> foreignResponseType = foreignMetadata.getIdentifierType().getReturnedClass();
            if (foreignResponseType.equals(String.class)) {
                metadata.put(addMetadataFromFieldTypeRequest.getRequestedPropertyName(), addMetadataFromFieldTypeRequest.getDynamicEntityDao().getMetadata().getFieldMetadata(addMetadataFromFieldTypeRequest.getPrefix(), addMetadataFromFieldTypeRequest.getRequestedPropertyName(), addMetadataFromFieldTypeRequest.getComponentProperties(), SupportedFieldType.FOREIGN_KEY, SupportedFieldType.STRING, addMetadataFromFieldTypeRequest.getType(), addMetadataFromFieldTypeRequest.getTargetClass(), addMetadataFromFieldTypeRequest.getPresentationAttribute(), addMetadataFromFieldTypeRequest.getMergedPropertyType(), addMetadataFromFieldTypeRequest.getDynamicEntityDao()));
            } else {
                metadata.put(addMetadataFromFieldTypeRequest.getRequestedPropertyName(), addMetadataFromFieldTypeRequest.getDynamicEntityDao().getMetadata().getFieldMetadata(addMetadataFromFieldTypeRequest.getPrefix(), addMetadataFromFieldTypeRequest.getRequestedPropertyName(), addMetadataFromFieldTypeRequest.getComponentProperties(), SupportedFieldType.FOREIGN_KEY, SupportedFieldType.INTEGER, addMetadataFromFieldTypeRequest.getType(), addMetadataFromFieldTypeRequest.getTargetClass(), addMetadataFromFieldTypeRequest.getPresentationAttribute(), addMetadataFromFieldTypeRequest.getMergedPropertyType(), addMetadataFromFieldTypeRequest.getDynamicEntityDao()));
            }
            ((BasicFieldMetadata) metadata.get(addMetadataFromFieldTypeRequest.getRequestedPropertyName())).setForeignKeyProperty(foreignMetadata.getIdentifierPropertyName());
            ((BasicFieldMetadata) metadata.get(addMetadataFromFieldTypeRequest.getRequestedPropertyName())).setForeignKeyClass(foreignKeyClass);
            ((BasicFieldMetadata) metadata.get(addMetadataFromFieldTypeRequest.getRequestedPropertyName())).setForeignKeyDisplayValueProperty(lookupDisplayProperty);
        } else if (addMetadataFromFieldTypeRequest.getExplicitType() != null && addMetadataFromFieldTypeRequest.getExplicitType() == SupportedFieldType.ADDITIONAL_FOREIGN_KEY || addMetadataFromFieldTypeRequest.getAdditionalForeignFields() != null && addMetadataFromFieldTypeRequest.getAdditionalForeignKeyIndexPosition() >= 0) {
            if (!addMetadataFromFieldTypeRequest.getType().isEntityType()) {
                throw new IllegalArgumentException("Only ManyToOne and OneToOne fields can be marked as a " + "SupportedFieldType of ADDITIONAL_FOREIGN_KEY");
            }
            ClassMetadata foreignMetadata;
            String foreignKeyClass;
            String lookupDisplayProperty;
            if (addMetadataFromFieldTypeRequest.getAdditionalForeignKeyIndexPosition() < 0) {
                Class<?>[] entities = addMetadataFromFieldTypeRequest.getDynamicEntityDao().getAllPolymorphicEntitiesFromCeiling(addMetadataFromFieldTypeRequest.getType().getReturnedClass());
                foreignMetadata = addMetadataFromFieldTypeRequest.getDynamicEntityDao().getSessionFactory().getClassMetadata(entities[entities.length - 1]);
                foreignKeyClass = entities[entities.length - 1].getName();
                lookupDisplayProperty = ((BasicFieldMetadata) addMetadataFromFieldTypeRequest.getPresentationAttribute()).getLookupDisplayProperty();
                if (StringUtils.isEmpty(lookupDisplayProperty) && AdminMainEntity.class.isAssignableFrom(entities[entities.length - 1])) {
                    lookupDisplayProperty = AdminMainEntity.MAIN_ENTITY_NAME_PROPERTY;
                }
                if (StringUtils.isEmpty(lookupDisplayProperty)) {
                    lookupDisplayProperty = "name";
                }
            } else {
                try {
                    foreignMetadata = addMetadataFromFieldTypeRequest.getDynamicEntityDao().getSessionFactory().getClassMetadata(Class.forName(addMetadataFromFieldTypeRequest.getAdditionalForeignFields()[addMetadataFromFieldTypeRequest.getAdditionalForeignKeyIndexPosition()].getForeignKeyClass()));
                    foreignKeyClass = addMetadataFromFieldTypeRequest.getAdditionalForeignFields()[addMetadataFromFieldTypeRequest.getAdditionalForeignKeyIndexPosition()].getForeignKeyClass();
                    lookupDisplayProperty = addMetadataFromFieldTypeRequest.getAdditionalForeignFields()[addMetadataFromFieldTypeRequest.getAdditionalForeignKeyIndexPosition()].getDisplayValueProperty();
                    if (StringUtils.isEmpty(lookupDisplayProperty) && AdminMainEntity.class.isAssignableFrom(Class.forName(foreignKeyClass))) {
                        lookupDisplayProperty = AdminMainEntity.MAIN_ENTITY_NAME_PROPERTY;
                    }
                    if (StringUtils.isEmpty(lookupDisplayProperty)) {
                        lookupDisplayProperty = "name";
                    }
                } catch (ClassNotFoundException e) {
                    throw new RuntimeException(e);
                }
            }
            Class<?> foreignResponseType = foreignMetadata.getIdentifierType().getReturnedClass();
            if (foreignResponseType.equals(String.class)) {
                metadata.put(addMetadataFromFieldTypeRequest.getRequestedPropertyName(), addMetadataFromFieldTypeRequest.getDynamicEntityDao().getMetadata().getFieldMetadata(addMetadataFromFieldTypeRequest.getPrefix(), addMetadataFromFieldTypeRequest.getRequestedPropertyName(), addMetadataFromFieldTypeRequest.getComponentProperties(), SupportedFieldType.ADDITIONAL_FOREIGN_KEY, SupportedFieldType.STRING, addMetadataFromFieldTypeRequest.getType(), addMetadataFromFieldTypeRequest.getTargetClass(), addMetadataFromFieldTypeRequest.getPresentationAttribute(), addMetadataFromFieldTypeRequest.getMergedPropertyType(), addMetadataFromFieldTypeRequest.getDynamicEntityDao()));
            } else {
                metadata.put(addMetadataFromFieldTypeRequest.getRequestedPropertyName(), addMetadataFromFieldTypeRequest.getDynamicEntityDao().getMetadata().getFieldMetadata(addMetadataFromFieldTypeRequest.getPrefix(), addMetadataFromFieldTypeRequest.getRequestedPropertyName(), addMetadataFromFieldTypeRequest.getComponentProperties(), SupportedFieldType.ADDITIONAL_FOREIGN_KEY, SupportedFieldType.INTEGER, addMetadataFromFieldTypeRequest.getType(), addMetadataFromFieldTypeRequest.getTargetClass(), addMetadataFromFieldTypeRequest.getPresentationAttribute(), addMetadataFromFieldTypeRequest.getMergedPropertyType(), addMetadataFromFieldTypeRequest.getDynamicEntityDao()));
            }
            ((BasicFieldMetadata) metadata.get(addMetadataFromFieldTypeRequest.getRequestedPropertyName())).setForeignKeyProperty(foreignMetadata.getIdentifierPropertyName());
            ((BasicFieldMetadata) metadata.get(addMetadataFromFieldTypeRequest.getRequestedPropertyName())).setForeignKeyClass(foreignKeyClass);
            ((BasicFieldMetadata) metadata.get(addMetadataFromFieldTypeRequest.getRequestedPropertyName())).setForeignKeyDisplayValueProperty(lookupDisplayProperty);
        }
        // return type not supported - just skip this property
        return MetadataProviderResponse.HANDLED;
    }
    return MetadataProviderResponse.NOT_HANDLED;
}
Also used : ClassMetadata(org.hibernate.metadata.ClassMetadata) Date(java.util.Date) BigDecimal(java.math.BigDecimal) BasicFieldMetadata(org.broadleafcommerce.openadmin.dto.BasicFieldMetadata) AdminMainEntity(org.broadleafcommerce.common.admin.domain.AdminMainEntity) FieldMetadataOverride(org.broadleafcommerce.openadmin.dto.override.FieldMetadataOverride) MetadataOverride(org.broadleafcommerce.openadmin.dto.override.MetadataOverride)

Example 55 with BasicFieldMetadata

use of org.broadleafcommerce.openadmin.dto.BasicFieldMetadata in project BroadleafCommerce by BroadleafCommerce.

the class MapFieldsFieldMetadataProvider method addMetadata.

@Override
public MetadataProviderResponse addMetadata(AddFieldMetadataRequest addMetadataRequest, Map<String, FieldMetadata> metadata) {
    if (!canHandleFieldForConfiguredMetadata(addMetadataRequest, metadata)) {
        return MetadataProviderResponse.NOT_HANDLED;
    }
    AdminPresentationMapFields annot = addMetadataRequest.getRequestedField().getAnnotation(AdminPresentationMapFields.class);
    for (AdminPresentationMapField mapField : annot.mapDisplayFields()) {
        if (mapField.fieldPresentation().fieldType() == SupportedFieldType.UNKNOWN) {
            throw new IllegalArgumentException("fieldType property on AdminPresentation must be set for AdminPresentationMapField");
        }
        FieldMetadataOverride override = constructBasicMetadataOverride(mapField.fieldPresentation(), null, null);
        override.setFriendlyName(mapField.fieldPresentation().friendlyName());
        FieldInfo myInfo = new FieldInfo();
        myInfo.setName(addMetadataRequest.getRequestedField().getName() + FieldManager.MAPFIELDSEPARATOR + mapField.fieldName());
        buildBasicMetadata(addMetadataRequest.getParentClass(), addMetadataRequest.getTargetClass(), metadata, myInfo, override, addMetadataRequest.getDynamicEntityDao());
        setClassOwnership(addMetadataRequest.getParentClass(), addMetadataRequest.getTargetClass(), metadata, myInfo);
        BasicFieldMetadata basicFieldMetadata = (BasicFieldMetadata) metadata.get(myInfo.getName());
        if (!mapField.targetClass().equals(Void.class)) {
            if (mapField.targetClass().isInterface()) {
                throw new IllegalArgumentException("targetClass on @AdminPresentationMapField must be a concrete class");
            }
            basicFieldMetadata.setMapFieldValueClass(mapField.targetClass().getName());
        }
        if (!StringUtils.isEmpty(mapField.manyToField())) {
            basicFieldMetadata.setManyToField(mapField.manyToField());
        }
        AdminPresentationMap annotMap = addMetadataRequest.getRequestedField().getAnnotation(AdminPresentationMap.class);
        if (annotMap != null && !StringUtils.isEmpty(annotMap.toOneTargetProperty())) {
            basicFieldMetadata.setToOneTargetProperty(annotMap.toOneTargetProperty());
        } else if (!StringUtils.isEmpty(annot.toOneTargetProperty())) {
            basicFieldMetadata.setToOneTargetProperty(annot.toOneTargetProperty());
        }
        if (annotMap != null && !StringUtils.isEmpty(annotMap.toOneParentProperty())) {
            basicFieldMetadata.setToOneParentProperty(annotMap.toOneParentProperty());
        } else if (!StringUtils.isEmpty(annot.toOneParentProperty())) {
            basicFieldMetadata.setToOneParentProperty(annot.toOneParentProperty());
        }
        String mapKeyValueProperty = "key";
        if (StringUtils.isNotBlank(myInfo.getMapKey())) {
            mapKeyValueProperty = myInfo.getMapKey();
        }
        if (annotMap != null) {
            if (StringUtils.isNotBlank(annotMap.mapKeyValueProperty())) {
                mapKeyValueProperty = annotMap.mapKeyValueProperty();
            }
        }
        basicFieldMetadata.setMapKeyValueProperty(mapKeyValueProperty);
    }
    return MetadataProviderResponse.HANDLED;
}
Also used : AdminPresentationMapField(org.broadleafcommerce.common.presentation.AdminPresentationMapField) AdminPresentationMapFields(org.broadleafcommerce.common.presentation.AdminPresentationMapFields) BasicFieldMetadata(org.broadleafcommerce.openadmin.dto.BasicFieldMetadata) FieldMetadataOverride(org.broadleafcommerce.openadmin.dto.override.FieldMetadataOverride) FieldInfo(org.broadleafcommerce.openadmin.server.dao.FieldInfo) AdminPresentationMap(org.broadleafcommerce.common.presentation.AdminPresentationMap) FieldMetadataOverride(org.broadleafcommerce.openadmin.dto.override.FieldMetadataOverride)

Aggregations

BasicFieldMetadata (org.broadleafcommerce.openadmin.dto.BasicFieldMetadata)66 FieldMetadata (org.broadleafcommerce.openadmin.dto.FieldMetadata)33 Property (org.broadleafcommerce.openadmin.dto.Property)28 ArrayList (java.util.ArrayList)19 HashMap (java.util.HashMap)19 Map (java.util.Map)14 Entity (org.broadleafcommerce.openadmin.dto.Entity)11 ClassMetadata (org.broadleafcommerce.openadmin.dto.ClassMetadata)10 AdminMainEntity (org.broadleafcommerce.common.admin.domain.AdminMainEntity)9 FieldMetadataOverride (org.broadleafcommerce.openadmin.dto.override.FieldMetadataOverride)9 Field (java.lang.reflect.Field)8 PersistenceException (org.broadleafcommerce.openadmin.server.service.persistence.PersistenceException)8 ServiceException (org.broadleafcommerce.common.exception.ServiceException)7 PersistencePackageRequest (org.broadleafcommerce.openadmin.server.domain.PersistencePackageRequest)7 ComboField (org.broadleafcommerce.openadmin.web.form.entity.ComboField)7 Field (org.broadleafcommerce.openadmin.web.form.entity.Field)7 Serializable (java.io.Serializable)6 SecurityServiceException (org.broadleafcommerce.common.exception.SecurityServiceException)6 MetadataOverride (org.broadleafcommerce.openadmin.dto.override.MetadataOverride)6 FieldInfo (org.broadleafcommerce.openadmin.server.dao.FieldInfo)6