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;
}
Aggregations