Coverage Report - org.kuali.rice.krms.impl.provider.repository.RuleRepositoryContextProvider
 
Classes in this File Line Coverage Branch Coverage Complexity
RuleRepositoryContextProvider
0%
0/26
0%
0/12
2.6
 
 1  
 /**
 2  
  * Copyright 2005-2011 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  0
 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  0
                 ContextSelectionCriteria contextSelectionCriteria = constructContextSelectionCriteria(selectionCriteria);
 42  0
                 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  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  
                 // extract the "standard" context qualifiers for the rule repository, name and namespaceCode
 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  
 }