001: /*
002: * $Id: DelegatorEcaHandler.java,v 1.1 2003/08/17 06:44:25 jonesde Exp $
003: *
004: * Copyright (c) 2003 The Open For Business Project - www.ofbiz.org
005: *
006: * Permission is hereby granted, free of charge, to any person obtaining a
007: * copy of this software and associated documentation files (the "Software"),
008: * to deal in the Software without restriction, including without limitation
009: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
010: * and/or sell copies of the Software, and to permit persons to whom the
011: * Software is furnished to do so, subject to the following conditions:
012: *
013: * The above copyright notice and this permission notice shall be included
014: * in all copies or substantial portions of the Software.
015: *
016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
017: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
018: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
019: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
020: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
021: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
022: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023: *
024: */
025: package org.ofbiz.entityext.eca;
026:
027: import java.util.Iterator;
028: import java.util.List;
029: import java.util.Map;
030: import java.util.Set;
031: import java.util.TreeSet;
032:
033: import org.ofbiz.base.util.Debug;
034: import org.ofbiz.entity.GenericDelegator;
035: import org.ofbiz.entity.GenericEntity;
036: import org.ofbiz.entity.GenericEntityException;
037: import org.ofbiz.entity.eca.EntityEcaHandler;
038: import org.ofbiz.entityext.EntityServiceFactory;
039: import org.ofbiz.service.DispatchContext;
040:
041: /**
042: * EntityEcaUtil
043: *
044: * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
045: * @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a>
046: * @version $Revision: 1.1 $
047: * @since 2.1
048: */
049: public class DelegatorEcaHandler implements EntityEcaHandler {
050:
051: public static final String module = DelegatorEcaHandler.class
052: .getName();
053:
054: protected GenericDelegator delegator = null;
055: protected String delegatorName = null;
056: protected String entityEcaReaderName = null;
057: protected DispatchContext dctx = null;
058:
059: public DelegatorEcaHandler() {
060: }
061:
062: public void setDelegator(GenericDelegator delegator) {
063: this .delegator = delegator;
064: this .delegatorName = delegator.getDelegatorName();
065: this .entityEcaReaderName = EntityEcaUtil
066: .getEntityEcaReaderName(this .delegatorName);
067: this .dctx = EntityServiceFactory.getDispatchContext(delegator);
068:
069: //preload the cache
070: EntityEcaUtil.getEntityEcaCache(this .entityEcaReaderName);
071: }
072:
073: public Map getEntityEventMap(String entityName) {
074: Map ecaCache = EntityEcaUtil
075: .getEntityEcaCache(this .entityEcaReaderName);
076: if (ecaCache == null)
077: return null;
078: return (Map) ecaCache.get(entityName);
079: }
080:
081: public void evalRules(String currentOperation, Map eventMap,
082: String event, GenericEntity value, boolean isError)
083: throws GenericEntityException {
084: // if the eventMap is passed we save a HashMap lookup, but if not that's okay we'll just look it up now
085: if (eventMap == null)
086: eventMap = this .getEntityEventMap(value.getEntityName());
087: if (eventMap == null || eventMap.size() == 0) {
088: //Debug.logInfo("Handler.evalRules for entity " + value.getEntityName() + ", event " + event + ", no eventMap for this entity", module);
089: return;
090: }
091:
092: List rules = (List) eventMap.get(event);
093: //Debug.logInfo("Handler.evalRules for entity " + value.getEntityName() + ", event " + event + ", num rules=" + (rules == null ? 0 : rules.size()), module);
094:
095: if (rules == null || rules.size() == 0) {
096: return;
097: }
098:
099: Iterator i = rules.iterator();
100: if (i.hasNext() && Debug.verboseOn())
101: Debug.logVerbose("Running ECA (" + event + ").", module);
102: Set actionsRun = new TreeSet();
103: while (i.hasNext()) {
104: EntityEcaRule eca = (EntityEcaRule) i.next();
105: eca.eval(currentOperation, this.dctx, value, isError,
106: actionsRun);
107: }
108: }
109: }
|