Search in sources :

Example 1 with ExtractValueRequest

use of org.broadleafcommerce.openadmin.server.service.persistence.module.provider.request.ExtractValueRequest in project BroadleafCommerce by BroadleafCommerce.

the class BasicPersistenceModule method extractPropertiesFromPersistentEntity.

protected void extractPropertiesFromPersistentEntity(Map<String, FieldMetadata> mergedProperties, Serializable entity, List<Property> props, String[] customCriteria) {
    FieldManager fieldManager = getFieldManager();
    try {
        if (entity instanceof AdminMainEntity) {
            // its display name.
            try {
                Property propertyItem = new Property();
                propertyItem.setName(AdminMainEntity.MAIN_ENTITY_NAME_PROPERTY);
                propertyItem.setValue(((AdminMainEntity) entity).getMainEntityName());
                props.add(propertyItem);
            } catch (Exception e) {
            // do nothing here except for not add the property. Exceptions could occur when there is a validation
            // issue and some properties/relationships that are used for gleaning the main entity name end up
            // not being set
            }
        }
        for (Entry<String, FieldMetadata> entry : mergedProperties.entrySet()) {
            String property = entry.getKey();
            BasicFieldMetadata metadata = (BasicFieldMetadata) entry.getValue();
            if (Class.forName(metadata.getInheritedFromType()).isAssignableFrom(entity.getClass()) || entity.getClass().isAssignableFrom(Class.forName(metadata.getInheritedFromType()))) {
                boolean proceed = true;
                if (property.contains(".")) {
                    StringTokenizer tokens = new StringTokenizer(property, ".");
                    Object testObject = entity;
                    while (tokens.hasMoreTokens()) {
                        String token = tokens.nextToken();
                        if (tokens.hasMoreTokens()) {
                            try {
                                testObject = fieldManager.getFieldValue(testObject, token);
                            } catch (FieldNotAvailableException e) {
                                proceed = false;
                                break;
                            }
                            if (testObject == null) {
                                Property propertyItem = new Property();
                                propertyItem.setName(property);
                                if (props.contains(propertyItem)) {
                                    proceed = false;
                                    break;
                                }
                                propertyItem.setValue(null);
                                props.add(propertyItem);
                                proceed = false;
                                break;
                            }
                        }
                    }
                }
                if (!proceed) {
                    continue;
                }
                boolean isFieldAccessible = true;
                Object value = null;
                try {
                    value = fieldManager.getFieldValue(entity, property);
                } catch (FieldNotAvailableException e) {
                    isFieldAccessible = false;
                }
                checkField: {
                    if (isFieldAccessible) {
                        Property propertyItem = new Property();
                        propertyItem.setName(property);
                        if (props.contains(propertyItem)) {
                            continue;
                        }
                        props.add(propertyItem);
                        String displayVal = propertyItem.getDisplayValue();
                        boolean handled = false;
                        for (FieldPersistenceProvider fieldPersistenceProvider : fieldPersistenceProviders) {
                            MetadataProviderResponse response = fieldPersistenceProvider.extractValue(new ExtractValueRequest(props, fieldManager, metadata, value, displayVal, persistenceManager, this, entity, customCriteria), propertyItem);
                            if (MetadataProviderResponse.NOT_HANDLED != response) {
                                handled = true;
                            }
                            if (MetadataProviderResponse.HANDLED_BREAK == response) {
                                break;
                            }
                        }
                        if (!handled) {
                            defaultFieldPersistenceProvider.extractValue(new ExtractValueRequest(props, fieldManager, metadata, value, displayVal, persistenceManager, this, entity, customCriteria), propertyItem);
                        }
                        break checkField;
                    }
                    // try a direct property acquisition via reflection
                    try {
                        String strVal = null;
                        Method method;
                        try {
                            // try a 'get' prefixed mutator first
                            String temp = "get" + property.substring(0, 1).toUpperCase() + property.substring(1, property.length());
                            method = entity.getClass().getMethod(temp, new Class[] {});
                        } catch (NoSuchMethodException e) {
                            method = entity.getClass().getMethod(property, new Class[] {});
                        }
                        value = method.invoke(entity, new String[] {});
                        Property propertyItem = new Property();
                        propertyItem.setName(property);
                        if (props.contains(propertyItem)) {
                            continue;
                        }
                        props.add(propertyItem);
                        if (value == null) {
                            strVal = null;
                        } else {
                            if (Date.class.isAssignableFrom(value.getClass())) {
                                strVal = getSimpleDateFormatter().format((Date) value);
                            } else if (Timestamp.class.isAssignableFrom(value.getClass())) {
                                strVal = getSimpleDateFormatter().format(new Date(((Timestamp) value).getTime()));
                            } else if (Calendar.class.isAssignableFrom(value.getClass())) {
                                strVal = getSimpleDateFormatter().format(((Calendar) value).getTime());
                            } else if (Double.class.isAssignableFrom(value.getClass())) {
                                strVal = getDecimalFormatter().format(value);
                            } else if (BigDecimal.class.isAssignableFrom(value.getClass())) {
                                strVal = getDecimalFormatter().format(value);
                            } else {
                                strVal = value.toString();
                            }
                        }
                        propertyItem.setValue(strVal);
                    } catch (NoSuchMethodException e) {
                        LOG.debug("Unable to find a specified property in the entity: " + StringUtil.sanitize(property));
                    // do nothing - this property is simply not in the bean
                    }
                }
            }
        }
    } catch (ClassNotFoundException e) {
        throw new PersistenceException(e);
    } catch (IllegalAccessException e) {
        throw new PersistenceException(e);
    } catch (InvocationTargetException e) {
        throw new PersistenceException(e);
    }
}
Also used : FieldMetadata(org.broadleafcommerce.openadmin.dto.FieldMetadata) BasicFieldMetadata(org.broadleafcommerce.openadmin.dto.BasicFieldMetadata) FieldPersistenceProvider(org.broadleafcommerce.openadmin.server.service.persistence.module.provider.FieldPersistenceProvider) Timestamp(java.sql.Timestamp) Property(org.broadleafcommerce.openadmin.dto.Property) AdminMainEntity(org.broadleafcommerce.common.admin.domain.AdminMainEntity) MetadataProviderResponse(org.broadleafcommerce.openadmin.server.service.type.MetadataProviderResponse) ExtractValueRequest(org.broadleafcommerce.openadmin.server.service.persistence.module.provider.request.ExtractValueRequest) Calendar(java.util.Calendar) Method(java.lang.reflect.Method) ServiceException(org.broadleafcommerce.common.exception.ServiceException) InvocationTargetException(java.lang.reflect.InvocationTargetException) PersistenceException(org.broadleafcommerce.openadmin.server.service.persistence.PersistenceException) CriteriaConversionException(org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.CriteriaConversionException) BeansException(org.springframework.beans.BeansException) ValidationException(org.broadleafcommerce.openadmin.server.service.ValidationException) ParentEntityPersistenceException(org.broadleafcommerce.openadmin.server.service.persistence.ParentEntityPersistenceException) SecurityServiceException(org.broadleafcommerce.common.exception.SecurityServiceException) Date(java.util.Date) InvocationTargetException(java.lang.reflect.InvocationTargetException) StringTokenizer(java.util.StringTokenizer) BasicFieldMetadata(org.broadleafcommerce.openadmin.dto.BasicFieldMetadata) PersistenceException(org.broadleafcommerce.openadmin.server.service.persistence.PersistenceException) ParentEntityPersistenceException(org.broadleafcommerce.openadmin.server.service.persistence.ParentEntityPersistenceException) CriteriaTransferObject(org.broadleafcommerce.openadmin.dto.CriteriaTransferObject)

