summaryrefslogtreecommitdiff
path: root/webapps/swat/source/class/swat/main/AbstractModuleFsm.js
blob: 273c9ad8a3360850bf1b098fd38f3b191a437105 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
/*
 * Copyright:
 *   (C) 2006 by Derrell Lipman
 *       All rights reserved
 *
 * License:
 *   LGPL 2.1: http://creativecommons.org/licenses/LGPL/2.1/
 */

/**
 * Common facilities for modules' finite state machines.  Each module's FSM
 * should extend this class.
 */
qx.OO.defineClass("swat.main.AbstractModuleFsm", qx.core.Object, function()
{
  qx.core.Object.call(this);

  // Create an array for pushing request objects
  this._requests = [ ];
});


qx.Proto.buildFsm = function(module)
{
  throw new Error("Module must overload buildFsm() " +
                  "to build its custom finite state machine.");
};

qx.Proto.addAwaitRpcResultState = function(module, blockedEvents)
{
  var fsm = module.fsm;
  var _this = this;

  /*
   * State: AwaitRpcResult
   *
   * Actions upon entry:
   *  - enable any objects in group "swat.main.fsmUtils.enable_during_rpc"
   *  - disable any objects in group "swat.main.fsmUtils.disable_during_rpc"
   *
   * Actions upon exit:
   *   - disable any objects in group "swat.main.fsmUtils.enable_during_rpc"
   *   - enable any objects in group "swat.main.fsmUtils.disable_during_rpc"
   *
   * Transition on:
   *  "completed" (on RPC)
   *  "failed" (on RPC)
   *  "execute" on swat.main.fsmUtils.abort_rpc
   */

  var stateInfo =
  {
    "autoActionsBeforeOnentry" :
    {
      // The name of a function.
      "setEnabled" :
      [
        {
          // We want to enable objects in the group
          // swat.main.fsmUtils.enable_during_rpc
          "parameters" : [ true ],

          // Call this.getObject(<object>).setEnabled(true) on
          // state entry, for each <object> in the group called
          // "swat.main.fsmUtils.enable_during_rpc".
          "groups"      : [ "swat.main.fsmUtils.enable_during_rpc" ]
        },

        {
          // We want to disable objects in the group
          // swat.main.fsmUtils.disable_during_rpc
          "parameters" : [ false ],

          // Call this.getObject(<object>).setEnabled(false) on
          // state entry, for each <object> in the group called
          // "swat.main.fsmUtils.disable_during_rpc".
          "groups"      : [ "swat.main.fsmUtils.disable_during_rpc" ]
        }
      ]
    },

    "autoActionsBeforeOnexit" :
    {
      // The name of a function.
      "setEnabled" :
      [
        {
          // We want to re-disable objects we had enabled, in the group
          // swat.main.fsmUtils.enable_during_rpc
          "parameters" : [ false ],

          // Call this.getObject(<object>).setEnabled(false) on
          // state entry, for each <object> in the group called
          // "swat.main.fsmUtils.enable_during_rpc".
          "groups"      : [ "swat.main.fsmUtils.enable_during_rpc" ]
        },

        {
          // We want to re-enable objects we had disabled, in the group
          // swat.main.fsmUtils.disable_during_rpc
          "parameters" : [ true ],

          // Call this.getObject(<object>).setEnabled(true) on
          // state entry, for each <object> in the group called
          // "swat.main.fsmUtils.disable_during_rpc".
          "groups"      : [ "swat.main.fsmUtils.disable_during_rpc" ]
        }
      ]
    },

    "onentry" :
      function(fsm, event)
      {
        var bAuthCompleted = false;

        // See if we just completed an authentication
        if (fsm.getPreviousState() == "State_Authenticate" &&
            event.getType() == "complete")
        {
          bAuthCompleted = true;
        }

        // If we didn't just complete an authentication and we're coming
        // from some other state...
        if (! bAuthCompleted &&
            fsm.getPreviousState() != "State_AwaitRpcResult")
        {
          // ... then push the previous state onto the state stack
          fsm.pushState(false);
        }
      },

    "events" :
    {
      "execute"  :
      {
        "swat.main.fsmUtils.abort_rpc" :
          "Transition_AwaitRpcResult_to_AwaitRpcResult_via_button_abort"
      },

      "completed" :
        "Transition_AwaitRpcResult_to_PopStack_via_complete",

      "failed" :
        qx.util.fsm.FiniteStateMachine.EventHandling.PREDICATE
    }
  };

  // If there are blocked events specified...
  if (blockedEvents)
  {
    // ... then add them to the state info events object
    for (var blockedEvent in blockedEvents)
    {
      // Ensure it's not already there.  Avoid programmer headaches.
      if (stateInfo["events"][blockedEvent])
      {
        throw new Error("Attempt to add blocked event " +
                        blockedEvent + " but it is already handled");
      }

      // Add the event.
      stateInfo["events"][blockedEvent] = blockedEvents[blockedEvent];
    }
  }

  var state = new qx.util.fsm.State( "State_AwaitRpcResult", stateInfo);
  fsm.addState(state);

  /*** Transitions that use a PREDICATE appear first ***/

  /*
   * Transition: AwaitRpcResult to GetAuthInfo
   *
   * Cause: "failed" (on RPC) where reason is PermissionDenied
   */
  var trans = new qx.util.fsm.Transition(
    "Transition_AwaitRpcResult_to_Authenticate",
    {
      "nextState" :
        "State_Authenticate",

      "predicate" :
        function(fsm, event)
        {
          var error = event.getData(); // retrieve the JSON-RPC error

          // Did we get get origin=Server, and either
          // code=NotLoggedIn or code=SessionExpired ? 
          var origins = swat.main.AbstractModuleFsm.JsonRpc_Origin;
          var serverErrors = swat.main.AbstractModuleFsm.JsonRpc_ServerError;
          if (error.origin == origins.Server &&
              (error.code == serverErrors.NotLoggedIn ||
               error.code == serverErrors.SessionExpired))
          {
            return true;
          }

          // fall through to next transition, also for "failed"
          return false;
        },

      "ontransition" :
        function(fsm, event)
        {
          var caption;

          var error = event.getData(); // retrieve the JSON-RPC error
          var serverErrors = swat.main.AbstractModuleFsm.JsonRpc_ServerError;

          switch(error.code)
          {
          case serverErrors.NotLoggedIn:
            caption = "Please log in.";
            break;

          case serverErrors.SessionExpired:
          default:
            caption = "Session Expired.  Please log in.";
            break;
          }

          // Retrieve the modal authentication window.
          var loginWin = swat.main.Authenticate.getInstance();

          // Ensure that it's saved in the current finite state machine
          loginWin.addToFsm(fsm);

          // Set the caption
          loginWin.setCaption(caption);

          // Set the domain info
          loginWin.setInfo(error.info);

          // Open the authentication window
          loginWin.open();
        }
    });
  state.addTransition(trans);

  /*
   * Transition: AwaitRpcResult to PopStack
   *
   * Cause: "failed" (on RPC)
   */
  var trans = new qx.util.fsm.Transition(
    "Transition_AwaitRpcResult_to_PopStack_via_failed",
    {
      "nextState" :
        qx.util.fsm.FiniteStateMachine.StateChange.POP_STATE_STACK,

      "ontransition" :
        function(fsm, event)
        {
          // Get the request object
          var rpcRequest = _this.getCurrentRpcRequest();
          
          // Generate the result for a completed request
          rpcRequest.setUserData("result",
                                  {
                                      type : "failed",
                                      data : event.getData()
                                  });
        }
    });
  state.addTransition(trans);

  /*** Remaining transitions are accessed via the jump table ***/

  /*
   * Transition: AwaitRpcResult to AwaitRpcResult
   *
   * Cause: "execute" on swat.main.fsmUtils.abort_rpc
   */
  var trans = new qx.util.fsm.Transition(
    "Transition_AwaitRpcResult_to_AwaitRpcResult_via_button_abort",
    {
      "nextState" :
        "State_AwaitRpcResult",

      "ontransition" :
        function(fsm, event)
        {
          // Get the request object
          var rpcRequest = _this.getCurrentRpcRequest();

          // Issue an abort for the pending request
          rpcRequest.request.abort();
        }
    });
  state.addTransition(trans);

  /*
   * Transition: AwaitRpcResult to PopStack
   *
   * Cause: "complete" (on RPC)
   */
  var trans = new qx.util.fsm.Transition(
    "Transition_AwaitRpcResult_to_PopStack_via_complete",
    {
      "nextState" :
        qx.util.fsm.FiniteStateMachine.StateChange.POP_STATE_STACK,

      "ontransition" :
        function(fsm, event)
        {
          // Get the request object
          var rpcRequest = _this.getCurrentRpcRequest();
          
          // Generate the result for a completed request
          rpcRequest.setUserData("result",
                                  {
                                      type : "complete",
                                      data : event.getData()
                                  });
        }
    });
  state.addTransition(trans);

  /*
   * State: Authenticate
   *
   * Transition on:
   *  "execute" on login_button
   */
  var state = new qx.util.fsm.State(
    "State_Authenticate",
    {
      "onentry" :
        function(fsm, event)
        {
          // Retrieve the login window object
          var win = fsm.getObject("login_window");

          // Clear the password field
          win.password.setValue("");

          // If there's no value selected for domain...
          if (win.domain.getValue() == null)
          {
            // ... then select the first value
            win.domain.setSelected(win.domain.getList().getFirstChild());
          }

          // Retrieve the current RPC request
          var rpcRequest = _this.getCurrentRpcRequest();

          // Did we just return from an RPC request and was it a login request?
          if (fsm.getPreviousState() == "State_AwaitRpcResult" &&
              rpcRequest.service == "samba.system" &&
              rpcRequest.params.length > 1 &&
              rpcRequest.params[1] == "login")
          {
            // Yup.  Display the result.  Pop the old request off the stack
            var loginRequest = _this.popRpcRequest();

            // Retrieve the result
            var result = loginRequest.getUserData("result");

            // Did we succeed?
            if (result.type == "failed")
            {
              // Nope.  Just reset the caption, and remain in this state.
              win.setCaption("Login Failed.  Try again.");
            }
            else
            {
              // Login was successful.  Generate an event that will transition
              // us back to the AwaitRpcResult state to again await the result
              // of the original RPC request.
              win.dispatchEvent(new qx.event.type.Event("complete"), true);

              // Reissue the original request.  (We already popped the login
              // request off the stack, so the current request is the original
              // one.)
              var origRequest = _this.getCurrentRpcRequest();
              
              // Retrieve the RPC object */
              var rpc = fsm.getObject("swat.main.rpc");

              // Set the service name
              rpc.setServiceName(origRequest.service);

              // Reissue the request
              origRequest.request =
                qx.io.remote.Rpc.prototype.callAsyncListeners.apply(
                  rpc,
                  origRequest.params);

              // Clear the password field, for good measure
              win.password.setValue("");

              // Close the login window
              win.close();
            }

            // Dispose of the login request
            loginRequest.request.dispose();
            loginRequest.request = null;
          }
        },

      "events" :
      {
        "execute"  :
        {
          "login_button" :
            "Transition_Authenticate_to_AwaitRpcResult_via_button_login"
        },

        "complete"  :
        {
          "login_window" :
            "Transition_Authenticate_to_AwaitRpcResult_via_complete"
        }
      }
    });
  fsm.addState(state);

  /*
   * Transition: Authenticate to AwaitRpcResult
   *
   * Cause: "execute" on login_button
   */
  var trans = new qx.util.fsm.Transition(
    "Transition_Authenticate_to_AwaitRpcResult_via_button_login",
    {
      "nextState" :
        "State_AwaitRpcResult",

      "ontransition" :
        function(fsm, event)
        {
          // Retrieve the login window object
          var win = fsm.getObject("login_window");

          // Issue a Login call
          _this.callRpc(fsm,
                        "samba.system",
                        "login",
                        [
                          win.userName.getValue(),
                          win.password.getValue(),
                          win.domain.getValue()
                        ]);
        }
    });
  state.addTransition(trans);

  /*
   * Transition: Authenticate to AwaitRpcResult
   *
   * Cause: "complete" on login_window
   *
   * We've already re-issued the original request, so we have nothing to do
   * here but transition back to the AwaitRpcResult state to again await the
   * result of the original request.
   */
  var trans = new qx.util.fsm.Transition(
    "Transition_Authenticate_to_AwaitRpcResult_via_complete",
    {
      "nextState" :
        "State_AwaitRpcResult"
    });
  state.addTransition(trans);
};


