Search in sources :

Example 11 with PersistencePerspective

use of org.broadleafcommerce.openadmin.dto.PersistencePerspective 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)

Example 12 with PersistencePerspective

use of org.broadleafcommerce.openadmin.dto.PersistencePerspective in project BroadleafCommerce by BroadleafCommerce.

the class OfferCustomPersistenceHandler method inspect.

@Override
public DynamicResultSet inspect(PersistencePackage persistencePackage, DynamicEntityDao dynamicEntityDao, InspectHelper helper) throws ServiceException {
    Class ceilingEntityClass = getClassForName(persistencePackage.getCeilingEntityFullyQualifiedClassname());
    PersistencePerspective persistencePerspective = persistencePackage.getPersistencePerspective();
    Map<MergedPropertyType, Map<String, FieldMetadata>> allMergedProperties = new HashMap<MergedPropertyType, Map<String, FieldMetadata>>();
    // retrieve the default properties for WorkflowEvents
    Map<String, FieldMetadata> properties = helper.getSimpleMergedProperties(ceilingEntityClass.getCanonicalName(), persistencePerspective);
    properties.put(SHOW_ADVANCED_VISIBILITY_OPTIONS, buildAdvancedVisibilityOptionsFieldMetaData());
    properties.put(QUALIFIERS_CAN_BE_QUALIFIERS, buildQualifiersCanBeQualifiersFieldMetaData());
    properties.put(QUALIFIERS_CAN_BE_TARGETS, buildQualifiersCanBeTargetsFieldMetaData());
    properties.put(STACKABLE, buildStackableFieldMetaData());
    if (isActiveFilter) {
        properties.put(IS_ACTIVE, buildIsActiveFieldMetaData());
    }
    allMergedProperties.put(MergedPropertyType.PRIMARY, properties);
    Class<?>[] entityClasses = dynamicEntityDao.getAllPolymorphicEntitiesFromCeiling(ceilingEntityClass);
    ClassMetadata mergedMetadata = helper.buildClassMetadata(entityClasses, persistencePackage, allMergedProperties);
    return new DynamicResultSet(mergedMetadata, null, null);
}
Also used : ClassMetadata(org.broadleafcommerce.openadmin.dto.ClassMetadata) FieldMetadata(org.broadleafcommerce.openadmin.dto.FieldMetadata) BasicFieldMetadata(org.broadleafcommerce.openadmin.dto.BasicFieldMetadata) PersistencePerspective(org.broadleafcommerce.openadmin.dto.PersistencePerspective) HashMap(java.util.HashMap) Map(java.util.Map) HashMap(java.util.HashMap) DynamicResultSet(org.broadleafcommerce.openadmin.dto.DynamicResultSet) MergedPropertyType(org.broadleafcommerce.openadmin.dto.MergedPropertyType)

Example 13 with PersistencePerspective

use of org.broadleafcommerce.openadmin.dto.PersistencePerspective in project BroadleafCommerce by BroadleafCommerce.

the class ProductCustomPersistenceHandler method getAdminInstance.

protected Product getAdminInstance(PersistencePackage persistencePackage, DynamicEntityDao dynamicEntityDao, RecordHelper helper, Entity entity) throws ClassNotFoundException {
    PersistencePerspective persistencePerspective = persistencePackage.getPersistencePerspective();
    Map<String, FieldMetadata> adminProperties = helper.getSimpleMergedProperties(Product.class.getName(), persistencePerspective);
    Object primaryKey = helper.getPrimaryKey(entity, adminProperties);
    String type = entity.getType()[0];
    Product adminInstance = (Product) dynamicEntityDao.retrieve(Class.forName(type), primaryKey);
    return adminInstance;
}
Also used : FieldMetadata(org.broadleafcommerce.openadmin.dto.FieldMetadata) BasicFieldMetadata(org.broadleafcommerce.openadmin.dto.BasicFieldMetadata) PersistencePerspective(org.broadleafcommerce.openadmin.dto.PersistencePerspective) Product(org.broadleafcommerce.core.catalog.domain.Product) CriteriaTransferObject(org.broadleafcommerce.openadmin.dto.CriteriaTransferObject)

Example 14 with PersistencePerspective

use of org.broadleafcommerce.openadmin.dto.PersistencePerspective in project BroadleafCommerce by BroadleafCommerce.

the class ProductCustomPersistenceHandler method add.

