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));
EntityCondition cond = EntityCondition.makeCondition(exprs, EntityOperator.AND);
EntityListIterator eli = null;
eli = delegator.find("Subscription", cond, null, null, null, null);
if (eli != null) {
GenericValue subscription;
while (((subscription = eli.next()) != null)) {
Calendar endDate = Calendar.getInstance();
endDate.setTime(UtilDateTime.nowTimestamp());
//check if the thruedate - cancel period (if provided) is earlier than todays date
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 useTimeUomId [" + subscription.getString("canclAutmExtTimeUomId") + "], defaulting to month", module);
}
endDate.add(field, Integer.valueOf(subscription.getString("canclAutmExtTime")).intValue());
}
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));
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();
Iterator<ShoppingCartItem> ci = cartItems.iterator();
while (ci.hasNext()) {
ShoppingCartItem shoppingCartItem = ci.next();
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
dispatcher.runAsync("sendOrderPayRetryNotification", UtilMisc.toMap("orderId", orderId));
count++;
}
}
eli.close();
}
} catch (GenericServiceException e) {
Debug.logError("Could call service to create cart", module);
return ServiceUtil.returnError(e.toString());