Search in sources :

Example 16 with EntityExpr

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

the class TechDataServices method lookupRoutingTask.

/**
 * Used to retrieve some RoutingTasks (WorkEffort) selected by Name or MachineGroup ordered by Name
 *
 * @param ctx the dispatch context
 * @param context a map containing workEffortName (routingTaskName) and fixedAssetId (MachineGroup or ANY)
 * @return result a map containing lookupResult (list of RoutingTask <=> workEffortId with currentStatusId = "ROU_ACTIVE" and workEffortTypeId = "ROU_TASK"
 */
public static Map<String, Object> lookupRoutingTask(DispatchContext ctx, Map<String, ? extends Object> context) {
    Delegator delegator = ctx.getDelegator();
    Map<String, Object> result = new HashMap<String, Object>();
    Locale locale = (Locale) context.get("locale");
    String workEffortName = (String) context.get("workEffortName");
    String fixedAssetId = (String) context.get("fixedAssetId");
    List<GenericValue> listRoutingTask = null;
    List<EntityExpr> constraints = new LinkedList<EntityExpr>();
    if (UtilValidate.isNotEmpty(workEffortName)) {
        constraints.add(EntityCondition.makeCondition("workEffortName", EntityOperator.GREATER_THAN_EQUAL_TO, workEffortName));
    }
    if (UtilValidate.isNotEmpty(fixedAssetId) && !"ANY".equals(fixedAssetId)) {
        constraints.add(EntityCondition.makeCondition("fixedAssetId", EntityOperator.EQUALS, fixedAssetId));
    }
    constraints.add(EntityCondition.makeCondition("currentStatusId", EntityOperator.EQUALS, "ROU_ACTIVE"));
    constraints.add(EntityCondition.makeCondition("workEffortTypeId", EntityOperator.EQUALS, "ROU_TASK"));
    try {
        listRoutingTask = EntityQuery.use(delegator).from("WorkEffort").where(constraints).orderBy("workEffortName").queryList();
    } catch (GenericEntityException e) {
        Debug.logWarning(e, module);
        return ServiceUtil.returnError(UtilProperties.getMessage(resource, "ManufacturingTechDataWorkEffortNotExist", UtilMisc.toMap("errorString", e.toString()), locale));
    }
    if (listRoutingTask == null) {
        listRoutingTask = new LinkedList<GenericValue>();
    }
    if (listRoutingTask.size() == 0) {
    // FIXME is it correct ?
    // listRoutingTask.add(UtilMisc.toMap("label","no Match","value","NO_MATCH"));
    }
    result.put("lookupResult", listRoutingTask);
    return result;
}
Also used : Locale(java.util.Locale) GenericValue(org.apache.ofbiz.entity.GenericValue) HashMap(java.util.HashMap) LinkedList(java.util.LinkedList) Delegator(org.apache.ofbiz.entity.Delegator) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) EntityExpr(org.apache.ofbiz.entity.condition.EntityExpr)

Example 17 with EntityExpr

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

the class OrderServices method setItemStatus.

/**
 * Service for changing the status on order item(s)
 */
