Search in sources :

Example 36 with Delegator

use of org.apache.ofbiz.entity.Delegator in project ofbiz-framework by apache.

the class ProductEvents method updateProductQuickAdminSelFeat.

public static String updateProductQuickAdminSelFeat(HttpServletRequest request, HttpServletResponse response) {
    Delegator delegator = (Delegator) request.getAttribute("delegator");
    Timestamp nowTimestamp = UtilDateTime.nowTimestamp();
    String productId = request.getParameter("productId");
    String variantProductId = request.getParameter("productId0");
    String useImagesProdId = request.getParameter("useImages");
    String productFeatureTypeId = request.getParameter("productFeatureTypeId");
    if (UtilValidate.isEmpty(productFeatureTypeId)) {
        String errMsg = "Error: please select a ProductFeature Type to add or update variant features.";
        request.setAttribute("_ERROR_MESSAGE_", errMsg);
        return "error";
    }
    try {
        boolean beganTransaction = TransactionUtil.begin();
        try {
            GenericValue productFeatureType = EntityQuery.use(delegator).from("ProductFeatureType").where("productFeatureTypeId", productFeatureTypeId).queryOne();
            if (productFeatureType == null) {
                String errMsg = "Error: the ProductFeature Type specified was not valid and one is require to add or update variant features.";
                request.setAttribute("_ERROR_MESSAGE_", errMsg);
                return "error";
            }
            // check for variantProductId - this will mean that we have multiple variants to update
            if (variantProductId != null) {
                // multiple products, so use a numeric suffix to get them all
                int attribIdx = 0;
                GenericValue product = EntityQuery.use(delegator).from("Product").where("productId", productId).queryOne();
                do {
                    GenericValue variantProduct = EntityQuery.use(delegator).from("Product").where("productId", variantProductId).queryOne();
                    String description = request.getParameter("description" + attribIdx);
                    // blank means null, which means delete the feature application
                    if ((description != null) && (description.trim().length() < 1)) {
                        description = null;
                    }
                    Set<String> variantDescRemoveToRemoveOnVirtual = new HashSet<>();
                    checkUpdateFeatureApplByDescription(variantProductId, variantProduct, description, productFeatureTypeId, productFeatureType, "STANDARD_FEATURE", nowTimestamp, delegator, null, variantDescRemoveToRemoveOnVirtual);
                    checkUpdateFeatureApplByDescription(productId, product, description, productFeatureTypeId, productFeatureType, "SELECTABLE_FEATURE", nowTimestamp, delegator, variantDescRemoveToRemoveOnVirtual, null);
                    // update image urls
                    if ((useImagesProdId != null) && (useImagesProdId.equals(variantProductId))) {
                        product.set("smallImageUrl", variantProduct.getString("smallImageUrl"));
                        product.set("mediumImageUrl", variantProduct.getString("mediumImageUrl"));
                        product.set("largeImageUrl", null);
                        product.set("detailImageUrl", null);
                        product.store();
                    }
                    attribIdx++;
                    variantProductId = request.getParameter("productId" + attribIdx);
                } while (variantProductId != null);
            }
            TransactionUtil.commit(beganTransaction);
        } catch (GenericEntityException e) {
            String errMsg = "Error updating quick admin selectable feature settings: " + e.toString();
            Debug.logError(e, errMsg, module);
            request.setAttribute("_ERROR_MESSAGE_", errMsg);
            TransactionUtil.rollback(beganTransaction, errMsg, e);
            return "error";
        }
    } catch (GenericTransactionException gte) {
        String errMsg = "Error updating quick admin selectable feature settings: " + gte.toString();
        Debug.logError(gte, errMsg, module);
        request.setAttribute("_ERROR_MESSAGE_", errMsg);
        return "error";
    }
    return "success";
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) Delegator(org.apache.ofbiz.entity.Delegator) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) GenericTransactionException(org.apache.ofbiz.entity.transaction.GenericTransactionException) Timestamp(java.sql.Timestamp) HashSet(java.util.HashSet)

Example 37 with Delegator

use of org.apache.ofbiz.entity.Delegator in project ofbiz-framework by apache.

the class ProductEvents method checkStoreCustomerRole.

/**
 * If ProductStore.requireCustomerRole == Y then the loggedin user must be associated with the store in the customer role.
 * This event method is called from the ProductEvents.storeCheckLogin and ProductEvents.storeLogin
 *
 * @param request
 * @param response
 * @return String with response, maybe "success" or "error" if logged in user is not associated with the ProductStore in the CUSTOMER role.
 */