/**
 * Issue a remote procedure call.
 *
 * @param fsm {qx.util.fsm.FiniteStateMachine}
 *   The finite state machine issuing this remote procedure call.
 *
 * @param service {String}
 *   The name of the remote service which provides the specified method.
 *
 * @param method {String}
 *   The name of the method within the specified service.
 *
 * @param params {Array}
 *   The parameters to be passed to the specified method.
 *
 * @return {Object}
 *   The request object for the just-issued RPC request.
 */
qx.Proto.callRpc = function(fsm, service, method, params)
{
  // Create an object to hold a copy of the parameters.  (We need a
  // qx.core.Object() to be able to store this in the finite state machine.)
  var rpcRequest = new qx.core.Object();

  // Save the service name
  rpcRequest.service = service;

  // Copy the parameters; we'll prefix our copy with additional params
  rpcRequest.params = params.slice(0);

  // Prepend the method
  rpcRequest.params.unshift(method);

  // Prepend the flag indicating to coalesce failure events
  rpcRequest.params.unshift(true);

  // Retrieve the RPC object */
  var rpc = fsm.getObject("swat.main.rpc");

  // Set the service name
  rpc.setServiceName(rpcRequest.service);

  // Issue the request
  rpcRequest.request =
    qx.io.remote.Rpc.prototype.callAsyncListeners.apply(rpc,
                                                        rpcRequest.params);

  // Make the rpc request object available to the AwaitRpcResult state
  this.pushRpcRequest(rpcRequest);

  // Give 'em what they came for
  return rpcRequest;
};


