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 import org.apache.commons.collections.CollectionUtils;
19 import org.apache.commons.lang.StringUtils;
20 import org.kuali.rice.krad.bo.PersistableBusinessObjectBase;
21 import org.kuali.rice.krad.service.KRADServiceLocator;
22 import org.kuali.rice.krad.service.SequenceAccessorService;
23 import org.kuali.rice.krad.util.ObjectUtils;
24 import org.kuali.rice.krms.api.repository.agenda.AgendaItemDefinition;
25 import org.kuali.rice.krms.api.repository.type.KrmsTypeDefinition;
26
27 import java.util.ArrayList;
28 import java.util.List;
29 import java.util.Map;
30
31
32
33
34
35
36
37 public class AgendaItemBo extends PersistableBusinessObjectBase {
38
39 public static final String COPY_OF_TEXT = "Copy of ";
40 private static final String KRMS_AGENDA_ITM_S = "KRMS_AGENDA_ITM_S";
41
42 private String id;
43 private String agendaId;
44 private String ruleId;
45 private String subAgendaId;
46 private String whenTrueId;
47 private String whenFalseId;
48 private String alwaysId;
49
50 private RuleBo rule;
51
52 private AgendaItemBo whenTrue;
53 private AgendaItemBo whenFalse;
54 private AgendaItemBo always;
55
56 private static SequenceAccessorService sequenceAccessorService;
57
58 public String getUl(AgendaItemBo firstItem) {
59 return ("<ul>" + getUlHelper(firstItem) + "</ul>");
60 }
61
62 public String getUlHelper(AgendaItemBo item) {
63 StringBuilder sb = new StringBuilder();
64 sb.append("<li>" + ruleId + "</li>");
65 if (whenTrue != null) {
66 sb.append("<ul><li>when true</li><ul>");
67 sb.append(getUlHelper(whenTrue));
68 sb.append("</ul></ul>");
69 }
70 if (whenFalse != null) {
71 sb.append("<ul><li>when false</li><ul>");
72 sb.append(getUlHelper(whenFalse));
73 sb.append("</ul></ul>");
74 }
75 if (always != null) {
76 sb.append(getUlHelper(always));
77 }
78 return sb.toString();
79 }
80
81 public String getRuleText() {
82 StringBuilder resultBuilder = new StringBuilder();
83 if (getRule() != null) {
84 if (StringUtils.isBlank(getRule().getName())) {
85 resultBuilder.append("- unnamed rule -");
86 } else {
87 resultBuilder.append(getRule().getName());
88 }
89 if (!StringUtils.isBlank(getRule().getDescription())) {
90 resultBuilder.append(": ");
91 resultBuilder.append(getRule().getDescription());
92 }
93
94 if (!CollectionUtils.isEmpty(getRule().getActions())) {
95 resultBuilder.append(" [");
96 ActionBo action = getRule().getActions().get(0);
97
98 KrmsTypeDefinition krmsTypeDefn =
99 KrmsRepositoryServiceLocator.getKrmsTypeRepositoryService().getTypeById(action.getTypeId());
100
101 resultBuilder.append(krmsTypeDefn.getName());
102 resultBuilder.append(": ");
103 resultBuilder.append(action.getName());
104
105 if (getRule().getActions().size() > 1) {
106 resultBuilder.append(" ... ");
107 }
108 resultBuilder.append("]");
109 }
110 } else {
111 throw new IllegalStateException();
112 }
113 return resultBuilder.toString();
114 }
115
116
117
118
119
120 public List<AgendaItemBo> getAlwaysList() {
121 List<AgendaItemBo> results = new ArrayList<AgendaItemBo>();
122
123 AgendaItemBo currentNode = this;
124 while (currentNode.always != null) {
125 results.add(currentNode.always);
126 currentNode = currentNode.always;
127 }
128
129 return results;
130 }
131
132
133
134
135 public String getId() {
136 return this.id;
137 }
138
139
140
141
142 public void setId(String id) {
143 this.id = id;
144 }
145
146
147
148
149 public String getAgendaId() {
150 return this.agendaId;
151 }
152
153
154
155
156 public void setAgendaId(String agendaId) {
157 this.agendaId = agendaId;
158 }
159
160
161
162
163 public String getRuleId() {
164 return this.ruleId;
165 }
166
167
168
169
170 public void setRuleId(String ruleId) {
171 this.ruleId = ruleId;
172 }
173
174
175
176
177 public String getSubAgendaId() {
178 return this.subAgendaId;
179 }
180
181
182
183
184 public void setSubAgendaId(String subAgendaId) {
185 this.subAgendaId = subAgendaId;
186 }
187
188
189
190
191
192 public String getWhenTrueId() {
193 return this.whenTrueId;
194 }
195
196
197
198
199 public void setWhenTrueId(String whenTrueId) {
200 this.whenTrueId = whenTrueId;
201 }
202
203
204
205
206 public String getWhenFalseId() {
207 return this.whenFalseId;
208 }
209
210
211
212
213 public void setWhenFalseId(String whenFalseId) {
214 this.whenFalseId = whenFalseId;
215 }
216
217
218
219
220 public String getAlwaysId() {
221 return this.alwaysId;
222 }
223
224
225
226
227 public void setAlwaysId(String alwaysId) {
228 this.alwaysId = alwaysId;
229 }
230
231
232
233
234 public AgendaItemBo getWhenTrue() {
235 return this.whenTrue;
236 }
237
238
239
240
241 public void setWhenTrue(AgendaItemBo whenTrue) {
242 this.whenTrue = whenTrue;
243 if (whenTrue != null) {
244 setWhenTrueId(whenTrue.getId());
245 } else {
246 setWhenTrueId(null);
247 }
248 }
249
250
251
252
253 public AgendaItemBo getWhenFalse() {
254 return this.whenFalse;
255 }
256
257
258
259
260 public void setWhenFalse(AgendaItemBo whenFalse) {
261 this.whenFalse = whenFalse;
262 if (whenFalse != null) {
263 setWhenFalseId(whenFalse.getId());
264 } else {
265 setWhenFalseId(null);
266 }
267 }
268
269
270
271
272 public AgendaItemBo getAlways() {
273 return this.always;
274 }
275
276
277
278
279 public void setAlways(AgendaItemBo always) {
280 this.always = always;
281 if (always != null) {
282 setAlwaysId(always.getId());
283 } else {
284 setAlwaysId(null);
285 }
286 }
287
288
289
290
291 public RuleBo getRule() {
292 return this.rule;
293 }
294
295
296
297
298 public void setRule(RuleBo rule) {
299 this.rule = rule;
300 if (rule != null) {
301 setRuleId(rule.getId());
302 } else {
303 setRuleId(null);
304 }
305 }
306
307
308
309
310
311
312
313 static AgendaItemDefinition to(AgendaItemBo bo) {
314 if (bo == null) { return null; }
315 AgendaItemDefinition.Builder builder =
316 AgendaItemDefinition.Builder.create(bo.getId(), bo.getAgendaId());
317 builder.setRuleId(bo.getRuleId());
318 builder.setSubAgendaId(bo.getSubAgendaId());
319 builder.setWhenTrueId(bo.getWhenTrueId());
320 builder.setWhenFalseId(bo.getWhenFalseId());
321 builder.setAlwaysId(bo.getAlwaysId());
322
323 return builder.build();
324 }
325
326
327
328
329
330
331 static AgendaItemBo from(AgendaItemDefinition im) {
332 if (im == null) { return null; }
333
334 AgendaItemBo bo = new AgendaItemBo();
335 bo.id = im.getId();
336 bo.agendaId = im.getAgendaId();
337 bo.ruleId = im.getRuleId();
338 bo.subAgendaId = im.getSubAgendaId();
339 bo.whenTrueId = im.getWhenTrueId();
340 bo.whenFalseId = im.getWhenFalseId();
341 bo.alwaysId = im.getAlwaysId();
342
343 return bo;
344 }
345
346
347
348
349
350
351
352
353 public AgendaItemBo copyAgendaItem(AgendaBo copiedAgenda, Map<String, RuleBo> oldRuleIdToNew,
354 Map<String, AgendaItemBo> oldAgendaItemIdToNew, List<AgendaItemBo> copiedAgendaItems, final String dts) {
355
356 AgendaItemBo copiedAgendaItem = (AgendaItemBo) ObjectUtils.deepCopy(this);
357 copiedAgendaItem.setId(getNewId());
358 copiedAgendaItem.setAgendaId(copiedAgenda.getId());
359
360 oldAgendaItemIdToNew.put(this.getId(), copiedAgendaItem);
361
362
363 if (!oldRuleIdToNew.containsKey(this.getRuleId())) {
364 if (this.getRule() != null) {
365 copiedAgendaItem.setRule(this.getRule().copyRule(COPY_OF_TEXT + this.getRule().getName() + " " + dts));
366 oldRuleIdToNew.put(this.getRuleId(), copiedAgendaItem.getRule());
367 }
368 } else {
369 copiedAgendaItem.setRule(oldRuleIdToNew.get(this.getRuleId()));
370 }
371
372 if (copiedAgendaItem.getWhenFalse() != null) {
373 if (!oldAgendaItemIdToNew.containsKey(this.getWhenFalseId())) {
374 copiedAgendaItem.setWhenFalse(this.getWhenFalse().copyAgendaItem(copiedAgenda, oldRuleIdToNew, oldAgendaItemIdToNew, copiedAgendaItems, dts));
375 oldAgendaItemIdToNew.put(this.getWhenFalseId(), copiedAgendaItem.getWhenFalse());
376 copiedAgendaItems.add(copiedAgendaItem.getWhenFalse());
377 } else {
378 copiedAgendaItem.setWhenFalse(oldAgendaItemIdToNew.get(this.getWhenFalseId()));
379 }
380 }
381
382 if (copiedAgendaItem.getWhenTrue() != null) {
383 if (!oldAgendaItemIdToNew.containsKey(this.getWhenTrueId())) {
384 copiedAgendaItem.setWhenTrue(this.getWhenTrue().copyAgendaItem(copiedAgenda, oldRuleIdToNew, oldAgendaItemIdToNew, copiedAgendaItems, dts));
385 oldAgendaItemIdToNew.put(this.getWhenTrueId(), copiedAgendaItem.getWhenTrue());
386 copiedAgendaItems.add(copiedAgendaItem.getWhenTrue());
387 } else {
388 copiedAgendaItem.setWhenTrue(oldAgendaItemIdToNew.get(this.getWhenTrueId()));
389 }
390 }
391
392 if (copiedAgendaItem.getAlways() != null) {
393 if (!oldAgendaItemIdToNew.containsKey(this.getAlwaysId())) {
394 copiedAgendaItem.setAlways(this.getAlways().copyAgendaItem(copiedAgenda, oldRuleIdToNew, oldAgendaItemIdToNew, copiedAgendaItems, dts));
395 oldAgendaItemIdToNew.put(this.getAlwaysId(), copiedAgendaItem.getAlways());
396 copiedAgendaItems.add(copiedAgendaItem.getAlways());
397 } else {
398 copiedAgendaItem.setAlways(oldAgendaItemIdToNew.get(this.getAlwaysId()));
399 }
400 }
401 return copiedAgendaItem;
402 }
403
404
405
406
407
408
409 public static void setSequenceAccessorService(SequenceAccessorService sas) {
410 sequenceAccessorService = sas;
411 }
412
413
414
415
416
417 private static String getNewId() {
418 if (sequenceAccessorService == null) {
419
420 return KRADServiceLocator.getSequenceAccessorService().getNextAvailableSequenceNumber(KRMS_AGENDA_ITM_S, AgendaItemBo.class) + "";
421 }
422 Long id = sequenceAccessorService.getNextAvailableSequenceNumber(KRMS_AGENDA_ITM_S, AgendaItemBo.class);
423 return id.toString();
424 }
425 }