Search in sources :

Example 31 with GenericEntityException

use of org.apache.ofbiz.entity.GenericEntityException 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 32 with GenericEntityException

use of org.apache.ofbiz.entity.GenericEntityException 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)

Example 33 with GenericEntityException

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

the class ProductServices method quickAddVariant.

public static Map<String, Object> quickAddVariant(DispatchContext dctx, Map<String, ? extends Object> context) {
    Delegator delegator = dctx.getDelegator();
    Map<String, Object> result = new HashMap<>();
    Locale locale = (Locale) context.get("locale");
    String errMsg = null;
    String productId = (String) context.get("productId");
    String variantProductId = (String) context.get("productVariantId");
    String productFeatureIds = (String) context.get("productFeatureIds");
    Long prodAssocSeqNum = (Long) context.get("sequenceNum");
    try {
        // read the product, duplicate it with the given id
        GenericValue product = EntityQuery.use(delegator).from("Product").where("productId", productId).queryOne();
        if (product == null) {
            Map<String, String> messageMap = UtilMisc.toMap("productId", productId);
            errMsg = UtilProperties.getMessage(resourceError, "productservices.product_not_found_with_ID", messageMap, locale);
            result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_ERROR);
            result.put(ModelService.ERROR_MESSAGE, errMsg);
            return result;
        }
        // check if product exists
        GenericValue variantProduct = EntityQuery.use(delegator).from("Product").where("productId", variantProductId).queryOne();
        boolean variantProductExists = (variantProduct != null);
        if (variantProduct == null) {
            // if product does not exist
            variantProduct = GenericValue.create(product);
            variantProduct.set("productId", variantProductId);
            variantProduct.set("isVirtual", "N");
            variantProduct.set("isVariant", "Y");
            variantProduct.set("primaryProductCategoryId", null);
            // create new
            variantProduct.create();
        } else {
            // if product does exist
            variantProduct.set("isVirtual", "N");
            variantProduct.set("isVariant", "Y");
            variantProduct.set("primaryProductCategoryId", null);
            // update entry
            variantProduct.store();
        }
        if (variantProductExists) {
            // Since the variant product is already a variant, first of all we remove the old features
            // and the associations of type PRODUCT_VARIANT: a given product can be a variant of only one product.
            delegator.removeByAnd("ProductAssoc", UtilMisc.toMap("productIdTo", variantProductId, "productAssocTypeId", "PRODUCT_VARIANT"));
            delegator.removeByAnd("ProductFeatureAppl", UtilMisc.toMap("productId", variantProductId, "productFeatureApplTypeId", "STANDARD_FEATURE"));
        }
        // add an association from productId to variantProductId of the PRODUCT_VARIANT
        Map<String, Object> productAssocMap = UtilMisc.toMap("productId", productId, "productIdTo", variantProductId, "productAssocTypeId", "PRODUCT_VARIANT", "fromDate", UtilDateTime.nowTimestamp());
        if (prodAssocSeqNum != null) {
            productAssocMap.put("sequenceNum", prodAssocSeqNum);
        }
        GenericValue productAssoc = delegator.makeValue("ProductAssoc", productAssocMap);
        productAssoc.create();
        // add the selected standard features to the new product given the productFeatureIds
        java.util.StringTokenizer st = new java.util.StringTokenizer(productFeatureIds, "|");
        while (st.hasMoreTokens()) {
            String productFeatureId = st.nextToken();
            GenericValue productFeature = EntityQuery.use(delegator).from("ProductFeature").where("productFeatureId", productFeatureId).queryOne();
            GenericValue productFeatureAppl = delegator.makeValue("ProductFeatureAppl", UtilMisc.toMap("productId", variantProductId, "productFeatureId", productFeatureId, "productFeatureApplTypeId", "STANDARD_FEATURE", "fromDate", UtilDateTime.nowTimestamp()));
            // set the default seq num if it's there...
            if (productFeature != null) {
                productFeatureAppl.set("sequenceNum", productFeature.get("defaultSequenceNum"));
            }
            productFeatureAppl.create();
        }
    } catch (GenericEntityException e) {
        Debug.logError(e, "Entity error creating quick add variant data", module);
        Map<String, String> messageMap = UtilMisc.toMap("errMessage", e.toString());
        errMsg = UtilProperties.getMessage(resourceError, "productservices.entity_error_quick_add_variant_data", messageMap, locale);
        result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_ERROR);
        result.put(ModelService.ERROR_MESSAGE, errMsg);
        return result;
    }
    result.put("productVariantId", variantProductId);
    return result;
}
Also used : Locale(java.util.Locale) GenericValue(org.apache.ofbiz.entity.GenericValue) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Delegator(org.apache.ofbiz.entity.Delegator) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 34 with GenericEntityException

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

