Search in sources :

Example 56 with BasicFieldMetadata

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

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

the class AdminEntityServiceImpl method getContextSpecificRelationshipId.

@Override
public String getContextSpecificRelationshipId(ClassMetadata cmd, Entity entity, String propertyName) {
    String prefix;
    if (propertyName.contains(".")) {
        prefix = propertyName.substring(0, propertyName.lastIndexOf("."));
    } else {
        prefix = "";
    }
    if (prefix.equals("")) {
        return entity.findProperty("id").getValue();
    } else {
        // we need to check all the parts of the prefix. For example, the prefix could include an @Embedded class like
        // defaultSku.dimension. In this case, we want the id from the defaultSku property, since the @Embedded does
        // not have an id property - nor should it.
        String[] prefixParts = prefix.split("\\.");
        for (int j = 0; j < prefixParts.length; j++) {
            StringBuilder sb = new StringBuilder();
            for (int x = 0; x < prefixParts.length - j; x++) {
                sb.append(prefixParts[x]);
                if (x < prefixParts.length - j - 1) {
                    sb.append(".");
                }
            }
            String tempPrefix = sb.toString();
            for (Property property : entity.getProperties()) {
                if (property.getName().startsWith(tempPrefix)) {
                    // in the case of the prefix defaultSku, we want defaultSku.id not defaultSku.skuAttributes.id
                    if (StringUtils.countMatches(property.getName().replace(tempPrefix, ""), ".") == 1) {
                        if (cmd.getPMap().containsKey(property.getName())) {
                            BasicFieldMetadata md = (BasicFieldMetadata) cmd.getPMap().get(property.getName()).getMetadata();
                            if (md.getFieldType().equals(SupportedFieldType.ID)) {
                                return property.getValue();
                            }
                        }
                    }
                }
            }
        }
    }
    if (!prefix.contains(".")) {
        // this may be an embedded class directly on the root entity (e.g. embeddablePriceList.restrictedPriceLists on OfferImpl)
        return entity.findProperty("id").getValue();
    }
    throw new RuntimeException("Unable to establish a relationship id");
}
Also used : BasicFieldMetadata(org.broadleafcommerce.openadmin.dto.BasicFieldMetadata) BLCSystemProperty(org.broadleafcommerce.common.util.BLCSystemProperty) Property(org.broadleafcommerce.openadmin.dto.Property)

Example 58 with BasicFieldMetadata

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

the class PasswordFieldMetadataProvider method addMetadataFromFieldType.

@Override
public MetadataProviderResponse addMetadataFromFieldType(AddMetadataFromFieldTypeRequest addMetadataFromFieldTypeRequest, Map<String, FieldMetadata> metadata) {
    if (addMetadataFromFieldTypeRequest.getPresentationAttribute() instanceof BasicFieldMetadata && SupportedFieldType.PASSWORD.equals(((BasicFieldMetadata) addMetadataFromFieldTypeRequest.getPresentationAttribute()).getExplicitFieldType())) {
        // build the metadata for the password field
        addMetadataFromFieldTypeRequest.getDynamicEntityDao().getDefaultFieldMetadataProvider().addMetadataFromFieldType(addMetadataFromFieldTypeRequest, metadata);
        ((BasicFieldMetadata) addMetadataFromFieldTypeRequest.getPresentationAttribute()).setFieldType(SupportedFieldType.PASSWORD);
        // clone the password field and add in a custom one
        BasicFieldMetadata confirmMd = (BasicFieldMetadata) addMetadataFromFieldTypeRequest.getPresentationAttribute().cloneFieldMetadata();
        confirmMd.setFieldName("passwordConfirm");
        confirmMd.setFriendlyName("AdminUserImpl_Admin_Password_Confirm");
        confirmMd.setExplicitFieldType(SupportedFieldType.PASSWORD_CONFIRM);
        confirmMd.setValidationConfigurations(new HashMap<String, List<Map<String, String>>>());
        metadata.put("passwordConfirm", confirmMd);
        return MetadataProviderResponse.HANDLED;
    } else {
        return MetadataProviderResponse.NOT_HANDLED;
    }
}
Also used : BasicFieldMetadata(org.broadleafcommerce.openadmin.dto.BasicFieldMetadata) List(java.util.List)