public static String checkStoreCustomerRole(HttpServletRequest request, HttpServletResponse response) {
    HttpSession session = request.getSession();
    Delegator delegator = (Delegator) request.getAttribute("delegator");
    GenericValue userLogin = (GenericValue) session.getAttribute("userLogin");
    GenericValue productStore = ProductStoreWorker.getProductStore(request);
    if (productStore != null && userLogin != null) {
        if ("Y".equals(productStore.getString("requireCustomerRole"))) {
            List<GenericValue> productStoreRoleList = null;
            try {
                productStoreRoleList = EntityQuery.use(delegator).from("ProductStoreRole").where("productStoreId", productStore.get("productStoreId"), "partyId", userLogin.get("partyId"), "roleTypeId", "CUSTOMER").filterByDate().queryList();
            } catch (GenericEntityException e) {
                Debug.logError(e, "Database error finding CUSTOMER ProductStoreRole records, required by the ProductStore with ID [" + productStore.getString("productStoreId") + "]", module);
            }
            if (UtilValidate.isEmpty(productStoreRoleList)) {
                // uh-oh, this user isn't associated...
                String errorMsg = "The " + productStore.getString("storeName") + " [" + productStore.getString("productStoreId") + "] ProductStore requires that customers be associated with it, and the logged in user is NOT associated with it in the CUSTOMER role; userLoginId=[" + userLogin.getString("userLoginId") + "], partyId=[" + userLogin.getString("partyId") + "]";
                Debug.logWarning(errorMsg, module);
                request.setAttribute("_ERROR_MESSAGE_", errorMsg);
                session.removeAttribute("userLogin");
                session.removeAttribute("autoUserLogin");
                return "error";
            }
        }
    }
    return "success";
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) Delegator(org.apache.ofbiz.entity.Delegator) HttpSession(javax.servlet.http.HttpSession) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException)

Example 38 with Delegator

use of org.apache.ofbiz.entity.Delegator in project ofbiz-framework by apache.

the class ProductPromoContentWrapper method getProductPromoContentAsText.

public static String getProductPromoContentAsText(GenericValue productPromo, String productPromoContentTypeId, HttpServletRequest request, String encoderType) {
    LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher");
    Delegator delegator = (Delegator) request.getAttribute("delegator");
    return getProductPromoContentAsText(productPromo, productPromoContentTypeId, UtilHttp.getLocale(request), EntityUtilProperties.getPropertyValue("content", "defaultMimeType", "text/html; charset=utf-8", delegator), null, null, productPromo.getDelegator(), dispatcher, encoderType);
}
Also used : LocalDispatcher(org.apache.ofbiz.service.LocalDispatcher) Delegator(org.apache.ofbiz.entity.Delegator)

Example 39 with Delegator

use of org.apache.ofbiz.entity.Delegator in project ofbiz-framework by apache.

the class ProductSearchSession method processSearchParameters.