@Override
public Entity add(PersistencePackage persistencePackage, DynamicEntityDao dynamicEntityDao, RecordHelper helper) throws ServiceException {
    Entity entity = persistencePackage.getEntity();
    try {
        PersistencePerspective persistencePerspective = persistencePackage.getPersistencePerspective();
        Product adminInstance = (Product) Class.forName(entity.getType()[0]).newInstance();
        Map<String, FieldMetadata> adminProperties = helper.getSimpleMergedProperties(Product.class.getName(), persistencePerspective);
        if (adminInstance instanceof ProductBundle) {
            removeBundleFieldRestrictions((ProductBundle) adminInstance, adminProperties, entity);
        }
        adminInstance = (Product) helper.createPopulatedInstance(adminInstance, entity, adminProperties, false);
        adminInstance = dynamicEntityDao.merge(adminInstance);
        // any Sku fields, and thus a Sku would not be created. Product still needs a default Sku so instantiate one
        if (adminInstance.getDefaultSku() == null) {
            Sku newSku = catalogService.createSku();
            dynamicEntityDao.persist(newSku);
            adminInstance.setDefaultSku(newSku);
            adminInstance = dynamicEntityDao.merge(adminInstance);
        }
        // also set the default product for the Sku
        adminInstance.getDefaultSku().setDefaultProduct(adminInstance);
        dynamicEntityDao.merge(adminInstance.getDefaultSku());
        // if this is a Pre-Add, skip the rest of the method
        if (entity.isPreAdd()) {
            return helper.getRecord(adminProperties, adminInstance, null, null);
        }
        boolean handled = false;
        if (extensionManager != null) {
            ExtensionResultStatusType result = extensionManager.getProxy().manageParentCategoryForAdd(persistencePackage, adminInstance);
            handled = ExtensionResultStatusType.NOT_HANDLED != result;
        }
        if (!handled) {
            setupXref(adminInstance);
        }
        return helper.getRecord(adminProperties, adminInstance, null, null);
    } catch (Exception e) {
        throw new ServiceException("Unable to add entity for " + entity.getType()[0], e);
    }
}
Also used : Entity(org.broadleafcommerce.openadmin.dto.Entity) FieldMetadata(org.broadleafcommerce.openadmin.dto.FieldMetadata) BasicFieldMetadata(org.broadleafcommerce.openadmin.dto.BasicFieldMetadata) PersistencePerspective(org.broadleafcommerce.openadmin.dto.PersistencePerspective) ServiceException(org.broadleafcommerce.common.exception.ServiceException) ProductBundle(org.broadleafcommerce.core.catalog.domain.ProductBundle) Product(org.broadleafcommerce.core.catalog.domain.Product) ExtensionResultStatusType(org.broadleafcommerce.common.extension.ExtensionResultStatusType) Sku(org.broadleafcommerce.core.catalog.domain.Sku) ServiceException(org.broadleafcommerce.common.exception.ServiceException)

Example 15 with PersistencePerspective

use of org.broadleafcommerce.openadmin.dto.PersistencePerspective in project BroadleafCommerce by BroadleafCommerce.

the class ProductOptionsCustomPersistenceHandler method update.

@Override
public Entity update(PersistencePackage persistencePackage, DynamicEntityDao dynamicEntityDao, RecordHelper helper) throws ServiceException {
    Entity entity = persistencePackage.getEntity();
    try {
        PersistencePerspective persistencePerspective = persistencePackage.getPersistencePerspective();
        Map<String, FieldMetadata> adminProperties = helper.getSimpleMergedProperties(ProductOption.class.getName(), persistencePerspective);
        Object primaryKey = helper.getPrimaryKey(entity, adminProperties);
        ProductOption adminInstance = (ProductOption) dynamicEntityDao.retrieve(Class.forName(entity.getType()[0]), primaryKey);
        adminInstance = (ProductOption) helper.createPopulatedInstance(adminInstance, entity, adminProperties, false);
        // validate "Use in Sku generation"
        if (needsAllowedValue(adminInstance)) {
            String errorMessage = "Must add at least 1 Allowed Value when Product Option is used in Sku generation";
            entity.addValidationError("useInSkuGeneration", errorMessage);
            return entity;
        }
        adminInstance = (ProductOption) dynamicEntityDao.merge(adminInstance);
        return helper.getRecord(adminProperties, adminInstance, null, null);
    } catch (Exception e) {
        throw new ServiceException("Unable to update entity for " + entity.getType()[0], e);
    }
}
Also used : ProductOption(org.broadleafcommerce.core.catalog.domain.ProductOption) Entity(org.broadleafcommerce.openadmin.dto.Entity) FieldMetadata(org.broadleafcommerce.openadmin.dto.FieldMetadata) PersistencePerspective(org.broadleafcommerce.openadmin.dto.PersistencePerspective) ServiceException(org.broadleafcommerce.common.exception.ServiceException) CriteriaTransferObject(org.broadleafcommerce.openadmin.dto.CriteriaTransferObject) ServiceException(org.broadleafcommerce.common.exception.ServiceException)

Aggregations

PersistencePerspective (org.broadleafcommerce.openadmin.dto.PersistencePerspective)51 FieldMetadata (org.broadleafcommerce.openadmin.dto.FieldMetadata)44 ServiceException (org.broadleafcommerce.common.exception.ServiceException)39 Entity (org.broadleafcommerce.openadmin.dto.Entity)34 BasicFieldMetadata (org.broadleafcommerce.openadmin.dto.BasicFieldMetadata)27 Serializable (java.io.Serializable)16 InvocationTargetException (java.lang.reflect.InvocationTargetException)16 SecurityServiceException (org.broadleafcommerce.common.exception.SecurityServiceException)16 CriteriaTransferObject (org.broadleafcommerce.openadmin.dto.CriteriaTransferObject)16 ForeignKey (org.broadleafcommerce.openadmin.dto.ForeignKey)14 Map (java.util.Map)10 DynamicResultSet (org.broadleafcommerce.openadmin.dto.DynamicResultSet)9 ValidationException (org.broadleafcommerce.openadmin.server.service.ValidationException)9 FilterMapping (org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.FilterMapping)7 HashMap (java.util.HashMap)6 AdornedTargetList (org.broadleafcommerce.openadmin.dto.AdornedTargetList)6 MapStructure (org.broadleafcommerce.openadmin.dto.MapStructure)6 SimpleValueMapStructure (org.broadleafcommerce.openadmin.dto.SimpleValueMapStructure)6 ParentEntityPersistenceException (org.broadleafcommerce.openadmin.server.service.persistence.ParentEntityPersistenceException)6 PersistenceException (org.broadleafcommerce.openadmin.server.service.persistence.PersistenceException)6