Example 59 with BasicFieldMetadata

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

the class DynamicEntityDaoImpl method getSimpleMergedProperties.

@Override
public Map<String, FieldMetadata> getSimpleMergedProperties(String entityName, PersistencePerspective persistencePerspective) {
    Class<?>[] entityClasses;
    try {
        entityClasses = getAllPolymorphicEntitiesFromCeiling(Class.forName(entityName));
    } catch (ClassNotFoundException e) {
        throw new RuntimeException(e);
    }
    if (!ArrayUtils.isEmpty(entityClasses)) {
        return getMergedProperties(entityName, entityClasses, (ForeignKey) persistencePerspective.getPersistencePerspectiveItems().get(PersistencePerspectiveItemType.FOREIGNKEY), persistencePerspective.getAdditionalNonPersistentProperties(), persistencePerspective.getAdditionalForeignKeys(), MergedPropertyType.PRIMARY, persistencePerspective.getPopulateToOneFields(), persistencePerspective.getIncludeFields(), persistencePerspective.getExcludeFields(), persistencePerspective.getConfigurationKey(), "");
    } else {
        Map<String, FieldMetadata> mergedProperties = new HashMap<>();
        Class<?> targetClass;
        try {
            targetClass = Class.forName(entityName);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
        Map<String, FieldMetadata> attributesMap = metadata.getFieldMetadataForTargetClass(null, targetClass, this, "");
        for (String property : attributesMap.keySet()) {
            FieldMetadata presentationAttribute = attributesMap.get(property);
            if (!presentationAttribute.getExcluded()) {
                Field field = FieldManager.getSingleField(targetClass, property);
                if (!Modifier.isStatic(field.getModifiers())) {
                    boolean handled = false;
                    for (FieldMetadataProvider provider : fieldMetadataProviders) {
                        MetadataProviderResponse response = provider.addMetadataFromFieldType(new AddMetadataFromFieldTypeRequest(field, targetClass, null, new ForeignKey[] {}, MergedPropertyType.PRIMARY, null, null, "", property, null, false, 0, attributesMap, presentationAttribute, ((BasicFieldMetadata) presentationAttribute).getExplicitFieldType(), field.getType(), this), mergedProperties);
                        if (MetadataProviderResponse.NOT_HANDLED != response) {
                            handled = true;
                        }
                        if (MetadataProviderResponse.HANDLED_BREAK == response) {
                            break;
                        }
                    }
                    if (!handled) {
                        // this provider is not included in the provider list on purpose - it is designed to handle basic
                        // AdminPresentation fields, and those fields not admin presentation annotated at all
                        defaultFieldMetadataProvider.addMetadataFromFieldType(new AddMetadataFromFieldTypeRequest(field, targetClass, null, new ForeignKey[] {}, MergedPropertyType.PRIMARY, null, null, "", property, null, false, 0, attributesMap, presentationAttribute, ((BasicFieldMetadata) presentationAttribute).getExplicitFieldType(), field.getType(), this), mergedProperties);
                    }
                }
            }
        }
        return mergedProperties;
    }
}
Also used : FieldMetadata(org.broadleafcommerce.openadmin.dto.FieldMetadata) BasicFieldMetadata(org.broadleafcommerce.openadmin.dto.BasicFieldMetadata) HashMap(java.util.HashMap) MetadataProviderResponse(org.broadleafcommerce.openadmin.server.service.type.MetadataProviderResponse) ForeignKey(org.broadleafcommerce.openadmin.dto.ForeignKey) Field(java.lang.reflect.Field) FieldMetadataProvider(org.broadleafcommerce.openadmin.server.dao.provider.metadata.FieldMetadataProvider) BasicFieldMetadata(org.broadleafcommerce.openadmin.dto.BasicFieldMetadata) PersistentClass(org.hibernate.mapping.PersistentClass) AdminPresentationClass(org.broadleafcommerce.common.presentation.AdminPresentationClass) AddMetadataFromFieldTypeRequest(org.broadleafcommerce.openadmin.server.dao.provider.metadata.request.AddMetadataFromFieldTypeRequest)

Example 60 with BasicFieldMetadata

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

the class DynamicEntityDaoImpl method buildEntityProperties.

protected void buildEntityProperties(Map<String, FieldMetadata> fields, ForeignKey foreignField, ForeignKey[] additionalForeignFields, String[] additionalNonPersistentProperties, Boolean populateManyToOneFields, String[] includeFields, String[] excludeFields, String configurationKey, String ceilingEntityFullyQualifiedClassname, String propertyName, Class<?> returnedClass, Class<?> targetClass, List<Class<?>> parentClasses, String prefix, Boolean isParentExcluded, String parentPrefix) {
    Class<?>[] polymorphicEntities = getAllPolymorphicEntitiesFromCeiling(returnedClass);
    List<Class<?>> clonedParentClasses = new ArrayList<>();
    for (Class<?> parentClass : parentClasses) {
        clonedParentClasses.add(parentClass);
    }
    clonedParentClasses.add(targetClass);
    Map<String, FieldMetadata> newFields = getMergedPropertiesRecursively(ceilingEntityFullyQualifiedClassname, polymorphicEntities, foreignField, additionalNonPersistentProperties, additionalForeignFields, MergedPropertyType.PRIMARY, populateManyToOneFields, includeFields, excludeFields, configurationKey, clonedParentClasses, prefix + propertyName + '.', isParentExcluded, parentPrefix);
    final String targetClassName = targetClass.getName();
    for (FieldMetadata newMetadata : newFields.values()) {
        newMetadata.setInheritedFromType(targetClassName);
        newMetadata.setAvailableToTypes(new String[] { targetClassName });
    }
    Map<String, FieldMetadata> convertedFields = new HashMap<>(newFields.size());
    for (Map.Entry<String, FieldMetadata> newField : newFields.entrySet()) {
        final FieldMetadata fieldMetadata = newField.getValue();
        final String key = newField.getKey();
        convertedFields.put(propertyName + '.' + key, fieldMetadata);
        if (fieldMetadata instanceof BasicFieldMetadata) {
            for (Map.Entry<String, List<Map<String, String>>> validationConfigurations : ((BasicFieldMetadata) fieldMetadata).getValidationConfigurations().entrySet()) {
                Class<?> validatorImpl = null;
                try {
                    validatorImpl = Class.forName(validationConfigurations.getKey());
                } catch (ClassNotFoundException e) {
                    Object bean = applicationContext.getBean(validationConfigurations.getKey());
                    if (bean != null) {
                        validatorImpl = bean.getClass();
                    }
                }
                if (validatorImpl != null && FieldNamePropertyValidator.class.isAssignableFrom(validatorImpl)) {
                    for (Map<String, String> configs : validationConfigurations.getValue()) {
                        for (Map.Entry<String, String> config : configs.entrySet()) {
                            final String value = config.getValue();
                            if (newFields.containsKey(value)) {
                                config.setValue(propertyName + "." + value);
                            }
                        }
                    }
                }
            }
        }
        if (isForeignKey(fieldMetadata)) {
            setOriginatingFieldForForeignKey(propertyName, key, fieldMetadata);
        }
    }
    fields.putAll(convertedFields);
}
Also used : FieldMetadata(org.broadleafcommerce.openadmin.dto.FieldMetadata) BasicFieldMetadata(org.broadleafcommerce.openadmin.dto.BasicFieldMetadata) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) FieldNamePropertyValidator(org.broadleafcommerce.openadmin.server.service.persistence.validation.FieldNamePropertyValidator) BasicFieldMetadata(org.broadleafcommerce.openadmin.dto.BasicFieldMetadata) PersistentClass(org.hibernate.mapping.PersistentClass) AdminPresentationClass(org.broadleafcommerce.common.presentation.AdminPresentationClass) List(java.util.List) ArrayList(java.util.ArrayList) LRUMap(org.apache.commons.collections4.map.LRUMap) Map(java.util.Map) HashMap(java.util.HashMap)

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