public static Map<String, Object> setItemStatus(DispatchContext ctx, Map<String, ? extends Object> context) {
    Delegator delegator = ctx.getDelegator();
    GenericValue userLogin = (GenericValue) context.get("userLogin");
    String orderId = (String) context.get("orderId");
    String orderItemSeqId = (String) context.get("orderItemSeqId");
    String fromStatusId = (String) context.get("fromStatusId");
    String statusId = (String) context.get("statusId");
    Timestamp statusDateTime = (Timestamp) context.get("statusDateTime");
    Locale locale = (Locale) context.get("locale");
    // check and make sure we have permission to change the order
    Security security = ctx.getSecurity();
    boolean hasPermission = OrderServices.hasPermission(orderId, userLogin, "UPDATE", security, delegator);
    if (!hasPermission) {
        return ServiceUtil.returnError(UtilProperties.getMessage(resource_error, "OrderYouDoNotHavePermissionToChangeThisOrdersStatus", locale));
    }
    List<EntityExpr> exprs = new ArrayList<>();
    exprs.add(EntityCondition.makeCondition("orderId", orderId));
    if (orderItemSeqId != null) {
        exprs.add(EntityCondition.makeCondition("orderItemSeqId", orderItemSeqId));
    }
    if (fromStatusId != null) {
        exprs.add(EntityCondition.makeCondition("statusId", fromStatusId));
    } else {
        exprs.add(EntityCondition.makeCondition("statusId", EntityOperator.NOT_IN, UtilMisc.toList("ITEM_COMPLETED", "ITEM_CANCELLED")));
    }
    List<GenericValue> orderItems = null;
    try {
        orderItems = EntityQuery.use(delegator).from("OrderItem").where(exprs).queryList();
    } catch (GenericEntityException e) {
        return ServiceUtil.returnError(UtilProperties.getMessage(resource_error, "OrderErrorCannotGetOrderItemEntity", locale) + e.getMessage());
    }
    if (UtilValidate.isNotEmpty(orderItems)) {
        List<GenericValue> toBeStored = new ArrayList<>();
        for (GenericValue orderItem : orderItems) {
            if (orderItem == null) {
                return ServiceUtil.returnError(UtilProperties.getMessage(resource_error, "OrderErrorCannotChangeItemStatusItemNotFound", locale));
            }
            if (Debug.verboseOn()) {
                Debug.logVerbose("[OrderServices.setItemStatus] : Status Change: [" + orderId + "] (" + orderItem.getString("orderItemSeqId"), module);
            }
            if (Debug.verboseOn()) {
                Debug.logVerbose("[OrderServices.setItemStatus] : From Status : " + orderItem.getString("statusId"), module);
            }
            if (Debug.verboseOn()) {
                Debug.logVerbose("[OrderServices.setOrderStatus] : To Status : " + statusId, module);
            }
            if (orderItem.getString("statusId").equals(statusId)) {
                continue;
            }
            try {
                GenericValue statusChange = EntityQuery.use(delegator).from("StatusValidChange").where("statusId", orderItem.getString("statusId"), "statusIdTo", statusId).queryOne();
                if (statusChange == null) {
                    Debug.logWarning(UtilProperties.getMessage(resource_error, "OrderItemStatusNotChangedIsNotAValidChange", UtilMisc.toMap("orderStatusId", orderItem.getString("statusId"), "statusId", statusId), locale), module);
                    continue;
                }
            } catch (GenericEntityException e) {
                return ServiceUtil.returnError(UtilProperties.getMessage(resource_error, "OrderErrorCouldNotChangeItemStatus", locale) + e.getMessage());
            }
            orderItem.set("statusId", statusId);
            toBeStored.add(orderItem);
            if (statusDateTime == null) {
                statusDateTime = UtilDateTime.nowTimestamp();
            }
            // now create a status change
            Map<String, Object> changeFields = new HashMap<>();
            changeFields.put("orderStatusId", delegator.getNextSeqId("OrderStatus"));
            changeFields.put("statusId", statusId);
            changeFields.put("orderId", orderId);
            changeFields.put("orderItemSeqId", orderItem.getString("orderItemSeqId"));
            changeFields.put("statusDatetime", statusDateTime);
            changeFields.put("statusUserLogin", userLogin.getString("userLoginId"));
            GenericValue orderStatus = delegator.makeValue("OrderStatus", changeFields);
            toBeStored.add(orderStatus);
        }
        // store the changes
        if (toBeStored.size() > 0) {
            try {
                delegator.storeAll(toBeStored);
            } catch (GenericEntityException e) {
                return ServiceUtil.returnError(UtilProperties.getMessage(resource_error, "OrderErrorCannotStoreStatusChanges", locale) + e.getMessage());
            }
        }
    }
    return ServiceUtil.returnSuccess();
}
Also used : Locale(java.util.Locale) GenericValue(org.apache.ofbiz.entity.GenericValue) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Security(org.apache.ofbiz.security.Security) Timestamp(java.sql.Timestamp) Delegator(org.apache.ofbiz.entity.Delegator) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) EntityExpr(org.apache.ofbiz.entity.condition.EntityExpr)

Example 18 with EntityExpr

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

the class OrderServices method runSubscriptionAutoReorders.

