View Javadoc
1   /**
2    * Copyright 2005-2016 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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  	// may want to move these out to a constants file somewhere so they are accessible to engine clients?
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  		// TODO should have an execution option that throws an error here if a context does not exist?
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  		// extract the "standard" context qualifiers for the rule repository, name and namespaceCode
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  }