1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.rice.kns.service.impl; |
17 | |
|
18 | |
import java.util.ArrayList; |
19 | |
import java.util.Iterator; |
20 | |
import java.util.List; |
21 | |
|
22 | |
import org.apache.commons.lang.StringUtils; |
23 | |
import org.apache.log4j.Logger; |
24 | |
import org.kuali.rice.kns.bo.AdHocRoutePerson; |
25 | |
import org.kuali.rice.kns.bo.AdHocRouteWorkgroup; |
26 | |
import org.kuali.rice.kns.document.Document; |
27 | |
import org.kuali.rice.kns.document.MaintenanceDocument; |
28 | |
import org.kuali.rice.kns.document.TransactionalDocument; |
29 | |
import org.kuali.rice.kns.exception.InfrastructureException; |
30 | |
import org.kuali.rice.kns.rule.BusinessRule; |
31 | |
import org.kuali.rice.kns.rule.event.AddAdHocRoutePersonEvent; |
32 | |
import org.kuali.rice.kns.rule.event.AddAdHocRouteWorkgroupEvent; |
33 | |
import org.kuali.rice.kns.rule.event.KualiDocumentEvent; |
34 | |
import org.kuali.rice.kns.service.DataDictionaryService; |
35 | |
import org.kuali.rice.kns.service.DictionaryValidationService; |
36 | |
import org.kuali.rice.kns.service.KualiRuleService; |
37 | |
import org.kuali.rice.kns.service.MaintenanceDocumentDictionaryService; |
38 | |
import org.kuali.rice.kns.service.TransactionalDocumentDictionaryService; |
39 | |
import org.kuali.rice.kns.util.GlobalVariables; |
40 | |
import org.kuali.rice.kns.util.KNSConstants; |
41 | |
import org.kuali.rice.kns.util.MessageMap; |
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | 0 | public class KualiRuleServiceImpl implements KualiRuleService { |
49 | 0 | private static final Logger LOG = Logger.getLogger(KualiRuleServiceImpl.class); |
50 | |
|
51 | |
private TransactionalDocumentDictionaryService transactionalDocumentDictionaryService; |
52 | |
private MaintenanceDocumentDictionaryService maintenanceDocumentDictionaryService; |
53 | |
private DictionaryValidationService dictionaryValidationService; |
54 | |
private DataDictionaryService dataDictionaryService; |
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
public boolean applyRules(KualiDocumentEvent event) { |
60 | 0 | if (event == null) { |
61 | 0 | throw new IllegalArgumentException("invalid (null) event"); |
62 | |
} |
63 | |
|
64 | 0 | event.validate(); |
65 | 0 | if ( LOG.isDebugEnabled() ) { |
66 | 0 | LOG.debug("calling applyRules for event " + event); |
67 | |
} |
68 | |
|
69 | 0 | BusinessRule rule = (BusinessRule) getBusinessRulesInstance(event.getDocument(), event.getRuleInterfaceClass()); |
70 | |
|
71 | 0 | boolean success = true; |
72 | 0 | if (rule != null) { |
73 | 0 | if ( LOG.isDebugEnabled() ) { |
74 | 0 | LOG.debug("processing " + event.getName() + " with rule " + rule.getClass().getName()); |
75 | |
} |
76 | 0 | increaseErrorPath(event.getErrorPathPrefix()); |
77 | |
|
78 | |
|
79 | 0 | List events = event.generateEvents(); |
80 | 0 | for (Iterator iter = events.iterator(); iter.hasNext();) { |
81 | 0 | KualiDocumentEvent element = (KualiDocumentEvent) iter.next(); |
82 | 0 | success &= applyRules(element); |
83 | 0 | } |
84 | |
|
85 | |
|
86 | 0 | success &= event.invokeRuleMethod(rule); |
87 | |
|
88 | 0 | decreaseErrorPath(event.getErrorPathPrefix()); |
89 | |
|
90 | |
|
91 | 0 | if (!success) { |
92 | 0 | if ( LOG.isDebugEnabled() ) { |
93 | 0 | LOG.error(event.getName() + " businessRule " + rule.getClass().getName() + " failed"); |
94 | |
} |
95 | |
} |
96 | |
else { |
97 | 0 | if ( LOG.isDebugEnabled() ) { |
98 | 0 | LOG.debug("processed " + event.getName() + " for rule " + rule.getClass().getName()); |
99 | |
} |
100 | |
} |
101 | |
|
102 | |
} |
103 | 0 | return success; |
104 | |
} |
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
public List generateAdHocRoutePersonEvents(Document document) { |
113 | 0 | List adHocRoutePersons = document.getAdHocRoutePersons(); |
114 | |
|
115 | 0 | List events = new ArrayList(); |
116 | |
|
117 | 0 | for (int i = 0; i < adHocRoutePersons.size(); i++) { |
118 | 0 | events.add(new AddAdHocRoutePersonEvent(KNSConstants.EXISTING_AD_HOC_ROUTE_PERSON_PROPERTY_NAME + "[" + i + "]", document, (AdHocRoutePerson) adHocRoutePersons.get(i))); |
119 | |
} |
120 | |
|
121 | 0 | return events; |
122 | |
} |
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
public List generateAdHocRouteWorkgroupEvents(Document document) { |
131 | 0 | List adHocRouteWorkgroups = document.getAdHocRouteWorkgroups(); |
132 | |
|
133 | 0 | List events = new ArrayList(); |
134 | |
|
135 | 0 | for (int i = 0; i < adHocRouteWorkgroups.size(); i++) { |
136 | 0 | events.add(new AddAdHocRouteWorkgroupEvent(KNSConstants.EXISTING_AD_HOC_ROUTE_WORKGROUP_PROPERTY_NAME + "[" + i + "]", document, (AdHocRouteWorkgroup) adHocRouteWorkgroups.get(i))); |
137 | |
} |
138 | |
|
139 | 0 | return events; |
140 | |
} |
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | |
|
152 | |
|
153 | |
public BusinessRule getBusinessRulesInstance(Document document, Class ruleInterface) { |
154 | |
|
155 | 0 | Class businessRulesClass = null; |
156 | 0 | if (document instanceof TransactionalDocument) { |
157 | 0 | TransactionalDocument transactionalDocument = (TransactionalDocument) document; |
158 | |
|
159 | 0 | businessRulesClass = transactionalDocumentDictionaryService.getBusinessRulesClass(transactionalDocument); |
160 | 0 | } |
161 | 0 | else if (document instanceof MaintenanceDocument) { |
162 | 0 | MaintenanceDocument maintenanceDocument = (MaintenanceDocument) document; |
163 | |
|
164 | 0 | businessRulesClass = maintenanceDocumentDictionaryService.getBusinessRulesClass(maintenanceDocument); |
165 | 0 | } |
166 | |
else { |
167 | 0 | LOG.error("unable to get businessRulesClass for unknown document type '" + document.getClass().getName() + "'"); |
168 | |
} |
169 | |
|
170 | |
|
171 | 0 | BusinessRule rule = null; |
172 | 0 | if (businessRulesClass != null) { |
173 | |
try { |
174 | 0 | if (ruleInterface.isAssignableFrom(businessRulesClass)) { |
175 | 0 | rule = (BusinessRule) businessRulesClass.newInstance(); |
176 | |
} |
177 | |
} |
178 | 0 | catch (IllegalAccessException e) { |
179 | 0 | throw new InfrastructureException("error processing business rules", e); |
180 | |
} |
181 | 0 | catch (InstantiationException e) { |
182 | 0 | throw new InfrastructureException("error processing business rules", e); |
183 | 0 | } |
184 | |
} |
185 | |
|
186 | 0 | return rule; |
187 | |
} |
188 | |
|
189 | |
|
190 | |
|
191 | |
|
192 | |
|
193 | |
|
194 | |
private void increaseErrorPath(String errorPathPrefix) { |
195 | 0 | MessageMap errorMap = GlobalVariables.getMessageMap(); |
196 | |
|
197 | 0 | if (!StringUtils.isBlank(errorPathPrefix)) { |
198 | 0 | errorMap.addToErrorPath(errorPathPrefix); |
199 | |
} |
200 | 0 | } |
201 | |
|
202 | |
|
203 | |
|
204 | |
|
205 | |
|
206 | |
|
207 | |
private void decreaseErrorPath(String errorPathPrefix) { |
208 | 0 | MessageMap errorMap = GlobalVariables.getMessageMap(); |
209 | |
|
210 | 0 | if (!StringUtils.isBlank(errorPathPrefix)) { |
211 | 0 | errorMap.removeFromErrorPath(errorPathPrefix); |
212 | |
} |
213 | 0 | } |
214 | |
|
215 | |
|
216 | |
|
217 | |
|
218 | |
|
219 | |
|
220 | |
public void setMaintenanceDocumentDictionaryService(MaintenanceDocumentDictionaryService maintenanceDocumentDictionaryService) { |
221 | 0 | this.maintenanceDocumentDictionaryService = maintenanceDocumentDictionaryService; |
222 | 0 | } |
223 | |
|
224 | |
|
225 | |
|
226 | |
|
227 | |
public MaintenanceDocumentDictionaryService getMaintenanceDocumentDictionaryService() { |
228 | 0 | return maintenanceDocumentDictionaryService; |
229 | |
} |
230 | |
|
231 | |
|
232 | |
|
233 | |
|
234 | |
public void setTransactionalDocumentDictionaryService(TransactionalDocumentDictionaryService transactionalDocumentDictionaryService) { |
235 | 0 | this.transactionalDocumentDictionaryService = transactionalDocumentDictionaryService; |
236 | 0 | } |
237 | |
|
238 | |
|
239 | |
|
240 | |
|
241 | |
public TransactionalDocumentDictionaryService getTransactionalDocumentDictionaryService() { |
242 | 0 | return transactionalDocumentDictionaryService; |
243 | |
} |
244 | |
|
245 | |
|
246 | |
|
247 | |
|
248 | |
public DictionaryValidationService getDictionaryValidationService() { |
249 | 0 | return dictionaryValidationService; |
250 | |
} |
251 | |
|
252 | |
|
253 | |
|
254 | |
|
255 | |
public void setDictionaryValidationService(DictionaryValidationService dictionaryValidationService) { |
256 | 0 | this.dictionaryValidationService = dictionaryValidationService; |
257 | 0 | } |
258 | |
|
259 | |
|
260 | |
|
261 | |
|
262 | |
public DataDictionaryService getDataDictionaryService() { |
263 | 0 | return dataDictionaryService; |
264 | |
} |
265 | |
|
266 | |
|
267 | |
|
268 | |
|
269 | |
public void setDataDictionaryService(DataDictionaryService dataDictionaryService) { |
270 | 0 | this.dataDictionaryService = dataDictionaryService; |
271 | 0 | } |
272 | |
} |