public static Map<String, Object> runSubscriptionAutoReorders(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");
    int count = 0;
    Map<String, Object> result = null;
    boolean beganTransaction = false;
    List<EntityExpr> exprs = UtilMisc.toList(EntityCondition.makeCondition("automaticExtend", EntityOperator.EQUALS, "Y"), EntityCondition.makeCondition("orderId", EntityOperator.NOT_EQUAL, null), EntityCondition.makeCondition("productId", EntityOperator.NOT_EQUAL, null));
    try {
        beganTransaction = TransactionUtil.begin();
    } catch (GenericTransactionException e1) {
        Debug.logError(e1, "[Delegator] Could not begin transaction: " + e1.toString(), module);
    }
    try (EntityListIterator eli = EntityQuery.use(delegator).from("Subscription").where(exprs).queryIterator()) {
        if (eli != null) {
            GenericValue subscription;
            while (((subscription = eli.next()) != null)) {
                Calendar endDate = Calendar.getInstance();
                endDate.setTime(UtilDateTime.nowTimestamp());
                // Check if today date + cancel period (if provided) is earlier than the thrudate
                int field = Calendar.MONTH;
                if (subscription.get("canclAutmExtTime") != null && subscription.get("canclAutmExtTimeUomId") != null) {
                    if ("TF_day".equals(subscription.getString("canclAutmExtTimeUomId"))) {
                        field = Calendar.DAY_OF_YEAR;
                    } else if ("TF_wk".equals(subscription.getString("canclAutmExtTimeUomId"))) {
                        field = Calendar.WEEK_OF_YEAR;
                    } else if ("TF_mon".equals(subscription.getString("canclAutmExtTimeUomId"))) {
                        field = Calendar.MONTH;
                    } else if ("TF_yr".equals(subscription.getString("canclAutmExtTimeUomId"))) {
                        field = Calendar.YEAR;
                    } else {
                        Debug.logWarning("Don't know anything about canclAutmExtTimeUomId [" + subscription.getString("canclAutmExtTimeUomId") + "], defaulting to month", module);
                    }
                    endDate.add(field, Integer.parseInt(subscription.getString("canclAutmExtTime")));
                }
                Calendar endDateSubscription = Calendar.getInstance();
                endDateSubscription.setTime(subscription.getTimestamp("thruDate"));
                if (endDate.before(endDateSubscription)) {
                    // nor expired yet.....
                    continue;
                }
                result = dispatcher.runSync("loadCartFromOrder", UtilMisc.toMap("orderId", subscription.get("orderId"), "userLogin", userLogin));
                if (ServiceUtil.isError(result)) {
                    return ServiceUtil.returnError(ServiceUtil.getErrorMessage(result));
                }
                ShoppingCart cart = (ShoppingCart) result.get("shoppingCart");
                // remove former orderId from cart (would cause duplicate entry).
                // orderId is set by order-creation services (including store-specific prefixes, e.g.)
                cart.setOrderId(null);
                // only keep the orderitem with the related product.
                List<ShoppingCartItem> cartItems = cart.items();
                for (ShoppingCartItem shoppingCartItem : cartItems) {
                    if (!subscription.get("productId").equals(shoppingCartItem.getProductId())) {
                        cart.removeCartItem(shoppingCartItem, dispatcher);
                    }
                }
                CheckOutHelper helper = new CheckOutHelper(dispatcher, delegator, cart);
                // store the order
                Map<String, Object> createResp = helper.createOrder(userLogin);
                if (createResp != null && ServiceUtil.isError(createResp)) {
                    Debug.logError("Cannot create order for shopping list - " + subscription, module);
                } else {
                    String orderId = (String) createResp.get("orderId");
                    // authorize the payments
                    Map<String, Object> payRes = null;
                    try {
                        payRes = helper.processPayment(ProductStoreWorker.getProductStore(cart.getProductStoreId(), delegator), userLogin);
                    } catch (GeneralException e) {
                        Debug.logError(e, module);
                    }
                    if (payRes != null && ServiceUtil.isError(payRes)) {
                        Debug.logError("Payment processing problems with shopping list - " + subscription, module);
                    }
                    // remove the automatic extension flag
                    subscription.put("automaticExtend", "N");
                    subscription.store();
                    // send notification
                    if (orderId != null) {
                        dispatcher.runAsync("sendOrderPayRetryNotification", UtilMisc.toMap("orderId", orderId));
                    }
                    count++;
                }
            }
        }
    } catch (GenericServiceException e) {
        Debug.logError("Could call service to create cart", module);
        return ServiceUtil.returnError(e.toString());
    } catch (CartItemModifyException e) {
        Debug.logError("Could not modify cart: " + e.toString(), module);
        return ServiceUtil.returnError(e.toString());
    } catch (GenericEntityException e) {
        try {
            // only rollback the transaction if we started one...
            TransactionUtil.rollback(beganTransaction, "Error creating subscription auto-reorders", e);
        } catch (GenericEntityException e2) {
            Debug.logError(e2, "[Delegator] Could not rollback transaction: " + e2.toString(), module);
        }
        Debug.logError(e, "Error while creating new shopping list based automatic reorder" + e.toString(), module);
        return ServiceUtil.returnError(UtilProperties.getMessage(resource, "OrderShoppingListCreationError", UtilMisc.toMap("errorString", e.toString()), locale));
    } 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);
        }
    }
    return ServiceUtil.returnSuccess(UtilProperties.getMessage(resource, "OrderRunSubscriptionAutoReorders", UtilMisc.toMap("count", count), locale));
}
Also used : Locale(java.util.Locale) GenericValue(org.apache.ofbiz.entity.GenericValue) LocalDispatcher(org.apache.ofbiz.service.LocalDispatcher) GeneralException(org.apache.ofbiz.base.util.GeneralException) Calendar(com.ibm.icu.util.Calendar) CheckOutHelper(org.apache.ofbiz.order.shoppingcart.CheckOutHelper) Delegator(org.apache.ofbiz.entity.Delegator) ShoppingCart(org.apache.ofbiz.order.shoppingcart.ShoppingCart) CartItemModifyException(org.apache.ofbiz.order.shoppingcart.CartItemModifyException) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) GenericTransactionException(org.apache.ofbiz.entity.transaction.GenericTransactionException) ShoppingCartItem(org.apache.ofbiz.order.shoppingcart.ShoppingCartItem) GenericServiceException(org.apache.ofbiz.service.GenericServiceException) EntityListIterator(org.apache.ofbiz.entity.util.EntityListIterator) EntityExpr(org.apache.ofbiz.entity.condition.EntityExpr)

