Search in sources :

Example 1 with EntityListIterator

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

the class ShipmentServices method updateShipmentsFromStaging.

public static Map<String, Object> updateShipmentsFromStaging(DispatchContext dctx, Map<String, ? extends Object> context) {
    LocalDispatcher dispatcher = dctx.getDispatcher();
    Delegator delegator = dctx.getDelegator();
    GenericValue userLogin = (GenericValue) context.get("userLogin");
    Locale locale = (Locale) context.get("locale");
    Map<String, String> shipmentMap = new HashMap<String, String>();
    EntityQuery eq = EntityQuery.use(delegator).from("OdbcPackageIn").orderBy("shipmentId", "shipmentPackageSeqId", "voidIndicator");
    try (EntityListIterator eli = eq.queryIterator()) {
        GenericValue pkgInfo;
        while ((pkgInfo = eli.next()) != null) {
            String packageSeqId = pkgInfo.getString("shipmentPackageSeqId");
            String shipmentId = pkgInfo.getString("shipmentId");
            // locate the shipment package
            GenericValue shipmentPackage = EntityQuery.use(delegator).from("ShipmentPackage").where("shipmentId", shipmentId, "shipmentPackageSeqId", packageSeqId).queryOne();
            if (shipmentPackage != null) {
                if ("00001".equals(packageSeqId)) {
                    // only need to do this for the first package
                    GenericValue rtSeg = null;
                    rtSeg = EntityQuery.use(delegator).from("ShipmentRouteSegment").where("shipmentId", shipmentId, "shipmentRouteSegmentId", "00001").queryOne();
                    if (rtSeg == null) {
                        rtSeg = delegator.makeValue("ShipmentRouteSegment", UtilMisc.toMap("shipmentId", shipmentId, "shipmentRouteSegmentId", "00001"));
                        try {
                            delegator.create(rtSeg);
                        } catch (GenericEntityException e) {
                            Debug.logError(e, module);
                            return ServiceUtil.returnError(e.getMessage());
                        }
                    }
                    rtSeg.set("actualStartDate", pkgInfo.get("shippedDate"));
                    rtSeg.set("billingWeight", pkgInfo.get("billingWeight"));
                    rtSeg.set("actualCost", pkgInfo.get("shippingTotal"));
                    rtSeg.set("trackingIdNumber", pkgInfo.get("trackingNumber"));
                    delegator.store(rtSeg);
                }
                Map<String, Object> pkgCtx = new HashMap<String, Object>();
                pkgCtx.put("shipmentId", shipmentId);
                pkgCtx.put("shipmentPackageSeqId", packageSeqId);
                // first update the weight of the package
                GenericValue pkg = null;
                pkg = EntityQuery.use(delegator).from("ShipmentPackage").where(pkgCtx).queryOne();
                if (pkg == null) {
                    return ServiceUtil.returnError(UtilProperties.getMessage(resource, "ProductShipmentPackageNotFound", UtilMisc.toMap("shipmentPackageSeqId", packageSeqId, "shipmentId", shipmentId), locale));
                }
                pkg.set("weight", pkgInfo.get("packageWeight"));
                delegator.store(pkg);
                // need if we are the first package (only) update the route seg info
                pkgCtx.put("shipmentRouteSegmentId", "00001");
                GenericValue pkgRtSeg = null;
                pkgRtSeg = EntityQuery.use(delegator).from("ShipmentPackageRouteSeg").where(pkgCtx).queryOne();
                if (pkgRtSeg == null) {
                    pkgRtSeg = delegator.makeValue("ShipmentPackageRouteSeg", pkgCtx);
                    try {
                        delegator.create(pkgRtSeg);
                    } catch (GenericEntityException e) {
                        Debug.logError(e, module);
                        return ServiceUtil.returnError(e.getMessage());
                    }
                }
                pkgRtSeg.set("trackingCode", pkgInfo.get("trackingNumber"));
                pkgRtSeg.set("boxNumber", pkgInfo.get("shipmentPackageSeqId"));
                pkgRtSeg.set("packageServiceCost", pkgInfo.get("packageTotal"));
                delegator.store(pkgRtSeg);
                shipmentMap.put(shipmentId, pkgInfo.getString("voidIndicator"));
            }
        }
    } catch (GenericEntityException e) {
        Debug.logError(e, module);
        return ServiceUtil.returnError(e.getMessage());
    }
    // update the status of each shipment
    for (Map.Entry<String, String> entry : shipmentMap.entrySet()) {
        String shipmentId = entry.getKey();
        String voidInd = entry.getValue();
        Map<String, Object> shipCtx = new HashMap<String, Object>();
        shipCtx.put("shipmentId", shipmentId);
        if ("Y".equals(voidInd)) {
            shipCtx.put("statusId", "SHIPMENT_CANCELLED");
        } else {
            shipCtx.put("statusId", "SHIPMENT_SHIPPED");
        }
        shipCtx.put("userLogin", userLogin);
        Map<String, Object> shipResp = null;
        try {
            shipResp = dispatcher.runSync("updateShipment", shipCtx);
        } catch (GenericServiceException e) {
            Debug.logError(e, module);
            return ServiceUtil.returnError(e.getMessage());
        }
        if (ServiceUtil.isError(shipResp)) {
            return ServiceUtil.returnError(ServiceUtil.getErrorMessage(shipResp));
        }
        // remove the shipment info
        Map<String, Object> clearResp = null;
        try {
            clearResp = dispatcher.runSync("clearShipmentStaging", UtilMisc.<String, Object>toMap("shipmentId", shipmentId, "userLogin", userLogin));
        } catch (GenericServiceException e) {
            Debug.logError(e, module);
            return ServiceUtil.returnError(e.getMessage());
        }
        if (ServiceUtil.isError(clearResp)) {
            return ServiceUtil.returnError(ServiceUtil.getErrorMessage(clearResp));
        }
    }
    return ServiceUtil.returnSuccess();
}
Also used : Locale(java.util.Locale) GenericValue(org.apache.ofbiz.entity.GenericValue) LocalDispatcher(org.apache.ofbiz.service.LocalDispatcher) HashMap(java.util.HashMap) EntityQuery(org.apache.ofbiz.entity.util.EntityQuery) Delegator(org.apache.ofbiz.entity.Delegator) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) GenericServiceException(org.apache.ofbiz.service.GenericServiceException) EntityListIterator(org.apache.ofbiz.entity.util.EntityListIterator) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap)

