1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krms.impl.provider.repository;
17
18 import java.util.HashMap;
19 import java.util.Map;
20
21 import org.kuali.rice.krms.api.engine.ExecutionOptions;
22 import org.kuali.rice.krms.api.engine.SelectionCriteria;
23 import org.kuali.rice.krms.api.engine.Term;
24 import org.kuali.rice.krms.api.repository.RuleRepositoryService;
25 import org.kuali.rice.krms.api.repository.context.ContextDefinition;
26 import org.kuali.rice.krms.api.repository.context.ContextSelectionCriteria;
27 import org.kuali.rice.krms.framework.engine.Context;
28 import org.kuali.rice.krms.framework.engine.ContextProvider;
29
30 public class RuleRepositoryContextProvider implements ContextProvider {
31
32
33 private static final String NAME_CONTEXT_QUALIFIER = "name";
34 private static final String NAMESPACE_CODE_CONTEXT_QUALIFIER = "namespaceCode";
35
36 private RuleRepositoryService ruleRepositoryService;
37 private RepositoryToEngineTranslator repositoryToEngineTranslator;
38
39 @Override
40 public Context loadContext(SelectionCriteria selectionCriteria, Map<Term, Object> facts, ExecutionOptions executionOptions) {
41 ContextSelectionCriteria contextSelectionCriteria = constructContextSelectionCriteria(selectionCriteria);
42 ContextDefinition contextDefinition = ruleRepositoryService.selectContext(contextSelectionCriteria);
43
44
45
46 if (contextDefinition != null) {
47 return loadContextFromDefinition(contextDefinition);
48 }
49 return null;
50 }
51
52 protected Context loadContextFromDefinition(ContextDefinition contextDefinition) {
53 return repositoryToEngineTranslator.translateContextDefinition(contextDefinition);
54 }
55
56 public void setRuleRepositoryService(RuleRepositoryService ruleRepositoryService) {
57 this.ruleRepositoryService = ruleRepositoryService;
58 }
59
60 public void setRepositoryToEngineTranslator(RepositoryToEngineTranslator repositoryToEngineTranslator) {
61 this.repositoryToEngineTranslator = repositoryToEngineTranslator;
62 }
63
64 protected ContextSelectionCriteria constructContextSelectionCriteria(SelectionCriteria selectionCriteria) {
65 Map<String, String> givenContextQualifiers = selectionCriteria.getContextQualifiers();
66 if (givenContextQualifiers == null || givenContextQualifiers.isEmpty()) {
67 throw new IllegalArgumentException("Context qualifiers in the selection criteria were null or empty. At least one context qualifier must be passed in selection criteria.");
68 }
69
70
71
72 String namespaceCode = null;
73 String name = null;
74 Map<String, String> contextQualifiers = new HashMap<String, String>();
75 for (String key : givenContextQualifiers.keySet()) {
76 String value = givenContextQualifiers.get(key);
77 if (key.equals(NAME_CONTEXT_QUALIFIER)) {
78 name = value;
79 } else if (key.equals(NAMESPACE_CODE_CONTEXT_QUALIFIER)) {
80 namespaceCode = value;
81 } else {
82 contextQualifiers.put(key, value);
83 }
84 }
85
86 return ContextSelectionCriteria.newCriteria(namespaceCode, name, contextQualifiers);
87
88 }
89
90 }