Search in sources :

Example 31 with ExtensionResultStatusType

use of org.broadleafcommerce.common.extension.ExtensionResultStatusType in project BroadleafCommerce by BroadleafCommerce.

the class CategoryCustomPersistenceHandler method update.

@Override
public Entity update(PersistencePackage persistencePackage, DynamicEntityDao dynamicEntityDao, RecordHelper helper) throws ServiceException {
    Entity entity = persistencePackage.getEntity();
    try {
        entity = validateParentCategory(entity, false);
        if (entity.isValidationFailure()) {
            return entity;
        } else {
            PersistencePerspective persistencePerspective = persistencePackage.getPersistencePerspective();
            Map<String, FieldMetadata> adminProperties = helper.getSimpleMergedProperties(Category.class.getName(), persistencePerspective);
            Object primaryKey = helper.getPrimaryKey(entity, adminProperties);
            Category adminInstance = (Category) dynamicEntityDao.retrieve(Class.forName(entity.getType()[0]), primaryKey);
            CategoryXref oldDefault = getCurrentDefaultXref(adminInstance);
            adminInstance = (Category) helper.createPopulatedInstance(adminInstance, entity, adminProperties, false);
            adminInstance = dynamicEntityDao.merge(adminInstance);
            boolean handled = false;
            if (extensionManager != null) {
                ExtensionResultStatusType result = extensionManager.getProxy().manageParentCategoryForUpdate(persistencePackage, adminInstance);
                handled = ExtensionResultStatusType.NOT_HANDLED != result;
            }
            if (!handled) {
                setupXref(adminInstance);
                removeOldDefault(adminInstance, oldDefault, entity);
            }
            return helper.getRecord(adminProperties, adminInstance, null, null);
        }
    } catch (Exception e) {
        throw new ServiceException("Unable to update 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) Category(org.broadleafcommerce.core.catalog.domain.Category) PersistencePerspective(org.broadleafcommerce.openadmin.dto.PersistencePerspective) ServiceException(org.broadleafcommerce.common.exception.ServiceException) ExtensionResultStatusType(org.broadleafcommerce.common.extension.ExtensionResultStatusType) CategoryXref(org.broadleafcommerce.core.catalog.domain.CategoryXref) ServiceException(org.broadleafcommerce.common.exception.ServiceException)

Example 32 with ExtensionResultStatusType

use of org.broadleafcommerce.common.extension.ExtensionResultStatusType in project BroadleafCommerce by BroadleafCommerce.

the class ProductUrlFieldPersistenceProvider method populateValue.

@Override
public MetadataProviderResponse populateValue(PopulateValueRequest request, Serializable instance) {
    String propName = request.getProperty().getName();
    String val = request.getRequestedValue();
    if ("url".equals(propName) && ProductImpl.class.isAssignableFrom(instance.getClass())) {
        Product product = (Product) instance;
        ExtensionResultHolder<String> holder = new ExtensionResultHolder<>();
        ExtensionResultStatusType result = extensionManager.getProxy().modifyUrl(val, product, holder);
        if (ExtensionResultStatusType.HANDLED == result) {
            product.setUrl(holder.getResult());
            return MetadataProviderResponse.HANDLED;
        }
    }
    return super.populateValue(request, instance);
}
Also used : ProductImpl(org.broadleafcommerce.core.catalog.domain.ProductImpl) Product(org.broadleafcommerce.core.catalog.domain.Product) ExtensionResultStatusType(org.broadleafcommerce.common.extension.ExtensionResultStatusType) ExtensionResultHolder(org.broadleafcommerce.common.extension.ExtensionResultHolder)

Example 33 with ExtensionResultStatusType

use of org.broadleafcommerce.common.extension.ExtensionResultStatusType in project BroadleafCommerce by BroadleafCommerce.

the class AdminCatalogServiceImpl method generateSkusFromProduct.

@Override
public Integer generateSkusFromProduct(Long productId) {
    Product product = catalogService.findProductById(productId);
    if (CollectionUtils.isEmpty(product.getProductOptions())) {
        return -1;
    }
    List<List<ProductOptionValue>> allPermutations = generatePermutations(0, new ArrayList<ProductOptionValue>(), product.getProductOptions());
    // return -2 to indicate that one of the Product Options used in Sku generation has no Allowed Values
    if (allPermutations == null) {
        return -2;
    }
    LOG.info("Total number of permutations: " + allPermutations.size());
    LOG.info(allPermutations);
    // determine the permutations that I already have Skus for
    List<List<ProductOptionValue>> previouslyGeneratedPermutations = new ArrayList<List<ProductOptionValue>>();
    if (CollectionUtils.isNotEmpty(product.getAdditionalSkus())) {
        for (Sku additionalSku : product.getAdditionalSkus()) {
            if (CollectionUtils.isNotEmpty(additionalSku.getProductOptionValues())) {
                previouslyGeneratedPermutations.add(additionalSku.getProductOptionValues());
            }
        }
    }
    List<List<ProductOptionValue>> permutationsToGenerate = new ArrayList<List<ProductOptionValue>>();
    for (List<ProductOptionValue> permutation : allPermutations) {
        boolean previouslyGenerated = false;
        for (List<ProductOptionValue> generatedPermutation : previouslyGeneratedPermutations) {
            if (isSamePermutation(permutation, generatedPermutation)) {
                previouslyGenerated = true;
                break;
            }
        }
        if (!previouslyGenerated) {
            permutationsToGenerate.add(permutation);
        }
    }
    int numPermutationsCreated = 0;
    if (extensionManager != null) {
        ExtensionResultHolder<Integer> result = new ExtensionResultHolder<Integer>();
        ExtensionResultStatusType resultStatusType = extensionManager.getProxy().persistSkuPermutation(product, permutationsToGenerate, result);
        if (ExtensionResultStatusType.HANDLED == resultStatusType) {
            numPermutationsCreated = result.getResult();
        }
    }
    return numPermutationsCreated;
}
Also used : ArrayList(java.util.ArrayList) Product(org.broadleafcommerce.core.catalog.domain.Product) ExtensionResultStatusType(org.broadleafcommerce.common.extension.ExtensionResultStatusType) ProductOptionValue(org.broadleafcommerce.core.catalog.domain.ProductOptionValue) ArrayList(java.util.ArrayList) List(java.util.List) Sku(org.broadleafcommerce.core.catalog.domain.Sku) ExtensionResultHolder(org.broadleafcommerce.common.extension.ExtensionResultHolder)

Example 34 with ExtensionResultStatusType

use of org.broadleafcommerce.common.extension.ExtensionResultStatusType in project BroadleafCommerce by BroadleafCommerce.

the class AdminBasicEntityController method saveEntityJson.

/**
 * Builds JSON that looks like this:
 *
 * {"errors":
 *      [{"message":"This field is Required",
 *        "code": "requiredValidationFailure"
 *        "field":"defaultSku--name",
 *        "errorType", "field",
 *        "tab": "General"
 *        },
 *        {"message":"This field is Required",
 *        "code": "requiredValidationFailure"
 *        "field":"defaultSku--name",
 *        "errorType", "field",
 *        "tab": "General"
 *        }]
 * }
 */
@RequestMapping(value = "/{id}", method = RequestMethod.POST, produces = "application/json")
public String saveEntityJson(HttpServletRequest request, HttpServletResponse response, Model model, @PathVariable Map<String, String> pathVars, @PathVariable(value = "id") String id, @ModelAttribute(value = "entityForm") EntityForm entityForm, BindingResult result, RedirectAttributes ra) throws Exception {
    saveEntity(request, response, model, pathVars, id, entityForm, result, ra);
    JsonResponse json = new JsonResponse(response);
    if (result.hasErrors()) {
        populateJsonValidationErrors(entityForm, result, json);
    }
    List<String> dirtyList = buildDirtyList(pathVars, request, id);
    if (CollectionUtils.isNotEmpty(dirtyList)) {
        json.with("dirty", dirtyList);
    }
    ExtensionResultHolder<String> resultHolder = new ExtensionResultHolder<>();
    ExtensionResultStatusType resultStatusType = extensionManager.getProxy().overrideSaveEntityJsonResponse(response, result.hasErrors(), getSectionKey(pathVars), id, resultHolder);
    if (resultStatusType.equals(ExtensionResultStatusType.HANDLED)) {
        return resultHolder.getResult();
    }
    return json.done();
}
Also used : ExtensionResultStatusType(org.broadleafcommerce.common.extension.ExtensionResultStatusType) JsonResponse(org.broadleafcommerce.common.web.JsonResponse) ExtensionResultHolder(org.broadleafcommerce.common.extension.ExtensionResultHolder) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 35 with ExtensionResultStatusType

use of org.broadleafcommerce.common.extension.ExtensionResultStatusType in project BroadleafCommerce by BroadleafCommerce.

the class CatalogServiceImpl method findSkuByURI.

@Override
public Sku findSkuByURI(String uri) {
    if (extensionManager != null) {
        ExtensionResultHolder holder = new ExtensionResultHolder();
        ExtensionResultStatusType result = extensionManager.getProxy().findSkuByURI(createCatalogContextDTO(), uri, holder);
        if (ExtensionResultStatusType.HANDLED.equals(result)) {
            return (Sku) holder.getResult();
        }
    }
    List<Sku> skus = skuDao.findSkuByURI(uri);
    if (skus == null || skus.size() == 0) {
        return null;
    } else if (skus.size() == 1) {
        return skus.get(0);
    } else {
        // First check for a direct hit on the url
        for (Sku sku : skus) {
            if (uri.equals(sku.getProduct().getUrl() + sku.getUrlKey())) {
                return sku;
            }
        }
        // Otherwise, return the first product
        return skus.get(0);
    }
}
Also used : ExtensionResultStatusType(org.broadleafcommerce.common.extension.ExtensionResultStatusType) Sku(org.broadleafcommerce.core.catalog.domain.Sku) ExtensionResultHolder(org.broadleafcommerce.common.extension.ExtensionResultHolder)

Aggregations

ExtensionResultStatusType (org.broadleafcommerce.common.extension.ExtensionResultStatusType)59 ExtensionResultHolder (org.broadleafcommerce.common.extension.ExtensionResultHolder)35 ArrayList (java.util.ArrayList)11 Product (org.broadleafcommerce.core.catalog.domain.Product)9 Category (org.broadleafcommerce.core.catalog.domain.Category)7 BasicFieldMetadata (org.broadleafcommerce.openadmin.dto.BasicFieldMetadata)7 ServiceException (org.broadleafcommerce.common.exception.ServiceException)6 Sku (org.broadleafcommerce.core.catalog.domain.Sku)6 Entity (org.broadleafcommerce.openadmin.dto.Entity)6 FieldMetadata (org.broadleafcommerce.openadmin.dto.FieldMetadata)6 FieldNotAvailableException (org.broadleafcommerce.openadmin.server.service.persistence.module.FieldNotAvailableException)5 List (java.util.List)4 Query (javax.persistence.Query)4 TypedQuery (javax.persistence.TypedQuery)4 CriteriaQuery (javax.persistence.criteria.CriteriaQuery)4 IndexFieldType (org.broadleafcommerce.core.search.domain.IndexFieldType)4 PersistencePerspective (org.broadleafcommerce.openadmin.dto.PersistencePerspective)4 PersistenceException (org.broadleafcommerce.openadmin.server.service.persistence.PersistenceException)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 File (java.io.File)3