001: /*
002: * JFolder, Copyright 2001-2006 Gary Steinmetz
003: *
004: * Distributable under LGPL license.
005: * See terms of license at gnu.org.
006: */
007:
008: package org.jfolder.workflow.model.template.v1;
009:
010: //base classes
011: import java.lang.reflect.InvocationTargetException;
012: import java.lang.reflect.Method;
013:
014: //project specific classes
015: import org.jfolder.common.tagging.AppraiseConceptTagContext;
016: import org.jfolder.common.tagging.ConceptTagCharacteristic;
017: import org.jfolder.common.tagging.ConceptTagConstraint;
018: import org.jfolder.common.tagging.ConceptTagConstraintHolder;
019: import org.jfolder.common.tagging.ConceptTagHelper;
020: import org.jfolder.common.tagging.DetermineClassConceptTagContext;
021: import org.jfolder.common.tagging.DetermineClassConceptTagException;
022: import org.jfolder.common.tagging.DetermineValueAndClassConceptTagContext;
023: import org.jfolder.common.tagging.DetermineValueAndClassConceptTagException;
024: import org.jfolder.common.tagging.DynamicConceptTagConstraintContext;
025: import org.jfolder.common.tagging.InitializeConceptTagContext;
026: import org.jfolder.common.tagging.ReturnableConceptTag;
027: import org.jfolder.common.tagging.StaticConceptTagConstraintContext;
028: import org.jfolder.common.tagging.StudioConceptTagInstanceInfoContext;
029: import org.jfolder.common.tagging.StudioConceptTagTypeInfoContext;
030: import org.jfolder.common.tagging.ValueAndClassForConceptTag;
031: import org.jfolder.common.utils.misc.CommonExpressions;
032: import org.jfolder.common.utils.misc.MiscHelper;
033:
034: //other classes
035:
036: public class StaticWorkflowTemplateTag implements ReturnableConceptTag {
037:
038: //characteristics
039: private ConceptTagCharacteristic callerCtchar = null;
040: private ConceptTagCharacteristic methodCtchar = null;
041: private ConceptTagCharacteristic parameterCtchar = null;
042:
043: //caller details
044: private final static String CALLER_CTCON = "caller";
045: private final static String CALLER_TITLE = "Caller";
046: private final static String CALLER_SHORT_DESCRIPTION = "Object calling the method";
047:
048: //method details
049: private final static String METHOD_CTCON = "method";
050: private final static String METHOD_TITLE = "Method";
051: private final static String METHOD_SHORT_DESCRIPTION = "Method to be called";
052:
053: //parameter details
054: private final static String PARAMETER_CTCON = "parameter";
055: private final static String PARAMETER_TITLE = "Parameters";
056: private final static String PARAMETER_SHORT_DESCRIPTION = "Parameters to be passed to the method";
057:
058: public void initialize(InitializeConceptTagContext inIctc) {
059:
060: //
061: ConceptTagConstraintHolder rootCtconh = ConceptTagConstraintHolder
062: .newInstance(inIctc);
063:
064: //constrain "caller"
065: ConceptTagConstraint callerCtcon = ConceptTagConstraint
066: .newInstance(CALLER_CTCON, rootCtconh);
067: callerCtcon.setTitle(CALLER_TITLE);
068: callerCtcon.setShortDescription(CALLER_SHORT_DESCRIPTION);
069: //
070: StaticConceptTagConstraintContext callerSctcc = StaticConceptTagConstraintContext
071: .newInstance(callerCtcon);
072: callerSctcc.setRegularExpression(CommonExpressions.ANY_STRING);
073: //
074: callerSctcc.addDefaultValue("ClassNameHere");
075: //TO DO: check if class exists
076:
077: //constrain "method"
078: ConceptTagConstraint methodCtcon = ConceptTagConstraint
079: .newInstance(METHOD_CTCON, rootCtconh);
080: methodCtcon.setTitle(METHOD_TITLE);
081: methodCtcon.setShortDescription(METHOD_SHORT_DESCRIPTION);
082: //
083: StaticConceptTagConstraintContext methodSctcc = StaticConceptTagConstraintContext
084: .newInstance(methodCtcon);
085: methodSctcc.setRegularExpression(CommonExpressions.ANY_STRING);
086: //
087: methodSctcc.addDefaultValue("MethodNameHere");
088: //TO DO: check if method name goes with object
089:
090: //constrain "parameter"
091: ConceptTagConstraint parameterCtcon = ConceptTagConstraint
092: .newInstance(PARAMETER_CTCON, rootCtconh);
093: parameterCtcon.setMinimumLength(0);
094: parameterCtcon.setLengthUnbounded(true);
095: parameterCtcon.setTitle(PARAMETER_TITLE);
096: parameterCtcon.setShortDescription(PARAMETER_SHORT_DESCRIPTION);
097: //
098: DynamicConceptTagConstraintContext parameterDctcc = DynamicConceptTagConstraintContext
099: .newInstance(parameterCtcon);
100: parameterDctcc.getValidator().allowReturnableOnly();
101:
102: //TO DO: check correctness of parameters with method, caller
103:
104: inIctc.initialize();
105:
106: //initialize characteristic
107: this .callerCtchar = callerCtcon.getCharacteristic();
108: this .methodCtchar = methodCtcon.getCharacteristic();
109: this .parameterCtchar = parameterCtcon.getCharacteristic();
110:
111: }
112:
113: public void appraise(AppraiseConceptTagContext inCtic) {
114: if (inCtic instanceof StudioConceptTagInstanceInfoContext) {
115: StudioConceptTagInstanceInfoContext sctiic = (StudioConceptTagInstanceInfoContext) inCtic;
116: sctiic.setInstanceTitle("Static Call");
117: sctiic
118: .setInstanceDescription("Calls a function on a class");
119: } else if (inCtic instanceof StudioConceptTagTypeInfoContext) {
120: StudioConceptTagTypeInfoContext scttic = (StudioConceptTagTypeInfoContext) inCtic;
121: scttic.setTypeTitle("Static Call");
122: scttic.setTypeDescription("Calls a function on a class");
123: }
124: }
125:
126: public void determineValueAndClass(
127: DetermineValueAndClassConceptTagContext inRvacc) {
128:
129: try {
130:
131: ValueAndClassForConceptTag outValue = null;
132:
133: //initialize variables
134: Class rClass = Class.forName(this .callerCtchar
135: .getValueAsString(0, inRvacc));
136: String methodName = this .methodCtchar.getValueAsString(0,
137: inRvacc);
138: Class pClasses[] = new Class[this .parameterCtchar
139: .getValueLength()];
140: Object pValues[] = new Object[this .parameterCtchar
141: .getValueLength()];
142: for (int i = 0; i < this .parameterCtchar.getValueLength(); i++) {
143: ValueAndClassForConceptTag nextVac = this .parameterCtchar
144: .getDetermineValueAndClass(i, inRvacc);
145: pClasses[i] = nextVac.getValueClass();
146: pValues[i] = nextVac.getValue();
147: }
148:
149: //create method
150: Method method = rClass.getMethod(methodName, pClasses);
151:
152: //execute method
153: Class vacClass = method.getReturnType();
154: Object vacObject = method.invoke(null, pValues);
155: //
156: outValue = ValueAndClassForConceptTag.newInstance(
157: vacObject, vacClass);
158:
159: inRvacc.setDetermineValueAndClass(outValue);
160: } catch (DetermineValueAndClassConceptTagException dvaccte) {
161: inRvacc.setDetermineException(dvaccte);
162: } catch (ClassNotFoundException cnfe) {
163: inRvacc.setDetermineException(cnfe);
164: } catch (NoSuchMethodException nsme) {
165: inRvacc.setDetermineException(nsme);
166: } catch (IllegalAccessException iae) {
167: inRvacc.setDetermineException(iae);
168: } catch (InvocationTargetException ite) {
169: inRvacc.setDetermineException(ite);
170: }
171: }
172:
173: public void determineClass(DetermineClassConceptTagContext inRcc) {
174:
175: try {
176: //initialize variables
177: Class rClass = Class.forName(this .callerCtchar
178: .getValueAsString(0, inRcc));
179: String methodName = this .methodCtchar.getValueAsString(0,
180: inRcc);
181: Class pClasses[] = new Class[this .parameterCtchar
182: .getValueLength()];
183: for (int i = 0; i < this .parameterCtchar.getValueLength(); i++) {
184: pClasses[i] = this .parameterCtchar.getDetermineClass(i,
185: inRcc);
186: }
187:
188: //create method
189: Method method = rClass.getMethod(methodName, pClasses);
190:
191: //execute method
192: Class vacClass = method.getReturnType();
193:
194: inRcc.setDetermineClass(vacClass);
195: } catch (DetermineClassConceptTagException dccte) {
196: inRcc.setDetermineException(dccte);
197: } catch (ClassNotFoundException cnfe) {
198: inRcc.setDetermineException(cnfe);
199: } catch (NoSuchMethodException nsme) {
200: inRcc.setDetermineException(nsme);
201: }
202:
203: }
204: }
|