public static void processSearchParameters(Map<String, Object> parameters, HttpServletRequest request) {
    Delegator delegator = (Delegator) request.getAttribute("delegator");
    Boolean alreadyRun = (Boolean) request.getAttribute("processSearchParametersAlreadyRun");
    if (Boolean.TRUE.equals(alreadyRun)) {
        return;
    }
    request.setAttribute("processSearchParametersAlreadyRun", Boolean.TRUE);
    HttpSession session = request.getSession();
    boolean constraintsChanged = false;
    GenericValue productStore = ProductStoreWorker.getProductStore(request);
    // clear search? by default yes, but if the clearSearch parameter is N then don't
    String clearSearchString = (String) parameters.get("clearSearch");
    if (!"N".equals(clearSearchString)) {
        searchClear(session);
        constraintsChanged = true;
    } else {
        String removeConstraint = (String) parameters.get("removeConstraint");
        if (UtilValidate.isNotEmpty(removeConstraint)) {
            try {
                searchRemoveConstraint(Integer.parseInt(removeConstraint), session);
                constraintsChanged = true;
            } catch (Exception e) {
                Debug.logError(e, "Error removing constraint [" + removeConstraint + "]", module);
            }
        }
    }
    String prioritizeCategoryId = null;
    if (UtilValidate.isNotEmpty(parameters.get("PRIORITIZE_CATEGORY_ID"))) {
        prioritizeCategoryId = (String) parameters.get("PRIORITIZE_CATEGORY_ID");
    } else if (UtilValidate.isNotEmpty(parameters.get("S_TPC"))) {
        prioritizeCategoryId = (String) parameters.get("S_TPC");
    }
    if (UtilValidate.isNotEmpty(prioritizeCategoryId)) {
        ProductSearchOptions.setTopProductCategoryId(prioritizeCategoryId, session);
        constraintsChanged = true;
    }
    // if there is another category, add a constraint for it
    if (UtilValidate.isNotEmpty(parameters.get("SEARCH_CATEGORY_ID"))) {
        String searchCategoryId = (String) parameters.get("SEARCH_CATEGORY_ID");
        String searchSubCategories = (String) parameters.get("SEARCH_SUB_CATEGORIES");
        String searchCategoryExc = (String) parameters.get("SEARCH_CATEGORY_EXC");
        Boolean exclude = UtilValidate.isEmpty(searchCategoryExc) ? null : Boolean.valueOf(!"N".equals(searchCategoryExc));
        searchAddConstraint(new ProductSearch.CategoryConstraint(searchCategoryId, !"N".equals(searchSubCategories), exclude), session);
        constraintsChanged = true;
    }
    for (int catNum = 1; catNum < 10; catNum++) {
        if (UtilValidate.isNotEmpty(parameters.get("SEARCH_CATEGORY_ID" + catNum))) {
            String searchCategoryId = (String) parameters.get("SEARCH_CATEGORY_ID" + catNum);
            String searchSubCategories = (String) parameters.get("SEARCH_SUB_CATEGORIES" + catNum);
            String searchCategoryExc = (String) parameters.get("SEARCH_CATEGORY_EXC" + catNum);
            Boolean exclude = UtilValidate.isEmpty(searchCategoryExc) ? null : Boolean.valueOf(!"N".equals(searchCategoryExc));
            searchAddConstraint(new ProductSearch.CategoryConstraint(searchCategoryId, !"N".equals(searchSubCategories), exclude), session);
            constraintsChanged = true;
        }
    }
    // a shorter variation for categories
    for (int catNum = 1; catNum < 10; catNum++) {
        if (UtilValidate.isNotEmpty(parameters.get("S_CAT" + catNum))) {
            String searchCategoryId = (String) parameters.get("S_CAT" + catNum);
            String searchSubCategories = (String) parameters.get("S_CSB" + catNum);
            String searchCategoryExc = (String) parameters.get("S_CEX" + catNum);
            Boolean exclude = UtilValidate.isEmpty(searchCategoryExc) ? null : Boolean.valueOf(!"N".equals(searchCategoryExc));
            searchAddConstraint(new ProductSearch.CategoryConstraint(searchCategoryId, !"N".equals(searchSubCategories), exclude), session);
            constraintsChanged = true;
        }
    }
    // if there is any category selected try to use catalog and add a constraint for it
    if (UtilValidate.isNotEmpty(parameters.get("SEARCH_CATALOG_ID"))) {
        String searchCatalogId = (String) parameters.get("SEARCH_CATALOG_ID");
        if (searchCatalogId != null && !searchCatalogId.equalsIgnoreCase("")) {
            String topCategory = CatalogWorker.getCatalogTopCategoryId(request, searchCatalogId);
            if (UtilValidate.isEmpty(topCategory)) {
                topCategory = CatalogWorker.getCatalogTopEbayCategoryId(request, searchCatalogId);
            }
            List<GenericValue> categories = CategoryWorker.getRelatedCategoriesRet(request, "topLevelList", topCategory, true, false, true);
            searchAddConstraint(new ProductSearch.CatalogConstraint(searchCatalogId, categories), session);
            constraintsChanged = true;
        }
    }
    // if keywords were specified, add a constraint for them
    if (UtilValidate.isNotEmpty(parameters.get("SEARCH_STRING"))) {
        String keywordString = (String) parameters.get("SEARCH_STRING");
        String searchOperator = (String) parameters.get("SEARCH_OPERATOR");
        // defaults to true/Y, ie anything but N is true/Y
        boolean anyPrefixSuffix = !"N".equals(parameters.get("SEARCH_ANYPRESUF"));
        searchAddConstraint(new ProductSearch.KeywordConstraint(keywordString, anyPrefixSuffix, anyPrefixSuffix, null, "AND".equals(searchOperator)), session);
        constraintsChanged = true;
    }
    // if productName were specified, add a constraint for them
    if (UtilValidate.isNotEmpty(parameters.get("SEARCH_PRODUCT_NAME"))) {
        String productName = (String) parameters.get("SEARCH_PRODUCT_NAME");
        searchAddConstraint(new ProductSearch.ProductFieldConstraint(productName, "productName"), session);
        constraintsChanged = true;
    }
    // if internalName were specified, add a constraint for them
    if (UtilValidate.isNotEmpty(parameters.get("SEARCH_INTERNAL_PROD_NAME"))) {
        String internalName = (String) parameters.get("SEARCH_INTERNAL_PROD_NAME");
        searchAddConstraint(new ProductSearch.ProductFieldConstraint(internalName, "internalName"), session);
        constraintsChanged = true;
    }
    for (int kwNum = 1; kwNum < 10; kwNum++) {
        if (UtilValidate.isNotEmpty(parameters.get("SEARCH_STRING" + kwNum))) {
            String keywordString = (String) parameters.get("SEARCH_STRING" + kwNum);
            String searchOperator = (String) parameters.get("SEARCH_OPERATOR" + kwNum);
            // defaults to true/Y, ie anything but N is true/Y
            boolean anyPrefixSuffix = !"N".equals(parameters.get("SEARCH_ANYPRESUF" + kwNum));
            searchAddConstraint(new ProductSearch.KeywordConstraint(keywordString, anyPrefixSuffix, anyPrefixSuffix, null, "AND".equals(searchOperator)), session);
            constraintsChanged = true;
        }
    }
    for (Entry<String, Object> entry : parameters.entrySet()) {
        String parameterName = entry.getKey();
        if (parameterName.startsWith("SEARCH_FEAT") && !parameterName.startsWith("SEARCH_FEAT_EXC")) {
            String productFeatureId = (String) parameters.get(parameterName);
            if (UtilValidate.isNotEmpty(productFeatureId)) {
                String paramNameExt = parameterName.substring("SEARCH_FEAT".length());
                String searchCategoryExc = (String) parameters.get("SEARCH_FEAT_EXC" + paramNameExt);
                Boolean exclude = UtilValidate.isEmpty(searchCategoryExc) ? null : Boolean.valueOf(!"N".equals(searchCategoryExc));
                // Debug.logInfo("parameterName=" + parameterName + ", paramNameExt=" + paramNameExt + ", searchCategoryExc=" + searchCategoryExc + ", exclude=" + exclude, module);
                searchAddConstraint(new ProductSearch.FeatureConstraint(productFeatureId, exclude), session);
                constraintsChanged = true;
            }
        }
        // a shorter feature variation
        if (parameterName.startsWith("S_PFI")) {
            String productFeatureId = (String) parameters.get(parameterName);
            if (UtilValidate.isNotEmpty(productFeatureId)) {
                String paramNameExt = parameterName.substring("S_PFI".length());
                String searchCategoryExc = (String) parameters.get("S_PFX" + paramNameExt);
                Boolean exclude = UtilValidate.isEmpty(searchCategoryExc) ? null : Boolean.valueOf(!"N".equals(searchCategoryExc));
                searchAddConstraint(new ProductSearch.FeatureConstraint(productFeatureId, exclude), session);
                constraintsChanged = true;
            }
        }
        // if product features category were selected add a constraint for each
        if (parameterName.startsWith("SEARCH_PROD_FEAT_CAT") && !parameterName.startsWith("SEARCH_PROD_FEAT_CAT_EXC")) {
            String productFeatureCategoryId = (String) parameters.get(parameterName);
            if (UtilValidate.isNotEmpty(productFeatureCategoryId)) {
                String paramNameExt = parameterName.substring("SEARCH_PROD_FEAT_CAT".length());
                String searchProdFeatureCategoryExc = (String) parameters.get("SEARCH_PROD_FEAT_CAT_EXC" + paramNameExt);
                Boolean exclude = UtilValidate.isEmpty(searchProdFeatureCategoryExc) ? null : Boolean.valueOf(!"N".equals(searchProdFeatureCategoryExc));
                searchAddConstraint(new ProductSearch.FeatureCategoryConstraint(productFeatureCategoryId, exclude), session);
                constraintsChanged = true;
            }
        }
        // a shorter variation for feature category
        if (parameterName.startsWith("S_FCI")) {
            String productFeatureCategoryId = (String) parameters.get(parameterName);
            if (UtilValidate.isNotEmpty(productFeatureCategoryId)) {
                String paramNameExt = parameterName.substring("S_FCI".length());
                String searchProdFeatureCategoryExc = (String) parameters.get("S_FCX" + paramNameExt);
                Boolean exclude = UtilValidate.isEmpty(searchProdFeatureCategoryExc) ? null : Boolean.valueOf(!"N".equals(searchProdFeatureCategoryExc));
                searchAddConstraint(new ProductSearch.FeatureCategoryConstraint(productFeatureCategoryId, exclude), session);
                constraintsChanged = true;
            }
        }
        // if product features group were selected add a constraint for each
        if (parameterName.startsWith("SEARCH_PROD_FEAT_GRP") && !parameterName.startsWith("SEARCH_PROD_FEAT_GRP_EXC")) {
            String productFeatureGroupId = (String) parameters.get(parameterName);
            if (UtilValidate.isNotEmpty(productFeatureGroupId)) {
                String paramNameExt = parameterName.substring("SEARCH_PROD_FEAT_GRP".length());
                String searchProdFeatureGroupExc = (String) parameters.get("SEARCH_PROD_FEAT_GRP_EXC" + paramNameExt);
                Boolean exclude = UtilValidate.isEmpty(searchProdFeatureGroupExc) ? null : Boolean.valueOf(!"N".equals(searchProdFeatureGroupExc));
                searchAddConstraint(new ProductSearch.FeatureGroupConstraint(productFeatureGroupId, exclude), session);
                constraintsChanged = true;
            }
        }
        // a shorter variation for feature group
        if (parameterName.startsWith("S_FGI")) {
            String productFeatureGroupId = (String) parameters.get(parameterName);
            if (UtilValidate.isNotEmpty(productFeatureGroupId)) {
                String paramNameExt = parameterName.substring("S_FGI".length());
                String searchProdFeatureGroupExc = (String) parameters.get("S_FGX" + paramNameExt);
                Boolean exclude = UtilValidate.isEmpty(searchProdFeatureGroupExc) ? null : Boolean.valueOf(!"N".equals(searchProdFeatureGroupExc));
                searchAddConstraint(new ProductSearch.FeatureGroupConstraint(productFeatureGroupId, exclude), session);
                constraintsChanged = true;
            }
        }
    }
    // if features were selected add a constraint for each
    Map<String, String> featureIdByType = ParametricSearch.makeFeatureIdByTypeMap(parameters);
    if (featureIdByType.size() > 0) {
        constraintsChanged = true;
        searchAddFeatureIdConstraints(featureIdByType.values(), null, request);
    }
    // add a supplier to the search
    if (UtilValidate.isNotEmpty(parameters.get("SEARCH_SUPPLIER_ID")) || UtilValidate.isNotEmpty(parameters.get("S_SUP"))) {
        String supplierPartyId = (String) parameters.get("SEARCH_SUPPLIER_ID");
        if (UtilValidate.isEmpty(supplierPartyId)) {
            supplierPartyId = (String) parameters.get("S_SUP");
        }
        searchAddConstraint(new ProductSearch.SupplierConstraint(supplierPartyId), session);
        constraintsChanged = true;
    }
    // add a list price range to the search
    if (UtilValidate.isNotEmpty(parameters.get("LIST_PRICE_LOW")) || UtilValidate.isNotEmpty(parameters.get("LIST_PRICE_HIGH"))) {
        BigDecimal listPriceLow = null;
        BigDecimal listPriceHigh = null;
        String listPriceCurrency = UtilHttp.getCurrencyUom(request);
        if (UtilValidate.isNotEmpty(parameters.get("LIST_PRICE_LOW"))) {
            try {
                listPriceLow = new BigDecimal((String) parameters.get("LIST_PRICE_LOW"));
            } catch (NumberFormatException e) {
                Debug.logError("Error parsing LIST_PRICE_LOW parameter [" + (String) parameters.get("LIST_PRICE_LOW") + "]: " + e.toString(), module);
            }
        }
        if (UtilValidate.isNotEmpty(parameters.get("LIST_PRICE_HIGH"))) {
            try {
                listPriceHigh = new BigDecimal((String) parameters.get("LIST_PRICE_HIGH"));
            } catch (NumberFormatException e) {
                Debug.logError("Error parsing LIST_PRICE_HIGH parameter [" + (String) parameters.get("LIST_PRICE_HIGH") + "]: " + e.toString(), module);
            }
        }
        searchAddConstraint(new ProductSearch.ListPriceRangeConstraint(listPriceLow, listPriceHigh, listPriceCurrency), session);
        constraintsChanged = true;
    }
    if (UtilValidate.isNotEmpty(parameters.get("LIST_PRICE_RANGE")) || UtilValidate.isNotEmpty(parameters.get("S_LPR"))) {
        String listPriceRangeStr = (String) parameters.get("LIST_PRICE_RANGE");
        if (UtilValidate.isEmpty(listPriceRangeStr)) {
            listPriceRangeStr = (String) parameters.get("S_LPR");
        }
        int underscoreIndex = listPriceRangeStr.indexOf('_');
        String listPriceLowStr;
        String listPriceHighStr;
        if (underscoreIndex >= 0) {
            listPriceLowStr = listPriceRangeStr.substring(0, listPriceRangeStr.indexOf('_'));
            listPriceHighStr = listPriceRangeStr.substring(listPriceRangeStr.indexOf('_') + 1);
        } else {
            // no underscore: assume it is a low range with no high range, ie the ending underscore was left off
            listPriceLowStr = listPriceRangeStr;
            listPriceHighStr = null;
        }
        BigDecimal listPriceLow = null;
        BigDecimal listPriceHigh = null;
        String listPriceCurrency = UtilHttp.getCurrencyUom(request);
        if (UtilValidate.isNotEmpty(listPriceLowStr)) {
            try {
                listPriceLow = new BigDecimal(listPriceLowStr);
            } catch (NumberFormatException e) {
                Debug.logError("Error parsing low part of LIST_PRICE_RANGE parameter [" + listPriceLowStr + "]: " + e.toString(), module);
            }
        }
        if (UtilValidate.isNotEmpty(listPriceHighStr)) {
            try {
                listPriceHigh = new BigDecimal(listPriceHighStr);
            } catch (NumberFormatException e) {
                Debug.logError("Error parsing high part of LIST_PRICE_RANGE parameter [" + listPriceHighStr + "]: " + e.toString(), module);
            }
        }
        searchAddConstraint(new ProductSearch.ListPriceRangeConstraint(listPriceLow, listPriceHigh, listPriceCurrency), session);
        constraintsChanged = true;
    }
    // check the ProductStore to see if we should add the ExcludeVariantsConstraint
    if (productStore != null && !"N".equals(productStore.getString("prodSearchExcludeVariants"))) {
        searchAddConstraint(new ProductSearch.ExcludeVariantsConstraint(), session);
    // not consider this a change for now, shouldn't change often: constraintsChanged = true;
    }
    if ("true".equalsIgnoreCase((String) parameters.get("AVAILABILITY_FILTER"))) {
        searchAddConstraint(new ProductSearch.AvailabilityDateConstraint(), session);
        constraintsChanged = true;
    }
    if (UtilValidate.isNotEmpty(parameters.get("SEARCH_GOOD_IDENTIFICATION_TYPE")) || UtilValidate.isNotEmpty(parameters.get("SEARCH_GOOD_IDENTIFICATION_VALUE"))) {
        String include = (String) parameters.get("SEARCH_GOOD_IDENTIFICATION_INCL");
        if (UtilValidate.isEmpty(include)) {
            include = "Y";
        }
        Boolean inc = Boolean.TRUE;
        if ("N".equalsIgnoreCase(include)) {
            inc = Boolean.FALSE;
        }
        searchAddConstraint(new ProductSearch.GoodIdentificationConstraint((String) parameters.get("SEARCH_GOOD_IDENTIFICATION_TYPE"), (String) parameters.get("SEARCH_GOOD_IDENTIFICATION_VALUE"), inc), session);
        constraintsChanged = true;
    }
    String prodCatalogId = CatalogWorker.getCurrentCatalogId(request);
    String viewProductCategoryId = CatalogWorker.getCatalogViewAllowCategoryId(delegator, prodCatalogId);
    if (UtilValidate.isNotEmpty(viewProductCategoryId)) {
        ProductSearchConstraint viewAllowConstraint = new CategoryConstraint(viewProductCategoryId, true, null);
        searchAddConstraint(viewAllowConstraint, session);
    // not consider this a change for now, shouldn't change often: constraintsChanged = true;
    }
    // set the sort order
    String sortOrder = (String) parameters.get("sortOrder");
    if (UtilValidate.isEmpty(sortOrder)) {
        sortOrder = (String) parameters.get("S_O");
    }
    String sortAscending = (String) parameters.get("sortAscending");
    if (UtilValidate.isEmpty(sortAscending)) {
        sortAscending = (String) parameters.get("S_A");
    }
    boolean ascending = !"N".equals(sortAscending);
    if (sortOrder != null) {
        if ("SortKeywordRelevancy".equals(sortOrder) || "SKR".equals(sortOrder)) {
            searchSetSortOrder(new ProductSearch.SortKeywordRelevancy(), session);
        } else if (sortOrder.startsWith("SortProductField:")) {
            String fieldName = sortOrder.substring("SortProductField:".length());
            searchSetSortOrder(new ProductSearch.SortProductField(fieldName, ascending), session);
        } else if (sortOrder.startsWith("SPF:")) {
            String fieldName = sortOrder.substring("SPF:".length());
            searchSetSortOrder(new ProductSearch.SortProductField(fieldName, ascending), session);
        } else if (sortOrder.startsWith("SortProductPrice:")) {
            String priceTypeId = sortOrder.substring("SortProductPrice:".length());
            searchSetSortOrder(new ProductSearch.SortProductPrice(priceTypeId, ascending), session);
        } else if (sortOrder.startsWith("SPP:")) {
            String priceTypeId = sortOrder.substring("SPP:".length());
            searchSetSortOrder(new ProductSearch.SortProductPrice(priceTypeId, ascending), session);
        } else if (sortOrder.startsWith("SortProductFeature:")) {
            String featureId = sortOrder.substring("SortProductFeature:".length());
            searchSetSortOrder(new ProductSearch.SortProductFeature(featureId, ascending), session);
        } else if (sortOrder.startsWith("SPFT:")) {
            String priceTypeId = sortOrder.substring("SPFT:".length());
            searchSetSortOrder(new ProductSearch.SortProductPrice(priceTypeId, ascending), session);
        }
    }
    ProductSearchOptions productSearchOptions = getProductSearchOptions(session);
    if (constraintsChanged) {
        // query changed, clear out the VIEW_INDEX & VIEW_SIZE
        productSearchOptions.clearViewInfo();
    }
    productSearchOptions.setViewIndex((String) parameters.get("VIEW_INDEX"));
    productSearchOptions.setViewSize((String) parameters.get("VIEW_SIZE"));
    productSearchOptions.setPaging((String) parameters.get("PAGING"));
}
Also used : KeywordConstraint(org.apache.ofbiz.product.product.ProductSearch.KeywordConstraint) GenericValue(org.apache.ofbiz.entity.GenericValue) HttpSession(javax.servlet.http.HttpSession) FeatureConstraint(org.apache.ofbiz.product.product.ProductSearch.FeatureConstraint) IOException(java.io.IOException) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) CategoryConstraint(org.apache.ofbiz.product.product.ProductSearch.CategoryConstraint) ProductSearchConstraint(org.apache.ofbiz.product.product.ProductSearch.ProductSearchConstraint) FeatureConstraint(org.apache.ofbiz.product.product.ProductSearch.FeatureConstraint) CategoryConstraint(org.apache.ofbiz.product.product.ProductSearch.CategoryConstraint) KeywordConstraint(org.apache.ofbiz.product.product.ProductSearch.KeywordConstraint) BigDecimal(java.math.BigDecimal) Delegator(org.apache.ofbiz.entity.Delegator) SortKeywordRelevancy(org.apache.ofbiz.product.product.ProductSearch.SortKeywordRelevancy) CategoryConstraint(org.apache.ofbiz.product.product.ProductSearch.CategoryConstraint) ProductSearchConstraint(org.apache.ofbiz.product.product.ProductSearch.ProductSearchConstraint)

