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