Search in sources :

Example 56 with Delegator

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

the class ProductUtilServices method discVirtualsWithDiscVariants.

/**
 * First expire all ProductAssocs for all disc variants, then disc all virtuals that have all expired variant ProductAssocs
 */
public static Map<String, Object> discVirtualsWithDiscVariants(DispatchContext dctx, Map<String, ? extends Object> context) {
    Delegator delegator = dctx.getDelegator();
    Timestamp nowTimestamp = UtilDateTime.nowTimestamp();
    Locale locale = (Locale) context.get("locale");
    String errMsg = null;
    EntityCondition conditionOne = EntityCondition.makeCondition(UtilMisc.toList(EntityCondition.makeCondition("isVariant", EntityOperator.EQUALS, "Y"), EntityCondition.makeCondition("salesDiscontinuationDate", EntityOperator.NOT_EQUAL, null), EntityCondition.makeCondition("salesDiscontinuationDate", EntityOperator.LESS_THAN_EQUAL_TO, nowTimestamp)), EntityOperator.AND);
    try (EntityListIterator eliOne = EntityQuery.use(delegator).from("Product").where(conditionOne).queryIterator()) {
        GenericValue productOne = null;
        int numSoFarOne = 0;
        while ((productOne = eliOne.next()) != null) {
            String virtualProductId = ProductWorker.getVariantVirtualId(productOne);
            GenericValue virtualProduct = EntityQuery.use(delegator).from("Product").where("productId", virtualProductId).queryOne();
            if (virtualProduct == null) {
                continue;
            }
            List<GenericValue> passocList = EntityQuery.use(delegator).from("ProductAssoc").where("productId", virtualProductId, "productIdTo", productOne.get("productId"), "productAssocTypeId", "PRODUCT_VARIANT").filterByDate().queryList();
            if (passocList.size() > 0) {
                for (GenericValue passoc : passocList) {
                    passoc.set("thruDate", nowTimestamp);
                    passoc.store();
                }
                numSoFarOne++;
                if (numSoFarOne % 500 == 0) {
                    Debug.logInfo("Expired variant ProductAssocs for " + numSoFarOne + " sales discontinued variant products.", module);
                }
            }
        }
        // get all non-discontinued virtuals, see if all variant ProductAssocs are expired, if discontinue
        EntityCondition condition = EntityCondition.makeCondition(UtilMisc.toList(EntityCondition.makeCondition("isVirtual", EntityOperator.EQUALS, "Y"), EntityCondition.makeCondition(EntityCondition.makeCondition("salesDiscontinuationDate", EntityOperator.EQUALS, null), EntityOperator.OR, EntityCondition.makeCondition("salesDiscontinuationDate", EntityOperator.GREATER_THAN_EQUAL_TO, nowTimestamp))), EntityOperator.AND);
        try (EntityListIterator eli = EntityQuery.use(delegator).from("Product").where(condition).queryIterator()) {
            GenericValue product = null;
            int numSoFar = 0;
            while ((product = eli.next()) != null) {
                List<GenericValue> passocList = EntityQuery.use(delegator).from("ProductAssoc").where("productId", product.get("productId"), "productAssocTypeId", "PRODUCT_VARIANT").filterByDate().queryList();
                if (passocList.size() == 0) {
                    product.set("salesDiscontinuationDate", nowTimestamp);
                    delegator.store(product);
                    numSoFar++;
                    if (numSoFar % 500 == 0) {
                        Debug.logInfo("Sales discontinued " + numSoFar + " virtual products that have no valid variants.", module);
                    }
                }
            }
        } catch (GenericEntityException e) {
            Map<String, String> messageMap = UtilMisc.toMap("errMessage", e.toString());
            errMsg = UtilProperties.getMessage(resourceError, "productutilservices.entity_error_running_discVirtualsWithDiscVariants", messageMap, locale);
            Debug.logError(e, errMsg, module);
            return ServiceUtil.returnError(errMsg);
        }
    } catch (GenericEntityException e) {
        Map<String, String> messageMap = UtilMisc.toMap("errMessage", e.toString());
        errMsg = UtilProperties.getMessage(resourceError, "productutilservices.entity_error_running_discVirtualsWithDiscVariants", messageMap, locale);
        Debug.logError(e, errMsg, module);
        return ServiceUtil.returnError(errMsg);
    }
    return ServiceUtil.returnSuccess();
}
Also used : Locale(java.util.Locale) GenericValue(org.apache.ofbiz.entity.GenericValue) Delegator(org.apache.ofbiz.entity.Delegator) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) EntityCondition(org.apache.ofbiz.entity.condition.EntityCondition) EntityListIterator(org.apache.ofbiz.entity.util.EntityListIterator) Timestamp(java.sql.Timestamp) HashMap(java.util.HashMap) Map(java.util.Map) ModelKeyMap(org.apache.ofbiz.entity.model.ModelKeyMap)

