Search in sources :

Example 11 with PersistenceException

use of org.broadleafcommerce.openadmin.server.service.persistence.PersistenceException in project BroadleafCommerce by BroadleafCommerce.

the class BasicFieldPersistenceProvider method extractValue.

@Override
public MetadataProviderResponse extractValue(ExtractValueRequest extractValueRequest, Property property) throws PersistenceException {
    if (!canHandleExtraction(extractValueRequest, property)) {
        return MetadataProviderResponse.NOT_HANDLED;
    }
    try {
        if (extractValueRequest.getRequestedValue() != null) {
            String val = null;
            if (extractValueRequest.getMetadata().getForeignKeyCollection()) {
                ((BasicFieldMetadata) property.getMetadata()).setFieldType(extractValueRequest.getMetadata().getFieldType());
            } else if (extractValueRequest.getMetadata().getFieldType().equals(SupportedFieldType.BOOLEAN) && extractValueRequest.getRequestedValue() instanceof Character) {
                val = (extractValueRequest.getRequestedValue().equals('Y')) ? "true" : "false";
            } else if (Date.class.isAssignableFrom(extractValueRequest.getRequestedValue().getClass())) {
                val = extractValueRequest.getDataFormatProvider().getSimpleDateFormatter().format((Date) extractValueRequest.getRequestedValue());
            } else if (Timestamp.class.isAssignableFrom(extractValueRequest.getRequestedValue().getClass())) {
                val = extractValueRequest.getDataFormatProvider().getSimpleDateFormatter().format(new Date(((Timestamp) extractValueRequest.getRequestedValue()).getTime()));
            } else if (Calendar.class.isAssignableFrom(extractValueRequest.getRequestedValue().getClass())) {
                val = extractValueRequest.getDataFormatProvider().getSimpleDateFormatter().format(((Calendar) extractValueRequest.getRequestedValue()).getTime());
            } else if (Double.class.isAssignableFrom(extractValueRequest.getRequestedValue().getClass())) {
                val = extractValueRequest.getDataFormatProvider().getDecimalFormatter().format(extractValueRequest.getRequestedValue());
            } else if (BigDecimal.class.isAssignableFrom(extractValueRequest.getRequestedValue().getClass())) {
                BigDecimal decimal = (BigDecimal) extractValueRequest.getRequestedValue();
                DecimalFormat format = extractValueRequest.getDataFormatProvider().getDecimalFormatter();
                // track all the decimal places in the scale of the BigDecimal - even if they're all zeros
                StringBuilder sb = new StringBuilder();
                sb.append("0");
                if (decimal.scale() > 0) {
                    sb.append(".");
                    for (int j = 0; j < decimal.scale(); j++) {
                        sb.append("0");
                    }
                }
                format.applyPattern(sb.toString());
                val = format.format(extractValueRequest.getRequestedValue());
            } else if (extractValueRequest.getMetadata().getForeignKeyClass() != null) {
                try {
                    val = extractValueRequest.getFieldManager().getFieldValue(extractValueRequest.getRequestedValue(), extractValueRequest.getMetadata().getForeignKeyProperty()).toString();
                    if (extensionManager != null) {
                        ExtensionResultHolder<Serializable> resultHolder = new ExtensionResultHolder<Serializable>();
                        ExtensionResultStatusType result = extensionManager.getProxy().transformForeignKey(extractValueRequest, property, resultHolder);
                        if (ExtensionResultStatusType.NOT_HANDLED != result && resultHolder.getResult() != null) {
                            val = String.valueOf(resultHolder.getResult());
                        }
                    }
                    // see if there's a name property and use it for the display value
                    String entityName = null;
                    if (extractValueRequest.getRequestedValue() instanceof AdminMainEntity) {
                        entityName = ((AdminMainEntity) extractValueRequest.getRequestedValue()).getMainEntityName();
                    }
                    Object temp = null;
                    if (!StringUtils.isEmpty(extractValueRequest.getMetadata().getForeignKeyDisplayValueProperty())) {
                        String nameProperty = extractValueRequest.getMetadata().getForeignKeyDisplayValueProperty();
                        try {
                            temp = extractValueRequest.getFieldManager().getFieldValue(extractValueRequest.getRequestedValue(), nameProperty);
                        } catch (FieldNotAvailableException e) {
                        // do nothing
                        }
                    }
                    if (temp == null && StringUtils.isEmpty(entityName)) {
                        try {
                            temp = extractValueRequest.getFieldManager().getFieldValue(extractValueRequest.getRequestedValue(), "name");
                        } catch (FieldNotAvailableException e) {
                        // do nothing
                        }
                    }
                    if (temp != null) {
                        extractValueRequest.setDisplayVal(temp.toString());
                    } else if (!StringUtils.isEmpty(entityName)) {
                        extractValueRequest.setDisplayVal(entityName);
                    }
                } catch (FieldNotAvailableException e) {
                    throw new IllegalArgumentException(e);
                }
            } else if (SupportedFieldType.ID == extractValueRequest.getMetadata().getFieldType()) {
                val = extractValueRequest.getRequestedValue().toString();
                if (extensionManager != null) {
                    ExtensionResultHolder<Serializable> resultHolder = new ExtensionResultHolder<Serializable>();
                    ExtensionResultStatusType result = extensionManager.getProxy().transformId(extractValueRequest, property, resultHolder);
                    if (ExtensionResultStatusType.NOT_HANDLED != result && resultHolder.getResult() != null) {
                        val = String.valueOf(resultHolder.getResult());
                    }
                }
            } else {
                val = extractValueRequest.getRequestedValue().toString();
            }
            property.setValue(val);
            property.setDisplayValue(extractValueRequest.getDisplayVal());
        }
    } catch (IllegalAccessException e) {
        throw new PersistenceException(e);
    }
    return MetadataProviderResponse.HANDLED;
}
Also used : Serializable(java.io.Serializable) Calendar(java.util.Calendar) DecimalFormat(java.text.DecimalFormat) ExtensionResultStatusType(org.broadleafcommerce.common.extension.ExtensionResultStatusType) Timestamp(java.sql.Timestamp) Date(java.util.Date) BigDecimal(java.math.BigDecimal) BasicFieldMetadata(org.broadleafcommerce.openadmin.dto.BasicFieldMetadata) FieldNotAvailableException(org.broadleafcommerce.openadmin.server.service.persistence.module.FieldNotAvailableException) PersistenceException(org.broadleafcommerce.openadmin.server.service.persistence.PersistenceException) ExtensionResultHolder(org.broadleafcommerce.common.extension.ExtensionResultHolder) AdminMainEntity(org.broadleafcommerce.common.admin.domain.AdminMainEntity)

