001 /**
002 * Copyright 2005-2012 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package org.kuali.rice.krms.impl.repository;
017
018 import java.util.ArrayList;
019 import java.util.List;
020 import java.util.Map;
021
022 import org.apache.commons.collections.CollectionUtils;
023 import org.apache.commons.lang.StringUtils;
024 import org.kuali.rice.krad.bo.PersistableBusinessObjectBase;
025 import org.kuali.rice.krad.service.KRADServiceLocator;
026 import org.kuali.rice.krad.service.SequenceAccessorService;
027 import org.kuali.rice.krad.util.ObjectUtils;
028 import org.kuali.rice.krms.api.repository.agenda.AgendaItemDefinition;
029 import org.kuali.rice.krms.api.repository.type.KrmsTypeDefinition;
030
031 /**
032 * Agenda Item business object
033 *
034 * @author Kuali Rice Team (rice.collab@kuali.org)
035 *
036 */
037 public class AgendaItemBo extends PersistableBusinessObjectBase {
038
039 public static final String COPY_OF_TEXT = "Copy of ";
040 private static final String KRMS_AGENDA_ITM_S = "KRMS_AGENDA_ITM_S";
041
042 private String id;
043 private String agendaId;
044 private String ruleId;
045 private String subAgendaId;
046 private String whenTrueId;
047 private String whenFalseId;
048 private String alwaysId;
049
050 private RuleBo rule;
051
052 private AgendaItemBo whenTrue;
053 private AgendaItemBo whenFalse;
054 private AgendaItemBo always;
055
056 public String getUl(AgendaItemBo firstItem) {
057 return ("<ul>" + getUlHelper(firstItem) + "</ul>");
058 }
059
060 public String getUlHelper(AgendaItemBo item) {
061 StringBuilder sb = new StringBuilder();
062 sb.append("<li>" + ruleId + "</li>");
063 if (whenTrue != null) {
064 sb.append("<ul><li>when true</li><ul>");
065 sb.append(getUlHelper(whenTrue));
066 sb.append("</ul></ul>");
067 }
068 if (whenFalse != null) {
069 sb.append("<ul><li>when false</li><ul>");
070 sb.append(getUlHelper(whenFalse));
071 sb.append("</ul></ul>");
072 }
073 if (always != null) {
074 sb.append(getUlHelper(always));
075 }
076 return sb.toString();
077 }
078
079 public String getRuleText() {
080 StringBuilder resultBuilder = new StringBuilder();
081 if (getRule() != null) {
082 if (StringUtils.isBlank(getRule().getName())) {
083 resultBuilder.append("- unnamed rule -");
084 } else {
085 resultBuilder.append(getRule().getName());
086 }
087 if (!StringUtils.isBlank(getRule().getDescription())) {
088 resultBuilder.append(": ");
089 resultBuilder.append(getRule().getDescription());
090 }
091 // add a description of the action configured on the rule, if there is one
092 if (!CollectionUtils.isEmpty(getRule().getActions())) {
093 resultBuilder.append(" [");
094 ActionBo action = getRule().getActions().get(0);
095
096 KrmsTypeDefinition krmsTypeDefn =
097 KrmsRepositoryServiceLocator.getKrmsTypeRepositoryService().getTypeById(action.getTypeId());
098
099 resultBuilder.append(krmsTypeDefn.getName());
100 resultBuilder.append(": ");
101 resultBuilder.append(action.getName());
102
103 if (getRule().getActions().size() > 1) {
104 resultBuilder.append(" ... ");
105 }
106 resultBuilder.append("]");
107 }
108 } else {
109 throw new IllegalStateException();
110 }
111 return resultBuilder.toString();
112 }
113
114 // def List<AgendaItemBo> alwaysList
115 // def List<AgendaItemBo> whenTrueList
116 // def List<AgendaItemBo> whenFalseList
117
118 public List<AgendaItemBo> getAlwaysList() {
119 List<AgendaItemBo> results = new ArrayList<AgendaItemBo>();
120
121 AgendaItemBo currentNode = this;
122 while (currentNode.always != null) {
123 results.add(currentNode.always);
124 currentNode = currentNode.always;
125 }
126
127 return results;
128 }
129
130 /**
131 * @return the id
132 */
133 public String getId() {
134 return this.id;
135 }
136
137 /**
138 * @param id the id to set
139 */
140 public void setId(String id) {
141 this.id = id;
142 }
143
144 /**
145 * @return the agendaId
146 */
147 public String getAgendaId() {
148 return this.agendaId;
149 }
150
151 /**
152 * @param agendaId the agendaId to set
153 */
154 public void setAgendaId(String agendaId) {
155 this.agendaId = agendaId;
156 }
157
158 /**
159 * @return the ruleId
160 */
161 public String getRuleId() {
162 return this.ruleId;
163 }
164
165 /**
166 * @param ruleId the ruleId to set
167 */
168 public void setRuleId(String ruleId) {
169 this.ruleId = ruleId;
170 }
171
172 /**
173 * @return the subAgendaId
174 */
175 public String getSubAgendaId() {
176 return this.subAgendaId;
177 }
178
179 /**
180 * @param subAgendaId the subAgendaId to set
181 */
182 public void setSubAgendaId(String subAgendaId) {
183 this.subAgendaId = subAgendaId;
184 }
185
186
187 /**
188 * @return the whenTrueId
189 */
190 public String getWhenTrueId() {
191 return this.whenTrueId;
192 }
193
194 /**
195 * @param whenTrueId the whenTrueId to set
196 */
197 public void setWhenTrueId(String whenTrueId) {
198 this.whenTrueId = whenTrueId;
199 }
200
201 /**
202 * @return the whenFalseId
203 */
204 public String getWhenFalseId() {
205 return this.whenFalseId;
206 }
207
208 /**
209 * @param whenFalseId the whenFalseId to set
210 */
211 public void setWhenFalseId(String whenFalseId) {
212 this.whenFalseId = whenFalseId;
213 }
214
215 /**
216 * @return the alwaysId
217 */
218 public String getAlwaysId() {
219 return this.alwaysId;
220 }
221
222 /**
223 * @param alwaysId the alwaysId to set
224 */
225 public void setAlwaysId(String alwaysId) {
226 this.alwaysId = alwaysId;
227 }
228
229 /**
230 * @return the whenTrue
231 */
232 public AgendaItemBo getWhenTrue() {
233 return this.whenTrue;
234 }
235
236 /**
237 * @param whenTrue the whenTrue to set
238 */
239 public void setWhenTrue(AgendaItemBo whenTrue) {
240 this.whenTrue = whenTrue;
241 }
242
243 /**
244 * @return the whenFalse
245 */
246 public AgendaItemBo getWhenFalse() {
247 return this.whenFalse;
248 }
249
250 /**
251 * @param whenFalse the whenFalse to set
252 */
253 public void setWhenFalse(AgendaItemBo whenFalse) {
254 this.whenFalse = whenFalse;
255 }
256
257 /**
258 * @return the always
259 */
260 public AgendaItemBo getAlways() {
261 return this.always;
262 }
263
264 /**
265 * @param always the always to set
266 */
267 public void setAlways(AgendaItemBo always) {
268 this.always = always;
269 }
270
271 /**
272 * @return the rule
273 */
274 public RuleBo getRule() {
275 return this.rule;
276 }
277
278 /**
279 * @param rule the rule to set
280 */
281 public void setRule(RuleBo rule) {
282 this.rule = rule;
283 }
284
285
286 /**
287 * Converts a mutable bo to it's immutable counterpart
288 * @param bo the mutable business object
289 * @return the immutable object
290 */
291 static AgendaItemDefinition to(AgendaItemBo bo) {
292 if (bo == null) { return null; }
293 AgendaItemDefinition.Builder builder =
294 AgendaItemDefinition.Builder.create(bo.getId(), bo.getAgendaId());
295 builder.setRuleId(bo.getRuleId());
296 builder.setSubAgendaId(bo.getSubAgendaId());
297 builder.setWhenTrueId(bo.getWhenTrueId());
298 builder.setWhenFalseId(bo.getWhenFalseId());
299 builder.setAlwaysId(bo.getAlwaysId());
300
301 return builder.build();
302 }
303
304 /**
305 * Converts a immutable object to it's mutable bo counterpart
306 * @param im immutable object
307 * @return the mutable bo
308 */
309 static AgendaItemBo from(AgendaItemDefinition im) {
310 if (im == null) { return null; }
311
312 AgendaItemBo bo = new AgendaItemBo();
313 bo.id = im.getId();
314 bo.agendaId = im.getAgendaId();
315 bo.ruleId = im.getRuleId();
316 bo.subAgendaId = im.getSubAgendaId();
317 bo.whenTrueId = im.getWhenTrueId();
318 bo.whenFalseId = im.getWhenFalseId();
319 bo.alwaysId = im.getAlwaysId();
320
321 return bo;
322 }
323
324 /**
325 * Returns a copy of this AgendaItem
326 * @param copiedAgenda the new Agenda that the copied AgendiaItem will be associated with
327 * @param oldRuleIdToNew Map<String, RuleBo> mapping of old rule id to the new RuleBo
328 * @param dts DateTimeStamp to append to the copied AgendaItem name
329 * @return AgendaItemBo copy of this AgendaItem with new id and name
330 */
331 public AgendaItemBo copyAgendaItem(AgendaBo copiedAgenda, Map<String, RuleBo> oldRuleIdToNew, final String dts) {
332 // Use deepCopy and update all the ids.
333 AgendaItemBo copiedAgendaItem = (AgendaItemBo) ObjectUtils.deepCopy(this);
334
335 copiedAgendaItem.setId(getNewId());
336
337 copiedAgendaItem.setAgendaId(copiedAgenda.getId());
338
339 // Don't create another copy of a rule that we have already copied.
340 if (!oldRuleIdToNew.containsKey(this.getRuleId())) {
341 copiedAgendaItem.setRule(this.getRule().copyRule(COPY_OF_TEXT + this.getRule().getName() + " " + dts));
342 copiedAgendaItem.setRuleId(copiedAgendaItem.getRule().getId());
343 oldRuleIdToNew.put(this.getRuleId(), copiedAgendaItem.getRule());
344 } else {
345 copiedAgendaItem.setRule(oldRuleIdToNew.get(this.getRuleId()));
346 copiedAgendaItem.setRuleId(oldRuleIdToNew.get(this.getRuleId()).getId());
347 }
348
349 if (copiedAgendaItem.getWhenFalse() != null) {
350 copiedAgendaItem.setWhenFalse(this.getWhenFalse().copyAgendaItem(copiedAgenda, oldRuleIdToNew, dts));
351 copiedAgendaItem.setWhenFalseId(copiedAgendaItem.getWhenFalse().getId());
352 }
353
354 if (copiedAgendaItem.getWhenTrue() != null) {
355 copiedAgendaItem.setWhenTrue(this.getWhenTrue().copyAgendaItem(copiedAgenda, oldRuleIdToNew, dts));
356 copiedAgendaItem.setWhenTrueId(copiedAgendaItem.getWhenTrue().getId());
357 }
358
359 if (copiedAgendaItem.getAlways() != null) {
360 copiedAgendaItem.setAlways(this.getAlways().copyAgendaItem(copiedAgenda, oldRuleIdToNew, dts));
361 copiedAgendaItem.setAlwaysId(copiedAgendaItem.getAlways().getId());
362 }
363
364 return copiedAgendaItem;
365 }
366
367 /**
368 * Returns the next available AgendaItem id.
369 * @return String the next available id
370 */
371 private static String getNewId(){
372 SequenceAccessorService sas = KRADServiceLocator.getSequenceAccessorService();
373 Long id = sas.getNextAvailableSequenceNumber(KRMS_AGENDA_ITM_S, AgendaItemBo.class);
374 return id.toString();
375 }
376
377 }