Search in sources :

Example 21 with EntityQuery

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

the class ShoppingListServices method createListReorders.

public static Map<String, Object> createListReorders(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");
    boolean beganTransaction = false;
    EntityQuery eq = EntityQuery.use(delegator).from("ShoppingList").where("shoppingListTypeId", "SLT_AUTO_REODR", "isActive", "Y").orderBy("-lastOrderedDate");
    try {
        beganTransaction = TransactionUtil.begin();
    } catch (GenericTransactionException e1) {
        Debug.logError(e1, "[Delegator] Could not begin transaction: " + e1.toString(), module);
    }
    try (EntityListIterator eli = eq.queryIterator()) {
        if (eli != null) {
            GenericValue shoppingList;
            while (((shoppingList = eli.next()) != null)) {
                Timestamp lastOrder = shoppingList.getTimestamp("lastOrderedDate");
                RecurrenceInfo recurrence = null;
                GenericValue recurrenceInfo = shoppingList.getRelatedOne("RecurrenceInfo", false);
                Timestamp startDateTime = recurrenceInfo.getTimestamp("startDateTime");
                try {
                    recurrence = new RecurrenceInfo(recurrenceInfo);
                } catch (RecurrenceInfoException e) {
                    Debug.logError(e, module);
                }
                // check the next recurrence
                if (recurrence != null) {
                    long next = lastOrder == null ? recurrence.next(startDateTime.getTime()) : recurrence.next(lastOrder.getTime());
                    Timestamp now = UtilDateTime.nowTimestamp();
                    Timestamp nextOrder = UtilDateTime.getDayStart(UtilDateTime.getTimestamp(next));
                    if (nextOrder.after(now)) {
                        continue;
                    }
                } else {
                    continue;
                }
                ShoppingCart listCart = makeShoppingListCart(dispatcher, shoppingList, locale);
                CheckOutHelper helper = new CheckOutHelper(dispatcher, delegator, listCart);
                // store the order
                Map<String, Object> createResp = helper.createOrder(userLogin);
                if (createResp == null || (createResp != null && ServiceUtil.isError(createResp))) {
                    Debug.logError("Cannot create order for shopping list - " + shoppingList, module);
                } else {
                    String orderId = (String) createResp.get("orderId");
                    // authorize the payments
                    Map<String, Object> payRes = null;
                    try {
                        payRes = helper.processPayment(ProductStoreWorker.getProductStore(listCart.getProductStoreId(), delegator), userLogin);
                    } catch (GeneralException e) {
                        Debug.logError(e, module);
                    }
                    if (payRes != null && ServiceUtil.isError(payRes)) {
                        Debug.logError("Payment processing problems with shopping list - " + shoppingList, module);
                    }
                    shoppingList.set("lastOrderedDate", UtilDateTime.nowTimestamp());
                    shoppingList.store();
                    // send notification
                    try {
                        dispatcher.runAsync("sendOrderPayRetryNotification", UtilMisc.toMap("orderId", orderId));
                    } catch (GenericServiceException e) {
                        Debug.logError(e, module);
                    }
                    // increment the recurrence
                    recurrence.incrementCurrentCount();
                }
            }
        }
        return ServiceUtil.returnSuccess();
    } catch (GenericEntityException e) {
        try {
            // only rollback the transaction if we started one...
            TransactionUtil.rollback(beganTransaction, "Error creating shopping list auto-reorders", e);
        } catch (GenericEntityException e2) {
            Debug.logError(e2, "[Delegator] Could not rollback transaction: " + e2.toString(), module);
        }
        String errMsg = UtilProperties.getMessage(resource_error, "OrderErrorWhileCreatingNewShoppingListBasedAutomaticReorder", UtilMisc.toMap("errorString", e.toString()), locale);
        Debug.logError(e, errMsg, module);
        return ServiceUtil.returnError(errMsg);
    } finally {
        try {
            // only commit the transaction if we started one... this will throw an exception if it fails
            TransactionUtil.commit(beganTransaction);
        } catch (GenericEntityException e) {
            Debug.logError(e, "Could not commit transaction for creating new shopping list based automatic reorder", module);
        }
    }
}
Also used : Locale(java.util.Locale) RecurrenceInfoException(org.apache.ofbiz.service.calendar.RecurrenceInfoException) GenericValue(org.apache.ofbiz.entity.GenericValue) LocalDispatcher(org.apache.ofbiz.service.LocalDispatcher) GeneralException(org.apache.ofbiz.base.util.GeneralException) EntityQuery(org.apache.ofbiz.entity.util.EntityQuery) Timestamp(java.sql.Timestamp) CheckOutHelper(org.apache.ofbiz.order.shoppingcart.CheckOutHelper) Delegator(org.apache.ofbiz.entity.Delegator) ShoppingCart(org.apache.ofbiz.order.shoppingcart.ShoppingCart) RecurrenceInfo(org.apache.ofbiz.service.calendar.RecurrenceInfo) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) GenericTransactionException(org.apache.ofbiz.entity.transaction.GenericTransactionException) GenericServiceException(org.apache.ofbiz.service.GenericServiceException) EntityListIterator(org.apache.ofbiz.entity.util.EntityListIterator)

