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 | 0 | 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 | 0 | ContextSelectionCriteria contextSelectionCriteria = constructContextSelectionCriteria(selectionCriteria); |
42 | 0 | ContextDefinition contextDefinition = ruleRepositoryService.selectContext(contextSelectionCriteria); |
43 | |
|
44 | |
|
45 | |
|
46 | 0 | if (contextDefinition != null) { |
47 | 0 | return loadContextFromDefinition(contextDefinition); |
48 | |
} |
49 | 0 | return null; |
50 | |
} |
51 | |
|
52 | |
protected Context loadContextFromDefinition(ContextDefinition contextDefinition) { |
53 | 0 | return repositoryToEngineTranslator.translateContextDefinition(contextDefinition); |
54 | |
} |
55 | |
|
56 | |
public void setRuleRepositoryService(RuleRepositoryService ruleRepositoryService) { |
57 | 0 | this.ruleRepositoryService = ruleRepositoryService; |
58 | 0 | } |
59 | |
|
60 | |
public void setRepositoryToEngineTranslator(RepositoryToEngineTranslator repositoryToEngineTranslator) { |
61 | 0 | this.repositoryToEngineTranslator = repositoryToEngineTranslator; |
62 | 0 | } |
63 | |
|
64 | |
protected ContextSelectionCriteria constructContextSelectionCriteria(SelectionCriteria selectionCriteria) { |
65 | 0 | Map<String, String> givenContextQualifiers = selectionCriteria.getContextQualifiers(); |
66 | 0 | if (givenContextQualifiers == null || givenContextQualifiers.isEmpty()) { |
67 | 0 | 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 | 0 | String namespaceCode = null; |
73 | 0 | String name = null; |
74 | 0 | Map<String, String> contextQualifiers = new HashMap<String, String>(); |
75 | 0 | for (String key : givenContextQualifiers.keySet()) { |
76 | 0 | String value = givenContextQualifiers.get(key); |
77 | 0 | if (key.equals(NAME_CONTEXT_QUALIFIER)) { |
78 | 0 | name = value; |
79 | 0 | } else if (key.equals(NAMESPACE_CODE_CONTEXT_QUALIFIER)) { |
80 | 0 | namespaceCode = value; |
81 | |
} else { |
82 | 0 | contextQualifiers.put(key, value); |
83 | |
} |
84 | 0 | } |
85 | |
|
86 | 0 | return ContextSelectionCriteria.newCriteria(namespaceCode, name, contextQualifiers); |
87 | |
|
88 | |
} |
89 | |
|
90 | |
} |