Example 19 with EntityExpr

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

the class OrderReadHelper method getFeatureIdQtyMap.

public Map<String, BigDecimal> getFeatureIdQtyMap(String shipGroupSeqId) {
    Map<String, BigDecimal> featureMap = new HashMap<>();
    List<GenericValue> validItems = getValidOrderItems(shipGroupSeqId);
    if (validItems != null) {
        for (GenericValue item : validItems) {
            List<GenericValue> featureAppls = null;
            if (item.get("productId") != null) {
                try {
                    featureAppls = item.getDelegator().findByAnd("ProductFeatureAppl", UtilMisc.toMap("productId", item.getString("productId")), null, true);
                    List<EntityExpr> filterExprs = UtilMisc.toList(EntityCondition.makeCondition("productFeatureApplTypeId", EntityOperator.EQUALS, "STANDARD_FEATURE"));
                    filterExprs.add(EntityCondition.makeCondition("productFeatureApplTypeId", EntityOperator.EQUALS, "REQUIRED_FEATURE"));
                    featureAppls = EntityUtil.filterByOr(featureAppls, filterExprs);
                } catch (GenericEntityException e) {
                    Debug.logError(e, "Unable to get ProductFeatureAppl for item : " + item, module);
                }
                if (featureAppls != null) {
                    for (GenericValue appl : featureAppls) {
                        BigDecimal lastQuantity = featureMap.get(appl.getString("productFeatureId"));
                        if (lastQuantity == null) {
                            lastQuantity = BigDecimal.ZERO;
                        }
                        BigDecimal newQuantity = lastQuantity.add(getOrderItemQuantity(item));
                        featureMap.put(appl.getString("productFeatureId"), newQuantity);
                    }
                }
            }
            // get the ADDITIONAL_FEATURE adjustments
            List<GenericValue> additionalFeatures = null;
            try {
                additionalFeatures = item.getRelated("OrderAdjustment", UtilMisc.toMap("orderAdjustmentTypeId", "ADDITIONAL_FEATURE"), null, false);
            } catch (GenericEntityException e) {
                Debug.logError(e, "Unable to get OrderAdjustment from item : " + item, module);
            }
            if (additionalFeatures != null) {
                for (GenericValue adj : additionalFeatures) {
                    String featureId = adj.getString("productFeatureId");
                    if (featureId != null) {
                        BigDecimal lastQuantity = featureMap.get(featureId);
                        if (lastQuantity == null) {
                            lastQuantity = BigDecimal.ZERO;
                        }
                        BigDecimal newQuantity = lastQuantity.add(getOrderItemQuantity(item));
                        featureMap.put(featureId, newQuantity);
                    }
                }
            }
        }
    }
    return featureMap;
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) HashMap(java.util.HashMap) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) BigDecimal(java.math.BigDecimal) EntityExpr(org.apache.ofbiz.entity.condition.EntityExpr)

