1 /**
2 * Copyright 2005-2012 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.KrmsImplConstants.PropertyNames;
23
24 import java.util.*;
25
26 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 if (rule == null){
38 throw new IllegalArgumentException("rule is null");
39 }
40 final String nameKey = rule.getName();
41 final String namespaceKey = rule.getNamespace();
42 final RuleDefinition existing = getRuleByNameAndNamespace(nameKey, namespaceKey);
43 if (existing != null){
44 throw new IllegalStateException("the rule to create already exists: " + rule);
45 }
46 RuleBo ruleBo = RuleBo.from(rule);
47 businessObjectService.save(ruleBo);
48 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 if (rule == null){
59 throw new IllegalArgumentException("rule is null");
60 }
61
62 // must already exist to be able to update
63 final String ruleIdKey = rule.getId();
64 final RuleBo existing = businessObjectService.findBySinglePrimaryKey(RuleBo.class, ruleIdKey);
65 if (existing == null) {
66 throw new IllegalStateException("the rule does not exist: " + rule);
67 }
68 final RuleDefinition toUpdate;
69 if (!existing.getId().equals(rule.getId())){
70 // if passed in id does not match existing id, correct it
71 final RuleDefinition.Builder builder = RuleDefinition.Builder.create(rule);
72 builder.setId(existing.getId());
73 toUpdate = builder.build();
74 } else {
75 toUpdate = rule;
76 }
77
78 // copy all updateable fields to bo
79 RuleBo boToUpdate = RuleBo.from(toUpdate);
80
81 // delete any old, existing attributes
82 Map<String,String> fields = new HashMap<String,String>(1);
83 fields.put(PropertyNames.Rule.RULE_ID, toUpdate.getId());
84 businessObjectService.deleteMatching(RuleAttributeBo.class, fields);
85
86 // update the rule and create new attributes
87 businessObjectService.save(boToUpdate);
88 }
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 if (StringUtils.isBlank(ruleId)){
98 throw new IllegalArgumentException("rule id is null");
99 }
100 RuleBo bo = businessObjectService.findBySinglePrimaryKey(RuleBo.class, ruleId);
101 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 if (StringUtils.isBlank(name)) {
113 throw new IllegalArgumentException("name is blank");
114 }
115 if (StringUtils.isBlank(namespace)) {
116 throw new IllegalArgumentException("namespace is blank");
117 }
118
119 final Map<String, Object> map = new HashMap<String, Object>();
120 map.put("name", name);
121 map.put("namespace", namespace);
122
123 RuleBo myRule = businessObjectService.findByPrimaryKey(RuleBo.class, Collections.unmodifiableMap(map));
124 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 if (StringUtils.isBlank(attrId)){
180 return null;
181 }
182 RuleAttributeBo bo = businessObjectService.findBySinglePrimaryKey(RuleAttributeBo.class, attrId);
183 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 this.businessObjectService = businessObjectService;
193 }
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 ArrayList<RuleDefinition> rules = new ArrayList<RuleDefinition>();
203 for (RuleBo bo : ruleBos) {
204 RuleDefinition rule = RuleBo.to(bo);
205 rules.add(rule);
206 }
207 return Collections.unmodifiableList(rules);
208 }
209
210 }