the class ProductServices method prodFindAssociatedByType.

/**
 * Finds associated products by product ID and association ID.
 */
public static Map<String, Object> prodFindAssociatedByType(DispatchContext dctx, Map<String, ? extends Object> context) {
    // String type -- Type of association (ie PRODUCT_UPGRADE, PRODUCT_COMPLEMENT, PRODUCT_VARIANT)
    Delegator delegator = dctx.getDelegator();
    Map<String, Object> result = new HashMap<>();
    String productId = (String) context.get("productId");
    String productIdTo = (String) context.get("productIdTo");
    String type = (String) context.get("type");
    Locale locale = (Locale) context.get("locale");
    String errMsg = null;
    Boolean cvaBool = (Boolean) context.get("checkViewAllow");
    boolean checkViewAllow = (cvaBool == null ? false : cvaBool);
    String prodCatalogId = (String) context.get("prodCatalogId");
    Boolean bidirectional = (Boolean) context.get("bidirectional");
    bidirectional = bidirectional == null ? Boolean.FALSE : bidirectional;
    Boolean sortDescending = (Boolean) context.get("sortDescending");
    sortDescending = sortDescending == null ? Boolean.FALSE : sortDescending;
    if (productId == null && productIdTo == null) {
        errMsg = UtilProperties.getMessage(resourceError, "productservices.both_productId_and_productIdTo_cannot_be_null", locale);
        result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_ERROR);
        result.put(ModelService.ERROR_MESSAGE, errMsg);
        return result;
    }
    if (productId != null && productIdTo != null) {
        errMsg = UtilProperties.getMessage(resourceError, "productservices.both_productId_and_productIdTo_cannot_be_defined", locale);
        result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_ERROR);
        result.put(ModelService.ERROR_MESSAGE, errMsg);
        return result;
    }
    productId = productId == null ? productIdTo : productId;
    GenericValue product = null;
    try {
        product = EntityQuery.use(delegator).from("Product").where("productId", productId).cache().queryOne();
    } catch (GenericEntityException e) {
        Map<String, String> messageMap = UtilMisc.toMap("errMessage", e.getMessage());
        errMsg = UtilProperties.getMessage(resourceError, "productservices.problems_reading_product_entity", messageMap, locale);
        result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_ERROR);
        result.put(ModelService.ERROR_MESSAGE, errMsg);
        return result;
    }
    if (product == null) {
        errMsg = UtilProperties.getMessage(resourceError, "productservices.problems_getting_product_entity", locale);
        result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_ERROR);
        result.put(ModelService.ERROR_MESSAGE, errMsg);
        return result;
    }
    try {
        List<GenericValue> productAssocs = null;
        List<String> orderBy = new LinkedList<>();
        if (sortDescending) {
            orderBy.add("sequenceNum DESC");
        } else {
            orderBy.add("sequenceNum");
        }
        if (bidirectional) {
            EntityCondition cond = EntityCondition.makeCondition(UtilMisc.toList(EntityCondition.makeCondition("productId", productId), EntityCondition.makeCondition("productIdTo", productId)), EntityJoinOperator.OR);
            productAssocs = EntityQuery.use(delegator).from("ProductAssoc").where(EntityCondition.makeCondition(cond, EntityCondition.makeCondition("productAssocTypeId", type))).orderBy(orderBy).cache(true).queryList();
        } else {
            if (productIdTo == null) {
                productAssocs = product.getRelated("MainProductAssoc", UtilMisc.toMap("productAssocTypeId", type), orderBy, true);
            } else {
                productAssocs = product.getRelated("AssocProductAssoc", UtilMisc.toMap("productAssocTypeId", type), orderBy, true);
            }
        }
        // filter the list by date
        productAssocs = EntityUtil.filterByDate(productAssocs);
        // first check to see if there is a view allow category and if these products are in it...
        if (checkViewAllow && prodCatalogId != null && UtilValidate.isNotEmpty(productAssocs)) {
            String viewProductCategoryId = CatalogWorker.getCatalogViewAllowCategoryId(delegator, prodCatalogId);
            if (viewProductCategoryId != null) {
                if (productIdTo == null) {
                    productAssocs = CategoryWorker.filterProductsInCategory(delegator, productAssocs, viewProductCategoryId, "productIdTo");
                } else {
                    productAssocs = CategoryWorker.filterProductsInCategory(delegator, productAssocs, viewProductCategoryId, "productId");
                }
            }
        }
        result.put("assocProducts", productAssocs);
        result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_SUCCESS);
    } catch (GenericEntityException e) {
        Map<String, String> messageMap = UtilMisc.toMap("errMessage", e.getMessage());
        errMsg = UtilProperties.getMessage(resourceError, "productservices.problems_product_association_relation_error", messageMap, locale);
        result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_ERROR);
        result.put(ModelService.ERROR_MESSAGE, errMsg);
        return result;
    }
    return result;
}
Also used : Locale(java.util.Locale) GenericValue(org.apache.ofbiz.entity.GenericValue) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) EntityCondition(org.apache.ofbiz.entity.condition.EntityCondition) LinkedList(java.util.LinkedList) Delegator(org.apache.ofbiz.entity.Delegator) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 35 with GenericEntityException

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