Example 2 with EntityListIterator

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

the class ProductEvents method updateAllKeywords.

/**
 * Updates/adds keywords for all products
 *
 * @param request HTTPRequest object for the current request
 * @param response HTTPResponse object for the current request
 * @return String specifying the exit status of this event
 */
public static String updateAllKeywords(HttpServletRequest request, HttpServletResponse response) {
    Delegator delegator = (Delegator) request.getAttribute("delegator");
    Security security = (Security) request.getAttribute("security");
    Timestamp nowTimestamp = UtilDateTime.nowTimestamp();
    String updateMode = "CREATE";
    String errMsg = null;
    String doAll = request.getParameter("doAll");
    // check permissions before moving on...
    if (!security.hasEntityPermission("CATALOG", "_" + updateMode, request.getSession())) {
        Map<String, String> messageMap = UtilMisc.toMap("updateMode", updateMode);
        errMsg = UtilProperties.getMessage(resource, "productevents.not_sufficient_permissions", messageMap, UtilHttp.getLocale(request));
        request.setAttribute("_ERROR_MESSAGE_", errMsg);
        return "error";
    }
    EntityCondition condition = null;
    if (!"Y".equals(doAll)) {
        List<EntityCondition> condList = new LinkedList<>();
        condList.add(EntityCondition.makeCondition(EntityCondition.makeCondition("autoCreateKeywords", EntityOperator.EQUALS, null), EntityOperator.OR, EntityCondition.makeCondition("autoCreateKeywords", EntityOperator.NOT_EQUAL, "N")));
        if ("true".equals(EntityUtilProperties.getPropertyValue("prodsearch", "index.ignore.variants", delegator))) {
            condList.add(EntityCondition.makeCondition(EntityCondition.makeCondition("isVariant", EntityOperator.EQUALS, null), EntityOperator.OR, EntityCondition.makeCondition("isVariant", EntityOperator.NOT_EQUAL, "Y")));
        }
        if ("true".equals(EntityUtilProperties.getPropertyValue("prodsearch", "index.ignore.discontinued.sales", delegator))) {
            condList.add(EntityCondition.makeCondition(EntityCondition.makeCondition("salesDiscontinuationDate", EntityOperator.EQUALS, null), EntityOperator.OR, EntityCondition.makeCondition("salesDiscontinuationDate", EntityOperator.GREATER_THAN_EQUAL_TO, nowTimestamp)));
        }
        condition = EntityCondition.makeCondition(condList, EntityOperator.AND);
    } else {
        condition = EntityCondition.makeCondition(EntityCondition.makeCondition("autoCreateKeywords", EntityOperator.EQUALS, null), EntityOperator.OR, EntityCondition.makeCondition("autoCreateKeywords", EntityOperator.NOT_EQUAL, "N"));
    }
    int numProds = 0;
    int errProds = 0;
    boolean beganTx = false;
    try {
        // begin the transaction
        beganTx = TransactionUtil.begin(7200);
    } catch (GenericTransactionException gte) {
        Debug.logError(gte, "Unable to begin transaction", module);
    }
    try (EntityListIterator entityListIterator = EntityQuery.use(delegator).from("Product").where(condition).queryIterator()) {
        try {
            if (Debug.infoOn()) {
                long count = EntityQuery.use(delegator).from("Product").where(condition).queryCount();
                Debug.logInfo("========== Found " + count + " products to index ==========", module);
            }
        } catch (GenericEntityException gee) {
            Debug.logWarning(gee, gee.getMessage(), module);
            Map<String, String> messageMap = UtilMisc.toMap("gee", gee.toString());
            errMsg = UtilProperties.getMessage(resource, "productevents.error_getting_product_list", messageMap, UtilHttp.getLocale(request));
            request.setAttribute("_ERROR_MESSAGE_", errMsg);
            return "error";
        }
        GenericValue product;
        while ((product = entityListIterator.next()) != null) {
            try {
                KeywordIndex.indexKeywords(product, "Y".equals(doAll));
            } catch (GenericEntityException e) {
                Debug.logWarning("[ProductEvents.updateAllKeywords] Could not create product-keyword (write error); message: " + e.getMessage(), module);
                errProds++;
            }
            numProds++;
            if (numProds % 500 == 0) {
                Debug.logInfo("Keywords indexed for " + numProds + " so far", module);
            }
        }
    } catch (GenericEntityException e) {
        try {
            TransactionUtil.rollback(beganTx, e.getMessage(), e);
        } catch (GenericTransactionException e1) {
            Debug.logError(e1, module);
        }
        return "error";
    } catch (Throwable t) {
        Debug.logError(t, module);
        request.setAttribute("_ERROR_MESSAGE_", t.getMessage());
        try {
            TransactionUtil.rollback(beganTx, t.getMessage(), t);
        } catch (GenericTransactionException e2) {
            Debug.logError(e2, module);
        }
        return "error";
    }
    // commit the transaction
    try {
        TransactionUtil.commit(beganTx);
    } catch (GenericTransactionException e) {
        Debug.logError(e, module);
    }
    if (errProds == 0) {
        Map<String, String> messageMap = UtilMisc.toMap("numProds", Integer.toString(numProds));
        errMsg = UtilProperties.getMessage(resource, "productevents.keyword_creation_complete_for_products", messageMap, UtilHttp.getLocale(request));
        request.setAttribute("_EVENT_MESSAGE_", errMsg);
        return "success";
    } else {
        Map<String, String> messageMap = UtilMisc.toMap("numProds", Integer.toString(numProds));
        messageMap.put("errProds", Integer.toString(errProds));
        errMsg = UtilProperties.getMessage(resource, "productevents.keyword_creation_complete_for_products_with_errors", messageMap, UtilHttp.getLocale(request));
        request.setAttribute("_ERROR_MESSAGE_", errMsg);
        return "error";
    }
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) EntityCondition(org.apache.ofbiz.entity.condition.EntityCondition) Security(org.apache.ofbiz.security.Security) Timestamp(java.sql.Timestamp) LinkedList(java.util.LinkedList) 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) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with EntityListIterator

