1 30 package com.genimen.djeneric.tools.scriptengine.core.nodes; 31 32 import java.util.ArrayList ; 33 import java.util.HashMap ; 34 35 import com.genimen.djeneric.repository.DjExtent; 36 import com.genimen.djeneric.repository.DjOql; 37 import com.genimen.djeneric.repository.exceptions.DjenericException; 38 import com.genimen.djeneric.repository.exceptions.ObjectNotDefinedException; 39 import com.genimen.djeneric.tools.scriptengine.core.DjScriptParserEngine; 40 import com.genimen.djeneric.tools.scriptengine.core.EditorEventDefinition; 41 import com.genimen.djeneric.tools.scriptengine.core.ParseException; 42 import com.genimen.djeneric.tools.scriptengine.core.ScriptRunnerContainer; 43 import com.genimen.djeneric.tools.scriptengine.core.SimpleNode; 44 import com.genimen.djeneric.tools.scriptengine.core.util.DjScriptCompileTimeScope; 45 import com.genimen.djeneric.tools.scriptengine.core.util.DjScriptExecutionException; 46 import com.genimen.djeneric.tools.scriptengine.core.util.DjScriptExecutionTimeScope; 47 import com.genimen.djeneric.tools.scriptengine.core.util.Variable; 48 49 public class ChooseNode extends SimpleNode 50 { 51 String _objectType = null; 52 String _resultVariable = null; 53 String _customChooserClass = null; 54 55 public ChooseNode(int i) 56 { 57 super(i); 58 } 59 60 public ChooseNode(DjScriptParserEngine p, int i) 61 { 62 super(p, i); 63 } 64 65 public String getName() 66 { 67 return "choose"; 68 } 69 70 public String toString() 71 { 72 return getName(); 73 } 74 75 public String getObjectType() 76 { 77 return _objectType; 78 } 79 80 public void setObjectType(String string) 81 { 82 _objectType = string; 83 } 84 85 public String getResultVariable() 86 { 87 return _resultVariable; 88 } 89 90 public void setResultVariable(String string) 91 { 92 _resultVariable = string; 93 } 94 95 public void execute(DjScriptExecutionTimeScope context) throws DjScriptExecutionException 96 { 97 EventDefinitionNode events[] = (EventDefinitionNode[]) getChildren(EventDefinitionNode.class) 98 .toArray(new EventDefinitionNode[0]); 99 100 EditorEventDefinition[] eventDefs = new EditorEventDefinition[events.length]; 101 for (int i = 0; i < eventDefs.length; i++) 102 { 103 eventDefs[i] = new EditorEventDefinition(events[i].getEventName(), events[i].getEventTitle(), events[i] 104 .getOption()); 105 } 106 107 ScriptRunnerContainer container = context.getContainer(); 108 109 try 110 { 111 DjExtent extent = context.getPersistenceManager().getExtentByObjectType(getObjectType()); 112 Variable variable = context.getVariable(getResultVariable(), this); 113 114 DjOql oql = null; 115 116 OqlFilterNode filterNode = (OqlFilterNode) getChild(OqlFilterNode.class); 117 if (filterNode != null) 118 { 119 oql = context.getSession().createOql(extent); 120 121 HashMap parameters = new HashMap (); 122 StringBuffer restriction = new StringBuffer (); 123 try 124 { 125 filterNode.translateOql(context, restriction, parameters); 126 } 127 catch (ParseException e1) 128 { 129 throw new DjScriptExecutionException(e1.getMessage(), this); 130 } 131 oql.addBaseRestriction(restriction.toString()); 132 oql.setParameters(parameters); 133 } 134 135 ActionNode actionNode = (ActionNode) getParent(ActionNode.class); 136 137 container.choose(getCustomChooserClass(), extent, variable, context.getSession(), oql, eventDefs, actionNode 138 .getActionName()); 139 } 140 catch (DjenericException e) 141 { 142 throw new DjScriptExecutionException(e.getMessage(), this); 143 } 144 145 } 146 147 public void collectEvents(DjScriptCompileTimeScope ctxt, ArrayList ev) throws DjScriptExecutionException 148 { 149 if (!doCollectEvents(ctxt, ev)) 150 { 151 ActionNode a = (ActionNode) getParent(ActionNode.class); 152 String actionName = a.getActionName(); 153 ev.add(actionName + ".accept"); 154 ev.add(actionName + ".cancel"); 155 ev.add(actionName + ".close"); 156 ev.add(actionName + ".validate"); 157 } 158 } 159 160 public String getCustomChooserClass() 161 { 162 return _customChooserClass; 163 } 164 165 public void setCustomChooserClass(String string) 166 { 167 _customChooserClass = string; 168 } 169 170 public void validateScript(DjScriptCompileTimeScope ctxt) throws DjScriptExecutionException 171 { 172 try 173 { 174 ctxt.getPersistenceManager().getExtentByObjectType(_objectType); 175 } 176 catch (ObjectNotDefinedException onde) 177 { 178 throw new DjScriptExecutionException(onde.getMessage(), this); 179 } 180 181 ctxt.getVariable(_resultVariable, this); 182 } 183 } | Popular Tags |