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 java.util.Collection;
20 import java.util.Collections;
21 import java.util.HashMap;
22 import java.util.HashSet;
23 import java.util.List;
24 import java.util.Map;
25 import java.util.Set;
26
27 import org.apache.commons.lang.StringUtils;
28 import org.kuali.rice.krad.service.BusinessObjectService;
29 import org.kuali.rice.krms.api.repository.proposition.PropositionDefinition;
30 import org.kuali.rice.krms.api.repository.proposition.PropositionParameter;
31
32
33
34
35
36
37
38
39 public final class PropositionBoServiceImpl implements PropositionBoService {
40
41 private BusinessObjectService businessObjectService;
42
43
44
45
46
47
48
49 @Override
50 public PropositionDefinition createProposition(PropositionDefinition prop) {
51 if (prop == null){
52 throw new IllegalArgumentException("proposition is null");
53 }
54 if (null != prop.getId()) {
55 throw new IllegalStateException("for creation, PropositionDefinition.id must be null");
56 }
57
58 PropositionBo propositionBo = PropositionBo.from(prop);
59 businessObjectService.save(propositionBo);
60 return PropositionBo.to(propositionBo);
61 }
62
63
64
65
66
67
68 @Override
69 public void updateProposition(PropositionDefinition prop) {
70 if (prop == null) {
71 throw new IllegalArgumentException("proposition is null");
72 }
73 final String propIdKey = prop.getId();
74 final PropositionDefinition existing = getPropositionById(propIdKey);
75 if (existing == null) {
76 throw new IllegalStateException("the proposition does not exist: " + prop);
77 }
78 final PropositionDefinition toUpdate;
79 if (!existing.getId().equals(prop.getId())){
80 final PropositionDefinition.Builder builder = PropositionDefinition.Builder.create(prop);
81 builder.setId(existing.getId());
82 toUpdate = builder.build();
83 } else {
84 toUpdate = prop;
85 }
86
87 businessObjectService.save(PropositionBo.from(toUpdate));
88 }
89
90
91
92
93
94
95 @Override
96 public PropositionDefinition getPropositionById(String propId) {
97 if (StringUtils.isBlank(propId)){
98 throw new IllegalArgumentException("propId is null or blank");
99 }
100 PropositionBo bo = businessObjectService.findBySinglePrimaryKey(PropositionBo.class, propId);
101 return PropositionBo.to(bo);
102 }
103
104 @Override
105 public Set<PropositionDefinition> getPropositionsByType(String typeId) {
106 if (org.apache.commons.lang.StringUtils.isBlank(typeId)) {
107 throw new IllegalArgumentException("typeId is null or blank");
108 }
109
110 final Map<String, Object> map = new HashMap<String, Object>();
111 map.put("typeId", typeId);
112 Collection<PropositionBo> bos = (Collection<PropositionBo>) businessObjectService.findMatching(PropositionBo.class, map);
113
114 return convertBosToImmutables(bos);
115 }
116
117 @Override
118 public Set<PropositionDefinition> getPropositionsByRule(String ruleId) {
119 if (org.apache.commons.lang.StringUtils.isBlank(ruleId)) {
120 throw new IllegalArgumentException("ruleId is null or blank");
121 }
122
123 final Map<String, Object> map = new HashMap<String, Object>();
124 map.put("ruleId", ruleId);
125 Collection<PropositionBo> bos = (Collection<PropositionBo>) businessObjectService.findMatching(PropositionBo.class, map);
126
127 return convertBosToImmutables(bos);
128 }
129
130 public Set<PropositionDefinition> convertBosToImmutables(final Collection<PropositionBo> propositionBos) {
131 Set<PropositionDefinition> immutables = new HashSet<PropositionDefinition>();
132 if (propositionBos != null) {
133 PropositionDefinition immutable = null;
134 for (PropositionBo bo : propositionBos ) {
135 immutable = to(bo);
136 immutables.add(immutable);
137 }
138 }
139 return Collections.unmodifiableSet(immutables);
140 }
141
142 public PropositionDefinition to(PropositionBo propositionBo) {
143 return PropositionBo.to(propositionBo);
144 }
145
146
147
148
149
150
151
152 @Override
153 public void createParameter(PropositionParameter parameter) {
154 if (parameter == null){
155 throw new IllegalArgumentException("parameter is null");
156 }
157 final String propIdKey = parameter.getPropId();
158 final Integer seqNoKey = parameter.getSequenceNumber();
159 final PropositionParameter existing = getParameterByPropIdAndSequenceNumber(propIdKey, seqNoKey);
160 if (existing != null && existing.getPropId().equals(propIdKey) && existing.getSequenceNumber().equals(seqNoKey)){
161 throw new IllegalStateException("the parameter to create already exists: " + parameter);
162 }
163
164 businessObjectService.save(PropositionParameterBo.from(parameter));
165 }
166
167
168
169
170
171
172 @Override
173 public void updateParameter(PropositionParameter parameter) {
174 if (parameter == null) {
175 throw new IllegalArgumentException("parameter is null");
176 }
177 final String propIdKey = parameter.getPropId();
178 final Integer seqNoKey = parameter.getSequenceNumber();
179 final PropositionParameter existing = getParameterByPropIdAndSequenceNumber(propIdKey, seqNoKey);
180 if (existing == null) {
181 throw new IllegalStateException("the parameter does not exist: " + parameter);
182 }
183 final PropositionParameter toUpdate;
184 if (!existing.getId().equals(parameter.getId())){
185 final PropositionParameter.Builder builder = PropositionParameter.Builder.create(parameter);
186 builder.setId(existing.getId());
187 toUpdate = builder.build();
188 } else {
189 toUpdate = parameter;
190 }
191
192 businessObjectService.save(PropositionParameterBo.from(toUpdate));
193 }
194
195 @Override
196 public void deleteProposition(String propId) {
197 if (propId == null){ throw new IllegalArgumentException("propId is null"); }
198 final PropositionDefinition existing = getPropositionById(propId);
199 if (existing == null){ throw new IllegalStateException("the Proposition to delete does not exists: " + propId);}
200
201 List<PropositionParameter> propositionParameters = existing.getParameters();
202 for( PropositionParameter prop : propositionParameters) {
203 businessObjectService.delete(PropositionParameterBo.from(prop));
204 }
205
206 businessObjectService.delete(from(existing));
207 }
208
209
210
211
212
213
214 @Override
215 public List<PropositionParameter> getParameters(String propId) {
216 if (StringUtils.isBlank(propId)) {
217 throw new IllegalArgumentException("propId is null or blank");
218 }
219 final Map<String, Object> map = new HashMap<String, Object>();
220 map.put("propId", propId);
221 List<PropositionParameterBo> bos = (List<PropositionParameterBo>) businessObjectService.findMatchingOrderBy(PropositionParameterBo.class, map, "sequenceNumber", true);
222 return PropositionParameterBo.to(bos);
223 }
224
225
226
227
228
229
230 @Override
231 public PropositionParameter getParameterById(String id) {
232 if (StringUtils.isBlank(id)) {
233 throw new IllegalArgumentException("id is null or blank");
234 }
235 PropositionParameterBo bo = businessObjectService.findBySinglePrimaryKey(PropositionParameterBo.class, id);
236 return PropositionParameterBo.to(bo);
237 }
238
239
240
241
242
243
244 @Override
245 public PropositionParameter getParameterByPropIdAndSequenceNumber(
246 String propId, Integer sequenceNumber) {
247 if (StringUtils.isBlank(propId)) {
248 throw new IllegalArgumentException("propId is null or blank");
249 }
250 if (sequenceNumber == null) {
251 throw new IllegalArgumentException("sequenceNumber is null");
252 }
253 final Map<String, Object> map = new HashMap<String, Object>();
254 map.put("propId", propId);
255 map.put("sequenceNumber", sequenceNumber);
256 PropositionParameterBo bo = businessObjectService.findByPrimaryKey(PropositionParameterBo.class, map);
257 return PropositionParameterBo.to(bo);
258 }
259
260
261
262
263
264
265 public PropositionBo from(PropositionDefinition proposition) {
266 if (proposition == null) { return null; }
267 PropositionBo propositionBo = new PropositionBo();
268 propositionBo.setDescription(proposition.getDescription());
269 propositionBo.setTypeId(proposition.getTypeId());
270 propositionBo.setRuleId(proposition.getRuleId());
271 propositionBo.setPropositionTypeCode(proposition.getPropositionTypeCode());
272 propositionBo.setCompoundOpCode(proposition.getCompoundOpCode());
273 propositionBo.setId(proposition.getId());
274 propositionBo.setVersionNumber(proposition.getVersionNumber());
275 return propositionBo;
276 }
277
278
279
280
281
282
283 public void setBusinessObjectService(BusinessObjectService businessObjectService) {
284 this.businessObjectService = businessObjectService;
285 }
286
287 }