KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > tools > scriptengine > core > nodes > ChooseNode


1 /*
2  * Copyright (c) 2001-2005 by Genimen BV (www.genimen.com) All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, is permitted provided that the following conditions are met: -
6  * Redistributions of source code must retain the above copyright notice, this
7  * list of conditions and the following disclaimer. - Redistributions in binary
8  * form must reproduce the above copyright notice, this list of conditions and
9  * the following disclaimer in the documentation and/or other materials
10  * provided with the distribution. - All advertising materials mentioning
11  * features or use of this software must display the following acknowledgment:
12  * "This product includes Djeneric." - Products derived from this software may
13  * not be called "Djeneric" nor may "Djeneric" appear in their names without
14  * prior written permission of Genimen BV. - Redistributions of any form
15  * whatsoever must retain the following acknowledgment: "This product includes
16  * Djeneric."
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL GENIMEN BV, DJENERIC.ORG, OR CONTRIBUTORS
22  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */

30 package com.genimen.djeneric.tools.scriptengine.core.nodes;
31
32 import java.util.ArrayList JavaDoc;
33 import java.util.HashMap JavaDoc;
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 JavaDoc _objectType = null;
52   String JavaDoc _resultVariable = null;
53   String JavaDoc _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 JavaDoc getName()
66   {
67     return "choose";
68   }
69
70   public String JavaDoc toString()
71   {
72     return getName();
73   }
74
75   public String JavaDoc getObjectType()
76   {
77     return _objectType;
78   }
79
80   public void setObjectType(String JavaDoc string)
81   {
82     _objectType = string;
83   }
84
85   public String JavaDoc getResultVariable()
86   {
87     return _resultVariable;
88   }
89
90   public void setResultVariable(String JavaDoc 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 JavaDoc parameters = new HashMap JavaDoc();
122         StringBuffer JavaDoc restriction = new StringBuffer JavaDoc();
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 JavaDoc ev) throws DjScriptExecutionException
148   {
149     if (!doCollectEvents(ctxt, ev))
150     {
151       ActionNode a = (ActionNode) getParent(ActionNode.class);
152       String JavaDoc 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 JavaDoc getCustomChooserClass()
161   {
162     return _customChooserClass;
163   }
164
165   public void setCustomChooserClass(String JavaDoc 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