001: /*
002: * Copyright 2005-2006 The Kuali Foundation.
003: *
004: *
005: * Licensed under the Educational Community License, Version 1.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.opensource.org/licenses/ecl1.php
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package edu.iu.uis.eden.routetemplate;
018:
019: import java.net.URLEncoder;
020: import java.util.ArrayList;
021: import java.util.Iterator;
022: import java.util.List;
023:
024: import org.apache.commons.lang.builder.ToStringBuilder;
025:
026: import edu.iu.uis.eden.EdenConstants;
027: import edu.iu.uis.eden.WorkflowPersistable;
028: import edu.iu.uis.eden.exception.ResourceUnavailableException;
029: import edu.iu.uis.eden.plugin.attributes.WorkflowAttribute;
030:
031: /**
032: * A model bean which represents a template upon which a rule is created.
033: * The RuleTemplate is essentially a collection of {@link RuleAttribute}s
034: * (associated vai the {@link RuleTemplateAttribute} bean).
035: *
036: * @author rkirkend
037: * @author bmcgough
038: * @author jhopf
039: */
040: public class RuleTemplate implements WorkflowPersistable {
041:
042: private static final long serialVersionUID = -3387940485523951302L;
043: private Long ruleTemplateId;
044: private String name;
045: private String description;
046: private Integer lockVerNbr;
047: private Long delegationTemplateId;
048: private RuleTemplate delegationTemplate;
049:
050: private List ruleTemplateAttributes;
051: private List ruleTemplateOptions;
052:
053: // required to be lookupable
054: private String returnUrl;
055:
056: public RuleTemplate() {
057: ruleTemplateAttributes = new ArrayList();
058: initializeOptions();
059: }
060:
061: public void initializeOptions() {
062: ruleTemplateOptions = new ArrayList();
063: ruleTemplateOptions.add(new RuleTemplateOption(
064: EdenConstants.RULE_INSTRUCTIONS_CD, null));
065: ruleTemplateOptions.add(new RuleTemplateOption(
066: EdenConstants.ACTION_REQUEST_ACKNOWLEDGE_REQ, null));
067: ruleTemplateOptions.add(new RuleTemplateOption(
068: EdenConstants.ACTION_REQUEST_APPROVE_REQ, null));
069: ruleTemplateOptions.add(new RuleTemplateOption(
070: EdenConstants.ACTION_REQUEST_COMPLETE_REQ, null));
071: ruleTemplateOptions.add(new RuleTemplateOption(
072: EdenConstants.ACTION_REQUEST_FYI_REQ, null));
073: ruleTemplateOptions.add(new RuleTemplateOption(
074: EdenConstants.ACTION_REQUEST_DEFAULT_CD, null));
075: }
076:
077: public String getDelegateTemplateName() {
078: if (delegationTemplate != null) {
079: return delegationTemplate.getName();
080: }
081: return "";
082: }
083:
084: public String getRuleTemplateActionsUrl() {
085: return "<a href=\"RuleTemplate.do?methodToCall=report¤tRuleTemplateId="
086: + ruleTemplateId + "\" >report</a>" /*+ " | <a href=\"RuleTemplate.do?methodToCall=edit&ruleTemplate.ruleTemplateId=" + ruleTemplateId + "\" >edit</a>"*/;
087: // " | <a onclick=\"if (confirm('Delete this record?')){ return true; } else {return false;} \" href=\"RuleTemplate.do?methodToCall=delete&ruleTemplate.ruleTemplateId=" + ruleTemplateId + "&redirectUrl=Lookup.do?methodToCall=search&lookupableImplServiceName=RuleTemplateLookupableImplService\" >delete</a>";
088: }
089:
090: public void addRuleTemplateAttribute(
091: RuleTemplateAttribute ruleTemplateAttribute, Integer counter) {
092: boolean alreadyAdded = false;
093: int location = 0;
094: if (counter != null) {
095: for (Iterator templateAttributeIter = getRuleTemplateAttributes()
096: .iterator(); templateAttributeIter.hasNext();) {
097: RuleTemplateAttribute ruleTemplateAtt = (RuleTemplateAttribute) templateAttributeIter
098: .next();
099: // if (ruleTemplateAtt.getRuleAttributeId().longValue() == ruleTemplateAttribute.getRuleAttributeId().longValue()) {
100: if (counter.intValue() == location) {
101: ruleTemplateAtt
102: .setDefaultValue(ruleTemplateAttribute
103: .getDefaultValue());
104: ruleTemplateAtt
105: .setDisplayOrder(ruleTemplateAttribute
106: .getDisplayOrder());
107: ruleTemplateAtt.setLockVerNbr(ruleTemplateAttribute
108: .getLockVerNbr());
109: ruleTemplateAtt.setRequired(ruleTemplateAttribute
110: .getRequired());
111: ruleTemplateAtt
112: .setRuleTemplateAttributeId(ruleTemplateAttribute
113: .getRuleTemplateAttributeId());
114: ruleTemplateAtt
115: .setRuleTemplateId(ruleTemplateAttribute
116: .getRuleTemplateId());
117: alreadyAdded = true;
118: }
119: location++;
120: }
121: }
122: if (!alreadyAdded) {
123: ruleTemplateAttribute.setDisplayOrder(new Integer(
124: getRuleTemplateAttributes().size() + 1));
125: getRuleTemplateAttributes().add(ruleTemplateAttribute);
126: }
127: }
128:
129: public boolean containsRuleTemplateAttribute(
130: RuleTemplateAttribute templateAttribute) {
131: for (Iterator iter = ruleTemplateAttributes.iterator(); iter
132: .hasNext();) {
133: RuleTemplateAttribute ruleTemplateAttribute = (RuleTemplateAttribute) iter
134: .next();
135: if (ruleTemplateAttribute.getRuleAttribute().getName()
136: .equals(
137: templateAttribute.getRuleAttribute()
138: .getName())) {
139: return true;
140: }
141: }
142: return false;
143: }
144:
145: public RuleTemplateAttribute getRuleTemplateAttribute(int index) {
146: while (getRuleTemplateAttributes().size() <= index) {
147: getRuleTemplateAttributes()
148: .add(new RuleTemplateAttribute());
149: }
150: return (RuleTemplateAttribute) getRuleTemplateAttributes().get(
151: index);
152: }
153:
154: public RuleTemplateOption getRuleTemplateOption(int index) {
155: while (getRuleTemplateOptions().size() <= index) {
156: getRuleTemplateOptions().add(new RuleTemplateOption());
157: }
158: return (RuleTemplateOption) getRuleTemplateOptions().get(index);
159: }
160:
161: public List getRuleTemplateAttributes() {
162: return ruleTemplateAttributes;
163: }
164:
165: /**
166: * Returns a List of all WorkflowAttribute objects on this template.
167: *
168: * @throws ResourceUnavailableException if one of the WorkflowAttributes cannot be instantiated
169: */
170: public List getWorkflowAttributes()
171: throws ResourceUnavailableException {
172: List workflowAttributes = new ArrayList();
173: for (Iterator iter = getRuleTemplateAttributes().iterator(); iter
174: .hasNext();) {
175: RuleTemplateAttribute templateAttribute = (RuleTemplateAttribute) iter
176: .next();
177: Object attribute = templateAttribute.getAttribute();
178: if (attribute instanceof WorkflowAttribute) {
179: workflowAttributes.add(attribute);
180: }
181: }
182: return workflowAttributes;
183: }
184:
185: // public RoleAttribute getRoleAttributeByName(String className) throws ResourceUnavailableException {
186: // for (Iterator iter = getWorkflowAttributes().iterator(); iter.hasNext();) {
187: // WorkflowAttribute attribute = (WorkflowAttribute)iter.next();
188: // if (className.equals(attribute.getClass().getName())) {
189: // return (RoleAttribute) attribute;
190: // }
191: // }
192: // throw new RuntimeException("Didn't locate RoleAttribute " + className);
193: // }
194:
195: public void setRuleTemplateAttributes(List ruleTemplateAttributes) {
196: this .ruleTemplateAttributes = ruleTemplateAttributes;
197: }
198:
199: public String getDescription() {
200: return description;
201: }
202:
203: public void setDescription(String description) {
204: this .description = description;
205: }
206:
207: public Integer getLockVerNbr() {
208: return lockVerNbr;
209: }
210:
211: public void setLockVerNbr(Integer lockVerNbr) {
212: this .lockVerNbr = lockVerNbr;
213: }
214:
215: public String getName() {
216: return name;
217: }
218:
219: public void setName(String name) {
220: this .name = name;
221: }
222:
223: public Long getRuleTemplateId() {
224: return ruleTemplateId;
225: }
226:
227: public void setRuleTemplateId(Long ruleTemplateId) {
228: this .ruleTemplateId = ruleTemplateId;
229: }
230:
231: public Long getDelegationTemplateId() {
232: return delegationTemplateId;
233: }
234:
235: public void setDelegationTemplateId(Long delegationTemplateId) {
236: this .delegationTemplateId = delegationTemplateId;
237: }
238:
239: public RuleTemplate getDelegationTemplate() {
240: return delegationTemplate;
241: }
242:
243: public void setDelegationTemplate(RuleTemplate delegationTemplate) {
244: this .delegationTemplate = delegationTemplate;
245: }
246:
247: public Object copy(boolean preserveKeys) {
248: RuleTemplate ruleTemplateClone = new RuleTemplate();
249:
250: if (description != null) {
251: ruleTemplateClone.setDescription(new String(description));
252: }
253: if (name != null) {
254: ruleTemplateClone.setName(new String(name));
255: }
256: if (preserveKeys && ruleTemplateId != null) {
257: ruleTemplateClone.setRuleTemplateId(new Long(ruleTemplateId
258: .longValue()));
259: }
260: if ((ruleTemplateAttributes != null)
261: && !ruleTemplateAttributes.isEmpty()) {
262: List ruleTemplateAttributeList = new ArrayList();
263:
264: for (Iterator i = ruleTemplateAttributes.iterator(); i
265: .hasNext();) {
266: RuleTemplateAttribute ruleTemplateAttribute = (RuleTemplateAttribute) i
267: .next();
268: RuleTemplateAttribute ruleTemplateAttributeCopy = (RuleTemplateAttribute) ruleTemplateAttribute
269: .copy(preserveKeys);
270: ruleTemplateAttributeCopy
271: .setRuleTemplate(ruleTemplateClone);
272: ruleTemplateAttributeList
273: .add(ruleTemplateAttributeCopy);
274: }
275: ruleTemplateClone
276: .setRuleTemplateAttributes(ruleTemplateAttributeList);
277: }
278: if ((ruleTemplateOptions != null)
279: && !ruleTemplateOptions.isEmpty()) {
280: List ruleTemplateOptionList = new ArrayList();
281:
282: for (Iterator i = ruleTemplateOptions.iterator(); i
283: .hasNext();) {
284: RuleTemplateOption ruleTemplateOption = (RuleTemplateOption) i
285: .next();
286: RuleTemplateOption ruleTemplateOptionCopy = (RuleTemplateOption) ruleTemplateOption
287: .copy(preserveKeys);
288: ruleTemplateOptionCopy
289: .setRuleTemplate(ruleTemplateClone);
290: ruleTemplateOptionList.add(ruleTemplateOptionCopy);
291: }
292: ruleTemplateClone
293: .setRuleTemplateOptions(ruleTemplateOptionList);
294: }
295:
296: return ruleTemplateClone;
297: }
298:
299: public String getReturnUrl() {
300: return returnUrl;
301: }
302:
303: public void setReturnUrl(String returnUrl) {
304: this .returnUrl = returnUrl;
305: }
306:
307: /**
308: * Used from the rule quicklinks when doing the onestart focus channel.
309: */
310: public String getEncodedName() {
311: return URLEncoder.encode(getName());
312: }
313:
314: public List getRuleTemplateOptions() {
315: return ruleTemplateOptions;
316: }
317:
318: public void setRuleTemplateOptions(List ruleTemplateOptions) {
319: this .ruleTemplateOptions = ruleTemplateOptions;
320: }
321:
322: public RuleTemplateOption getRuleTemplateOption(String key) {
323: for (Iterator iter = ruleTemplateOptions.iterator(); iter
324: .hasNext();) {
325: RuleTemplateOption option = (RuleTemplateOption) iter
326: .next();
327: if (option.getKey().equals(key)) {
328: return option;
329: }
330: }
331: return null;
332: }
333:
334: public void setInstructions(RuleTemplateOption instructions) {
335: RuleTemplateOption option = getRuleTemplateOption(EdenConstants.RULE_INSTRUCTIONS_CD);
336: option.setValue(instructions.getValue());
337: option.setRuleTemplateOptionId(instructions
338: .getRuleTemplateOptionId());
339: option.setLockVerNbr(instructions.getLockVerNbr());
340: }
341:
342: public void setAcknowledge(RuleTemplateOption acknowledge) {
343: RuleTemplateOption option = getRuleTemplateOption(EdenConstants.ACTION_REQUEST_ACKNOWLEDGE_REQ);
344: option.setValue(acknowledge.getValue());
345: option.setRuleTemplateOptionId(acknowledge
346: .getRuleTemplateOptionId());
347: option.setLockVerNbr(acknowledge.getLockVerNbr());
348: }
349:
350: public void setComplete(RuleTemplateOption complete) {
351: RuleTemplateOption option = getRuleTemplateOption(EdenConstants.ACTION_REQUEST_COMPLETE_REQ);
352: option.setValue(complete.getValue());
353: option.setRuleTemplateOptionId(complete
354: .getRuleTemplateOptionId());
355: option.setLockVerNbr(complete.getLockVerNbr());
356: }
357:
358: public void setApprove(RuleTemplateOption approve) {
359: RuleTemplateOption option = getRuleTemplateOption(EdenConstants.ACTION_REQUEST_APPROVE_REQ);
360: option.setValue(approve.getValue());
361: option.setRuleTemplateOptionId(approve
362: .getRuleTemplateOptionId());
363: option.setLockVerNbr(approve.getLockVerNbr());
364: }
365:
366: public void setFyi(RuleTemplateOption fyi) {
367: RuleTemplateOption option = getRuleTemplateOption(EdenConstants.ACTION_REQUEST_FYI_REQ);
368: option.setValue(fyi.getValue());
369: option.setRuleTemplateOptionId(fyi.getRuleTemplateOptionId());
370: option.setLockVerNbr(fyi.getLockVerNbr());
371: }
372:
373: public void setDefaultActionRequestValue(
374: RuleTemplateOption defaultActionRequestValue) {
375: RuleTemplateOption option = getRuleTemplateOption(EdenConstants.ACTION_REQUEST_DEFAULT_CD);
376: option.setValue(defaultActionRequestValue.getValue());
377: option.setRuleTemplateOptionId(defaultActionRequestValue
378: .getRuleTemplateOptionId());
379: option.setLockVerNbr(defaultActionRequestValue.getLockVerNbr());
380: }
381:
382: public RuleTemplateOption getInstructions() {
383: return getRuleTemplateOption(EdenConstants.RULE_INSTRUCTIONS_CD);
384: }
385:
386: public RuleTemplateOption getAcknowledge() {
387: return getRuleTemplateOption(EdenConstants.ACTION_REQUEST_ACKNOWLEDGE_REQ);
388: }
389:
390: public RuleTemplateOption getComplete() {
391: return getRuleTemplateOption(EdenConstants.ACTION_REQUEST_COMPLETE_REQ);
392: }
393:
394: public RuleTemplateOption getApprove() {
395: return getRuleTemplateOption(EdenConstants.ACTION_REQUEST_APPROVE_REQ);
396: }
397:
398: public RuleTemplateOption getFyi() {
399: return getRuleTemplateOption(EdenConstants.ACTION_REQUEST_FYI_REQ);
400: }
401:
402: public RuleTemplateOption getDefaultActionRequestValue() {
403: return getRuleTemplateOption(EdenConstants.ACTION_REQUEST_DEFAULT_CD);
404: }
405:
406: public String toString() {
407: return new ToStringBuilder(this ).append("ruleTemplateId",
408: ruleTemplateId).append("name", name).append(
409: "description", description).append(
410: "delegationTemplateId", delegationTemplateId).append(
411: "ruleTemplateAttributes",
412: ruleTemplateAttributes == null ? "null" : "size: "
413: + ruleTemplateAttributes.size()).append(
414: "returnUrl", returnUrl)
415: .append("lockVerNbr", lockVerNbr).append(
416: "ruleTemplateOptions", ruleTemplateOptions)
417: .toString();
418:
419: }
420: }
|