use of org.apache.ofbiz.entity.transaction.GenericTransactionException in project ofbiz-framework by apache.
the class ProductSearchEvents method searchExpireFromCategory.
/**
* Sets the thru date of the results of a search to the specified date for 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 searchExpireFromCategory(HttpServletRequest request, HttpServletResponse response) {
Delegator delegator = (Delegator) request.getAttribute("delegator");
String productCategoryId = request.getParameter("SE_SEARCH_CATEGORY_ID");
String thruDateStr = request.getParameter("thruDate");
String errMsg = null;
Timestamp thruDate;
try {
thruDate = Timestamp.valueOf(thruDateStr);
} catch (RuntimeException e) {
Map<String, String> messageMap = UtilMisc.toMap("errDateFormat", e.toString());
errMsg = UtilProperties.getMessage(resource, "productsearchevents.thruDate_not_formatted_properly", messageMap, UtilHttp.getLocale(request));
Debug.logError(e, errMsg, module);
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 numExpired = 0;
while ((searchResultView = eli.next()) != null) {
String productId = searchResultView.getString("mainProductId");
// get all tuples that match product and category
List<GenericValue> pcmList = EntityQuery.use(delegator).from("ProductCategoryMember").where("productCategoryId", productCategoryId, "productId", productId).queryList();
// set those thrudate to that specificed maybe remove then add new one
for (GenericValue pcm : pcmList) {
if (pcm.get("thruDate") == null) {
pcm.set("thruDate", thruDate);
pcm.store();
numExpired++;
}
}
}
Map<String, String> messageMap = UtilMisc.toMap("numExpired", Integer.toString(numExpired));
errMsg = UtilProperties.getMessage(resource, "productsearchevents.expired_x_items", 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";
}
use of org.apache.ofbiz.entity.transaction.GenericTransactionException in project ofbiz-framework by apache.
the class ProductSearchEvents method searchExportProductList.
/**
* Formats the results of a search to the screen as a tab-delimited output
*@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 searchExportProductList(HttpServletRequest request, HttpServletResponse response) {
Delegator delegator = (Delegator) request.getAttribute("delegator");
String errMsg = null;
List<Map<String, Object>> productExportList = new LinkedList<>();
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;
while ((searchResultView = eli.next()) != null) {
Map<String, Object> productMap = new HashMap<>();
String productId = searchResultView.getString("mainProductId");
productMap.put("productId", productId);
productMap.put("productFeatureCustom", EntityQuery.use(delegator).from("ProductFeatureAndAppl").where("productId", productId, "productFeatureTypeId", "HAZMAT").filterByDate().queryFirst());
List<GenericValue> productCategories = EntityQuery.use(delegator).from("ProductCategoryAndMember").where("productId", productId).filterByDate().queryList();
productMap.put("productCategories", productCategories);
List<GenericValue> productFeatures = EntityQuery.use(delegator).from("ProductFeatureAndAppl").where("productId", productId).filterByDate().queryList();
productMap.put("productFeatures", productFeatures);
productExportList.add(productMap);
}
} 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";
}
request.setAttribute("productExportList", productExportList);
return "success";
}
use of org.apache.ofbiz.entity.transaction.GenericTransactionException in project ofbiz-framework by apache.
the class ProductSearchEvents method searchAddFeature.
/**
* Adds a feature to search results
*@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 searchAddFeature(HttpServletRequest request, HttpServletResponse response) {
Delegator delegator = (Delegator) request.getAttribute("delegator");
Locale locale = UtilHttp.getLocale(request);
String productFeatureId = request.getParameter("productFeatureId");
String fromDateStr = request.getParameter("fromDate");
String thruDateStr = request.getParameter("thruDate");
String amountStr = request.getParameter("amount");
String sequenceNumStr = request.getParameter("sequenceNum");
String productFeatureApplTypeId = request.getParameter("productFeatureApplTypeId");
Timestamp thruDate = null;
Timestamp fromDate = null;
BigDecimal amount = null;
Long sequenceNum = null;
try {
if (UtilValidate.isNotEmpty(fromDateStr)) {
fromDate = Timestamp.valueOf(fromDateStr);
}
if (UtilValidate.isNotEmpty(thruDateStr)) {
thruDate = Timestamp.valueOf(thruDateStr);
}
if (UtilValidate.isNotEmpty(amountStr)) {
amount = new BigDecimal(amountStr);
}
if (UtilValidate.isNotEmpty(sequenceNumStr)) {
sequenceNum = Long.valueOf(sequenceNumStr);
}
} catch (RuntimeException e) {
String errorMsg = UtilProperties.getMessage(resource, "productSearchEvents.error_casting_types", locale) + " : " + e.toString();
request.setAttribute("_ERROR_MESSAGE_", errorMsg);
Debug.logError(e, errorMsg, module);
return "error";
}
try {
boolean beganTransaction = TransactionUtil.begin(DEFAULT_TX_TIMEOUT);
try (EntityListIterator eli = getProductSearchResults(request)) {
if (eli == null) {
String 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 pfa = delegator.makeValue("ProductFeatureAppl");
pfa.set("productId", productId);
pfa.set("productFeatureId", productFeatureId);
pfa.set("fromDate", fromDate);
pfa.set("thruDate", thruDate);
pfa.set("productFeatureApplTypeId", productFeatureApplTypeId);
pfa.set("amount", amount);
pfa.set("sequenceNum", sequenceNum);
pfa.create();
numAdded++;
}
Map<String, Object> messageMap = UtilMisc.toMap("numAdded", Integer.valueOf(numAdded), "productFeatureId", productFeatureId);
String eventMsg = UtilProperties.getMessage(resource, "productSearchEvents.added_param_features", messageMap, locale) + ".";
request.setAttribute("_EVENT_MESSAGE_", eventMsg);
} catch (GenericEntityException e) {
String errorMsg = UtilProperties.getMessage(resource, "productSearchEvents.error_getting_results", locale) + " : " + e.toString();
request.setAttribute("_ERROR_MESSAGE_", errorMsg);
Debug.logError(e, errorMsg, module);
TransactionUtil.rollback(beganTransaction, errorMsg, e);
return "error";
} finally {
TransactionUtil.commit(beganTransaction);
}
} catch (GenericTransactionException e) {
String errorMsg = UtilProperties.getMessage(resource, "productSearchEvents.error_getting_results", locale) + " : " + e.toString();
request.setAttribute("_ERROR_MESSAGE_", errorMsg);
Debug.logError(e, errorMsg, module);
return "error";
}
return "success";
}
use of org.apache.ofbiz.entity.transaction.GenericTransactionException in project ofbiz-framework by apache.
the class OrderServices method callProcessOrderPayments.
// generic method for processing an order's payment(s)
public static Map<String, Object> callProcessOrderPayments(DispatchContext dctx, Map<String, ? extends Object> context) {
LocalDispatcher dispatcher = dctx.getDispatcher();
Delegator delegator = dctx.getDelegator();
Locale locale = (Locale) context.get("locale");
Transaction trans = null;
try {
// disable transaction processing
trans = TransactionUtil.suspend();
// get the cart
ShoppingCart cart = (ShoppingCart) context.get("shoppingCart");
GenericValue userLogin = cart.getUserLogin();
Boolean manualHold = (Boolean) context.get("manualHold");
if (manualHold == null) {
manualHold = Boolean.FALSE;
}
if (!"PURCHASE_ORDER".equals(cart.getOrderType())) {
String productStoreId = cart.getProductStoreId();
GenericValue productStore = ProductStoreWorker.getProductStore(productStoreId, delegator);
CheckOutHelper coh = new CheckOutHelper(dispatcher, delegator, cart);
// process payment
Map<String, Object> payResp;
try {
payResp = coh.processPayment(productStore, userLogin, false, manualHold.booleanValue());
} catch (GeneralException e) {
Debug.logError(e, module);
return ServiceUtil.returnError(e.getMessage());
}
if (ServiceUtil.isError(payResp)) {
return ServiceUtil.returnError(UtilProperties.getMessage(resource, "OrderProcessOrderPayments", locale), null, null, payResp);
}
}
return ServiceUtil.returnSuccess();
} catch (GenericTransactionException e) {
return ServiceUtil.returnError(e.getMessage());
} finally {
// resume transaction
try {
TransactionUtil.resume(trans);
} catch (GenericTransactionException e) {
Debug.logWarning(e, e.getMessage(), module);
}
}
}
use of org.apache.ofbiz.entity.transaction.GenericTransactionException 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);
}
}
}
Aggregations