Author: sichen
Date: Wed Nov  8 13:14:46 2006
New Revision: 472647

URL: http://svn.apache.org/viewvc?view=rev&rev=472647
Log:
OFBIZ-333: Enhance ServiceMultiEventHandler to support configurable global 
transaction

Modified:
    incubator/ofbiz/trunk/framework/webapp/dtd/site-conf.xsd
    
incubator/ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/control/ConfigXMLReader.java
    
incubator/ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/control/RequestManager.java
    
incubator/ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/event/ServiceMultiEventHandler.java

Modified: incubator/ofbiz/trunk/framework/webapp/dtd/site-conf.xsd
URL: 
http://svn.apache.org/viewvc/incubator/ofbiz/trunk/framework/webapp/dtd/site-conf.xsd?view=diff&rev=472647&r1=472646&r2=472647
==============================================================================
--- incubator/ofbiz/trunk/framework/webapp/dtd/site-conf.xsd (original)
+++ incubator/ofbiz/trunk/framework/webapp/dtd/site-conf.xsd Wed Nov  8 
13:14:46 2006
@@ -157,6 +157,14 @@
         <xs:attribute type="xs:string" name="type" use="required"/>
         <xs:attribute type="xs:string" name="path"/>
         <xs:attribute type="xs:string" name="invoke"/>
+        <xs:attribute name="global-transaction" default="true">
+            <xs:simpleType>
+                <xs:restriction base="xs:token">
+                    <xs:enumeration value="true"/>
+                    <xs:enumeration value="false"/>
+                </xs:restriction>
+            </xs:simpleType>
+        </xs:attribute>
     </xs:attributeGroup>
     <xs:element name="response">
         <xs:complexType>

Modified: 
incubator/ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/control/ConfigXMLReader.java
URL: 
http://svn.apache.org/viewvc/incubator/ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/control/ConfigXMLReader.java?view=diff&rev=472647&r1=472646&r2=472647
==============================================================================
--- 
incubator/ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/control/ConfigXMLReader.java
 (original)
+++ 
incubator/ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/control/ConfigXMLReader.java
 Wed Nov  8 13:14:46 2006
@@ -109,6 +109,7 @@
     public static final String EVENT_PATH = "path";
     public static final String EVENT_TYPE = "type";
     public static final String EVENT_METHOD = "invoke";
+    public static final String EVENT_GLOBAL_TRANSACTION = "global-transaction";
 
     public static final String RESPONSE = "response";
     public static final String RESPONSE_NAME = "name";
@@ -234,6 +235,9 @@
                 uriMap.put(EVENT_TYPE, type);
                 uriMap.put(EVENT_PATH, path);
                 uriMap.put(EVENT_METHOD, invoke);
+                
+                // Check for a global-transaction attribute - default to true
+                uriMap.put(EVENT_GLOBAL_TRANSACTION, 
eventElement.hasAttribute(EVENT_GLOBAL_TRANSACTION) ? 
eventElement.getAttribute(EVENT_GLOBAL_TRANSACTION) : "true");
             }
 
             // Check for a description.
@@ -438,6 +442,9 @@
                     eventMap.put(EVENT_TYPE, 
eventElement.getAttribute(EVENT_TYPE));
                     eventMap.put(EVENT_PATH, 
eventElement.getAttribute(EVENT_PATH));
                     eventMap.put(EVENT_METHOD, 
eventElement.getAttribute(EVENT_METHOD));
+                
+                    // Check for a global-transaction attribute - default to 
true
+                    eventMap.put(EVENT_GLOBAL_TRANSACTION, 
eventElement.hasAttribute(EVENT_GLOBAL_TRANSACTION) ? 
eventElement.getAttribute(EVENT_GLOBAL_TRANSACTION) : "true");
                     eventList.add(eventMap);
                 }
                 map.put(FIRSTVISIT, eventList);
