Search in sources :

Example 1 with TypedPredicate

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;
}
Also used : Entity(org.broadleafcommerce.openadmin.dto.Entity) Serializable(java.io.Serializable) ValidationException(org.broadleafcommerce.openadmin.server.service.ValidationException) ForeignKey(org.broadleafcommerce.openadmin.dto.ForeignKey) BigDecimal(java.math.BigDecimal) PersistencePerspective(org.broadleafcommerce.openadmin.dto.PersistencePerspective) FieldNotAvailableException(org.broadleafcommerce.openadmin.server.service.persistence.module.FieldNotAvailableException) CriteriaTransferObject(org.broadleafcommerce.openadmin.dto.CriteriaTransferObject) List(java.util.List) TypedPredicate(org.broadleafcommerce.common.util.TypedPredicate)

Aggregations

Serializable (java.io.Serializable)1 BigDecimal (java.math.BigDecimal)1 List (java.util.List)1 TypedPredicate (org.broadleafcommerce.common.util.TypedPredicate)1 CriteriaTransferObject (org.broadleafcommerce.openadmin.dto.CriteriaTransferObject)1 Entity (org.broadleafcommerce.openadmin.dto.Entity)1 ForeignKey (org.broadleafcommerce.openadmin.dto.ForeignKey)1 PersistencePerspective (org.broadleafcommerce.openadmin.dto.PersistencePerspective)1 ValidationException (org.broadleafcommerce.openadmin.server.service.ValidationException)1 FieldNotAvailableException (org.broadleafcommerce.openadmin.server.service.persistence.module.FieldNotAvailableException)1