Search in sources :

Example 26 with ExtensionResultHolder

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

the class AbstractGeneratedResourceHandler method getRawResource.

/**
 * This method can be used to read in a resource given a path and at least one resource location
 *
 * @param path
 * @param locations
 * @return the resource from the file system, classpath, etc, if it exists
 */
protected Resource getRawResource(String path, List<Resource> locations) {
    ExtensionResultHolder erh = new ExtensionResultHolder();
    extensionManager.getProxy().getOverrideResource(path, erh);
    if (erh.getContextMap().get(ResourceRequestExtensionHandler.RESOURCE_ATTR) != null) {
        return (Resource) erh.getContextMap().get(ResourceRequestExtensionHandler.RESOURCE_ATTR);
    }
    for (Resource location : locations) {
        try {
            Resource resource = location.createRelative(path);
            if (resource.exists() && resource.isReadable()) {
                return resource;
            }
        } catch (IOException ex) {
            LOG.debug("Failed to create relative resource - trying next resource location", ex);
        }
    }
    return null;
}
Also used : GeneratedResource(org.broadleafcommerce.common.resource.GeneratedResource) Resource(org.springframework.core.io.Resource) IOException(java.io.IOException) ExtensionResultHolder(org.broadleafcommerce.common.extension.ExtensionResultHolder)

Example 27 with ExtensionResultHolder

use of org.broadleafcommerce.common.extension.ExtensionResultHolder 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 28 with ExtensionResultHolder

use of org.broadleafcommerce.common.extension.ExtensionResultHolder 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 29 with ExtensionResultHolder

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

the class BroadleafAdminRequestProcessor method prepareProfile.

protected void prepareProfile(WebRequest request, BroadleafRequestContext brc) {
    AdminUser adminUser = adminRemoteSecurityService.getPersistentAdminUser();
    if (adminUser == null) {
        // clear any profile
        if (BLCRequestUtils.isOKtoUseSession(request)) {
            request.removeAttribute(PROFILE_REQ_PARAM, WebRequest.SCOPE_GLOBAL_SESSION);
        }
    } else {
        Site profile = null;
        if (StringUtils.isNotBlank(request.getParameter(PROFILE_REQ_PARAM))) {
            Long profileId = Long.parseLong(request.getParameter(PROFILE_REQ_PARAM));
            profile = siteService.retrievePersistentSiteById(profileId);
            if (profile == null) {
                throw new IllegalArgumentException(String.format("Unable to find the requested profile: %s", profileId));
            }
            String token = request.getParameter(staleStateProtectionService.getStateVersionTokenParameter());
            staleStateProtectionService.compareToken(token);
            staleStateProtectionService.invalidateState(true);
        }
        if (profile == null) {
            Long previouslySetProfileId = null;
            if (BLCRequestUtils.isOKtoUseSession(request)) {
                previouslySetProfileId = (Long) request.getAttribute(PROFILE_REQ_PARAM, WebRequest.SCOPE_GLOBAL_SESSION);
            }
            if (previouslySetProfileId != null) {
                profile = siteService.retrievePersistentSiteById(previouslySetProfileId);
            }
        }
        if (profile == null) {
            List<Site> profiles = new ArrayList<Site>();
            if (brc.getNonPersistentSite() != null) {
                Site currentSite = siteService.retrievePersistentSiteById(brc.getNonPersistentSite().getId());
                if (extensionManager != null) {
                    ExtensionResultHolder<Set<Site>> profilesResult = new ExtensionResultHolder<Set<Site>>();
                    extensionManager.retrieveProfiles(currentSite, profilesResult);
                    if (!CollectionUtils.isEmpty(profilesResult.getResult())) {
                        profiles.addAll(profilesResult.getResult());
                    }
                }
            }
            if (profiles.size() > 0) {
                profile = profiles.get(0);
            }
        }
        if (profile != null) {
            if (BLCRequestUtils.isOKtoUseSession(request)) {
                request.setAttribute(PROFILE_REQ_PARAM, profile.getId(), WebRequest.SCOPE_GLOBAL_SESSION);
            }
            brc.setCurrentProfile(profile);
        }
    }
}
Also used : Site(org.broadleafcommerce.common.site.domain.Site) Set(java.util.Set) ArrayList(java.util.ArrayList) AdminUser(org.broadleafcommerce.openadmin.server.security.domain.AdminUser) ExtensionResultHolder(org.broadleafcommerce.common.extension.ExtensionResultHolder)

Example 30 with ExtensionResultHolder

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

the class BroadleafAdminRequestProcessor method prepareCatalog.

