KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: DelegatorEcaHandler.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 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.Iterator JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Map JavaDoc;
30 import java.util.Set JavaDoc;
31 import java.util.TreeSet JavaDoc;
32
33 import org.ofbiz.base.util.Debug;
34 import org.ofbiz.entity.GenericDelegator;
35 import org.ofbiz.entity.GenericEntity;
36 import org.ofbiz.entity.GenericEntityException;
37 import org.ofbiz.entity.eca.EntityEcaHandler;
38 import org.ofbiz.entityext.EntityServiceFactory;
39 import org.ofbiz.service.DispatchContext;
40
41 /**
42  * EntityEcaUtil
43  *
44  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
45  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
46  * @version $Rev: 5462 $
47  * @since 2.1
48  */

49 public class DelegatorEcaHandler implements EntityEcaHandler {
50
51     public static final String JavaDoc module = DelegatorEcaHandler.class.getName();
52
53     protected GenericDelegator delegator = null;
54     protected String JavaDoc delegatorName = null;
55     protected String JavaDoc entityEcaReaderName = null;
56     protected DispatchContext dctx = null;
57
58     public DelegatorEcaHandler() { }
59
60     public void setDelegator(GenericDelegator delegator) {
61         this.delegator = delegator;
62         this.delegatorName = delegator.getDelegatorName();
63         this.entityEcaReaderName = EntityEcaUtil.getEntityEcaReaderName(this.delegatorName);
64         this.dctx = EntityServiceFactory.getDispatchContext(delegator);
65         
66         //preload the cache
67
EntityEcaUtil.getEntityEcaCache(this.entityEcaReaderName);
68     }
69
70     public Map JavaDoc getEntityEventMap(String JavaDoc entityName) {
71         Map JavaDoc ecaCache = EntityEcaUtil.getEntityEcaCache(this.entityEcaReaderName);
72         if (ecaCache == null) return null;
73         return (Map JavaDoc) ecaCache.get(entityName);
74     }
75
76     public void evalRules(String JavaDoc currentOperation, Map JavaDoc eventMap, String JavaDoc event, GenericEntity value, boolean isError) throws GenericEntityException {
77         // if the eventMap is passed we save a HashMap lookup, but if not that's okay we'll just look it up now
78
if (eventMap == null) eventMap = this.getEntityEventMap(value.getEntityName());
79         if (eventMap == null || eventMap.size() == 0) {
80             //Debug.logInfo("Handler.evalRules for entity " + value.getEntityName() + ", event " + event + ", no eventMap for this entity", module);
81
return;
82         }
83
84         List JavaDoc rules = (List JavaDoc) eventMap.get(event);
85         //Debug.logInfo("Handler.evalRules for entity " + value.getEntityName() + ", event " + event + ", num rules=" + (rules == null ? 0 : rules.size()), module);
86

87         if (rules == null || rules.size() == 0) {
88             return;
89         }
90         
91         Iterator JavaDoc i = rules.iterator();
92         if (i.hasNext() && Debug.verboseOn()) Debug.logVerbose("Running ECA (" + event + ").", module);
93         Set JavaDoc actionsRun = new TreeSet JavaDoc();
94         while (i.hasNext()) {
95             EntityEcaRule eca = (EntityEcaRule) i.next();
96             eca.eval(currentOperation, this.dctx, value, isError, actionsRun);
97         }
98     }
99 }
100
Popular Tags