use of org.broadleafcommerce.common.util.TypedPredicate in project BroadleafCommerce by BroadleafCommerce.
the class DefaultBasicPersistenceModuleExtensionHandler method rebalanceForUpdate.
@Override
public ExtensionResultStatusType rebalanceForUpdate(final BasicPersistenceModule basicPersistenceModule, PersistencePackage persistencePackage, Serializable instance, Map<String, FieldMetadata> mergedProperties, Object primaryKey, ExtensionResultHolder<Serializable> resultHolder) {
try {
Entity entity = persistencePackage.getEntity();
PersistencePerspective persistencePerspective = persistencePackage.getPersistencePerspective();
ForeignKey foreignKey = (ForeignKey) persistencePerspective.getPersistencePerspectiveItems().get(PersistencePerspectiveItemType.FOREIGNKEY);
Integer requestedSequence = Integer.valueOf(entity.findProperty(foreignKey.getSortField()).getValue());
Integer previousSequence = new BigDecimal(String.valueOf(basicPersistenceModule.getFieldManager().getFieldValue(instance, foreignKey.getSortField()))).intValue();
final String idPropertyName = basicPersistenceModule.getIdPropertyName(mergedProperties);
final Object pKey = primaryKey;
instance = basicPersistenceModule.createPopulatedInstance(instance, entity, mergedProperties, false, persistencePackage.isValidateUnsubmittedProperties());
if (!previousSequence.equals(requestedSequence)) {
// Sequence has changed. Rebalance the list
Serializable manyToField = (Serializable) basicPersistenceModule.getFieldManager().getFieldValue(instance, foreignKey.getManyToField());
List<Serializable> records = (List<Serializable>) basicPersistenceModule.getFieldManager().getFieldValue(manyToField, foreignKey.getOriginatingField());
Serializable myRecord = (Serializable) CollectionUtils.find(records, new TypedPredicate<Serializable>() {
@Override
public boolean eval(Serializable record) {
try {
return (pKey.equals(basicPersistenceModule.getFieldManager().getFieldValue(record, idPropertyName)));
} catch (IllegalAccessException e) {
return false;
} catch (FieldNotAvailableException e) {
return false;
}
}
});
records.remove(myRecord);
if (CollectionUtils.isEmpty(records)) {
records.add(myRecord);
} else {
records.add(requestedSequence - 1, myRecord);
}
int index = 1;
Class<?> type = basicPersistenceModule.getFieldManager().getField(myRecord.getClass(), foreignKey.getSortField()).getType();
boolean isBigDecimal = BigDecimal.class.isAssignableFrom(type);
for (Serializable record : records) {
basicPersistenceModule.getFieldManager().setFieldValue(record, foreignKey.getSortField(), isBigDecimal ? new BigDecimal(index) : Long.valueOf(index));
index++;
}
}
resultHolder.setResult(instance);
} catch (IllegalAccessException e) {
throw ExceptionHelper.refineException(e);
} catch (FieldNotAvailableException e) {
throw ExceptionHelper.refineException(e);
} catch (ValidationException e) {
throw ExceptionHelper.refineException(e);
} catch (InstantiationException e) {
throw ExceptionHelper.refineException(e);
}
return ExtensionResultStatusType.HANDLED;
}
Aggregations