protected void prepareCatalog(WebRequest request, BroadleafRequestContext brc) {
    AdminUser adminUser = adminRemoteSecurityService.getPersistentAdminUser();
    if (adminUser == null) {
        // clear any catalog
        if (BLCRequestUtils.isOKtoUseSession(request)) {
            request.removeAttribute(CATALOG_REQ_PARAM, WebRequest.SCOPE_GLOBAL_SESSION);
        }
    } else {
        Catalog catalog = null;
        if (StringUtils.isNotBlank(request.getParameter(CATALOG_REQ_PARAM))) {
            Long catalogId = Long.parseLong(request.getParameter(CATALOG_REQ_PARAM));
            catalog = siteService.findCatalogById(catalogId);
            if (catalog == null) {
                throw new IllegalArgumentException(String.format("Unable to find the requested catalog: %s", catalogId));
            }
            String token = request.getParameter(staleStateProtectionService.getStateVersionTokenParameter());
            staleStateProtectionService.compareToken(token);
            staleStateProtectionService.invalidateState(true);
        }
        if (catalog == null) {
            Long previouslySetCatalogId = null;
            if (BLCRequestUtils.isOKtoUseSession(request)) {
                previouslySetCatalogId = (Long) request.getAttribute(CATALOG_REQ_PARAM, WebRequest.SCOPE_GLOBAL_SESSION);
            }
            if (previouslySetCatalogId != null) {
                catalog = siteService.findCatalogById(previouslySetCatalogId);
            }
        }
        if (catalog == null) {
            List<Catalog> catalogs = new ArrayList<Catalog>();
            if (brc.getNonPersistentSite() != null) {
                Site currentSite = siteService.retrievePersistentSiteById(brc.getNonPersistentSite().getId());
                if (extensionManager != null) {
                    ExtensionResultHolder<Set<Catalog>> catalogResult = new ExtensionResultHolder<Set<Catalog>>();
                    extensionManager.retrieveCatalogs(currentSite, catalogResult);
                    if (!CollectionUtils.isEmpty(catalogResult.getResult())) {
                        catalogs.addAll(catalogResult.getResult());
                    }
                }
            }
            if (catalogs.size() > 0) {
                catalog = catalogs.get(0);
            }
        }
        if (catalog != null) {
            if (BLCRequestUtils.isOKtoUseSession(request)) {
                request.setAttribute(CATALOG_REQ_PARAM, catalog.getId(), WebRequest.SCOPE_GLOBAL_SESSION);
            }
            brc.setCurrentCatalog(catalog);
        }
        if (extensionManager != null) {
            if (brc.getNonPersistentSite() != null) {
                Site currentSite = siteService.retrievePersistentSiteById(brc.getNonPersistentSite().getId());
                ExtensionResultHolder<Catalog> catalogResult = new ExtensionResultHolder<Catalog>();
                extensionManager.overrideCurrentCatalog(request, currentSite, catalogResult);
                if (catalogResult.getResult() != null) {
                    brc.setCurrentCatalog(catalogResult.getResult());
                }
                ExtensionResultHolder<Site> profileResult = new ExtensionResultHolder<Site>();
                extensionManager.overrideCurrentProfile(request, currentSite, profileResult);
                if (profileResult.getResult() != null) {
                    brc.setCurrentProfile(profileResult.getResult());
                }
            }
        }
    }
}
Also used : Site(org.broadleafcommerce.common.site.domain.Site) Set(java.util.Set) ArrayList(java.util.ArrayList) Catalog(org.broadleafcommerce.common.site.domain.Catalog) AdminUser(org.broadleafcommerce.openadmin.server.security.domain.AdminUser) ExtensionResultHolder(org.broadleafcommerce.common.extension.ExtensionResultHolder)

Aggregations

ExtensionResultHolder (org.broadleafcommerce.common.extension.ExtensionResultHolder)56 ExtensionResultStatusType (org.broadleafcommerce.common.extension.ExtensionResultStatusType)35 ArrayList (java.util.ArrayList)11 Product (org.broadleafcommerce.core.catalog.domain.Product)7 Sku (org.broadleafcommerce.core.catalog.domain.Sku)6 PersistenceException (org.broadleafcommerce.openadmin.server.service.persistence.PersistenceException)6 ParentEntityPersistenceException (org.broadleafcommerce.openadmin.server.service.persistence.ParentEntityPersistenceException)5 FieldNotAvailableException (org.broadleafcommerce.openadmin.server.service.persistence.module.FieldNotAvailableException)5 List (java.util.List)4 Set (java.util.Set)4 File (java.io.File)3 Serializable (java.io.Serializable)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 HashMap (java.util.HashMap)3 Query (javax.persistence.Query)3 TypedQuery (javax.persistence.TypedQuery)3 CriteriaQuery (javax.persistence.criteria.CriteriaQuery)3 AdminMainEntity (org.broadleafcommerce.common.admin.domain.AdminMainEntity)3 Site (org.broadleafcommerce.common.site.domain.Site)3 Category (org.broadleafcommerce.core.catalog.domain.Category)3