KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > entityext > eca > EntityEcaAction


1 /*
2  * $Id: EntityEcaAction.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2002-2003 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  */

25 package org.ofbiz.entityext.eca;
26
27 import java.util.Map JavaDoc;
28
29 import org.ofbiz.base.util.Debug;
30 import org.ofbiz.base.util.UtilMisc;
31 import org.ofbiz.base.util.UtilValidate;
32 import org.ofbiz.entity.GenericEntity;
33 import org.ofbiz.entity.GenericEntityException;
34 import org.ofbiz.entity.GenericValue;
35 import org.ofbiz.entity.transaction.TransactionUtil;
36 import org.ofbiz.service.DispatchContext;
37 import org.ofbiz.service.GenericServiceException;
38 import org.ofbiz.service.LocalDispatcher;
39 import org.ofbiz.service.ModelService;
40 import org.ofbiz.service.ServiceUtil;
41 import org.w3c.dom.Element JavaDoc;
42
43 /**
44  * EntityEcaAction
45  *
46  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
47  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
48  * @version $Rev: 5462 $
49  * @since 2.1
50  */

51 public class EntityEcaAction implements java.io.Serializable JavaDoc {
52     public static final String JavaDoc module = EntityEcaAction.class.getName();
53
54     protected String JavaDoc serviceName = null;
55     protected String JavaDoc serviceMode = null;
56     protected String JavaDoc runAsUser = null;
57     protected String JavaDoc valueAttr = null;
58     protected boolean resultToValue = true;
59     protected boolean abortOnError = false;
60     protected boolean rollbackOnError = false;
61     protected boolean persist = false;
62
63     protected EntityEcaAction() {}
64
65     public EntityEcaAction(Element JavaDoc action) {
66         this.serviceName = action.getAttribute("service");
67         this.serviceMode = action.getAttribute("mode");
68         // default is true, so anything but false is true
69
this.resultToValue = !"false".equals(action.getAttribute("result-to-value"));
70         // default is false, so anything but true is false
71
this.abortOnError = "true".equals(action.getAttribute("abort-on-error"));
72         this.rollbackOnError = "true".equals(action.getAttribute("rollback-on-error"));
73         this.persist = "true".equals(action.getAttribute("persist"));
74         this.runAsUser = action.getAttribute("run-as-user");
75         this.valueAttr = action.getAttribute("value-attr");
76     }
77
78     public void runAction(DispatchContext dctx, GenericEntity value) throws GenericEntityException {
79         Map JavaDoc actionResult = null;
80         
81         try {
82             // pull out context parameters needed for this service.
83
Map JavaDoc actionContext = dctx.getModelService(serviceName).makeValid(value, ModelService.IN_PARAM);
84             // if value-attr is specified, insert the value object in that attr name
85
if (valueAttr != null && valueAttr.length() > 0) {
86                 actionContext.put(valueAttr, value);
87             }
88             
89             //Debug.logInfo("Running Entity ECA action service " + this.serviceName + " triggered by entity: " + value.getEntityName(), module);
90
//Debug.logInfo("Running Entity ECA action service " + this.serviceName + "; value=" + value + "; actionContext=" + actionContext, module);
91

92             // setup the run-as-user
93
GenericValue userLoginToRunAs = null;
94             if (UtilValidate.isNotEmpty(this.runAsUser)) {
95                 userLoginToRunAs = dctx.getDelegator().findByPrimaryKeyCache("UserLogin", UtilMisc.toMap("userLoginId", this.runAsUser));
96                 if (userLoginToRunAs != null) {
97                     actionContext.put("userLogin", userLoginToRunAs);
98                 }
99             }
100
101             LocalDispatcher dispatcher = dctx.getDispatcher();
102             if ("sync".equals(this.serviceMode)) {
103                 actionResult = dispatcher.runSync(this.serviceName, actionContext);
104                 if (ServiceUtil.isError(actionResult)) {
105                     throw new GenericServiceException("Error running Entity ECA action service: " + ServiceUtil.getErrorMessage(actionResult));
106                 }
107             } else if ("async".equals(this.serviceMode)) {
108                 dispatcher.runAsync(serviceName, actionContext, persist);
109             }
110         } catch (GenericServiceException e) {
111             // check abortOnError and rollbackOnError
112
if (rollbackOnError) {
113                 String JavaDoc errMsg = "Entity ECA action service failed and rollback-on-error is true, so setting rollback only.";
114                 Debug.logError(errMsg, module);
115                 TransactionUtil.setRollbackOnly(errMsg, e);
116             }
117
118             if (this.abortOnError) {
119                 throw new EntityEcaException("Error running Entity ECA action service: " + e.toString(), e);
120             } else {
121                 Debug.logError(e, "Error running Entity ECA action service", module);
122             }
123         }
124
125         // use the result to update the context fields.
126
if (resultToValue) {
127             value.setNonPKFields(actionResult);
128         }
129     }
130 }
131
Popular Tags