1 25 package org.ofbiz.service.mail; 26 27 import java.util.Iterator ; 28 import java.util.LinkedList ; 29 import java.util.List ; 30 import java.util.Set ; 31 32 import org.ofbiz.service.GenericServiceException; 33 import org.ofbiz.service.LocalDispatcher; 34 import org.ofbiz.base.util.UtilXml; 35 import org.ofbiz.base.util.Debug; 36 import org.ofbiz.entity.GenericValue; 37 38 import org.w3c.dom.Element ; 39 40 46 public class ServiceMcaRule implements java.io.Serializable { 47 48 public static final String module = ServiceMcaRule.class.getName(); 49 50 protected String ruleName = null; 51 protected List conditions = new LinkedList (); 52 protected List actions = new LinkedList (); 53 protected boolean enabled = true; 54 55 public ServiceMcaRule(Element mca) { 56 this.ruleName = mca.getAttribute("mail-rule-name"); 57 58 List condFList = UtilXml.childElementList(mca, "condition-field"); 59 Iterator cfi = condFList.iterator(); 60 while (cfi.hasNext()) { 61 Element condElement = (Element ) cfi.next(); 62 conditions.add(new ServiceMcaCondition(condElement, ServiceMcaCondition.CONDITION_FIELD)); 63 } 64 65 List condHList = UtilXml.childElementList(mca, "condition-header"); 66 Iterator chi = condHList.iterator(); 67 while (chi.hasNext()) { 68 Element condElement = (Element ) chi.next(); 69 conditions.add(new ServiceMcaCondition(condElement, ServiceMcaCondition.CONDITION_HEADER)); 70 } 71 72 List condSList = UtilXml.childElementList(mca, "condition-service"); 73 Iterator csi = condSList.iterator(); 74 while (csi.hasNext()) { 75 Element condElement = (Element ) csi.next(); 76 conditions.add(new ServiceMcaCondition(condElement, ServiceMcaCondition.CONDITION_SERVICE)); 77 } 78 79 List actList = UtilXml.childElementList(mca, "action"); 80 Iterator ai = actList.iterator(); 81 while (ai.hasNext()) { 82 Element actionElement = (Element ) ai.next(); 83 actions.add(new ServiceMcaAction(actionElement)); 84 } 85 } 86 87 public void eval(LocalDispatcher dispatcher, MimeMessageWrapper messageWrapper, Set actionsRun, GenericValue userLogin) throws GenericServiceException { 88 if (!enabled) { 89 Debug.logInfo("Service MCA [" + ruleName + "] is disabled; not running.", module); 90 return; 91 } 92 93 boolean allCondTrue = true; 94 Iterator i = conditions.iterator(); 95 while (i.hasNext()) { 96 ServiceMcaCondition cond = (ServiceMcaCondition) i.next(); 97 if (!cond.eval(dispatcher, messageWrapper, userLogin)) { 98 allCondTrue = false; 99 break; 100 } 101 } 102 103 if (allCondTrue) { 104 Iterator a = actions.iterator(); 105 boolean allOkay = true; 106 while (a.hasNext() && allOkay) { 107 ServiceMcaAction action = (ServiceMcaAction) a.next(); 108 if (!actionsRun.contains(action.serviceName)) { 109 if (action.runAction(dispatcher, messageWrapper, userLogin)) { 110 actionsRun.add(action.serviceName); 111 } else { 112 allOkay = false; 113 } 114 } 115 } 116 } 117 } 118 119 public void setEnabled(boolean enabled) { 120 this.enabled = enabled; 121 } 122 123 public boolean isEnabled() { 124 return this.enabled; 125 } 126 } 127 | Popular Tags |