Example 57 with Delegator

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

the class VariantEvents method quickAddChosenVariant.

/**
 * Creates variant products from a virtual product and a combination of selectable features
 *@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 quickAddChosenVariant(HttpServletRequest request, HttpServletResponse response) {
    String errMsg = "";
    Delegator delegator = (Delegator) request.getAttribute("delegator");
    String productId = request.getParameter("productId");
    String variantProductId = request.getParameter("variantProductId");
    String featureTypeSizeStr = request.getParameter("featureTypeSize");
    if (UtilValidate.isEmpty(productId)) {
        errMsg = UtilProperties.getMessage(resource, "variantevents.productId_required_but_missing", UtilHttp.getLocale(request));
        request.setAttribute("_ERROR_MESSAGE_", errMsg);
        return "error";
    }
    if (UtilValidate.isEmpty(variantProductId)) {
        errMsg = UtilProperties.getMessage(resource, "variantevents.variantProductId_required_but_missing_enter_an_id", UtilHttp.getLocale(request));
        request.setAttribute("_ERROR_MESSAGE_", errMsg);
        return "error";
    }
    int featureTypeSize = 0;
    try {
        featureTypeSize = Integer.parseInt(featureTypeSizeStr);
    } catch (NumberFormatException e) {
        Map<String, String> messageMap = UtilMisc.toMap("featureTypeSizeStr", featureTypeSizeStr);
        errMsg = UtilProperties.getMessage(resource, "variantevents.featureTypeSize_not_number", messageMap, UtilHttp.getLocale(request));
        request.setAttribute("_ERROR_MESSAGE_", errMsg);
        return "error";
    }
    try {
        boolean beganTransacton = TransactionUtil.begin();
        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(resource, "variantevents.product_not_found_with_ID", messageMap, UtilHttp.getLocale(request));
                TransactionUtil.rollback(beganTransacton, errMsg, null);
                request.setAttribute("_ERROR_MESSAGE_", errMsg);
                return "error";
            }
            // check if product exists
            GenericValue variantProduct = EntityQuery.use(delegator).from("Product").where("productId", variantProductId).queryOne();
            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();
            }
            // add an association from productId to variantProductId of the PRODUCT_VARIANT
            GenericValue productAssoc = delegator.makeValue("ProductAssoc", UtilMisc.toMap("productId", productId, "productIdTo", variantProductId, "productAssocTypeId", "PRODUCT_VARIANT", "fromDate", UtilDateTime.nowTimestamp()));
            productAssoc.create();
            // add the selected standard features to the new product given the productFeatureIds
            for (int i = 0; i < featureTypeSize; i++) {
                String productFeatureId = request.getParameter("feature_" + i);
                if (productFeatureId == null) {
                    Map<String, String> messageMap = UtilMisc.toMap("i", Integer.toString(i));
                    errMsg = UtilProperties.getMessage(resource, "variantevents.productFeatureId_for_feature_type_number_not_found", messageMap, UtilHttp.getLocale(request));
                    TransactionUtil.rollback(beganTransacton, errMsg, null);
                    request.setAttribute("_ERROR_MESSAGE_", errMsg);
                    return "error";
                }
                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();
            }
            TransactionUtil.commit(beganTransacton);
        } catch (GenericEntityException e) {
            Map<String, String> messageMap = UtilMisc.toMap("errMessage", e.toString());
            errMsg = UtilProperties.getMessage(resource, "variantevents.entity_error_quick_add_variant_data", messageMap, UtilHttp.getLocale(request));
            TransactionUtil.rollback(beganTransacton, errMsg, null);
            Debug.logError(e, "Entity error creating quick add variant data", module);
            request.setAttribute("_ERROR_MESSAGE_", errMsg);
            return "error";
        }
    } catch (GenericTransactionException e) {
        Debug.logError(e, "Transaction error creating quick add variant data", module);
        Map<String, String> messageMap = UtilMisc.toMap("errMessage", e.toString());
        errMsg = UtilProperties.getMessage(resource, "variantevents.transaction_error_quick_add_variant_data", messageMap, UtilHttp.getLocale(request));
        request.setAttribute("_ERROR_MESSAGE_", errMsg);
        return "error";
    }
    Map<String, String> messageMap = UtilMisc.toMap("variantProductId", variantProductId);
    String sucMsg = UtilProperties.getMessage(resource, "variantevents.successfully_created_variant_product_with_id", messageMap, UtilHttp.getLocale(request));
    request.setAttribute("_EVENT_MESSAGE_", sucMsg);
    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) Map(java.util.Map)

Example 58 with Delegator

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

the class ProductStoreEvents method getChildProductStoreGroupTree.

// Please note : the structure of map in this function is according to the JSON data map of the jsTree
@SuppressWarnings("unchecked")
public static String getChildProductStoreGroupTree(HttpServletRequest request, HttpServletResponse response) {
    Delegator delegator = (Delegator) request.getAttribute("delegator");
    String parentGroupId = request.getParameter("parentGroupId");
    String onclickFunction = request.getParameter("onclickFunction");
    List productStoreGroupList = new LinkedList();
    List<GenericValue> children;
    List<String> sortList = org.apache.ofbiz.base.util.UtilMisc.toList("sequenceNum");
    try {
        GenericValue productStoreGroup = EntityQuery.use(delegator).from("ProductStoreGroup").where("productStoreGroupId", parentGroupId).cache(true).queryOne();
        if (productStoreGroup != null) {
            children = EntityQuery.use(delegator).from("ProductStoreGroupRollupAndChild").where("parentGroupId", parentGroupId).cache(true).filterByDate().queryList();
            if (UtilValidate.isNotEmpty(children)) {
                for (GenericValue child : children) {
                    String productStoreGroupId = child.getString("productStoreGroupId");
                    Map josonMap = new HashMap();
                    List<GenericValue> childList = null;
                    // Get the child list of chosen category
                    childList = EntityQuery.use(delegator).from("ProductStoreGroupRollupAndChild").where("parentGroupId", productStoreGroupId).cache(true).filterByDate().queryList();
                    if (UtilValidate.isNotEmpty(childList)) {
                        josonMap.put("state", "closed");
                    }
                    Map dataMap = new HashMap();
                    Map dataAttrMap = new HashMap();
                    dataAttrMap.put("onClick", onclickFunction + "('" + productStoreGroupId + "')");
                    String hrefStr = "EditProductStoreGroupAndAssoc";
                    dataAttrMap.put("href", hrefStr);
                    dataMap.put("attr", dataAttrMap);
                    dataMap.put("title", child.get("productStoreGroupName") + " [" + child.get("productStoreGroupId") + "]");
                    josonMap.put("data", dataMap);
                    Map attrMap = new HashMap();
                    attrMap.put("parentGroupId", productStoreGroupId);
                    josonMap.put("attr", attrMap);
                    josonMap.put("sequenceNum", child.get("sequenceNum"));
                    josonMap.put("title", child.get("productStoreGroupName"));
                    productStoreGroupList.add(josonMap);
                }
                List<Map<Object, Object>> sortedProductStoreGroupList = UtilMisc.sortMaps(productStoreGroupList, sortList);
                request.setAttribute("storeGroupTree", sortedProductStoreGroupList);
            }
        }
    } catch (GenericEntityException e) {
        Debug.logError(e, module);
        return "error";
    }
    return "success";
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) Delegator(org.apache.ofbiz.entity.Delegator) HashMap(java.util.HashMap) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) List(java.util.List) LinkedList(java.util.LinkedList) HashMap(java.util.HashMap) Map(java.util.Map) LinkedList(java.util.LinkedList)

Example 59 with Delegator

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

the class ProductStoreWorker method getProductStorePaymentProperties.

public static String getProductStorePaymentProperties(ServletRequest request, String paymentMethodTypeId, String paymentServiceTypeEnumId, boolean anyServiceType) {
    Delegator delegator = (Delegator) request.getAttribute("delegator");
    String productStoreId = ProductStoreWorker.getProductStoreId(request);
    return ProductStoreWorker.getProductStorePaymentProperties(delegator, productStoreId, paymentMethodTypeId, paymentServiceTypeEnumId, anyServiceType);
}
Also used : Delegator(org.apache.ofbiz.entity.Delegator)

Example 60 with Delegator

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

the class SupplierProductServices method getSuppliersForProduct.

/*
     * Parameters: productId, partyId, currencyUomId, quantity
     * Result: a List of SupplierProduct entities for productId,
     *         filtered by date and optionally by partyId, ordered with lowest price first
     */
