1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
32
33
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
53
54
55
56 @Override
57 public void updateRule(RuleDefinition rule) {
58 if (rule == null){
59 throw new IllegalArgumentException("rule is null");
60 }
61
62
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
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
79 RuleBo boToUpdate = RuleBo.from(toUpdate);
80
81
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
87 businessObjectService.save(boToUpdate);
88 }
89
90
91
92
93
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
106
107
108
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
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
188
189
190
191 public void setBusinessObjectService(final BusinessObjectService businessObjectService) {
192 this.businessObjectService = businessObjectService;
193 }
194
195
196
197
198
199
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 }