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