KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > service > mail > ServiceMcaRule


1 /*
2  * $Id: ServiceMcaRule.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2001-2005 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.service.mail;
26
27 import java.util.Iterator JavaDoc;
28 import java.util.LinkedList JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Set JavaDoc;
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 JavaDoc;
39
40 /**
41  *
42  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
43  * @version $Rev: 5462 $
44  * @since 3.3
45  */

46 public class ServiceMcaRule implements java.io.Serializable JavaDoc {
47
48     public static final String JavaDoc module = ServiceMcaRule.class.getName();
49
50     protected String JavaDoc ruleName = null;
51     protected List JavaDoc conditions = new LinkedList JavaDoc();
52     protected List JavaDoc actions = new LinkedList JavaDoc();
53     protected boolean enabled = true;
54
55     public ServiceMcaRule(Element JavaDoc mca) {
56         this.ruleName = mca.getAttribute("mail-rule-name");
57
58         List JavaDoc condFList = UtilXml.childElementList(mca, "condition-field");
59         Iterator JavaDoc cfi = condFList.iterator();
60         while (cfi.hasNext()) {
61             Element JavaDoc condElement = (Element JavaDoc) cfi.next();
62             conditions.add(new ServiceMcaCondition(condElement, ServiceMcaCondition.CONDITION_FIELD));
63         }
64
65         List JavaDoc condHList = UtilXml.childElementList(mca, "condition-header");
66         Iterator JavaDoc chi = condHList.iterator();
67         while (chi.hasNext()) {
68             Element JavaDoc condElement = (Element JavaDoc) chi.next();
69             conditions.add(new ServiceMcaCondition(condElement, ServiceMcaCondition.CONDITION_HEADER));
70         }
71
72         List JavaDoc condSList = UtilXml.childElementList(mca, "condition-service");
73         Iterator JavaDoc csi = condSList.iterator();
74         while (csi.hasNext()) {
75             Element JavaDoc condElement = (Element JavaDoc) csi.next();
76             conditions.add(new ServiceMcaCondition(condElement, ServiceMcaCondition.CONDITION_SERVICE));
77         }
78
79         List JavaDoc actList = UtilXml.childElementList(mca, "action");
80         Iterator JavaDoc ai = actList.iterator();
81         while (ai.hasNext()) {
82             Element JavaDoc actionElement = (Element JavaDoc) ai.next();
83             actions.add(new ServiceMcaAction(actionElement));
84         }
85     }
86
87     public void eval(LocalDispatcher dispatcher, MimeMessageWrapper messageWrapper, Set JavaDoc 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 JavaDoc 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 JavaDoc 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