use of org.apache.ofbiz.entity.util.EntityListIterator 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 4 with EntityListIterator

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

the class PromoServices method purgeOldStoreAutoPromos.

public static Map<String, Object> purgeOldStoreAutoPromos(DispatchContext dctx, Map<String, ? extends Object> context) {
    Delegator delegator = dctx.getDelegator();
    String productStoreId = (String) context.get("productStoreId");
    Locale locale = (Locale) context.get("locale");
    Timestamp nowTimestamp = UtilDateTime.nowTimestamp();
    List<EntityCondition> condList = new LinkedList<>();
    if (UtilValidate.isEmpty(productStoreId)) {
        condList.add(EntityCondition.makeCondition("productStoreId", EntityOperator.EQUALS, productStoreId));
    }
    condList.add(EntityCondition.makeCondition("userEntered", EntityOperator.EQUALS, "Y"));
    condList.add(EntityCondition.makeCondition("thruDate", EntityOperator.NOT_EQUAL, null));
    condList.add(EntityCondition.makeCondition("thruDate", EntityOperator.LESS_THAN, nowTimestamp));
    try (EntityListIterator eli = EntityQuery.use(delegator).from("ProductStorePromoAndAppl").where(condList).queryIterator()) {
        GenericValue productStorePromoAndAppl = null;
        while ((productStorePromoAndAppl = eli.next()) != null) {
            GenericValue productStorePromo = delegator.makeValue("ProductStorePromoAppl");
            productStorePromo.setAllFields(productStorePromoAndAppl, true, null, null);
            productStorePromo.remove();
        }
    } catch (GenericEntityException e) {
        Debug.logError(e, "Error removing expired ProductStorePromo records: " + e.toString(), module);
        return ServiceUtil.returnError(UtilProperties.getMessage(resource, "ProductPromoCodeCannotBeRemoved", UtilMisc.toMap("errorString", e.toString()), locale));
    }
    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) LinkedList(java.util.LinkedList)

