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.util.List;
020:
021: import org.kuali.rice.definition.ObjectDefinition;
022: import org.kuali.rice.resourceloader.GlobalResourceLoader;
023:
024: import edu.iu.uis.eden.EdenConstants;
025: import edu.iu.uis.eden.KEWServiceLocator;
026: import edu.iu.uis.eden.WorkflowPersistable;
027: import edu.iu.uis.eden.exception.WorkflowRuntimeException;
028: import edu.iu.uis.eden.plugin.attributes.WorkflowAttribute;
029:
030: /**
031: * A model bean which services as the link between a {@link RuleTemplate} and
032: * a {@link RuleAttribute}.
033: *
034: * @author rkirkend
035: * @author bmcgough
036: * @author jhopf
037: */
038: public class RuleTemplateAttribute implements WorkflowPersistable,
039: Comparable {
040:
041: private static final long serialVersionUID = -3580049225424553828L;
042: private Long ruleTemplateAttributeId;
043: private Long ruleTemplateId;
044: private Long ruleAttributeId;
045: private Boolean required;
046: private Integer displayOrder;
047: private String defaultValue;
048: private Integer lockVerNbr;
049:
050: private RuleTemplate ruleTemplate;
051: private RuleAttribute ruleAttribute;
052: private List ruleExtensions;
053:
054: public RuleTemplateAttribute() {
055: this .required = new Boolean(false);
056: }
057:
058: public int compareTo(Object obj) {
059: if (obj instanceof RuleTemplateAttribute) {
060: RuleTemplateAttribute comparedObject = (RuleTemplateAttribute) obj;
061:
062: if ((this .getDisplayOrder() != null)
063: && (comparedObject.getDisplayOrder() != null)) {
064: return this .getDisplayOrder().compareTo(
065: comparedObject.getDisplayOrder());
066: }
067: }
068: return 0;
069: }
070:
071: public Object getAttribute() {
072: try {
073: ObjectDefinition objectDefinition = new ObjectDefinition(
074: getRuleAttribute().getClassName(),
075: getRuleAttribute().getMessageEntity());
076: Object attribute = GlobalResourceLoader
077: .getObject(objectDefinition);
078: if (attribute == null) {
079: throw new WorkflowRuntimeException(
080: "Could not find attribute " + objectDefinition);
081: }
082: if (attribute instanceof WorkflowAttribute) {
083: ((WorkflowAttribute) attribute).setRequired(required
084: .booleanValue());
085: }
086: return attribute;
087: } catch (Exception e) {
088: throw new RuntimeException(
089: "Caught error attempting to load attribute class: "
090: + getRuleAttribute().getClassName(), e);
091: }
092: }
093:
094: public boolean isWorkflowAttribute() {
095: try {
096: Object attributeObject = getWorkflowAttribute();//GlobalResourceLoader.getResourceLoader().getObject(new ObjectDefinition(getRuleAttribute().getClassName()));
097: if (attributeObject == null) {
098: return false;
099: }
100: Class attributeClass = attributeObject.getClass();
101: return WorkflowAttribute.class
102: .isAssignableFrom(attributeClass);
103: } catch (Exception e) {
104: throw new RuntimeException(
105: "Caught error attempting to load WorkflowAttribute class: "
106: + getRuleAttribute().getClassName(), e);
107: }
108: }
109:
110: public boolean isRuleValidationAttribute() {
111: // just check the type here to avoid having to load the class from the class loader if it's not actually there
112: return EdenConstants.RULE_VALIDATION_ATTRIBUTE_TYPE
113: .equals(getRuleAttribute().getType());
114: /*try {
115: Class attributeClass = SpringServiceLocator.getExtensionService().loadClass(getRuleAttribute().getClassName());
116: if (attributeClass == null) {
117: return false;
118: }
119: return RuleValidationAttribute.class.isAssignableFrom(attributeClass);
120: } catch (Exception e) {
121: throw new RuntimeException("Caught error attempting to load RuleValidationAttribute class: " + getRuleAttribute().getClassName(), e);
122: }*/
123: }
124:
125: /**
126: * Instantiates and returns a new instance of the WorkflowAttribute class configured on this template.
127: * The calling code should be sure to call isWorkflowAttribute first to verify the type of this attribute
128: * is that of a WorkflowAttribute. Otherwise a RuntimeException will be thrown.
129: */
130: public WorkflowAttribute getWorkflowAttribute() {
131: try {
132: ObjectDefinition objectDefinition = new ObjectDefinition(
133: getRuleAttribute().getClassName(),
134: getRuleAttribute().getMessageEntity());
135: WorkflowAttribute workflowAttribute = (WorkflowAttribute) GlobalResourceLoader
136: .getResourceLoader().getObject(objectDefinition);
137: if (workflowAttribute == null) {
138: throw new WorkflowRuntimeException(
139: "Could not find workflow attribute "
140: + objectDefinition);
141: }
142: workflowAttribute.setRequired(required.booleanValue());
143: return workflowAttribute;
144: } catch (Exception e) {
145: throw new RuntimeException(
146: "Caught exception instantiating new "
147: + getRuleAttribute().getClassName(), e);
148: }
149: }
150:
151: /**
152: * Instantiates and returns a new instance of the RuleValidationAttribute class configured on this template.
153: * The calling code should be sure to call isRuleValidationAttribute first to verify the type of this attribute
154: * is that of a RuleValidationAttribute. Otherwise a RuntimeException will be thrown.
155: */
156: public RuleValidationAttribute getRuleValidationAttribute() {
157: try {
158: return (RuleValidationAttribute) getAttribute();
159: } catch (Exception e) {
160: throw new RuntimeException(
161: "Caught exception instantiating new "
162: + getRuleAttribute().getClassName(), e);
163: }
164: }
165:
166: public List getRuleExtensions() {
167: return ruleExtensions;
168: }
169:
170: public void setRuleExtensions(List ruleExtensions) {
171: this .ruleExtensions = ruleExtensions;
172: }
173:
174: public RuleAttribute getRuleAttribute() {
175: if (ruleAttribute == null && ruleAttributeId != null) {
176: ruleAttribute = ((RuleAttributeService) KEWServiceLocator
177: .getService(KEWServiceLocator.RULE_ATTRIBUTE_SERVICE))
178: .findByRuleAttributeId(ruleAttributeId);
179: }
180: return ruleAttribute;
181: }
182:
183: public void setRuleAttribute(RuleAttribute ruleAttribute) {
184: this .ruleAttribute = ruleAttribute;
185: }
186:
187: public RuleTemplate getRuleTemplate() {
188: return ruleTemplate;
189: }
190:
191: public void setRuleTemplate(RuleTemplate ruleTemplate) {
192: this .ruleTemplate = ruleTemplate;
193: }
194:
195: public String getDefaultValue() {
196: return defaultValue;
197: }
198:
199: public void setDefaultValue(String defaultValue) {
200: this .defaultValue = defaultValue;
201: }
202:
203: public Integer getDisplayOrder() {
204: return displayOrder;
205: }
206:
207: public void setDisplayOrder(Integer displayOrder) {
208: this .displayOrder = displayOrder;
209: }
210:
211: public Integer getLockVerNbr() {
212: return lockVerNbr;
213: }
214:
215: public void setLockVerNbr(Integer lockVerNbr) {
216: this .lockVerNbr = lockVerNbr;
217: }
218:
219: public Boolean getRequired() {
220: return required;
221: }
222:
223: public void setRequired(Boolean required) {
224: this .required = required;
225: }
226:
227: public Long getRuleAttributeId() {
228: return ruleAttributeId;
229: }
230:
231: public void setRuleAttributeId(Long ruleAttributeId) {
232: this .ruleAttributeId = ruleAttributeId;
233: }
234:
235: public Long getRuleTemplateAttributeId() {
236: return ruleTemplateAttributeId;
237: }
238:
239: public void setRuleTemplateAttributeId(Long ruleTemplateAttributeId) {
240: this .ruleTemplateAttributeId = ruleTemplateAttributeId;
241: }
242:
243: public Long getRuleTemplateId() {
244: return ruleTemplateId;
245: }
246:
247: public void setRuleTemplateId(Long ruleTemplateId) {
248: this .ruleTemplateId = ruleTemplateId;
249: }
250:
251: public Object copy(boolean preserveKeys) {
252: RuleTemplateAttribute ruleTemplateAttributeClone = new RuleTemplateAttribute();
253: if (defaultValue != null) {
254: ruleTemplateAttributeClone.setDefaultValue(new String(
255: defaultValue));
256: }
257: if (displayOrder != null) {
258: ruleTemplateAttributeClone.setDisplayOrder(new Integer(
259: displayOrder.intValue()));
260: }
261: if (required != null) {
262: ruleTemplateAttributeClone.setRequired(new Boolean(required
263: .booleanValue()));
264: }
265: if (ruleAttribute != null) {
266: ruleTemplateAttributeClone
267: .setRuleAttribute((RuleAttribute) ruleAttribute
268: .copy(preserveKeys));
269: }
270: if (preserveKeys && ruleTemplateAttributeId != null) {
271: ruleTemplateAttributeClone
272: .setRuleTemplateAttributeId(new Long(
273: ruleTemplateAttributeId.longValue()));
274: }
275: return ruleTemplateAttributeClone;
276: }
277: }
|