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