@@ -455,6 +462,9 @@
                     eventMap.put(EVENT_TYPE, 
eventElement.getAttribute(EVENT_TYPE));
                     eventMap.put(EVENT_PATH, 
eventElement.getAttribute(EVENT_PATH));
                     eventMap.put(EVENT_METHOD, 
eventElement.getAttribute(EVENT_METHOD));
+                
+                    // Check for a global-transaction attribute - default to 
true
+                    eventMap.put(EVENT_GLOBAL_TRANSACTION, 
eventElement.hasAttribute(EVENT_GLOBAL_TRANSACTION) ? 
eventElement.getAttribute(EVENT_GLOBAL_TRANSACTION) : "true");
                     eventList.add(eventMap);
                 }
                 map.put(PREPROCESSOR, eventList);
@@ -472,6 +482,9 @@
                     eventMap.put(EVENT_TYPE, 
eventElement.getAttribute(EVENT_TYPE));
                     eventMap.put(EVENT_PATH, 
eventElement.getAttribute(EVENT_PATH));
                     eventMap.put(EVENT_METHOD, 
eventElement.getAttribute(EVENT_METHOD));
+                
+                    // Check for a global-transaction attribute - default to 
true
+                    eventMap.put(EVENT_GLOBAL_TRANSACTION, 
eventElement.hasAttribute(EVENT_GLOBAL_TRANSACTION) ? 
eventElement.getAttribute(EVENT_GLOBAL_TRANSACTION) : "true");
                     eventList.add(eventMap);
                 }
                 map.put(POSTPROCESSOR, eventList);
@@ -489,6 +502,9 @@
                     eventMap.put(EVENT_TYPE, 
eventElement.getAttribute(EVENT_TYPE));
                     eventMap.put(EVENT_PATH, 
eventElement.getAttribute(EVENT_PATH));
                     eventMap.put(EVENT_METHOD, 
eventElement.getAttribute(EVENT_METHOD));
+                
+                    // Check for a global-transaction attribute - default to 
true
+                    eventMap.put(EVENT_GLOBAL_TRANSACTION, 
eventElement.hasAttribute(EVENT_GLOBAL_TRANSACTION) ? 
eventElement.getAttribute(EVENT_GLOBAL_TRANSACTION) : "true");
                     eventList.add(eventMap);
                 }
                 map.put("after-login", eventList);
@@ -506,6 +522,9 @@
                     eventMap.put(EVENT_TYPE, 
eventElement.getAttribute(EVENT_TYPE));
                     eventMap.put(EVENT_PATH, 
eventElement.getAttribute(EVENT_PATH));
                     eventMap.put(EVENT_METHOD, 
eventElement.getAttribute(EVENT_METHOD));
+                
+                    // Check for a global-transaction attribute - default to 
true
+                    eventMap.put(EVENT_GLOBAL_TRANSACTION, 
eventElement.hasAttribute(EVENT_GLOBAL_TRANSACTION) ? 
eventElement.getAttribute(EVENT_GLOBAL_TRANSACTION) : "true");
                     eventList.add(eventMap);
                 }
                 map.put("before-logout", eventList);

Modified: 
incubator/ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/control/RequestManager.java
URL: 
http://svn.apache.org/viewvc/incubator/ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/control/RequestManager.java?view=diff&rev=472647&r1=472646&r2=472647
==============================================================================
--- 
incubator/ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/control/RequestManager.java
 (original)
+++ 
incubator/ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/control/RequestManager.java
 Wed Nov  8 13:14:46 2006
@@ -152,6 +152,21 @@
         }
     }
 
