3 * (C) 2006 by Derrell Lipman
7 * LGPL 2.1: http://creativecommons.org/licenses/LGPL/2.1/
11 * Swat LDB Browser class finite state machine
13 qx.OO.defineClass("swat.module.ldbbrowse.Fsm", swat.main.AbstractModuleFsm,
16 swat.main.AbstractModuleFsm.call(this);
20 qx.Proto.buildFsm = function(module)
29 * - if returning from RPC, display the result
32 * "execute" on search button
33 * "treeopenwhileempty" on tree
34 * "changeselection" on tree
36 var state = new qx.util.fsm.State(
42 // Did we just return from an RPC request?
43 if (fsm.getPreviousState() == "State_AwaitRpcResult")
45 // Yup. Display the result. We need to get the request object
46 var rpcRequest = _this.popRpcRequest();
49 var gui = swat.module.ldbbrowse.Gui.getInstance();
51 // Did we get a Resource Not Found error? We'll get this after a
52 // session timeout, because the request is retried but can't
53 // succeed because the database has been closed by the session
55 var result = rpcRequest.getUserData("result");
56 var origins = swat.main.AbstractModuleFsm.JsonRpc_Origin;
57 var serverErrors = swat.main.AbstractModuleFsm.JsonRpc_ServerError;
58 if (result.type == "failed" &&
59 result.data.origin == origins.Server &&
60 result.data.code == serverErrors.ResourceError)
62 // Yup. Re-open the database
63 var dbName = fsm.getObject("dbName");
64 dbName.dispatchEvent(new qx.event.type.Event("changeSelection"),
69 // Otherwise, display the result
70 gui.displayData(module, rpcRequest);
73 // Dispose of the request
74 rpcRequest.request.dispose();
75 rpcRequest.request = null;
81 // If the search button is activated, issue a search request
85 "Transition_Idle_to_AwaitRpcResult_via_search",
88 "Transition_Idle_to_AwaitRpcResult_via_commit"
91 // If a previously unexpanded tree node is expanded, issue a request
92 // to retrieve its contents.
96 "Transition_Idle_to_AwaitRpcResult_via_tree_open"
99 // If the selection changes, issue a request to retrieve contents to
100 // populate the attribute/value table.
104 "Transition_Idle_to_AwaitRpcResult_via_tree_selection_changed",
107 "Transition_Idle_to_AwaitRpcResult_via_db_changed"
112 // Replace the initial Idle state with this one
113 fsm.replaceState(state, true);
116 * Transition: Idle to AwaitRpcResult
118 * Cause: "execute" on search button
121 * Issue a search request
123 var trans = new qx.util.fsm.Transition(
124 "Transition_Idle_to_AwaitRpcResult_via_search",
127 "State_AwaitRpcResult",
132 // Get our module descriptor
133 var module = fsm.getObject("swat.main.module");
135 // Retrieve the database handle
136 var dbHandle = module.dbHandle;
138 // Retrieve the search expression
139 var searchExpr = fsm.getObject("searchExpr").getValue();
141 // Retrieve the base DN
142 var baseDN = fsm.getObject("baseDN").getValue();
144 // Retrieve the selected scope
145 var scope = fsm.getObject("scope").getValue();
147 // We want all attributes
148 var attributes = [ "*" ];
150 // Issue a Search call
151 var request = _this.callRpc(fsm,
162 // When we get the result, we'll need to know what type of request
164 request.setUserData("requestType", "search");
167 state.addTransition(trans);
170 * Transition: Idle to AwaitRpcResult
172 * Cause: "execute" on OK button
175 * Commit modification or add new record to ldb
177 var trans = new qx.util.fsm.Transition(
178 "Transition_Idle_to_AwaitRpcResult_via_commit",
181 "State_AwaitRpcResult",
186 // Get our module descriptor
187 var module = fsm.getObject("swat.main.module");
189 // Retrieve the database handle
190 var dbHandle = module.dbHandle;
192 // Retrieve the ldbmod object
193 var ldbmod = fsm.getObject("ldbmod");
195 var ldif = ldbmod.getLdif();
197 // Issue a Search call
198 var request = _this.callRpc(fsm,
203 // When we get the result, we'll need to know what type of request
205 request.setUserData("requestType", ldbmod.getOpType());
208 state.addTransition(trans);
211 * Transition: Idle to AwaitRpcResult
213 * Cause: "treeOpenWhileEmpty" on tree
216 * Issue a search request
218 var trans = new qx.util.fsm.Transition(
219 "Transition_Idle_to_AwaitRpcResult_via_tree_open",
222 "State_AwaitRpcResult",
227 var parent = event.getData();
228 var hierarchy = parent.getHierarchy(new Array());
230 parent.debug("Requesting children...");
232 // Strip off the root node
235 // Get the tree object
236 var tree = fsm.getObject("tree");
238 // Determine the children. Differs depending on root or otherwise
243 // If parent is the root...
246 // ... then we want the defaultNamingContext, ...
247 attributes = [ "defaultNamingContext", "namingContexts" ];
249 // ... and we want only base scope
252 // ... and an empty base DN
257 // otherwise, retrieve the DN,
258 attributes = [ "dn" ];
260 // ... and we want one level of scope
263 // ... and base DN is the parent
264 baseDN = hierarchy.reverse().join(",");
267 // Build the search expression
268 var searchExpr = "(objectclass=*)";
270 // Get our module descriptor
271 var module = fsm.getObject("swat.main.module");
273 // Retrieve the database handle
274 var dbHandle = module.dbHandle;
276 // Issue a Get Statistics call
277 var request = _this.callRpc(fsm,
288 // When we get the result, we'll need to know what type of request
290 request.setUserData("requestType", "tree_open");
292 // We'll also need some of our parameters
293 request.setUserData("parent", parent);
294 request.setUserData("attributes", attributes);
297 state.addTransition(trans);
300 * Transition: Idle to AwaitRpcResult
302 * Cause: "changeSelection" on tree
305 * Issue a search request
307 var trans = new qx.util.fsm.Transition(
308 "Transition_Idle_to_AwaitRpcResult_via_tree_selection_changed",
311 "State_AwaitRpcResult",
316 var element = event.getData()[0];
317 var hierarchy = element.getHierarchy(new Array());
319 // Strip off the root node
322 // Get the tree object
323 var tree = fsm.getObject("tree");
325 // If element is the root...
328 // ... then just clear out the attribute/value table.
329 var tableModel = fsm.getObject("tableModel:browse");
330 tableModel.setData([]);
331 return null; // don't search additional transitionis
340 var element = event.getData()[0];
341 var hierarchy = element.getHierarchy(new Array());
343 // Strip off the root node
346 // Get the tree object
347 var tree = fsm.getObject("tree");
349 // Determine the children. Differs depending on root or otherwise
354 // We want all attributes
355 attributes = [ "*" ];
357 // We want the attributes only for the current element
360 // Base DN is the current element
361 baseDN = hierarchy.reverse().join(",");
363 // Build the search expression
364 var searchExpr = "(objectclass=*)";
366 // Get our module descriptor
367 var module = fsm.getObject("swat.main.module");
369 // Retrieve the database handle
370 var dbHandle = module.dbHandle;
372 // Issue a Get Statistics call
373 var request = _this.callRpc(fsm,
384 // When we get the result, we'll need to know what type of request
386 request.setUserData("requestType", "tree_selection_changed");
389 state.addTransition(trans);
392 * Transition: Idle to AwaitRpcResult
394 * Cause: "changeSelection" on dbName
397 * Issue a connect request
399 var trans = new qx.util.fsm.Transition(
400 "Transition_Idle_to_AwaitRpcResult_via_db_changed",
403 "State_AwaitRpcResult",
408 // Obtain the name of the database to be connected to
409 var dbName = fsm.getObject("dbName").getValue();
411 // Issue a Get Statistics call
412 var request = _this.callRpc(fsm,
417 // When we get the result, we'll need to know what type of request
419 request.setUserData("requestType", "database_name_changed");
422 state.addTransition(trans);
424 // Create the list of events that should be blocked while we're awaiting the
425 // results of another RPC request
428 // If a previously unexpanded tree node is expanded, issue a request
429 // to retrieve its contents.
430 "treeOpenWhileEmpty":
433 qx.util.fsm.FiniteStateMachine.EventHandling.BLOCKED
436 // If the selection changes, issue a request to retrieve contents to
437 // populate the attribute/value table.
441 qx.util.fsm.FiniteStateMachine.EventHandling.BLOCKED,
444 qx.util.fsm.FiniteStateMachine.EventHandling.BLOCKED
448 // Add the AwaitRpcResult state and all of its transitions
449 this.addAwaitRpcResultState(module, blockedEvents);
454 * Singleton Instance Getter
456 qx.Class.getInstance = qx.lang.Function.returnInstance;