Example 40 with Delegator

use of org.apache.ofbiz.entity.Delegator in project ofbiz-framework by apache.

the class ProductSearchEvents method searchAddToCategory.

/**
 *  Adds the results of a search to the specified catogory
 *@param request The HTTPRequest object for the current request
 *@param response The HTTPResponse object for the current request
 *@return String specifying the exit status of this event
 */
public static String searchAddToCategory(HttpServletRequest request, HttpServletResponse response) {
    Delegator delegator = (Delegator) request.getAttribute("delegator");
    String productCategoryId = request.getParameter("SE_SEARCH_CATEGORY_ID");
    String fromDateStr = request.getParameter("fromDate");
    Timestamp fromDate = null;
    String errMsg = null;
    try {
        fromDate = Timestamp.valueOf(fromDateStr);
    } catch (RuntimeException e) {
        Map<String, String> messageMap = UtilMisc.toMap("errDateFormat", e.toString());
        errMsg = UtilProperties.getMessage(resource, "productsearchevents.fromDate_not_formatted_properly", messageMap, UtilHttp.getLocale(request));
        request.setAttribute("_ERROR_MESSAGE_", errMsg);
        return "error";
    }
    try {
        boolean beganTransaction = TransactionUtil.begin(DEFAULT_TX_TIMEOUT);
        try (EntityListIterator eli = getProductSearchResults(request)) {
            if (eli == null) {
                errMsg = UtilProperties.getMessage(resource, "productsearchevents.no_results_found_probably_error_constraints", UtilHttp.getLocale(request));
                request.setAttribute("_ERROR_MESSAGE_", errMsg);
                return "error";
            }
            GenericValue searchResultView = null;
            int numAdded = 0;
            while ((searchResultView = eli.next()) != null) {
                String productId = searchResultView.getString("mainProductId");
                GenericValue pcm = delegator.makeValue("ProductCategoryMember");
                pcm.set("productCategoryId", productCategoryId);
                pcm.set("productId", productId);
                pcm.set("fromDate", fromDate);
                pcm.create();
                numAdded++;
            }
            Map<String, String> messageMap = UtilMisc.toMap("numAdded", Integer.toString(numAdded));
            errMsg = UtilProperties.getMessage(resource, "productsearchevents.added_x_product_category_members", messageMap, UtilHttp.getLocale(request));
            request.setAttribute("_EVENT_MESSAGE_", errMsg);
        } catch (GenericEntityException e) {
            Map<String, String> messageMap = UtilMisc.toMap("errSearchResult", e.toString());
            errMsg = UtilProperties.getMessage(resource, "productsearchevents.error_getting_search_results", messageMap, UtilHttp.getLocale(request));
            Debug.logError(e, errMsg, module);
            request.setAttribute("_ERROR_MESSAGE_", errMsg);
            TransactionUtil.rollback(beganTransaction, errMsg, e);
            return "error";
        } finally {
            TransactionUtil.commit(beganTransaction);
        }
    } catch (GenericTransactionException e) {
        Map<String, String> messageMap = UtilMisc.toMap("errSearchResult", e.toString());
        errMsg = UtilProperties.getMessage(resource, "productsearchevents.error_getting_search_results", messageMap, UtilHttp.getLocale(request));
        Debug.logError(e, errMsg, module);
        request.setAttribute("_ERROR_MESSAGE_", errMsg);
        return "error";
    }
    return "success";
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) Delegator(org.apache.ofbiz.entity.Delegator) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) GenericTransactionException(org.apache.ofbiz.entity.transaction.GenericTransactionException) EntityListIterator(org.apache.ofbiz.entity.util.EntityListIterator) Timestamp(java.sql.Timestamp) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

Delegator (org.apache.ofbiz.entity.Delegator)869 GenericValue (org.apache.ofbiz.entity.GenericValue)721 GenericEntityException (org.apache.ofbiz.entity.GenericEntityException)611 Locale (java.util.Locale)485 HashMap (java.util.HashMap)328 LocalDispatcher (org.apache.ofbiz.service.LocalDispatcher)324 GenericServiceException (org.apache.ofbiz.service.GenericServiceException)278 BigDecimal (java.math.BigDecimal)205 LinkedList (java.util.LinkedList)166 Timestamp (java.sql.Timestamp)163 GeneralException (org.apache.ofbiz.base.util.GeneralException)130 IOException (java.io.IOException)117 Map (java.util.Map)113 EntityCondition (org.apache.ofbiz.entity.condition.EntityCondition)61 Security (org.apache.ofbiz.security.Security)60 HttpSession (javax.servlet.http.HttpSession)59 Properties (java.util.Properties)37 UtilProperties (org.apache.ofbiz.base.util.UtilProperties)37 EntityUtilProperties (org.apache.ofbiz.entity.util.EntityUtilProperties)35 EntityListIterator (org.apache.ofbiz.entity.util.EntityListIterator)33