Example 22 with EntityQuery

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

the class LoginServices method createUserLoginPasswordHistory.

public static void createUserLoginPasswordHistory(Delegator delegator, String userLoginId, String currentPassword) throws GenericEntityException {
    int passwordChangeHistoryLimit = 0;
    try {
        passwordChangeHistoryLimit = EntityUtilProperties.getPropertyAsInteger("security", "password.change.history.limit", 0).intValue();
    } catch (NumberFormatException nfe) {
        // No valid value is found so don't bother to save any password history
        passwordChangeHistoryLimit = 0;
    }
    if (passwordChangeHistoryLimit == 0 || passwordChangeHistoryLimit < 0) {
        // Not saving password history, so return from here.
        return;
    }
    EntityQuery eq = EntityQuery.use(delegator).from("UserLoginPasswordHistory").where("userLoginId", userLoginId).orderBy("-fromDate").cursorScrollInsensitive();
    Timestamp nowTimestamp = UtilDateTime.nowTimestamp();
    try (EntityListIterator eli = eq.queryIterator()) {
        GenericValue pwdHist;
        if ((pwdHist = eli.next()) != null) {
            // updating password so set end date on previous password in history
            pwdHist.set("thruDate", nowTimestamp);
            pwdHist.store();
            // check if we have hit the limit on number of password changes to be saved. If we did then delete the oldest password from history.
            eli.last();
            int rowIndex = eli.currentIndex();
            if (rowIndex == passwordChangeHistoryLimit) {
                eli.afterLast();
                pwdHist = eli.previous();
                pwdHist.remove();
            }
        }
    }
    // save this password in history
    GenericValue userLoginPwdHistToCreate = delegator.makeValue("UserLoginPasswordHistory", UtilMisc.toMap("userLoginId", userLoginId, "fromDate", nowTimestamp));
    boolean useEncryption = "true".equals(EntityUtilProperties.getPropertyValue("security", "password.encrypt", delegator));
    userLoginPwdHistToCreate.set("currentPassword", useEncryption ? HashCrypt.cryptUTF8(getHashType(), null, currentPassword) : currentPassword);
    userLoginPwdHistToCreate.create();
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) EntityListIterator(org.apache.ofbiz.entity.util.EntityListIterator) EntityQuery(org.apache.ofbiz.entity.util.EntityQuery) Timestamp(java.sql.Timestamp)

Aggregations

GenericValue (org.apache.ofbiz.entity.GenericValue)22 EntityQuery (org.apache.ofbiz.entity.util.EntityQuery)22 GenericEntityException (org.apache.ofbiz.entity.GenericEntityException)20 EntityListIterator (org.apache.ofbiz.entity.util.EntityListIterator)19 LinkedList (java.util.LinkedList)15 Delegator (org.apache.ofbiz.entity.Delegator)15 EntityCondition (org.apache.ofbiz.entity.condition.EntityCondition)14 Timestamp (java.sql.Timestamp)11 GenericServiceException (org.apache.ofbiz.service.GenericServiceException)11 HashMap (java.util.HashMap)10 LocalDispatcher (org.apache.ofbiz.service.LocalDispatcher)10 Locale (java.util.Locale)9 Map (java.util.Map)7 DynamicViewEntity (org.apache.ofbiz.entity.model.DynamicViewEntity)5 GenericTransactionException (org.apache.ofbiz.entity.transaction.GenericTransactionException)5 ArrayList (java.util.ArrayList)4 List (java.util.List)4 ProductSearchConstraint (org.apache.ofbiz.product.product.ProductSearch.ProductSearchConstraint)4 ProductSearchContext (org.apache.ofbiz.product.product.ProductSearch.ProductSearchContext)4 GeneralException (org.apache.ofbiz.base.util.GeneralException)3