Aggregations

InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 Timestamp (java.sql.Timestamp)1 Calendar (java.util.Calendar)1 Date (java.util.Date)1 StringTokenizer (java.util.StringTokenizer)1 AdminMainEntity (org.broadleafcommerce.common.admin.domain.AdminMainEntity)1 SecurityServiceException (org.broadleafcommerce.common.exception.SecurityServiceException)1 ServiceException (org.broadleafcommerce.common.exception.ServiceException)1 BasicFieldMetadata (org.broadleafcommerce.openadmin.dto.BasicFieldMetadata)1 CriteriaTransferObject (org.broadleafcommerce.openadmin.dto.CriteriaTransferObject)1 FieldMetadata (org.broadleafcommerce.openadmin.dto.FieldMetadata)1 Property (org.broadleafcommerce.openadmin.dto.Property)1 ValidationException (org.broadleafcommerce.openadmin.server.service.ValidationException)1 ParentEntityPersistenceException (org.broadleafcommerce.openadmin.server.service.persistence.ParentEntityPersistenceException)1 PersistenceException (org.broadleafcommerce.openadmin.server.service.persistence.PersistenceException)1 CriteriaConversionException (org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.CriteriaConversionException)1 FieldPersistenceProvider (org.broadleafcommerce.openadmin.server.service.persistence.module.provider.FieldPersistenceProvider)1 ExtractValueRequest (org.broadleafcommerce.openadmin.server.service.persistence.module.provider.request.ExtractValueRequest)1 MetadataProviderResponse (org.broadleafcommerce.openadmin.server.service.type.MetadataProviderResponse)1