/**
 * Push an RPC request onto the request stack.
 *
 * @param request {Object}
 *   The just-issued rpc request object
 */
qx.Proto.pushRpcRequest = function(rpcRequest)
{
  this._requests.push(rpcRequest);
};


/**
 * Retrieve the most recent RPC request from the request stack and pop the
 * stack.
 *
 * @return {Object}
 *   The rpc request object from the top of the request stack
 */
qx.Proto.popRpcRequest = function()
{
  if (this._requests.length == 0)
  {
    throw new Error("Attempt to pop an RPC request when list is empty.");
  }

  var rpcRequest = this._requests.pop();
  return rpcRequest;
};


/**
 * Retrieve the most recent RPC request.
 *
 * @return {Object}
 *   The rpc request object at the top of the request stack
 */
qx.Proto.getCurrentRpcRequest = function()
{
  if (this._requests.length == 0)
  {
    throw new Error("Attempt to retrieve an RPC request when list is empty.");
  }

  return this._requests[this._requests.length - 1];
};


/**
 * JSON-RPC error origins
 */
qx.Class.JsonRpc_Origin =
{
  Server              : 1,
  Application         : 2,
  Transport           : 3,
  Client              : 4
};


/**
 * JSON-RPC Errors for origin == Server
 */
qx.Class.JsonRpc_ServerError =
{
  /**
   * Error code, value 0: Unknown Error
   *
   * The default error code, used only when no specific error code is passed
   * to the JsonRpcError constructor.  This code should generally not be used.
   */
  Unknown               : 0,

  /**
   * Error code, value 1: Illegal Service
   *
   * The service name contains illegal characters or is otherwise deemed
   * unacceptable to the JSON-RPC server.
   */
  IllegalService        : 1,

  /**
   * Error code, value 2: Service Not Found
   *
   * The requested service does not exist at the JSON-RPC server.
   */
  ServiceNotFound       : 2,

  /**
   * Error code, value 3: Class Not Found
   *
   * If the JSON-RPC server divides service methods into subsets (classes),
   * this indicates that the specified class was not found.  This is slightly
   * more detailed than "Method Not Found", but that error would always also
   * be legal (and true) whenever this one is returned. (Not used in this
   * implementation)
   */
  ClassNotFound         : 3, // not used in this implementation

  /**
   * Error code, value 4: Method Not Found
   *
   * The method specified in the request is not found in the requested
   * service.
   */
  MethodNotFound        : 4,

  /*
   * Error code, value 5: Parameter Mismatch
   *
   * If a method discovers that the parameters (arguments) provided to it do
   * not match the requisite types for the method's parameters, it should
   * return this error code to indicate so to the caller.
   *
   * This error is also used to indicate an illegal parameter value, in server
   * scripts.
   */
  ParameterMismatch     : 5,

  /**
   * Error code, value 6: Permission Denied
   *
   * A JSON-RPC service provider can require authentication, and that
   * authentication can be implemented such the method takes authentication
   * parameters, or such that a method or class of methods requires prior
   * authentication.  If the caller has not properly authenticated to use the
   * requested method, this error code is returned.
   */
  PermissionDenied      : 6,

  /*** Errors generated by this server which are not qooxdoo-standard ***/

  /*
   * Error code, value 1000: Unexpected Output
   *
   * The called method illegally generated output to the browser, which would
   * have preceeded the JSON-RPC data.
   */
  UnexpectedOutput      : 1000,

  /*
   * Error code, value 1001: Resource Error
   *
   * Too many resources were requested, a system limitation on the total number
   * of resources has been reached, or a resource or resource id was misused.
   */
  ResourceError         : 1001,

  /*
   * Error code, value 1002: Not Logged In
   *
   * The user has logged out and must re-authenticate, or this is a brand new
   * session and the user must log in.
   *
   */
  NotLoggedIn           : 1002,

  /*
   * Error code, value 1003: Session Expired
   *
   * The session has expired and the user must re-authenticate.
   *
   */
  SessionExpired        : 1003,

  /*
   * Error code, value 1004: Login Failed
   *
   * An attempt to log in failed.
   *
   */
  LoginFailed           : 1004
};