Search in sources :

Example 91 with FieldMetadata

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

the class AdornedTargetCollectionFieldMetadataProvider method buildAdminPresentationAdornedTargetCollectionOverride.

protected void buildAdminPresentationAdornedTargetCollectionOverride(String prefix, Boolean isParentExcluded, Map<String, FieldMetadata> mergedProperties, Map<String, AdminPresentationAdornedTargetCollectionOverride> presentationAdornedTargetCollectionOverrides, String propertyName, String key, DynamicEntityDao dynamicEntityDao) {
    AdminPresentationAdornedTargetCollectionOverride override = presentationAdornedTargetCollectionOverrides.get(propertyName);
    if (override != null) {
        AdminPresentationAdornedTargetCollection 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("buildAdminPresentationAdornedTargetCollectionOverride: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("buildAdminPresentationAdornedTargetCollectionOverride:Showing " + key + "because an override annotation declared " + testKey + " to not be excluded");
                    }
                    metadata.setExcluded(false);
                }
            }
            if (!(mergedProperties.get(key) instanceof AdornedTargetCollectionMetadata)) {
                return;
            }
            AdornedTargetCollectionMetadata serverMetadata = (AdornedTargetCollectionMetadata) 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 = constructAdornedTargetCollectionMetadataOverride(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);
                    buildAdornedTargetCollectionMetadata(parentClass, targetClass, temp, info, localMetadata, dynamicEntityDao);
                    AdornedTargetCollectionMetadata result = (AdornedTargetCollectionMetadata) temp.get(field.getName());
                    result.setInheritedFromType(serverMetadata.getInheritedFromType());
                    result.setAvailableToTypes(serverMetadata.getAvailableToTypes());
                    mergedProperties.put(key, result);
                    if (isParentExcluded) {
                        if (LOG.isDebugEnabled()) {
                            LOG.debug("buildAdminPresentationAdornedTargetCollectionOverride:Excluding " + key + "because the parent was excluded");
                        }
                        serverMetadata.setExcluded(true);
                    }
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
}
Also used : AdminPresentationAdornedTargetCollection(org.broadleafcommerce.common.presentation.AdminPresentationAdornedTargetCollection) FieldMetadata(org.broadleafcommerce.openadmin.dto.FieldMetadata) HashMap(java.util.HashMap) FieldMetadataOverride(org.broadleafcommerce.openadmin.dto.override.FieldMetadataOverride) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException) AdminPresentationAdornedTargetCollectionOverride(org.broadleafcommerce.common.presentation.override.AdminPresentationAdornedTargetCollectionOverride) Field(java.lang.reflect.Field) AdornedTargetCollectionMetadata(org.broadleafcommerce.openadmin.dto.AdornedTargetCollectionMetadata) FieldInfo(org.broadleafcommerce.openadmin.server.dao.FieldInfo)

Example 92 with FieldMetadata

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

the class AdornedTargetCollectionFieldMetadataProvider method overrideViaAnnotation.

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

Example 93 with FieldMetadata

use of org.broadleafcommerce.openadmin.dto.FieldMetadata 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 94 with FieldMetadata

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

the class AdminPermissionCustomPersistenceHandler method update.

@Override
public Entity update(PersistencePackage persistencePackage, DynamicEntityDao dynamicEntityDao, RecordHelper helper) throws ServiceException {
    Entity entity = checkPermissionName(persistencePackage);
    try {
        PersistencePerspective persistencePerspective = persistencePackage.getPersistencePerspective();
        Map<String, FieldMetadata> adminProperties = helper.getSimpleMergedProperties(AdminPermission.class.getName(), persistencePerspective);
        Object primaryKey = helper.getPrimaryKey(entity, adminProperties);
        AdminPermission adminInstance = (AdminPermission) dynamicEntityDao.retrieve(Class.forName(entity.getType()[0]), primaryKey);
        adminInstance = (AdminPermission) helper.createPopulatedInstance(adminInstance, entity, adminProperties, false);
        adminInstance = dynamicEntityDao.merge(adminInstance);
        Entity adminEntity = helper.getRecord(adminProperties, adminInstance, null, null);
        return adminEntity;
    } catch (Exception e) {
        throw new ServiceException("Unable to update entity for " + entity.getType()[0], e);
    }
}
Also used : Entity(org.broadleafcommerce.openadmin.dto.Entity) FieldMetadata(org.broadleafcommerce.openadmin.dto.FieldMetadata) PersistencePerspective(org.broadleafcommerce.openadmin.dto.PersistencePerspective) ServiceException(org.broadleafcommerce.common.exception.ServiceException) AdminPermission(org.broadleafcommerce.openadmin.server.security.domain.AdminPermission) CriteriaTransferObject(org.broadleafcommerce.openadmin.dto.CriteriaTransferObject) ServiceException(org.broadleafcommerce.common.exception.ServiceException)

