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