KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > rule > RuleTypeImpl


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.repo.rule;
18
19 import java.util.List JavaDoc;
20
21 import org.alfresco.i18n.I18NUtil;
22 import org.alfresco.repo.action.CommonResourceAbstractBase;
23 import org.alfresco.repo.rule.ruletrigger.RuleTrigger;
24 import org.alfresco.service.cmr.action.ActionService;
25 import org.alfresco.service.cmr.repository.NodeRef;
26 import org.alfresco.service.cmr.rule.Rule;
27 import org.alfresco.service.cmr.rule.RuleService;
28 import org.alfresco.service.cmr.rule.RuleType;
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31
32 /**
33  * Rule type implementation class.
34  *
35  * @author Roy Wetherall
36  */

37 public class RuleTypeImpl extends CommonResourceAbstractBase implements RuleType
38 {
39     /**
40      * The logger
41      */

42     private static Log logger = LogFactory.getLog(RuleTypeImpl.class);
43     
44     /**
45      * The action service
46      */

47     private ActionService actionService;
48     
49     /**
50      * The rule service
51      */

52     private RuleService ruleService;
53     
54     /**
55      * Constructor
56      *
57      * @param ruleTriggers the rule triggers
58      */

59     public RuleTypeImpl(List JavaDoc<RuleTrigger> ruleTriggers)
60     {
61         if (ruleTriggers != null)
62         {
63             for (RuleTrigger trigger : ruleTriggers)
64             {
65                 trigger.registerRuleType(this);
66             }
67         }
68     }
69     
70     /**
71      * Set the action service
72      *
73      * @param actionService the action service
74      */

75     public void setActionService(ActionService actionService)
76     {
77         this.actionService = actionService;
78     }
79     
80     /**
81      * Set the rule service
82      *
83      * @param ruleService the rule service
84      */

85     public void setRuleService(RuleService ruleService)
86     {
87         this.ruleService = ruleService;
88     }
89
90     /**
91      * Rule type initialise method
92      */

93     public void init()
94     {
95         ((RuntimeRuleService)this.ruleService).registerRuleType(this);
96     }
97     
98     /**
99      * @see org.alfresco.service.cmr.rule.RuleType#getName()
100      */

101     public String JavaDoc getName()
102     {
103         return this.name;
104     }
105
106     /**
107      * @see org.alfresco.service.cmr.rule.RuleType#getDisplayLabel()
108      */

109     public String JavaDoc getDisplayLabel()
110     {
111         return I18NUtil.getMessage(this.name + "." + "display-label");
112     }
113     
114     /**
115      * @see org.alfresco.service.cmr.rule.RuleType#triggerRuleType(org.alfresco.service.cmr.repository.NodeRef, org.alfresco.service.cmr.repository.NodeRef)
116      */

117     public void triggerRuleType(NodeRef nodeRef, NodeRef actionedUponNodeRef)
118     {
119         if (this.ruleService.hasRules(nodeRef) == true)
120         {
121             List JavaDoc<Rule> rules = this.ruleService.getRules(
122                     nodeRef,
123                     true,
124                     this.name);
125             
126             for (Rule rule : rules)
127             {
128                 if (logger.isDebugEnabled() == true)
129                 {
130                     logger.debug("Triggering rule " + rule.getId());
131                 }
132                 
133                 if (rule.getExecuteAsychronously() == true)
134                 {
135                     // Execute the rule now since it will be be queued for async execution later
136
this.actionService.executeAction(rule, actionedUponNodeRef);
137                 }
138                 else
139                 {
140                     // Queue the rule to be executed at the end of the transaction (but still in the transaction)
141
((RuntimeRuleService)this.ruleService).addRulePendingExecution(nodeRef, actionedUponNodeRef, rule);
142                 }
143             }
144         }
145         else
146         {
147             if (logger.isDebugEnabled() == true)
148             {
149                 logger.debug("This node has no rules to trigger.");
150             }
151         }
152     }
153     
154     /**
155      * @see org.springframework.beans.factory.BeanNameAware#setBeanName(java.lang.String)
156      */

157     public void setBeanName(String JavaDoc name)
158     {
159         this.name = name;
160     }
161 }
162
Popular Tags