Example 20 with EntityExpr

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

the class OrderReadHelper method getItemFeatureSet.

public Set<String> getItemFeatureSet(GenericValue item) {
    Set<String> featureSet = new LinkedHashSet<>();
    List<GenericValue> featureAppls = null;
    if (item.get("productId") != null) {
        try {
            featureAppls = item.getDelegator().findByAnd("ProductFeatureAppl", UtilMisc.toMap("productId", item.getString("productId")), null, true);
            List<EntityExpr> filterExprs = UtilMisc.toList(EntityCondition.makeCondition("productFeatureApplTypeId", EntityOperator.EQUALS, "STANDARD_FEATURE"));
            filterExprs.add(EntityCondition.makeCondition("productFeatureApplTypeId", EntityOperator.EQUALS, "REQUIRED_FEATURE"));
            featureAppls = EntityUtil.filterByOr(featureAppls, filterExprs);
        } catch (GenericEntityException e) {
            Debug.logError(e, "Unable to get ProductFeatureAppl for item : " + item, module);
        }
        if (featureAppls != null) {
            for (GenericValue appl : featureAppls) {
                featureSet.add(appl.getString("productFeatureId"));
            }
        }
    }
    // get the ADDITIONAL_FEATURE adjustments
    List<GenericValue> additionalFeatures = null;
    try {
        additionalFeatures = item.getRelated("OrderAdjustment", UtilMisc.toMap("orderAdjustmentTypeId", "ADDITIONAL_FEATURE"), null, false);
    } catch (GenericEntityException e) {
        Debug.logError(e, "Unable to get OrderAdjustment from item : " + item, module);
    }
    if (additionalFeatures != null) {
        for (GenericValue adj : additionalFeatures) {
            String featureId = adj.getString("productFeatureId");
            if (featureId != null) {
                featureSet.add(featureId);
            }
        }
    }
    return featureSet;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) GenericValue(org.apache.ofbiz.entity.GenericValue) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) EntityExpr(org.apache.ofbiz.entity.condition.EntityExpr)

Aggregations

EntityExpr (org.apache.ofbiz.entity.condition.EntityExpr)57 GenericValue (org.apache.ofbiz.entity.GenericValue)51 GenericEntityException (org.apache.ofbiz.entity.GenericEntityException)47 Delegator (org.apache.ofbiz.entity.Delegator)29 LinkedList (java.util.LinkedList)27 HashMap (java.util.HashMap)24 Locale (java.util.Locale)23 BigDecimal (java.math.BigDecimal)19 EntityCondition (org.apache.ofbiz.entity.condition.EntityCondition)18 GenericServiceException (org.apache.ofbiz.service.GenericServiceException)16 LocalDispatcher (org.apache.ofbiz.service.LocalDispatcher)15 Timestamp (java.sql.Timestamp)13 ArrayList (java.util.ArrayList)11 GeneralException (org.apache.ofbiz.base.util.GeneralException)7 List (java.util.List)6 Map (java.util.Map)6 EntityListIterator (org.apache.ofbiz.entity.util.EntityListIterator)6 Calendar (com.ibm.icu.util.Calendar)4 LinkedHashSet (java.util.LinkedHashSet)4 EntityConditionList (org.apache.ofbiz.entity.condition.EntityConditionList)4