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