Example 95 with FieldMetadata

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

the class AdminPermissionCustomPersistenceHandler method add.

@Override
public Entity add(PersistencePackage persistencePackage, DynamicEntityDao dynamicEntityDao, RecordHelper helper) throws ServiceException {
    if (persistencePackage.getEntity().findProperty("id") != null && !StringUtils.isEmpty(persistencePackage.getEntity().findProperty("id").getValue())) {
        return update(persistencePackage, dynamicEntityDao, helper);
    }
    Entity entity = checkPermissionName(persistencePackage);
    try {
        PersistencePerspective persistencePerspective = persistencePackage.getPersistencePerspective();
        AdminPermission adminInstance = (AdminPermission) Class.forName(entity.getType()[0]).newInstance();
        Map<String, FieldMetadata> adminProperties = helper.getSimpleMergedProperties(AdminPermission.class.getName(), persistencePerspective);
        adminInstance = (AdminPermission) helper.createPopulatedInstance(adminInstance, entity, adminProperties, false);
        adminInstance = dynamicEntityDao.merge(adminInstance);
        Entity adminEntity = helper.getRecord(adminProperties, adminInstance, null, null);
        return adminEntity;
    } catch (Exception e) {
        throw new ServiceException("Unable to add entity for " + entity.getType()[0], e);
    }
}
Also used : Entity(org.broadleafcommerce.openadmin.dto.Entity) FieldMetadata(org.broadleafcommerce.openadmin.dto.FieldMetadata) PersistencePerspective(org.broadleafcommerce.openadmin.dto.PersistencePerspective) ServiceException(org.broadleafcommerce.common.exception.ServiceException) AdminPermission(org.broadleafcommerce.openadmin.server.security.domain.AdminPermission) ServiceException(org.broadleafcommerce.common.exception.ServiceException)

Aggregations

FieldMetadata (org.broadleafcommerce.openadmin.dto.FieldMetadata)114 BasicFieldMetadata (org.broadleafcommerce.openadmin.dto.BasicFieldMetadata)80 Entity (org.broadleafcommerce.openadmin.dto.Entity)51 ServiceException (org.broadleafcommerce.common.exception.ServiceException)46 PersistencePerspective (org.broadleafcommerce.openadmin.dto.PersistencePerspective)44 HashMap (java.util.HashMap)39 Property (org.broadleafcommerce.openadmin.dto.Property)38 Map (java.util.Map)28 ArrayList (java.util.ArrayList)23 ClassMetadata (org.broadleafcommerce.openadmin.dto.ClassMetadata)22 Field (java.lang.reflect.Field)19 SecurityServiceException (org.broadleafcommerce.common.exception.SecurityServiceException)19 PersistencePackageRequest (org.broadleafcommerce.openadmin.server.domain.PersistencePackageRequest)19 Serializable (java.io.Serializable)18 InvocationTargetException (java.lang.reflect.InvocationTargetException)18 DynamicResultSet (org.broadleafcommerce.openadmin.dto.DynamicResultSet)18 CriteriaTransferObject (org.broadleafcommerce.openadmin.dto.CriteriaTransferObject)17 BasicCollectionMetadata (org.broadleafcommerce.openadmin.dto.BasicCollectionMetadata)15 FieldMetadataOverride (org.broadleafcommerce.openadmin.dto.override.FieldMetadataOverride)15 AdornedTargetCollectionMetadata (org.broadleafcommerce.openadmin.dto.AdornedTargetCollectionMetadata)13