+    /** Gets the event global-transaction from the requestMap */
+    public boolean getEventGlobalTransaction(String uriStr) {
+        Map uri = getRequestMapMap(uriStr);
+
+        if (uri != null) {
+            return new Boolean((String) 
uri.get(ConfigXMLReader.EVENT_GLOBAL_TRANSACTION)).booleanValue();
+        } else {
+            if (Debug.verboseOn()) {
+                Debug.logWarning("[RequestManager.getEventGlobalTransaction] 
Global-transaction of event for request \"" +
+                    uriStr + "\" not found, defaulting to true", module);
+            }
+            return false;
+        }
+    }
+
     /** Gets the view name from the requestMap */
     public String getViewName(String uriStr) {
         Map uri = getRequestMapMap(uriStr);

Modified: 
incubator/ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/event/ServiceMultiEventHandler.java
URL: 
http://svn.apache.org/viewvc/incubator/ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/event/ServiceMultiEventHandler.java?view=diff&rev=472647&r1=472646&r2=472647
==============================================================================
--- 
incubator/ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/event/ServiceMultiEventHandler.java
 (original)
+++ 
incubator/ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/event/ServiceMultiEventHandler.java
 Wed Nov  8 13:14:46 2006
@@ -44,6 +44,8 @@
 import org.ofbiz.service.ServiceAuthException;
 import org.ofbiz.service.ServiceUtil;
 import org.ofbiz.service.ServiceValidationException;
+import org.ofbiz.webapp.control.RequestHandler;
+import org.ofbiz.webapp.control.RequestManager;
 
 /**
  * ServiceMultiEventHandler - Event handler for running a service multiple 
times; for bulk forms
@@ -54,11 +56,15 @@
 
     public static final String SYNC = "sync";
     public static final String ASYNC = "async";
+    private RequestManager requestManager;
 
     /**
      * @see 
org.ofbiz.webapp.event.EventHandler#init(javax.servlet.ServletContext)
      */
     public void init(ServletContext context) throws EventHandlerException {
+
+        // Provide a means to check the controller for event tag attributes at 
execution time
+        this.requestManager = new RequestManager(context);
     }
 
     /**
@@ -154,15 +160,23 @@
         List errorMessages = FastList.newInstance();
         List successMessages = FastList.newInstance();
 
+        // Check the global-transaction attribute of the event from the 
controller to see if the
+        //  event should be wrapped in a transaction
+        String requestUri = 
RequestHandler.getRequestUri(request.getPathInfo());
+        boolean eventGlobalTransaction = 
requestManager.getEventGlobalTransaction(requestUri);
+
         // big try/finally to make sure commit or rollback are run
         boolean beganTrans = false;
         String returnString = null;
         try {
-            // start the transaction
-            try {
-                beganTrans = 
TransactionUtil.begin(modelService.transactionTimeout * rowCount);
-            } catch (GenericTransactionException e) {
-                throw new EventHandlerException("Problem starting 
transaction", e);
+
+            if (eventGlobalTransaction) {
+                // start the global transaction
+                try {
+                    beganTrans = 
TransactionUtil.begin(modelService.transactionTimeout * rowCount);
+                } catch (GenericTransactionException e) {
+                    throw new EventHandlerException("Problem starting 
multi-service global transaction", e);
+                }
             }
 
             // now loop throw the rows and prepare/invoke the service for each
@@ -334,11 +348,13 @@
             }
         } finally {
             if (errorMessages.size() > 0) {
-                // rollback the transaction
-                try {
-                    TransactionUtil.rollback(beganTrans, "Error in 
multi-service event handling: " + errorMessages.toString(), null);
-                } catch (GenericTransactionException e) {
-                    Debug.logError(e, "Could not rollback transaction", 
module);
+                if (eventGlobalTransaction) {
+                    // rollback the global transaction
+                    try {
+                        TransactionUtil.rollback(beganTrans, "Error in 
multi-service event handling: " + errorMessages.toString(), null);
+                    } catch (GenericTransactionException e) {
+                        Debug.logError(e, "Could not rollback multi-service 
global transaction", module);
+                    }
                 }
                 errorMessages.add(0, errorPrefixStr);
                 errorMessages.add(errorSuffixStr);
@@ -351,12 +367,14 @@
                 request.setAttribute("_ERROR_MESSAGE_", errorBuf.toString());
                 returnString = "error";
             } else {
-                // commit the transaction
-                try {
-                    TransactionUtil.commit(beganTrans);
-                } catch (GenericTransactionException e) {
-                    Debug.logError(e, "Could not commit transaction", module);
-                    throw new EventHandlerException("Commit transaction 
failed");
+                if (eventGlobalTransaction) {
+                    // commit the global transaction
+                    try {
+                        TransactionUtil.commit(beganTrans);
+                    } catch (GenericTransactionException e) {
+                        Debug.logError(e, "Could not commit multi-service 
global transaction", module);
+                        throw new EventHandlerException("Commit multi-service 
global transaction failed");
+                    }
                 }
                 if (successMessages.size() > 0) {
                     request.setAttribute("_EVENT_MESSAGE_LIST_", 
successMessages);


Reply via email to