Aggregations

PersistenceException (org.broadleafcommerce.openadmin.server.service.persistence.PersistenceException)11 BasicFieldMetadata (org.broadleafcommerce.openadmin.dto.BasicFieldMetadata)6 Property (org.broadleafcommerce.openadmin.dto.Property)6 AdminMainEntity (org.broadleafcommerce.common.admin.domain.AdminMainEntity)5 FieldNotAvailableException (org.broadleafcommerce.openadmin.server.service.persistence.module.FieldNotAvailableException)5 Serializable (java.io.Serializable)4 FieldMetadata (org.broadleafcommerce.openadmin.dto.FieldMetadata)4 ParentEntityPersistenceException (org.broadleafcommerce.openadmin.server.service.persistence.ParentEntityPersistenceException)4 Field (java.lang.reflect.Field)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 ArrayList (java.util.ArrayList)3 Date (java.util.Date)3 Entity (org.broadleafcommerce.openadmin.dto.Entity)3 ValidationException (org.broadleafcommerce.openadmin.server.service.ValidationException)3 MetadataProviderResponse (org.broadleafcommerce.openadmin.server.service.type.MetadataProviderResponse)3 BeansException (org.springframework.beans.BeansException)3 BigDecimal (java.math.BigDecimal)2 Timestamp (java.sql.Timestamp)2 DecimalFormat (java.text.DecimalFormat)2 Calendar (java.util.Calendar)2