Coverage Report - org.kuali.rice.krms.impl.repository.ActionBoServiceImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
ActionBoServiceImpl
88%
62/70
89%
25/28
3.889
 
 1  
 /*
 2  
  * Copyright 2006-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  
 
 17  
 package org.kuali.rice.krms.impl.repository;
 18  
 
 19  
 
 20  
 import java.util.ArrayList;
 21  
 import java.util.Collection;
 22  
 import java.util.Collections;
 23  
 import java.util.HashMap;
 24  
 import java.util.List;
 25  
 import java.util.Map;
 26  
 
 27  
 import org.apache.commons.lang.StringUtils;
 28  
 import org.kuali.rice.krad.service.BusinessObjectService;
 29  
 import org.kuali.rice.krms.api.repository.action.ActionDefinition;
 30  
 import org.kuali.rice.krms.impl.util.KRMSPropertyConstants;
 31  
 
 32  25
 public final class ActionBoServiceImpl implements ActionBoService {
 33  
 
 34  
     private BusinessObjectService businessObjectService;
 35  
 
 36  
         /**
 37  
          * This overridden method creates a KRMS Action in the repository
 38  
          * 
 39  
          * @see org.kuali.rice.krms.impl.repository.ActionBoService#createAction(org.kuali.rice.krms.api.repository.action.ActionDefinition)
 40  
          */
 41  
         @Override
 42  
         public ActionDefinition createAction(ActionDefinition action) {
 43  3
                 if (action == null){
 44  1
                 throw new IllegalArgumentException("action is null");
 45  
                 }
 46  2
                 final String actionNameKey = action.getName();
 47  2
                 final String actionNamespaceKey = action.getNamespace();
 48  2
                 final ActionDefinition existing = getActionByNameAndNamespace(actionNameKey, actionNamespaceKey);
 49  2
                 if (existing != null){
 50  1
             throw new IllegalStateException("the action to create already exists: " + action);                        
 51  
                 }        
 52  
                 
 53  1
                 ActionBo bo = ActionBo.from(action);
 54  1
                 businessObjectService.save(bo);
 55  
                 
 56  1
                 return ActionBo.to(bo);
 57  
         }
 58  
 
 59  
         /**
 60  
          * This overridden method updates an existing Action in the repository.
 61  
          * 
 62  
          * @see org.kuali.rice.krms.impl.repository.ActionBoService#updateAction(org.kuali.rice.krms.api.repository.action.ActionDefinition)
 63  
          */
 64  
         @Override
 65  
         public void updateAction(ActionDefinition action) {
 66  3
                 if (action == null){
 67  1
                 throw new IllegalArgumentException("action is null");
 68  
                 }
 69  
 
 70  
                 // must already exist to be able to update
 71  2
                 final String actionIdKey = action.getId();
 72  2
                 final ActionBo existing = businessObjectService.findBySinglePrimaryKey(ActionBo.class, actionIdKey);
 73  2
         if (existing == null) {
 74  1
             throw new IllegalStateException("the action does not exist: " + action);
 75  
         }
 76  
         final ActionDefinition toUpdate;
 77  1
         if (!existing.getId().equals(action.getId())){
 78  
                         // if passed in id does not match existing id, correct it
 79  0
                 final ActionDefinition.Builder builder = ActionDefinition.Builder.create(action);
 80  0
                 builder.setId(existing.getId());
 81  0
                 toUpdate = builder.build();
 82  0
         } else {
 83  1
                 toUpdate = action;
 84  
         }
 85  
      
 86  
                 // copy all updateable fields to bo
 87  1
         ActionBo boToUpdate = ActionBo.from(toUpdate);
 88  
 
 89  
                 // delete any old, existing attributes
 90  1
                 Map<String,String> fields = new HashMap<String,String>(1);
 91  1
                 fields.put(KRMSPropertyConstants.Action.ACTION_ID, toUpdate.getId());
 92  1
                 businessObjectService.deleteMatching(ActionAttributeBo.class, fields);
 93  
         
 94  
                 // update the action and create new attributes
 95  1
         businessObjectService.save(boToUpdate);
 96  1
         }
 97  
 
 98  
         /**
 99  
          * This overridden method retrieves an Action from the repository.
 100  
          * 
 101  
          * @see org.kuali.rice.krms.impl.repository.ActionBoService#getActionsByActionId(java.lang.String)
 102  
          */
 103  
         @Override
 104  
         public ActionDefinition getActionByActionId(String actionId) {
 105  4
                 if (StringUtils.isBlank(actionId)){
 106  2
             throw new IllegalArgumentException("action ID is null or blank");                        
 107  
                 }
 108  2
                 ActionBo bo = businessObjectService.findBySinglePrimaryKey(ActionBo.class, actionId);
 109  2
                 return ActionBo.to(bo);
 110  
         }
 111  
 
 112  
         /**
 113  
          * This overridden method retrieves an Action from the repository.
 114  
          * 
 115  
          * @see org.kuali.rice.krms.impl.repository.ActionBoService#getActionByNameAndNamespace(java.lang.String, java.lang.String)
 116  
          */
 117  
         @Override
 118  
         public ActionDefinition getActionByNameAndNamespace(String name, String namespace) {
 119  8
         if (StringUtils.isBlank(name)) {
 120  2
             throw new IllegalArgumentException("name is blank");
 121  
         }
 122  6
         if (StringUtils.isBlank(namespace)) {
 123  2
             throw new IllegalArgumentException("namespace is blank");
 124  
         }
 125  
 
 126  4
         final Map<String, Object> map = new HashMap<String, Object>();
 127  4
         map.put("name", name);
 128  4
         map.put("namespace", namespace);
 129  
 
 130  4
         ActionBo myAction = businessObjectService.findByPrimaryKey(ActionBo.class, Collections.unmodifiableMap(map));
 131  4
         return ActionBo.to(myAction);
 132  
         }
 133  
 
 134  
         /**
 135  
          * This overridden method retrieves a List of Actions associated with a Rule.
 136  
          * 
 137  
          * @see org.kuali.rice.krms.impl.repository.ActionBoService#getActionsByRuleId(java.lang.String)
 138  
          */
 139  
         @Override
 140  
         public List<ActionDefinition> getActionsByRuleId(String ruleId) {
 141  4
                 if (StringUtils.isBlank(ruleId)){
 142  2
             throw new IllegalArgumentException("ruleId is null or blank");                        
 143  
                 }
 144  2
         final Map<String, Object> map = new HashMap<String, Object>();
 145  2
         map.put("ruleId", ruleId);
 146  2
                 List<ActionBo> bos = (List<ActionBo>) businessObjectService.findMatchingOrderBy(ActionBo.class, map, "sequenceNumber", true);
 147  2
                 return convertListOfBosToImmutables(bos);
 148  
         }
 149  
 
 150  
         /**
 151  
          * This overridden method retrieves a specific Action associated with a Rule.
 152  
          * 
 153  
          * @see org.kuali.rice.krms.impl.repository.ActionBoService#getActionByRuleIdAndSequenceNumber(java.lang.String, java.lang.Integer)
 154  
          */
 155  
         @Override
 156  
         public ActionDefinition getActionByRuleIdAndSequenceNumber(String ruleId, Integer sequenceNumber) {
 157  5
                 if (StringUtils.isBlank(ruleId)) {
 158  2
             throw new IllegalArgumentException("ruleId is null or blank");
 159  
                 }
 160  3
                 if (sequenceNumber == null) {
 161  1
             throw new IllegalArgumentException("sequenceNumber is null");
 162  
                 }
 163  2
         final Map<String, Object> map = new HashMap<String, Object>();
 164  2
         map.put("ruleId", ruleId);
 165  2
         map.put("sequenceNumber", sequenceNumber);
 166  2
                 ActionBo bo = businessObjectService.findByPrimaryKey(ActionBo.class, map);
 167  2
                 return ActionBo.to(bo);
 168  
         }
 169  
 
 170  
         /**
 171  
          * This method retrieves an ActionAttributeBo by id
 172  
          * 
 173  
          * @see org.kuali.rice.krms.impl.repository.ActionBoService#getActionsByRuleId(java.lang.String)
 174  
          */
 175  
         public ActionAttributeBo getActionAttributeById(String attrId) {
 176  0
                 if (StringUtils.isBlank(attrId)){
 177  0
             return null;                        
 178  
                 }
 179  0
                 ActionAttributeBo bo = businessObjectService.findBySinglePrimaryKey(ActionAttributeBo.class, attrId);
 180  0
                 return bo;
 181  
         }
 182  
 
 183  
     /**
 184  
      * Sets the businessObjectService attribute value.
 185  
      *
 186  
      * @param businessObjectService The businessObjectService to set.
 187  
      */
 188  
     public void setBusinessObjectService(final BusinessObjectService businessObjectService) {
 189  14
         this.businessObjectService = businessObjectService;
 190  14
     }
 191  
 
 192  
     /**
 193  
      * Converts a List<ActionBo> to an Unmodifiable List<Action>
 194  
      *
 195  
      * @param ActionBos a mutable List<ActionBo> to made completely immutable.
 196  
      * @return An unmodifiable List<Action>
 197  
      */
 198  
     List<ActionDefinition> convertListOfBosToImmutables(final Collection<ActionBo> actionBos) {
 199  2
             if (actionBos == null) return Collections.emptyList();
 200  1
         ArrayList<ActionDefinition> actions = new ArrayList<ActionDefinition>();
 201  1
         for (ActionBo bo : actionBos) {
 202  1
             ActionDefinition action = ActionBo.to(bo);
 203  1
             actions.add(action);
 204  1
         }
 205  1
         return Collections.unmodifiableList(actions);
 206  
     }
 207  
 
 208  
 
 209  
 }