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);
}
}
}
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();
}
Aggregations