use of org.apache.ofbiz.entity.transaction.GenericTransactionException in project ofbiz-framework by apache.
the class ServiceSemaphore method dbWrite.
private synchronized void dbWrite(GenericValue value, boolean delete) throws SemaphoreFailException {
Transaction parent = null;
boolean beganTx = false;
boolean isError = false;
try {
// prepare the suspended transaction
if (TransactionUtil.isTransactionInPlace()) {
parent = TransactionUtil.suspend();
}
beganTx = TransactionUtil.begin();
if (!beganTx) {
throw new SemaphoreFailException("Cannot obtain unique transaction for semaphore logging");
}
// store the value
try {
if (delete) {
value.refresh();
value.remove();
lock = null;
} else {
lock = value.create();
}
} catch (GenericEntityException e) {
Debug.logError(e, module);
isError = true;
throw new SemaphoreFailException("Cannot obtain unique transaction for semaphore logging");
} finally {
if (isError) {
try {
TransactionUtil.rollback(beganTx, "ServiceSemaphore: dbWrite()", new Exception());
} catch (GenericTransactionException e) {
Debug.logError(e, module);
}
}
if (!isError) {
try {
TransactionUtil.commit(beganTx);
} catch (GenericTransactionException e) {
Debug.logError(e, module);
}
}
}
} catch (GenericTransactionException e) {
Debug.logError(e, module);
} finally {
if (parent != null) {
try {
TransactionUtil.resume(parent);
} catch (GenericTransactionException e) {
Debug.logError(e, module);
}
}
}
}
use of org.apache.ofbiz.entity.transaction.GenericTransactionException in project ofbiz-framework by apache.
the class JavaEventHandler method invoke.
private String invoke(String eventPath, String eventMethod, Class<?> eventClass, Class<?>[] paramTypes, Object[] params, int transactionTimeout) throws EventHandlerException {
boolean beganTransaction = false;
if (eventClass == null) {
throw new EventHandlerException("Error invoking event, the class " + eventPath + " was not found");
}
if (eventPath == null || eventMethod == null) {
throw new EventHandlerException("Invalid event method or path; call initialize()");
}
if (Debug.verboseOn())
Debug.logVerbose("[Processing]: JAVA Event", module);
try {
if (transactionTimeout > 0) {
beganTransaction = TransactionUtil.begin(transactionTimeout);
} else {
beganTransaction = TransactionUtil.begin();
}
Method m = eventClass.getMethod(eventMethod, paramTypes);
String eventReturn = (String) m.invoke(null, params);
if (Debug.verboseOn())
Debug.logVerbose("[Event Return]: " + eventReturn, module);
return eventReturn;
} catch (java.lang.reflect.InvocationTargetException e) {
Throwable t = e.getTargetException();
if (t != null) {
Debug.logError(t, "Problems Processing Event", module);
throw new EventHandlerException("Problems processing event: " + t.toString(), t);
} else {
Debug.logError(e, "Problems Processing Event", module);
throw new EventHandlerException("Problems processing event: " + e.toString(), e);
}
} catch (Exception e) {
Debug.logError(e, "Problems Processing Event", module);
throw new EventHandlerException("Problems processing event: " + e.toString(), e);
} finally {
try {
TransactionUtil.commit(beganTransaction);
} catch (GenericTransactionException e) {
Debug.logError(e, module);
}
}
}
use of org.apache.ofbiz.entity.transaction.GenericTransactionException in project ofbiz-framework by apache.
the class SimpleEventHandler method invoke.
/**
* @see org.apache.ofbiz.webapp.event.EventHandler#invoke(ConfigXMLReader.Event, ConfigXMLReader.RequestMap, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
*/
public String invoke(Event event, RequestMap requestMap, HttpServletRequest request, HttpServletResponse response) throws EventHandlerException {
boolean beganTransaction = false;
String xmlResource = event.path;
String eventName = event.invoke;
Locale locale = UtilHttp.getLocale(request);
if (Debug.verboseOn())
Debug.logVerbose("[Set path/method]: " + xmlResource + " / " + eventName, module);
if (xmlResource == null) {
throw new EventHandlerException("XML Resource (eventPath) cannot be null");
}
if (eventName == null) {
throw new EventHandlerException("Event Name (eventMethod) cannot be null");
}
if (Debug.verboseOn())
Debug.logVerbose("[Processing]: SIMPLE Event", module);
try {
beganTransaction = TransactionUtil.begin();
String eventReturn = SimpleMethod.runSimpleEvent(xmlResource, eventName, request, response);
if (Debug.verboseOn())
Debug.logVerbose("[Event Return]: " + eventReturn, module);
return eventReturn;
} catch (MiniLangException e) {
Debug.logError(e, module);
String errMsg = UtilProperties.getMessage(SimpleEventHandler.err_resource, "simpleEventHandler.event_not_completed", (locale != null ? locale : Locale.getDefault())) + ": ";
request.setAttribute("_ERROR_MESSAGE_", errMsg + e.getMessage());
return "error";
} catch (GenericTransactionException e) {
Debug.logError(e, module);
return "error";
} finally {
try {
TransactionUtil.commit(beganTransaction);
} catch (GenericTransactionException e) {
Debug.logError(e, module);
}
}
}
use of org.apache.ofbiz.entity.transaction.GenericTransactionException in project ofbiz-framework by apache.
the class ProductSearchEvents method searchRemoveFeature.
/**
* Removes a feature from 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 searchRemoveFeature(HttpServletRequest request, HttpServletResponse response) {
Delegator delegator = (Delegator) request.getAttribute("delegator");
Locale locale = UtilHttp.getLocale(request);
String productFeatureId = request.getParameter("productFeatureId");
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 numRemoved = 0;
while ((searchResultView = eli.next()) != null) {
String productId = searchResultView.getString("mainProductId");
numRemoved += delegator.removeByAnd("ProductFeatureAppl", UtilMisc.toMap("productId", productId, "productFeatureId", productFeatureId));
}
Map<String, Object> messageMap = UtilMisc.toMap("numRemoved", Integer.valueOf(numRemoved), "productFeatureId", productFeatureId);
String eventMsg = UtilProperties.getMessage(resource, "productSearchEvents.removed_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 ProductSearchEvents method searchRemoveFromCategory.
/**
* Removes the results of a search from the specified category
*@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 searchRemoveFromCategory(HttpServletRequest request, HttpServletResponse response) {
Delegator delegator = (Delegator) request.getAttribute("delegator");
String productCategoryId = request.getParameter("SE_SEARCH_CATEGORY_ID");
String errMsg = null;
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";
}
int numRemoved = 0;
GenericValue searchResultView = null;
while ((searchResultView = eli.next()) != null) {
String productId = searchResultView.getString("mainProductId");
numRemoved += delegator.removeByAnd("ProductCategoryMember", UtilMisc.toMap("productCategoryId", productCategoryId, "productId", productId));
}
Map<String, String> messageMap = UtilMisc.toMap("numRemoved", Integer.toString(numRemoved));
errMsg = UtilProperties.getMessage(resource, "productsearchevents.removed_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";
}
Aggregations