Example 5 with EntityListIterator

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

the class ProductUtilServices method removeCategoryMembersOfDiscProducts.

/**
 * for all disc products, remove from category memberships
 */
public static Map<String, Object> removeCategoryMembersOfDiscProducts(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 condition = EntityCondition.makeCondition(UtilMisc.toList(EntityCondition.makeCondition("salesDiscontinuationDate", EntityOperator.NOT_EQUAL, null), EntityCondition.makeCondition("salesDiscontinuationDate", EntityOperator.LESS_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) {
            String productId = product.getString("productId");
            List<GenericValue> productCategoryMemberList = EntityQuery.use(delegator).from("ProductCategoryMember").where("productId", productId).queryList();
            if (productCategoryMemberList.size() > 0) {
                for (GenericValue productCategoryMember : productCategoryMemberList) {
                    // coded this way rather than a removeByAnd so it can be easily changed...
                    productCategoryMember.remove();
                }
                numSoFar++;
                if (numSoFar % 500 == 0) {
                    Debug.logInfo("Removed category members for " + numSoFar + " sales discontinued products.", module);
                }
            }
        }
        Debug.logInfo("Completed - Removed category members for " + numSoFar + " sales discontinued products.", module);
    } catch (GenericEntityException e) {
        Map<String, String> messageMap = UtilMisc.toMap("errMessage", e.toString());
        errMsg = UtilProperties.getMessage(resourceError, "productutilservices.entity_error_running_removeCategoryMembersOfDiscProducts", 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)

Aggregations

EntityListIterator (org.apache.ofbiz.entity.util.EntityListIterator)65 GenericValue (org.apache.ofbiz.entity.GenericValue)59 GenericEntityException (org.apache.ofbiz.entity.GenericEntityException)57 Delegator (org.apache.ofbiz.entity.Delegator)33 EntityCondition (org.apache.ofbiz.entity.condition.EntityCondition)29 LinkedList (java.util.LinkedList)25 HashMap (java.util.HashMap)24 Timestamp (java.sql.Timestamp)23 Locale (java.util.Locale)21 EntityQuery (org.apache.ofbiz.entity.util.EntityQuery)19 GenericTransactionException (org.apache.ofbiz.entity.transaction.GenericTransactionException)18 Map (java.util.Map)16 GenericServiceException (org.apache.ofbiz.service.GenericServiceException)13 LocalDispatcher (org.apache.ofbiz.service.LocalDispatcher)12 DynamicViewEntity (org.apache.ofbiz.entity.model.DynamicViewEntity)8 ModelKeyMap (org.apache.ofbiz.entity.model.ModelKeyMap)8 GeneralException (org.apache.ofbiz.base.util.GeneralException)7 ModelEntity (org.apache.ofbiz.entity.model.ModelEntity)7 ArrayList (java.util.ArrayList)6 EntityExpr (org.apache.ofbiz.entity.condition.EntityExpr)6