the class ProductServices method prodGetFeatures.

/**
 * Gets the product features of a product.
 */
public static Map<String, Object> prodGetFeatures(DispatchContext dctx, Map<String, ? extends Object> context) {
    // String type          -- Type of feature (STANDARD_FEATURE, SELECTABLE_FEATURE)
    // String distinct      -- Distinct feature (SIZE, COLOR)
    Delegator delegator = dctx.getDelegator();
    Map<String, Object> result = new HashMap<>();
    String productId = (String) context.get("productId");
    String distinct = (String) context.get("distinct");
    String type = (String) context.get("type");
    Locale locale = (Locale) context.get("locale");
    String errMsg = null;
    List<GenericValue> features = null;
    try {
        Map<String, String> fields = UtilMisc.toMap("productId", productId);
        if (distinct != null) {
            fields.put("productFeatureTypeId", distinct);
        }
        if (type != null) {
            fields.put("productFeatureApplTypeId", type);
        }
        features = EntityQuery.use(delegator).from("ProductFeatureAndAppl").where(fields).orderBy("sequenceNum", "productFeatureTypeId").cache(true).queryList();
        result.put("productFeatures", features);
        result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_SUCCESS);
    } catch (GenericEntityException e) {
        Map<String, String> messageMap = UtilMisc.toMap("errMessage", e.toString());
        errMsg = UtilProperties.getMessage(resourceError, "productservices.problem_reading_product_feature_entity", messageMap, locale);
        result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_ERROR);
        result.put(ModelService.ERROR_MESSAGE, errMsg);
    }
    return result;
}
Also used : Locale(java.util.Locale) GenericValue(org.apache.ofbiz.entity.GenericValue) Delegator(org.apache.ofbiz.entity.Delegator) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Aggregations

GenericEntityException (org.apache.ofbiz.entity.GenericEntityException)913 GenericValue (org.apache.ofbiz.entity.GenericValue)847 Delegator (org.apache.ofbiz.entity.Delegator)599 Locale (java.util.Locale)384 HashMap (java.util.HashMap)336 GenericServiceException (org.apache.ofbiz.service.GenericServiceException)270 LocalDispatcher (org.apache.ofbiz.service.LocalDispatcher)259 LinkedList (java.util.LinkedList)231 BigDecimal (java.math.BigDecimal)213 Timestamp (java.sql.Timestamp)171 Map (java.util.Map)109 GeneralException (org.apache.ofbiz.base.util.GeneralException)95 EntityCondition (org.apache.ofbiz.entity.condition.EntityCondition)78 IOException (java.io.IOException)75 EntityListIterator (org.apache.ofbiz.entity.util.EntityListIterator)57 Security (org.apache.ofbiz.security.Security)54 ArrayList (java.util.ArrayList)48 EntityExpr (org.apache.ofbiz.entity.condition.EntityExpr)47 GenericTransactionException (org.apache.ofbiz.entity.transaction.GenericTransactionException)39 LinkedHashMap (java.util.LinkedHashMap)37