View Javadoc

1   /**
2    * Copyright 2005-2011 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.krms.impl.repository;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  
21  import org.apache.commons.collections.CollectionUtils;
22  import org.apache.commons.lang.StringUtils;
23  import org.kuali.rice.krad.bo.PersistableBusinessObjectBase;
24  import org.kuali.rice.krms.api.repository.agenda.AgendaItem;
25  import org.kuali.rice.krms.api.repository.type.KrmsTypeDefinition;
26  
27  /**
28   * Agenda Item business object
29   * 
30   * @author Kuali Rice Team (rice.collab@kuali.org)
31   *
32   */
33  public class AgendaItemBo extends PersistableBusinessObjectBase {
34  
35  	private String id;
36  	private String agendaId;
37  	private String ruleId;
38  	private String subAgendaId;
39  	private String whenTrueId;
40  	private String whenFalseId;
41  	private String alwaysId;
42  	
43  	private RuleBo rule;
44  	
45  	private AgendaItemBo whenTrue;
46  	private AgendaItemBo whenFalse;
47  	private AgendaItemBo always;
48  	
49  	public String getUl(AgendaItemBo firstItem) {
50  		return ("<ul>" + getUlHelper(firstItem) + "</ul>");
51  	}
52  	
53  	public String getUlHelper(AgendaItemBo item) {
54  		StringBuilder sb = new StringBuilder();
55  		sb.append("<li>" + ruleId + "</li>");
56  		if (whenTrue != null) {
57  			sb.append("<ul><li>when true</li><ul>");
58  			sb.append(getUlHelper(whenTrue));
59  			sb.append("</ul></ul>");
60  		}
61  		if (whenFalse != null) {
62  			sb.append("<ul><li>when false</li><ul>");
63  			sb.append(getUlHelper(whenFalse));
64  			sb.append("</ul></ul>");
65  		}
66  		if (always != null) {
67  			sb.append(getUlHelper(always));
68  		}
69  		return sb.toString();
70  	}
71  
72      public String getRuleText() {
73          StringBuilder resultBuilder = new StringBuilder();
74          if (getRule() != null) {
75              if (StringUtils.isBlank(getRule().getName())) {
76                  resultBuilder.append("- unnamed rule -");
77              } else {
78                  resultBuilder.append(getRule().getName());
79              }
80              if (!StringUtils.isBlank(getRule().getDescription())) {
81                  resultBuilder.append(": ");
82                  resultBuilder.append(getRule().getDescription());
83              }
84              // add a description of the action configured on the rule, if there is one
85              if (!CollectionUtils.isEmpty(getRule().getActions())) {
86                  resultBuilder.append("   [");
87                  ActionBo action = getRule().getActions().get(0);
88  
89                  KrmsTypeDefinition krmsTypeDefn =
90                          KrmsRepositoryServiceLocator.getKrmsTypeRepositoryService().getTypeById(action.getTypeId());
91  
92                  resultBuilder.append(krmsTypeDefn.getName());
93                  resultBuilder.append(": ");
94                  resultBuilder.append(action.getName());
95  
96                  if (getRule().getActions().size() > 1) {
97                      resultBuilder.append(" ... ");
98                  }
99                  resultBuilder.append("]");
100             }
101         } else {
102             throw new IllegalStateException();
103         }
104         return resultBuilder.toString();
105     }
106 
107 //	def List<AgendaItemBo> alwaysList
108 //	def List<AgendaItemBo> whenTrueList
109 //	def List<AgendaItemBo> whenFalseList
110 	
111 	public List<AgendaItemBo> getAlwaysList() {
112 		List<AgendaItemBo> results = new ArrayList<AgendaItemBo>();
113 		
114 		AgendaItemBo currentNode = this;
115 		while (currentNode.always != null) {
116 			results.add(currentNode.always);
117 			currentNode = currentNode.always;
118 		}
119 		
120 		return results;
121 	}
122 
123 	/**
124 	 * @return the id
125 	 */
126 	public String getId() {
127 		return this.id;
128 	}
129 
130 	/**
131 	 * @param id the id to set
132 	 */
133 	public void setId(String id) {
134 		this.id = id;
135 	}
136 
137 	/**
138 	 * @return the agendaId
139 	 */
140 	public String getAgendaId() {
141 		return this.agendaId;
142 	}
143 
144 	/**
145 	 * @param agendaId the agendaId to set
146 	 */
147 	public void setAgendaId(String agendaId) {
148 		this.agendaId = agendaId;
149 	}
150 
151 	/**
152 	 * @return the ruleId
153 	 */
154 	public String getRuleId() {
155 		return this.ruleId;
156 	}
157 
158 	/**
159 	 * @param ruleId the ruleId to set
160 	 */
161 	public void setRuleId(String ruleId) {
162 		this.ruleId = ruleId;
163 	}
164 
165 	/**
166 	 * @return the subAgendaId
167 	 */
168 	public String getSubAgendaId() {
169 		return this.subAgendaId;
170 	}
171 
172 	/**
173 	 * @param subAgendaId the subAgendaId to set
174 	 */
175 	public void setSubAgendaId(String subAgendaId) {
176 		this.subAgendaId = subAgendaId;
177 	}
178 
179 
180 	/**
181 	 * @return the whenTrueId
182 	 */
183 	public String getWhenTrueId() {
184 		return this.whenTrueId;
185 	}
186 
187 	/**
188 	 * @param whenTrueId the whenTrueId to set
189 	 */
190 	public void setWhenTrueId(String whenTrueId) {
191 		this.whenTrueId = whenTrueId;
192 	}
193 
194 	/**
195 	 * @return the whenFalseId
196 	 */
197 	public String getWhenFalseId() {
198 		return this.whenFalseId;
199 	}
200 
201 	/**
202 	 * @param whenFalseId the whenFalseId to set
203 	 */
204 	public void setWhenFalseId(String whenFalseId) {
205 		this.whenFalseId = whenFalseId;
206 	}
207 
208 	/**
209 	 * @return the alwaysId
210 	 */
211 	public String getAlwaysId() {
212 		return this.alwaysId;
213 	}
214 
215 	/**
216 	 * @param alwaysId the alwaysId to set
217 	 */
218 	public void setAlwaysId(String alwaysId) {
219 		this.alwaysId = alwaysId;
220 	}
221 
222 	/**
223 	 * @return the whenTrue
224 	 */
225 	public AgendaItemBo getWhenTrue() {
226 		return this.whenTrue;
227 	}
228 
229 	/**
230 	 * @param whenTrue the whenTrue to set
231 	 */
232 	public void setWhenTrue(AgendaItemBo whenTrue) {
233 		this.whenTrue = whenTrue;
234 	}
235 
236 	/**
237 	 * @return the whenFalse
238 	 */
239 	public AgendaItemBo getWhenFalse() {
240 		return this.whenFalse;
241 	}
242 
243 	/**
244 	 * @param whenFalse the whenFalse to set
245 	 */
246 	public void setWhenFalse(AgendaItemBo whenFalse) {
247 		this.whenFalse = whenFalse;
248 	}
249 
250 	/**
251 	 * @return the always
252 	 */
253 	public AgendaItemBo getAlways() {
254 		return this.always;
255 	}
256 
257 	/**
258 	 * @param always the always to set
259 	 */
260 	public void setAlways(AgendaItemBo always) {
261 		this.always = always;
262 	}
263 	
264     /**
265      * @return the rule
266      */
267     public RuleBo getRule() {
268         return this.rule;
269     }
270 
271     /**
272      * @param rule the rule to set
273      */
274     public void setRule(RuleBo rule) {
275         this.rule = rule;
276     }
277 
278 	
279     /**
280 	* Converts a mutable bo to it's immutable counterpart
281 	* @param bo the mutable business object
282 	* @return the immutable object
283 	*/
284    static AgendaItem to(AgendaItemBo bo) {
285 	   if (bo == null) { return null; }
286 	   org.kuali.rice.krms.api.repository.agenda.AgendaItem.Builder builder = 
287 		   org.kuali.rice.krms.api.repository.agenda.AgendaItem.Builder.create(bo.getId(), bo.getAgendaId());
288 	   builder.setRuleId(bo.getRuleId());
289 	   builder.setSubAgendaId(bo.getSubAgendaId());
290 	   builder.setWhenTrueId(bo.getWhenTrueId());
291 	   builder.setWhenFalseId(bo.getWhenFalseId());
292 	   builder.setAlwaysId(bo.getAlwaysId());
293 	   
294 	   return builder.build();
295    }
296 
297    /**
298 	* Converts a immutable object to it's mutable bo counterpart
299 	* @param im immutable object
300 	* @return the mutable bo
301 	*/
302    static AgendaItemBo from(AgendaItem im) {
303 	   if (im == null) { return null; }
304 
305 	   AgendaItemBo bo = new AgendaItemBo();
306 	   bo.id = im.getId();
307 	   bo.agendaId = im.getAgendaId();
308 	   bo.ruleId = im.getRuleId();
309 	   bo.subAgendaId = im.getSubAgendaId();
310 	   bo.whenTrueId = im.getWhenTrueId();
311 	   bo.whenFalseId = im.getWhenFalseId();
312 	   bo.alwaysId = im.getAlwaysId();
313 	   
314 	   return bo;
315    }
316 }