Coverage Report - org.kuali.rice.krms.impl.repository.RuleRepositoryServiceImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
RuleRepositoryServiceImpl
4%
3/73
0%
0/34
4
 
 1  
 package org.kuali.rice.krms.impl.repository;
 2  
 
 3  
 import java.util.ArrayList;
 4  
 import java.util.Collection;
 5  
 import java.util.Collections;
 6  
 import java.util.HashMap;
 7  
 import java.util.List;
 8  
 import java.util.Map;
 9  
 import java.util.Map.Entry;
 10  
 
 11  
 import org.apache.commons.lang.StringUtils;
 12  
 import org.kuali.rice.kns.service.BusinessObjectService;
 13  
 import org.kuali.rice.kns.service.KNSServiceLocator;
 14  
 import org.kuali.rice.krms.api.repository.RuleRepositoryService;
 15  
 import org.kuali.rice.krms.api.repository.agenda.AgendaTreeDefinition;
 16  
 import org.kuali.rice.krms.api.repository.agenda.AgendaTreeRuleEntry;
 17  
 import org.kuali.rice.krms.api.repository.agenda.AgendaTreeSubAgendaEntry;
 18  
 import org.kuali.rice.krms.api.repository.context.ContextDefinition;
 19  
 import org.kuali.rice.krms.api.repository.context.ContextSelectionCriteria;
 20  
 import org.kuali.rice.krms.api.repository.rule.RuleDefinition;
 21  
 
 22  
 /**
 23  
  *
 24  
  */
 25  1
 public class RuleRepositoryServiceImpl implements RuleRepositoryService {
 26  
     protected BusinessObjectService businessObjectService;
 27  
         
 28  
         /**
 29  
          * This overridden method ...
 30  
          * 
 31  
          * @see org.kuali.rice.krms.api.repository.RuleRepositoryService#selectContext(org.kuali.rice.krms.api.repository.context.ContextSelectionCriteria)
 32  
          */
 33  
     @Override
 34  
     public ContextDefinition selectContext(
 35  
                     ContextSelectionCriteria contextSelectionCriteria) {
 36  0
             if (contextSelectionCriteria == null){
 37  0
                     throw new IllegalArgumentException("selection criteria is null");
 38  
             }
 39  0
             if (StringUtils.isBlank(contextSelectionCriteria.getNamespaceCode())){
 40  0
                     throw new IllegalArgumentException("selection criteria namespace code is null or blank");
 41  
             }
 42  0
             Map<String, String> attributesById = KrmsRepositoryServiceLocator.getKrmsAttributeDefinitionService()
 43  
                     .convertAttributeKeys(contextSelectionCriteria.getContextQualifiers(),
 44  
                                                               contextSelectionCriteria.getNamespaceCode());
 45  0
             Map<String, String> contextQualifiers = new HashMap<String,String>();
 46  
             
 47  
             // TODO: use new criteria API so we can match multiple qualifiers at once.
 48  
             
 49  0
             contextQualifiers.put("namespace", contextSelectionCriteria.getNamespaceCode());
 50  0
             contextQualifiers.put("name", contextSelectionCriteria.getName());
 51  0
             for(Entry<String,String> attributeEntry : attributesById.entrySet()) {
 52  0
                         contextQualifiers.put("attributeBos.attributeDefinitionId", attributeEntry.getKey());
 53  0
                         contextQualifiers.put("attributeBos.value", attributeEntry.getValue());
 54  
                 }            
 55  
             
 56  0
             List<ContextBo> resultBos = (List<ContextBo>) getBusinessObjectService().findMatching(ContextBo.class, contextQualifiers);
 57  
 
 58  
             //assuming 1 ?
 59  0
             ContextDefinition result = null;
 60  0
             if (resultBos != null) {
 61  0
                     if (resultBos.size() == 1) {
 62  0
                             ContextBo bo = resultBos.iterator().next();
 63  0
                             return ContextBo.to(bo);
 64  
                     }
 65  0
                     else throw new IllegalArgumentException("ambiguous qualifiers");
 66  
             }
 67  0
             return result;
 68  
     }
 69  
 
 70  
         @Override
 71  
         public AgendaTreeDefinition getAgendaTree(String agendaId) {
 72  0
                 if (StringUtils.isBlank(agendaId)){
 73  0
                     throw new IllegalArgumentException("agenda id is null or blank");
 74  
             }
 75  
                 // Get agenda items from db, then build up agenda tree structure
 76  0
                 AgendaBo agendaBo = getBusinessObjectService().findBySinglePrimaryKey(AgendaBo.class, agendaId);
 77  0
                 String agendaItemId = agendaBo.getFirstItemId();
 78  
                 
 79  
                 // walk thru the agenda items, building an agenda tree definition Builder along the way
 80  0
                 AgendaTreeDefinition.Builder myBuilder = AgendaTreeDefinition.Builder.create();
 81  0
                 myBuilder.setAgendaId( agendaId );
 82  0
                 myBuilder = walkAgendaItemTree(agendaItemId, myBuilder);
 83  
                 
 84  
                 // build the agenda tree and return it
 85  0
                 return myBuilder.build();
 86  
         }
 87  
         
 88  
         @Override
 89  
         public List<AgendaTreeDefinition> getAgendaTrees(List<String> agendaIds) {
 90  0
                 List<AgendaTreeDefinition> agendaTrees = new ArrayList<AgendaTreeDefinition>();
 91  0
                 for (String agendaId : agendaIds){
 92  0
                         agendaTrees.add( getAgendaTree(agendaId) );
 93  
                 }
 94  0
         return Collections.unmodifiableList(agendaTrees);                
 95  
         }
 96  
         
 97  
         @Override
 98  
         public RuleDefinition getRule(String ruleId) {
 99  0
                 if (StringUtils.isBlank(ruleId)){
 100  0
                         return null;                        
 101  
                 }
 102  0
                 RuleBo bo = getBusinessObjectService().findBySinglePrimaryKey(RuleBo.class, ruleId);
 103  0
                 return RuleBo.to(bo);
 104  
         }
 105  
         
 106  
         @Override
 107  
         public List<RuleDefinition> getRules(List<String> ruleIds) {
 108  0
                 Map<String,String> fieldValues = new HashMap<String,String>();
 109  0
                 for (String ruleId : ruleIds){
 110  0
                         fieldValues.put("id", ruleId);
 111  
                 }
 112  0
                 Collection<RuleBo> bos = getBusinessObjectService().findMatching(RuleBo.class, fieldValues);
 113  0
                 ArrayList<RuleDefinition> rules = new ArrayList<RuleDefinition>();
 114  0
         for (RuleBo bo : bos) {
 115  0
             RuleDefinition rule = RuleBo.to(bo);
 116  0
             rules.add(rule);
 117  0
         }
 118  0
         return Collections.unmodifiableList(rules);
 119  
         }
 120  
 
 121  
         /**
 122  
          * Recursive method to create AgendaTreeDefinition builder
 123  
          *         
 124  
          *  
 125  
          */
 126  
         private AgendaTreeDefinition.Builder walkAgendaItemTree(String agendaItemId, AgendaTreeDefinition.Builder builder){
 127  
                 //TODO: prevent circular, endless looping
 128  0
                 if (StringUtils.isBlank(agendaItemId)){
 129  0
                         return null;
 130  
                 }
 131  
                 // Get AgendaItem Business Object from database
 132  
                 // NOTE: if we read agendaItem one at a time from db.   Could consider linking in OJB and getting all at once
 133  0
                 AgendaItemBo agendaItemBo = getBusinessObjectService().findBySinglePrimaryKey(AgendaItemBo.class, agendaItemId);
 134  
                 
 135  
                 // If Rule  
 136  
                 // TODO: validate that only either rule or subagenda, not both
 137  0
                 if (!StringUtils.isBlank( agendaItemBo.getRuleId() )){
 138  
                         // setup new rule entry builder
 139  0
                         AgendaTreeRuleEntry.Builder ruleEntryBuilder = AgendaTreeRuleEntry.Builder
 140  
                                         .create(agendaItemBo.getId(), agendaItemBo.getRuleId());
 141  0
                         ruleEntryBuilder.setRuleId( agendaItemBo.getRuleId() );
 142  0
                         ruleEntryBuilder.setAgendaItemId( agendaItemBo.getId() );
 143  0
                         if (agendaItemBo.getWhenTrueId() != null){
 144  
                                 // Go follow the true branch, creating AgendaTreeDefinintion Builder for the
 145  
                                 // true branch level
 146  0
                                 AgendaTreeDefinition.Builder myBuilder = AgendaTreeDefinition.Builder.create();
 147  0
                                 myBuilder.setAgendaId( agendaItemBo.getAgendaId() );
 148  0
                                 ruleEntryBuilder.setIfTrue( walkAgendaItemTree(agendaItemBo.getWhenTrueId(),myBuilder));
 149  
                         }
 150  0
                         if (agendaItemBo.getWhenFalseId() != null){
 151  
                                 // Go follow the false branch, creating AgendaTreeDefinintion Builder 
 152  0
                                 AgendaTreeDefinition.Builder myBuilder = AgendaTreeDefinition.Builder.create();
 153  0
                                 myBuilder.setAgendaId( agendaItemBo.getAgendaId() );
 154  0
                                 ruleEntryBuilder.setIfFalse( walkAgendaItemTree(agendaItemBo.getWhenFalseId(), myBuilder));
 155  
                         }
 156  
                         // Build the Rule Entry and add it to the AgendaTreeDefinition builder
 157  0
                         builder.addRuleEntry( ruleEntryBuilder.build() );
 158  
                 }
 159  
                 // if SubAgenda and a sub agenda tree entry
 160  0
                 if (!StringUtils.isBlank(agendaItemBo.getSubAgendaId())) {
 161  0
                         AgendaTreeSubAgendaEntry.Builder subAgendaEntryBuilder = 
 162  
                                 AgendaTreeSubAgendaEntry.Builder.create(agendaItemBo.getId(), agendaItemBo.getSubAgendaId());
 163  0
                         builder.addSubAgendaEntry( subAgendaEntryBuilder.build() );
 164  
                         }
 165  
 
 166  
                 // if this agenda item has an "After Id", follow that branch
 167  0
                 if (!StringUtils.isBlank( agendaItemBo.getAlwaysId() )){
 168  0
                         builder = walkAgendaItemTree( agendaItemBo.getAlwaysId(), builder);
 169  
                         
 170  
                 }
 171  0
                 return builder;
 172  
         }
 173  
 
 174  
         /**
 175  
      * Sets the businessObjectService property.
 176  
      *
 177  
      * @param businessObjectService The businessObjectService to set.
 178  
      */
 179  
     public void setBusinessObjectService(final BusinessObjectService businessObjectService) {
 180  1
         this.businessObjectService = businessObjectService;
 181  1
     }
 182  
 
 183  
     protected BusinessObjectService getBusinessObjectService() {
 184  0
                 if ( businessObjectService == null ) {
 185  0
                         businessObjectService = KNSServiceLocator.getBusinessObjectService();
 186  
                 }
 187  0
                 return businessObjectService;
 188  
         }
 189  
 }