public static Map<String, Object> getSuppliersForProduct(DispatchContext dctx, Map<String, ? extends Object> context) {
    Map<String, Object> results;
    Delegator delegator = dctx.getDelegator();
    GenericValue product = null;
    String productId = (String) context.get("productId");
    String partyId = (String) context.get("partyId");
    String currencyUomId = (String) context.get("currencyUomId");
    BigDecimal quantity = (BigDecimal) context.get("quantity");
    String canDropShip = (String) context.get("canDropShip");
    try {
        product = EntityQuery.use(delegator).from("Product").where("productId", productId).cache().queryOne();
        if (product == null) {
            results = ServiceUtil.returnSuccess();
            results.put("supplierProducts", null);
            return results;
        }
        List<GenericValue> supplierProducts = product.getRelated("SupplierProduct", null, null, true);
        // if there were no related SupplierProduct entities and the item is a variant, then get the SupplierProducts of the virtual parent product
        if (supplierProducts.size() == 0 && product.getString("isVariant") != null && "Y".equals(product.getString("isVariant"))) {
            String virtualProductId = ProductWorker.getVariantVirtualId(product);
            GenericValue virtualProduct = EntityQuery.use(delegator).from("Product").where("productId", virtualProductId).cache().queryOne();
            if (virtualProduct != null) {
                supplierProducts = virtualProduct.getRelated("SupplierProduct", null, null, true);
            }
        }
        // filter the list by date
        supplierProducts = EntityUtil.filterByDate(supplierProducts, UtilDateTime.nowTimestamp(), "availableFromDate", "availableThruDate", true);
        // filter the list down by the partyId if one is provided
        if (partyId != null) {
            supplierProducts = EntityUtil.filterByAnd(supplierProducts, UtilMisc.toMap("partyId", partyId));
        }
        // filter the list down by the currencyUomId if one is provided
        if (currencyUomId != null) {
            supplierProducts = EntityUtil.filterByAnd(supplierProducts, UtilMisc.toMap("currencyUomId", currencyUomId));
        }
        // filter the list down by the minimumOrderQuantity if one is provided
        if (quantity != null) {
            // minimumOrderQuantity
            supplierProducts = EntityUtil.filterByCondition(supplierProducts, EntityCondition.makeCondition("minimumOrderQuantity", EntityOperator.LESS_THAN_EQUAL_TO, quantity));
        }
        // filter the list down by the canDropShip if one is provided
        if (canDropShip != null) {
            supplierProducts = EntityUtil.filterByAnd(supplierProducts, UtilMisc.toMap("canDropShip", canDropShip));
        }
        // sort resulting list of SupplierProduct entities by price in ASCENDING order
        supplierProducts = EntityUtil.orderBy(supplierProducts, UtilMisc.toList("lastPrice ASC"));
        results = ServiceUtil.returnSuccess();
        results.put("supplierProducts", supplierProducts);
    } catch (GenericEntityException ex) {
        Debug.logError(ex, ex.getMessage(), module);
        return ServiceUtil.returnError(ex.getMessage());
    }
    return results;
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) Delegator(org.apache.ofbiz.entity.Delegator) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) BigDecimal(java.math.BigDecimal)

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