use of org.broadleafcommerce.openadmin.server.service.persistence.PersistenceException 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);
}
}
use of org.broadleafcommerce.openadmin.server.service.persistence.PersistenceException in project BroadleafCommerce by BroadleafCommerce.
the class MapFieldPersistenceProvider method populateValue.
@Override
public MetadataProviderResponse populateValue(PopulateValueRequest populateValueRequest, Serializable instance) {
if (!canHandlePersistence(populateValueRequest, instance)) {
return MetadataProviderResponse.NOT_HANDLED;
}
boolean dirty = false;
try {
Class<?> startingValueType = getStartingValueType(populateValueRequest);
Class<?> valueType = getValueType(populateValueRequest, startingValueType);
if (ValueAssignable.class.isAssignableFrom(valueType)) {
boolean persistValue = false;
ValueAssignable assignableValue;
Object parent;
try {
parent = populateValueRequest.getFieldManager().getFieldValue(instance, populateValueRequest.getProperty().getName());
if (parent instanceof List) {
parent = ((List) parent).get(0);
}
if (parent == null) {
parent = startingValueType.newInstance();
if (!startingValueType.equals(valueType)) {
setupJoinEntityParent(populateValueRequest, instance, parent);
}
populateValueRequest.getFieldManager().setFieldValue(instance, populateValueRequest.getProperty().getName(), parent);
persistValue = true;
}
assignableValue = establishAssignableValue(populateValueRequest, parent);
} catch (FieldNotAvailableException e) {
throw new IllegalArgumentException(e);
}
dirty = persistValue || (assignableValue != null && ObjectUtils.notEqual(assignableValue.getValue(), populateValueRequest.getProperty().getValue()));
if (dirty) {
updateAssignableValue(populateValueRequest, instance, parent, valueType, persistValue, assignableValue);
}
} else {
// handle the map value set itself
if (MetadataProviderResponse.NOT_HANDLED == super.populateValue(populateValueRequest, instance)) {
return MetadataProviderResponse.NOT_HANDLED;
}
}
} catch (Exception e) {
throw new PersistenceException(e);
}
populateValueRequest.getProperty().setIsDirty(dirty);
return MetadataProviderResponse.HANDLED_BREAK;
}
use of org.broadleafcommerce.openadmin.server.service.persistence.PersistenceException in project BroadleafCommerce by BroadleafCommerce.
the class MediaFieldPersistenceProvider method populateValue.
@Override
public MetadataProviderResponse populateValue(PopulateValueRequest populateValueRequest, Serializable instance) throws PersistenceException {
if (!canHandlePersistence(populateValueRequest, instance)) {
return MetadataProviderResponse.NOT_HANDLED;
}
String prop = populateValueRequest.getProperty().getName();
if (prop.contains(FieldManager.MAPFIELDSEPARATOR)) {
Field field = populateValueRequest.getFieldManager().getField(instance.getClass(), prop.substring(0, prop.indexOf(FieldManager.MAPFIELDSEPARATOR)));
if (field.getAnnotation(OneToMany.class) == null) {
throw new UnsupportedOperationException("MediaFieldPersistenceProvider is currently only compatible with map fields when modelled using @OneToMany");
}
}
MetadataProviderResponse response = MetadataProviderResponse.HANDLED;
boolean dirty = false;
try {
setNonDisplayableValues(populateValueRequest);
Class<?> valueType = getStartingValueType(populateValueRequest);
if (Media.class.isAssignableFrom(valueType)) {
Media newMedia = mediaBuilderService.convertJsonToMedia(populateValueRequest.getProperty().getUnHtmlEncodedValue(), valueType);
boolean persist = false;
boolean noPrimary = false;
boolean update = false;
Media media;
Boolean cleared;
try {
checkExtension: {
if (extensionManager != null) {
ExtensionResultHolder<Tuple<Media, Boolean>> result = new ExtensionResultHolder<Tuple<Media, Boolean>>();
ExtensionResultStatusType statusType = extensionManager.getProxy().retrieveMedia(instance, populateValueRequest, result);
if (ExtensionResultStatusType.NOT_HANDLED != statusType) {
Tuple<Media, Boolean> tuple = result.getResult();
media = tuple.getFirst();
cleared = tuple.getSecond();
break checkExtension;
}
}
media = (Media) populateValueRequest.getFieldManager().getFieldValue(instance, populateValueRequest.getProperty().getName());
cleared = false;
}
if (newMedia == null) {
noPrimary = true;
dirty = true;
update = false;
if (!cleared && media != null) {
// remove entry in sku to media map
populateValueRequest.getFieldManager().setFieldValue(instance, populateValueRequest.getProperty().getName(), null);
populateValueRequest.getPersistenceManager().getDynamicEntityDao().remove(media);
}
} else if (media == null) {
media = (Media) valueType.newInstance();
mediaBuilderService.instantiateMediaFields(media);
Object parent = extractParent(populateValueRequest, instance);
populateValueRequest.getFieldManager().setFieldValue(media, populateValueRequest.getMetadata().getToOneParentProperty(), parent);
populateValueRequest.getFieldManager().setFieldValue(media, populateValueRequest.getMetadata().getMapKeyValueProperty(), prop.substring(prop.indexOf(FieldManager.MAPFIELDSEPARATOR) + FieldManager.MAPFIELDSEPARATOR.length(), prop.length()));
persist = true;
}
} catch (FieldNotAvailableException e) {
throw new IllegalArgumentException(e);
}
if (media != null) {
if (media instanceof Status && 'Y' == ((Status) media).getArchived()) {
persist = true;
}
populateValueRequest.getProperty().setOriginalValue(convertMediaToJson(media));
}
if (!noPrimary) {
dirty = establishDirtyState(newMedia, media);
update = dirty;
}
if (dirty) {
if (update) {
updateMedia(populateValueRequest, newMedia, persist, media);
}
response = MetadataProviderResponse.HANDLED_BREAK;
}
} else {
throw new UnsupportedOperationException("MediaFields only work with Media types.");
}
} catch (Exception e) {
throw ExceptionHelper.refineException(PersistenceException.class, PersistenceException.class, e);
}
populateValueRequest.getProperty().setIsDirty(dirty);
return response;
}
use of org.broadleafcommerce.openadmin.server.service.persistence.PersistenceException in project BroadleafCommerce by BroadleafCommerce.
the class BasicFieldPersistenceProvider method addSearchMapping.
@Override
public MetadataProviderResponse addSearchMapping(AddSearchMappingRequest addSearchMappingRequest, List<FilterMapping> filterMappings) {
if (!canHandleSearchMapping(addSearchMappingRequest, filterMappings)) {
return MetadataProviderResponse.NOT_HANDLED;
}
Class clazz;
try {
clazz = Class.forName(addSearchMappingRequest.getMergedProperties().get(addSearchMappingRequest.getPropertyName()).getInheritedFromType());
} catch (ClassNotFoundException e) {
throw new PersistenceException(e);
}
Field field = addSearchMappingRequest.getFieldManager().getField(clazz, addSearchMappingRequest.getPropertyName());
Class<?> targetType = null;
if (field != null) {
targetType = field.getType();
}
BasicFieldMetadata metadata = (BasicFieldMetadata) addSearchMappingRequest.getMergedProperties().get(addSearchMappingRequest.getPropertyName());
FilterAndSortCriteria fasc = addSearchMappingRequest.getRequestedCto().get(addSearchMappingRequest.getPropertyName());
FilterMapping filterMapping = new FilterMapping().withInheritedFromClass(clazz).withFullPropertyName(addSearchMappingRequest.getPropertyName()).withFilterValues(fasc.getFilterValues()).withSortDirection(fasc.getSortDirection()).withOrder(fasc.getOrder()).withNullsLast(fasc.isNullsLast());
filterMappings.add(filterMapping);
if (fasc.hasSpecialFilterValue()) {
filterMapping.setDirectFilterValues(new EmptyFilterValues());
// Handle special values on a case by case basis
List<String> specialValues = fasc.getSpecialFilterValues();
if (specialValues.contains(FilterAndSortCriteria.IS_NULL_FILTER_VALUE)) {
filterMapping.setRestriction(new Restriction().withPredicateProvider(new IsNullPredicateProvider()));
}
if (specialValues.contains(FilterAndSortCriteria.IS_NOT_NULL_FILTER_VALUE)) {
filterMapping.setRestriction(new Restriction().withPredicateProvider(new IsNotNullPredicateProvider()));
}
} else {
if (fasc.getRestrictionType() != null) {
filterMapping.setRestriction(addSearchMappingRequest.getRestrictionFactory().getRestriction(fasc.getRestrictionType().getType(), addSearchMappingRequest.getPropertyName()));
} else {
switch(metadata.getFieldType()) {
case BOOLEAN:
if (targetType == null || targetType.equals(Boolean.class) || targetType.equals(boolean.class)) {
filterMapping.setRestriction(addSearchMappingRequest.getRestrictionFactory().getRestriction(RestrictionType.BOOLEAN.getType(), addSearchMappingRequest.getPropertyName()));
} else {
filterMapping.setRestriction(addSearchMappingRequest.getRestrictionFactory().getRestriction(RestrictionType.CHARACTER.getType(), addSearchMappingRequest.getPropertyName()));
}
break;
case DATE:
filterMapping.setRestriction(addSearchMappingRequest.getRestrictionFactory().getRestriction(RestrictionType.DATE.getType(), addSearchMappingRequest.getPropertyName()));
break;
case DECIMAL:
case MONEY:
filterMapping.setRestriction(addSearchMappingRequest.getRestrictionFactory().getRestriction(RestrictionType.DECIMAL.getType(), addSearchMappingRequest.getPropertyName()));
break;
case INTEGER:
filterMapping.setRestriction(addSearchMappingRequest.getRestrictionFactory().getRestriction(RestrictionType.LONG.getType(), addSearchMappingRequest.getPropertyName()));
break;
case BROADLEAF_ENUMERATION:
filterMapping.setRestriction(addSearchMappingRequest.getRestrictionFactory().getRestriction(RestrictionType.STRING_EQUAL.getType(), addSearchMappingRequest.getPropertyName()));
break;
default:
filterMapping.setRestriction(addSearchMappingRequest.getRestrictionFactory().getRestriction(RestrictionType.STRING_LIKE.getType(), addSearchMappingRequest.getPropertyName()));
break;
case FOREIGN_KEY:
if (!addSearchMappingRequest.getRequestedCto().get(addSearchMappingRequest.getPropertyName()).getFilterValues().isEmpty()) {
ForeignKey foreignKey = (ForeignKey) addSearchMappingRequest.getPersistencePerspective().getPersistencePerspectiveItems().get(PersistencePerspectiveItemType.FOREIGNKEY);
if (metadata.getForeignKeyCollection()) {
if (ForeignKeyRestrictionType.COLLECTION_SIZE_EQ.toString().equals(foreignKey.getRestrictionType().toString())) {
filterMapping.setRestriction(addSearchMappingRequest.getRestrictionFactory().getRestriction(RestrictionType.COLLECTION_SIZE_EQUAL.getType(), addSearchMappingRequest.getPropertyName()));
filterMapping.setFieldPath(new FieldPath());
} else {
filterMapping.setRestriction(addSearchMappingRequest.getRestrictionFactory().getRestriction(RestrictionType.LONG.getType(), addSearchMappingRequest.getPropertyName()));
filterMapping.setFieldPath(new FieldPath().withTargetProperty(addSearchMappingRequest.getPropertyName() + "." + metadata.getForeignKeyProperty()));
}
} else if (addSearchMappingRequest.getRequestedCto().get(addSearchMappingRequest.getPropertyName()).getFilterValues().get(0) == null || "null".equals(addSearchMappingRequest.getRequestedCto().get(addSearchMappingRequest.getPropertyName()).getFilterValues().get(0))) {
filterMapping.setRestriction(addSearchMappingRequest.getRestrictionFactory().getRestriction(RestrictionType.IS_NULL_LONG.getType(), addSearchMappingRequest.getPropertyName()));
} else if (metadata.getSecondaryType() == SupportedFieldType.STRING) {
filterMapping.setRestriction(addSearchMappingRequest.getRestrictionFactory().getRestriction(RestrictionType.STRING_EQUAL.getType(), addSearchMappingRequest.getPropertyName()));
filterMapping.setFieldPath(new FieldPath().withTargetProperty(addSearchMappingRequest.getPropertyName() + "." + metadata.getForeignKeyProperty()));
} else {
filterMapping.setRestriction(addSearchMappingRequest.getRestrictionFactory().getRestriction(RestrictionType.LONG_EQUAL.getType(), addSearchMappingRequest.getPropertyName()));
filterMapping.setFieldPath(new FieldPath().withTargetProperty(addSearchMappingRequest.getPropertyName() + "." + metadata.getForeignKeyProperty()));
}
}
break;
case ADDITIONAL_FOREIGN_KEY:
int additionalForeignKeyIndexPosition = Arrays.binarySearch(addSearchMappingRequest.getPersistencePerspective().getAdditionalForeignKeys(), new ForeignKey(addSearchMappingRequest.getPropertyName(), null, null), new Comparator<ForeignKey>() {
@Override
public int compare(ForeignKey o1, ForeignKey o2) {
return o1.getManyToField().compareTo(o2.getManyToField());
}
});
ForeignKey foreignKey = null;
if (additionalForeignKeyIndexPosition >= 0) {
foreignKey = addSearchMappingRequest.getPersistencePerspective().getAdditionalForeignKeys()[additionalForeignKeyIndexPosition];
}
// default to just using a ForeignKeyRestrictionType.ID_EQ
if (metadata.getForeignKeyCollection()) {
if (ForeignKeyRestrictionType.COLLECTION_SIZE_EQ.toString().equals(foreignKey.getRestrictionType().toString())) {
filterMapping.setRestriction(addSearchMappingRequest.getRestrictionFactory().getRestriction(RestrictionType.COLLECTION_SIZE_EQUAL.getType(), addSearchMappingRequest.getPropertyName()));
filterMapping.setFieldPath(new FieldPath());
} else {
filterMapping.setRestriction(addSearchMappingRequest.getRestrictionFactory().getRestriction(RestrictionType.LONG.getType(), addSearchMappingRequest.getPropertyName()));
filterMapping.setFieldPath(new FieldPath().withTargetProperty(addSearchMappingRequest.getPropertyName() + "." + metadata.getForeignKeyProperty()));
}
} else if (CollectionUtils.isEmpty(addSearchMappingRequest.getRequestedCto().get(addSearchMappingRequest.getPropertyName()).getFilterValues()) || addSearchMappingRequest.getRequestedCto().get(addSearchMappingRequest.getPropertyName()).getFilterValues().get(0) == null || "null".equals(addSearchMappingRequest.getRequestedCto().get(addSearchMappingRequest.getPropertyName()).getFilterValues().get(0))) {
filterMapping.setRestriction(addSearchMappingRequest.getRestrictionFactory().getRestriction(RestrictionType.IS_NULL_LONG.getType(), addSearchMappingRequest.getPropertyName()));
} else if (metadata.getSecondaryType() == SupportedFieldType.STRING) {
filterMapping.setRestriction(addSearchMappingRequest.getRestrictionFactory().getRestriction(RestrictionType.STRING_EQUAL.getType(), addSearchMappingRequest.getPropertyName()));
filterMapping.setFieldPath(new FieldPath().withTargetProperty(addSearchMappingRequest.getPropertyName() + "." + metadata.getForeignKeyProperty()));
} else {
filterMapping.setRestriction(addSearchMappingRequest.getRestrictionFactory().getRestriction(RestrictionType.LONG_EQUAL.getType(), addSearchMappingRequest.getPropertyName()));
filterMapping.setFieldPath(new FieldPath().withTargetProperty(addSearchMappingRequest.getPropertyName() + "." + metadata.getForeignKeyProperty()));
}
break;
case ID:
switch(metadata.getSecondaryType()) {
case INTEGER:
filterMapping.setRestriction(addSearchMappingRequest.getRestrictionFactory().getRestriction(RestrictionType.LONG_EQUAL.getType(), addSearchMappingRequest.getPropertyName()));
break;
case STRING:
filterMapping.setRestriction(addSearchMappingRequest.getRestrictionFactory().getRestriction(RestrictionType.STRING_EQUAL.getType(), addSearchMappingRequest.getPropertyName()));
break;
}
break;
}
}
}
return MetadataProviderResponse.HANDLED;
}
use of org.broadleafcommerce.openadmin.server.service.persistence.PersistenceException in project BroadleafCommerce by BroadleafCommerce.
the class DefaultFieldPersistenceProvider method populateValue.
@Override
public MetadataProviderResponse populateValue(PopulateValueRequest populateValueRequest, Serializable instance) throws PersistenceException {
boolean dirty;
try {
Property p = populateValueRequest.getProperty();
Object value = populateValueRequest.getFieldManager().getFieldValue(instance, p.getName());
if (value != null) {
p.setOriginalValue(String.valueOf(value));
p.setOriginalDisplayValue(p.getOriginalValue());
}
dirty = checkDirtyState(populateValueRequest, instance, populateValueRequest.getRequestedValue());
populateValueRequest.getFieldManager().setFieldValue(instance, populateValueRequest.getProperty().getName(), populateValueRequest.getRequestedValue());
} catch (Exception e) {
throw new PersistenceException(e);
}
populateValueRequest.getProperty().setIsDirty(dirty);
return MetadataProviderResponse.HANDLED;
}
Aggregations