| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
package org.kuali.rice.krad.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.krad.bo.AdHocRoutePerson; |
| 24 | |
import org.kuali.rice.krad.bo.AdHocRouteWorkgroup; |
| 25 | |
import org.kuali.rice.krad.document.Document; |
| 26 | |
import org.kuali.rice.krad.document.MaintenanceDocument; |
| 27 | |
import org.kuali.rice.krad.document.TransactionalDocument; |
| 28 | |
import org.kuali.rice.krad.exception.InfrastructureException; |
| 29 | |
import org.kuali.rice.krad.rule.BusinessRule; |
| 30 | |
import org.kuali.rice.krad.rule.event.AddAdHocRoutePersonEvent; |
| 31 | |
import org.kuali.rice.krad.rule.event.AddAdHocRouteWorkgroupEvent; |
| 32 | |
import org.kuali.rice.krad.rule.event.KualiDocumentEvent; |
| 33 | |
import org.kuali.rice.krad.service.DataDictionaryService; |
| 34 | |
import org.kuali.rice.krad.service.DictionaryValidationService; |
| 35 | |
import org.kuali.rice.krad.service.DocumentDictionaryService; |
| 36 | |
import org.kuali.rice.krad.service.KRADServiceLocatorWeb; |
| 37 | |
import org.kuali.rice.krad.service.KualiRuleService; |
| 38 | |
import org.kuali.rice.krad.util.GlobalVariables; |
| 39 | |
import org.kuali.rice.krad.util.KRADConstants; |
| 40 | |
import org.kuali.rice.krad.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 DocumentDictionaryService documentDictionaryService; |
| 51 | |
private DictionaryValidationService dictionaryValidationService; |
| 52 | |
private DataDictionaryService dataDictionaryService; |
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
public boolean applyRules(KualiDocumentEvent event) { |
| 58 | 0 | if (event == null) { |
| 59 | 0 | throw new IllegalArgumentException("invalid (null) event"); |
| 60 | |
} |
| 61 | |
|
| 62 | 0 | event.validate(); |
| 63 | 0 | if ( LOG.isDebugEnabled() ) { |
| 64 | 0 | LOG.debug("calling applyRules for event " + event); |
| 65 | |
} |
| 66 | |
|
| 67 | 0 | BusinessRule rule = getBusinessRulesInstance(event.getDocument(), event.getRuleInterfaceClass()); |
| 68 | |
|
| 69 | 0 | boolean success = true; |
| 70 | 0 | if (rule != null) { |
| 71 | 0 | if ( LOG.isDebugEnabled() ) { |
| 72 | 0 | LOG.debug("processing " + event.getName() + " with rule " + rule.getClass().getName()); |
| 73 | |
} |
| 74 | 0 | increaseErrorPath(event.getErrorPathPrefix()); |
| 75 | |
|
| 76 | |
|
| 77 | 0 | List<KualiDocumentEvent> events = event.generateEvents(); |
| 78 | 0 | for (KualiDocumentEvent generatedEvent : events) { |
| 79 | 0 | success &= applyRules(generatedEvent); |
| 80 | |
} |
| 81 | |
|
| 82 | |
|
| 83 | 0 | success &= event.invokeRuleMethod(rule); |
| 84 | |
|
| 85 | 0 | decreaseErrorPath(event.getErrorPathPrefix()); |
| 86 | |
|
| 87 | |
|
| 88 | 0 | if (!success) { |
| 89 | 0 | if ( LOG.isDebugEnabled() ) { |
| 90 | 0 | LOG.debug(event.getName() + " businessRule " + rule.getClass().getName() + " failed"); |
| 91 | |
} |
| 92 | |
} |
| 93 | |
else { |
| 94 | 0 | if ( LOG.isDebugEnabled() ) { |
| 95 | 0 | LOG.debug("processed " + event.getName() + " for rule " + rule.getClass().getName()); |
| 96 | |
} |
| 97 | |
} |
| 98 | |
|
| 99 | |
} |
| 100 | 0 | return success; |
| 101 | |
} |
| 102 | |
|
| 103 | |
|
| 104 | |
|
| 105 | |
|
| 106 | |
|
| 107 | |
|
| 108 | |
|
| 109 | |
public List<AddAdHocRoutePersonEvent> generateAdHocRoutePersonEvents(Document document) { |
| 110 | 0 | List<AdHocRoutePerson> adHocRoutePersons = document.getAdHocRoutePersons(); |
| 111 | |
|
| 112 | 0 | List<AddAdHocRoutePersonEvent> events = new ArrayList<AddAdHocRoutePersonEvent>(); |
| 113 | |
|
| 114 | 0 | for (int i = 0; i < adHocRoutePersons.size(); i++) { |
| 115 | 0 | events.add(new AddAdHocRoutePersonEvent( |
| 116 | |
KRADConstants.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( |
| 135 | |
KRADConstants.EXISTING_AD_HOC_ROUTE_WORKGROUP_PROPERTY_NAME + "[" + i + "]", document, adHocRouteWorkgroups.get(i))); |
| 136 | |
} |
| 137 | |
|
| 138 | 0 | return events; |
| 139 | |
} |
| 140 | |
|
| 141 | |
|
| 142 | |
|
| 143 | |
|
| 144 | |
|
| 145 | |
|
| 146 | |
|
| 147 | |
|
| 148 | |
|
| 149 | |
|
| 150 | |
|
| 151 | |
|
| 152 | |
public BusinessRule getBusinessRulesInstance(Document document, Class<? extends BusinessRule> ruleInterface) { |
| 153 | |
|
| 154 | 0 | Class<? extends BusinessRule> businessRulesClass = null; |
| 155 | 0 | if (document instanceof TransactionalDocument) { |
| 156 | 0 | TransactionalDocument transactionalDocument = (TransactionalDocument) document; |
| 157 | |
|
| 158 | 0 | businessRulesClass = getDocumentDictionaryService().getBusinessRulesClass(transactionalDocument); |
| 159 | 0 | } |
| 160 | 0 | else if (document instanceof MaintenanceDocument) { |
| 161 | 0 | MaintenanceDocument maintenanceDocument = (MaintenanceDocument) document; |
| 162 | |
|
| 163 | 0 | businessRulesClass = getDocumentDictionaryService().getBusinessRulesClass(maintenanceDocument); |
| 164 | 0 | } |
| 165 | |
else { |
| 166 | 0 | LOG.error("unable to get businessRulesClass for unknown document type '" + document.getClass().getName() + "'"); |
| 167 | |
} |
| 168 | |
|
| 169 | |
|
| 170 | 0 | BusinessRule rule = null; |
| 171 | 0 | if (businessRulesClass != null) { |
| 172 | |
try { |
| 173 | 0 | if (ruleInterface.isAssignableFrom(businessRulesClass)) { |
| 174 | 0 | rule = businessRulesClass.newInstance(); |
| 175 | |
} |
| 176 | |
} |
| 177 | 0 | catch (IllegalAccessException e) { |
| 178 | 0 | throw new InfrastructureException("error processing business rules", e); |
| 179 | |
} |
| 180 | 0 | catch (InstantiationException e) { |
| 181 | 0 | throw new InfrastructureException("error processing business rules", e); |
| 182 | 0 | } |
| 183 | |
} |
| 184 | |
|
| 185 | 0 | return rule; |
| 186 | |
} |
| 187 | |
|
| 188 | |
|
| 189 | |
|
| 190 | |
|
| 191 | |
|
| 192 | |
|
| 193 | |
private void increaseErrorPath(String errorPathPrefix) { |
| 194 | 0 | MessageMap errorMap = GlobalVariables.getMessageMap(); |
| 195 | |
|
| 196 | 0 | if (!StringUtils.isBlank(errorPathPrefix)) { |
| 197 | 0 | errorMap.addToErrorPath(errorPathPrefix); |
| 198 | |
} |
| 199 | 0 | } |
| 200 | |
|
| 201 | |
|
| 202 | |
|
| 203 | |
|
| 204 | |
|
| 205 | |
|
| 206 | |
private void decreaseErrorPath(String errorPathPrefix) { |
| 207 | 0 | MessageMap errorMap = GlobalVariables.getMessageMap(); |
| 208 | |
|
| 209 | 0 | if (!StringUtils.isBlank(errorPathPrefix)) { |
| 210 | 0 | errorMap.removeFromErrorPath(errorPathPrefix); |
| 211 | |
} |
| 212 | 0 | } |
| 213 | |
|
| 214 | |
public DocumentDictionaryService getDocumentDictionaryService() { |
| 215 | 0 | if (documentDictionaryService == null) { |
| 216 | 0 | this.documentDictionaryService = KRADServiceLocatorWeb.getDocumentDictionaryService(); |
| 217 | |
} |
| 218 | 0 | return documentDictionaryService; |
| 219 | |
} |
| 220 | |
|
| 221 | |
public void setDocumentDictionaryService(DocumentDictionaryService documentDictionaryService) { |
| 222 | 0 | this.documentDictionaryService = documentDictionaryService; |
| 223 | 0 | } |
| 224 | |
|
| 225 | |
public DictionaryValidationService getDictionaryValidationService() { |
| 226 | 0 | return dictionaryValidationService; |
| 227 | |
} |
| 228 | |
|
| 229 | |
public void setDictionaryValidationService(DictionaryValidationService dictionaryValidationService) { |
| 230 | 0 | this.dictionaryValidationService = dictionaryValidationService; |
| 231 | 0 | } |
| 232 | |
|
| 233 | |
public DataDictionaryService getDataDictionaryService() { |
| 234 | 0 | return dataDictionaryService; |
| 235 | |
} |
| 236 | |
|
| 237 | |
public void setDataDictionaryService(DataDictionaryService dataDictionaryService) { |
| 238 | 0 | this.dataDictionaryService = dataDictionaryService; |
| 239 | 0 | } |
| 240 | |
} |