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 final Map<String, Object> map = new HashMap<String, Object>();
110 map.put("typeId", typeId);
111 Set<PropositionBo> bos = (Set<PropositionBo>) businessObjectService.findMatching(PropositionBo.class, map);
112 return convertBosToImmutables(bos);
113 }
114
115 @Override
116 public Set<PropositionDefinition> getPropositionsByRule(String ruleId) {
117 if (org.apache.commons.lang.StringUtils.isBlank(ruleId)) {
118 throw new IllegalArgumentException("ruleId is null or blank");
119 }
120 final Map<String, Object> map = new HashMap<String, Object>();
121 map.put("ruleId", ruleId);
122 Set<PropositionBo> bos = (Set<PropositionBo>) businessObjectService.findMatching(PropositionBo.class, map);
123 return convertBosToImmutables(bos);
124 }
125
126 public Set<PropositionDefinition> convertBosToImmutables(final Collection<PropositionBo> propositionBos) {
127 Set<PropositionDefinition> immutables = new HashSet<PropositionDefinition>();
128 if (propositionBos != null) {
129 PropositionDefinition immutable = null;
130 for (PropositionBo bo : propositionBos ) {
131 immutable = to(bo);
132 immutables.add(immutable);
133 }
134 }
135 return Collections.unmodifiableSet(immutables);
136 }
137
138 public PropositionDefinition to(PropositionBo propositionBo) {
139 return PropositionBo.to(propositionBo);
140 }
141
142
143
144
145
146
147
148 @Override
149 public void createParameter(PropositionParameter parameter) {
150 if (parameter == null){
151 throw new IllegalArgumentException("parameter is null");
152 }
153 final String propIdKey = parameter.getPropId();
154 final Integer seqNoKey = parameter.getSequenceNumber();
155 final PropositionParameter existing = getParameterByPropIdAndSequenceNumber(propIdKey, seqNoKey);
156 if (existing != null && existing.getPropId().equals(propIdKey) && existing.getSequenceNumber().equals(seqNoKey)){
157 throw new IllegalStateException("the parameter to create already exists: " + parameter);
158 }
159
160 businessObjectService.save(PropositionParameterBo.from(parameter));
161 }
162
163
164
165
166
167
168 @Override
169 public void updateParameter(PropositionParameter parameter) {
170 if (parameter == null) {
171 throw new IllegalArgumentException("parameter is null");
172 }
173 final String propIdKey = parameter.getPropId();
174 final Integer seqNoKey = parameter.getSequenceNumber();
175 final PropositionParameter existing = getParameterByPropIdAndSequenceNumber(propIdKey, seqNoKey);
176 if (existing == null) {
177 throw new IllegalStateException("the parameter does not exist: " + parameter);
178 }
179 final PropositionParameter toUpdate;
180 if (!existing.getId().equals(parameter.getId())){
181 final PropositionParameter.Builder builder = PropositionParameter.Builder.create(parameter);
182 builder.setId(existing.getId());
183 toUpdate = builder.build();
184 } else {
185 toUpdate = parameter;
186 }
187
188 businessObjectService.save(PropositionParameterBo.from(toUpdate));
189 }
190
191 @Override
192 public void deleteProposition(String propId) {
193 if (propId == null){ throw new IllegalArgumentException("propId is null"); }
194 final PropositionDefinition existing = getPropositionById(propId);
195 if (existing == null){ throw new IllegalStateException("the Proposition to delete does not exists: " + propId);}
196 businessObjectService.delete(from(existing));
197 }
198
199
200
201
202
203
204 @Override
205 public List<PropositionParameter> getParameters(String propId) {
206 if (StringUtils.isBlank(propId)) {
207 throw new IllegalArgumentException("propId is null or blank");
208 }
209 final Map<String, Object> map = new HashMap<String, Object>();
210 map.put("propId", propId);
211 List<PropositionParameterBo> bos = (List<PropositionParameterBo>) businessObjectService.findMatchingOrderBy(PropositionParameterBo.class, map, "sequenceNumber", true);
212 return PropositionParameterBo.to(bos);
213 }
214
215
216
217
218
219
220 @Override
221 public PropositionParameter getParameterById(String id) {
222 if (StringUtils.isBlank(id)) {
223 throw new IllegalArgumentException("id is null or blank");
224 }
225 PropositionParameterBo bo = businessObjectService.findBySinglePrimaryKey(PropositionParameterBo.class, id);
226 return PropositionParameterBo.to(bo);
227 }
228
229
230
231
232
233
234 @Override
235 public PropositionParameter getParameterByPropIdAndSequenceNumber(
236 String propId, Integer sequenceNumber) {
237 if (StringUtils.isBlank(propId)) {
238 throw new IllegalArgumentException("propId is null or blank");
239 }
240 if (sequenceNumber == null) {
241 throw new IllegalArgumentException("sequenceNumber is null");
242 }
243 final Map<String, Object> map = new HashMap<String, Object>();
244 map.put("propId", propId);
245 map.put("sequenceNumber", sequenceNumber);
246 PropositionParameterBo bo = businessObjectService.findByPrimaryKey(PropositionParameterBo.class, map);
247 return PropositionParameterBo.to(bo);
248 }
249
250
251
252
253
254
255 public PropositionBo from(PropositionDefinition proposition) {
256 if (proposition == null) { return null; }
257 PropositionBo propositionBo = new PropositionBo();
258 propositionBo.setDescription(proposition.getDescription());
259 propositionBo.setTypeId(proposition.getTypeId());
260 propositionBo.setRuleId(proposition.getRuleId());
261 propositionBo.setPropositionTypeCode(proposition.getPropositionTypeCode());
262 propositionBo.setCompoundOpCode(proposition.getCompoundOpCode());
263 propositionBo.setId(proposition.getId());
264 propositionBo.setVersionNumber(proposition.getVersionNumber());
265 return propositionBo;
266 }
267
268
269
270
271
272
273 public void setBusinessObjectService(BusinessObjectService businessObjectService) {
274 this.businessObjectService = businessObjectService;
275 }
276
277 }