summaryrefslogtreecommitdiff
path: root/webapps/swat/source/class/swat/module/ldbbrowse/Gui.js
blob: a9db18b712639cea5be9b0f19c15944502b59ba9 (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
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
/*
 * Copyright:
 *   (C) 2006 by Derrell Lipman
 *       All rights reserved
 *
 * License:
 *   LGPL 2.1: http://creativecommons.org/licenses/LGPL/2.1/
 */

/**
 * Swat LDB Browser class graphical user interface
 */
qx.OO.defineClass("swat.module.ldbbrowse.Gui", qx.core.Object,
function()
{
  qx.core.Object.call(this);
});

//qx.OO.addProperty({ name : "_table", type : "object" });
//qx.OO.addProperty({ name : "_ldbmod", type : "object" });

/**
 * Build the raw graphical user interface.
 */
qx.Proto.buildGui = function(module)
{
  var o;
  var fsm = module.fsm;

  // We need a horizontal box layout for the database name
  var hlayout = new qx.ui.layout.HorizontalBoxLayout();
  hlayout.set({
                  top: 20,
                  left: 20,
                  right: 20,
                  height: 30
              });

  // Create a label for the database name
  o = new qx.ui.basic.Atom("Database:");
  o.setWidth(100);
  o.setHorizontalChildrenAlign("right");

  // Add the label to the horizontal layout
  hlayout.add(o);

  // Create a combo box for for the database name
  o = new qx.ui.form.ComboBox();
  o.getField().setWidth("100%");
  o.setEditable(false);

  // Add our global database name (the only option, for now)
  var item = new qx.ui.form.ListItem(module.dbFile);
  o.add(item);
  
  // We want to be notified if the selection changes
  o.addEventListener("changeSelection", fsm.eventListener, fsm);

  // Save the database name object so we can react to changes
  fsm.addObject("dbName", o);
    
  // Add the combo box to the horizontal layout
  hlayout.add(o);

  // Add the database name selection to the canvas
  module.canvas.add(hlayout);

  // Create and position the tabview
  var tabView_ = new qx.ui.pageview.tabview.TabView;
  tabView_.set({
                   top: 60,
                   left: 20,
                   right: 20,
                   bottom: 20
               });

  // Create each of the tabs
  var tabView_Browse =
  new qx.ui.pageview.tabview.Button("Browse");
  var tabView_Search =
  new qx.ui.pageview.tabview.Button("Search");

  // Specify the initially-selected tab
  tabView_Browse.setChecked(true);

  // Add each of the tabs to the tabview
  tabView_.getBar().add(tabView_Browse, tabView_Search);

  // Create the pages to display when each tab is selected
  var tabViewPage_Browse =
  new qx.ui.pageview.tabview.Page(tabView_Browse);
  var tabViewPage_Search =
  new qx.ui.pageview.tabview.Page(tabView_Search);

  // Build the browse page
  this._buildPageBrowse(module, tabViewPage_Browse);

  // Build the search page
  this._buildPageSearch(module, tabViewPage_Search);

  // Add the pages to the tabview
  tabView_.getPane().add(tabViewPage_Browse, tabViewPage_Search);

  // Add the tabview to our canvas
  module.canvas.add(tabView_);
};


/**
 * Populate the graphical user interface with the specified data
 *
 * @param module {swat.main.Module}
 *   The module descriptor for the module.
 *
 * @result {Object}
 *   The result returned by SAMBA to our request.  We display the data
 *   provided by this result.
 */
qx.Proto.displayData = function(module, rpcRequest)
{
  var gui = module.gui;
  var fsm = module.fsm;
  var result = rpcRequest.getUserData("result")
  var requestType = rpcRequest.getUserData("requestType");

  // Did the request fail?
  if (result.type == "failed")
  {
    // Yup.  We're not going to do anything particularly elegant...
    alert("Async(" + result.id + ") exception: " + result.data);
    return;
  }

  // Dispatch to the appropriate handler, depending on the request type
  switch(requestType)
  {
  case "search":
    this._displaySearchResults(module, rpcRequest);
    break;

  case "add":
  case "modify":
  case "delete":
    this._displayCommitResults(module, rpcRequest, requestType);
    break;
 
  case "tree_open":
    this._displayTreeOpenResults(module, rpcRequest);
    break;

  case "tree_selection_changed":

    // Always update the table, even if it is not visible
    this._displayTreeSelectionChangedResults(module, rpcRequest);

    // Update the base field in ldbmod
    this._displayLdbmodBaseChanged(module, rpcRequest);

    break;

  case "database_name_changed":
    this._clearAllFields(module, rpcRequest);
    break;

  default:
    throw new Error("Unexpected request type: " + requestType);
  }

  // Force flushing of pending DOM updates.  This is actually a
  // work-around for a bug.  Without this, occasionally, updates to the
  // gui aren't displayed until some 'action' takes place, e.g. key
  // press or mouse movement.
  qx.ui.core.Widget.flushGlobalQueues();
};


qx.Proto._setAppearances = function()
{
    // Modify the default appearance of a ComboBox for use in Search tab:
    //   use more of the available width.
    //
    // If we had multiple uses, we'd create a new appearance which didn't
    // contain a width.  That way, we'd be able to assign a specific width to
    // each ComboBox instance.  Since we don't have multiple of them, we can
    // just modify this default appearance.
    //
    // See http://qooxdoo.org/documentation/user_manual/appearance for an
    // explanation of what's going on here.  The missing significant point in
    // the discussion is that in the current qooxdoo appearance
    // implementation, it's not possible to override a specific widget's
    // appearance with explicit settings just for that widget (stupid!).  I
    // expect that to change in a future version.
    var appMgr = qx.manager.object.AppearanceManager.getInstance();
    var theme = appMgr.getAppearanceTheme();
    var appearance = theme._appearances["combo-box"];
    if (! appearance)
    {
        return;
    }
    var oldInitial = appearance.initial;
    appearance.initial = function(vTheme)
    {
        var res = oldInitial ? oldInitial.apply(this, arguments) : {};
        res.width = "80%";
        return res;
    }
};


qx.Proto._buildPageSearch = function(module, page)
{
  var fsm = module.fsm;

  // We need a vertical box layout for the various input fields
  var vlayout = new qx.ui.layout.VerticalBoxLayout();
  vlayout.set({
               overflow: "hidden",
               height: 120,
               top: 10,
               left: 0,
               right: 0,
               bottom: 10
           });

  // Create a label for the list of required attributes
  var label = new qx.ui.basic.Atom("Search Expression");
  label.setHorizontalChildrenAlign("left");

  // Add the label to the horizontal layout
  vlayout.add(label);

  // Create a combo box for entry of the search expression
  var filter = new qx.ui.form.TextField();
  filter.set({ width:300 });
  fsm.addObject("searchExpr", filter);
    
  // Add the combo box to the horizontal layout
  vlayout.add(filter);

  // Create a label for the list of required attributes
  var label = new qx.ui.basic.Atom("Base");
  label.setHorizontalChildrenAlign("left");

  // Add the label to the horizontal layout
  vlayout.add(label);

  // Create a combo box for entry of the search expression
  var base = new qx.ui.form.TextField();
  base.set({ width:300 });
  fsm.addObject("baseDN", base);
    
  // Add the combo box to the horizontal layout
  vlayout.add(base);

  // Create a label for the list of required attributes
  var label = new qx.ui.basic.Atom("Scope");
  label.setWidth(100);
  label.setHorizontalChildrenAlign("left");

  // Add the label to the scope vertical layout
  vlayout.add(label);

  // Use a horizontal box layout to keep the search button aligned
  var hlayout = new qx.ui.layout.HorizontalBoxLayout();
  hlayout.setWidth(300);
  hlayout.setHeight(30);

  var cbScope = new qx.ui.form.ComboBoxEx();
  cbScope.setSelection([ ["subtree", "Subtree"], ["one", "One Level"], ["base", "Base"]]);
  cbScope.setSelectedIndex(0);

  fsm.addObject("scope", cbScope);

  hlayout.add(cbScope);

  // Add a sapcer
  hlayout.add(new qx.ui.basic.HorizontalSpacer());

  // Create the 'Search' button
  var search = new qx.ui.form.Button('Search');
  search.setWidth(100);
  search.addEventListener("execute", fsm.eventListener, fsm);

  // We'll be receiving events on the search object, so save its friendly name
  fsm.addObject("search", search, "swat.main.fsmUtils.disable_during_rpc");

  // Add the search button to the vertical layout
  hlayout.add(search);

  vlayout.add(hlayout);

  // Add the vlayout to the page
  page.add(vlayout);

  var ldifview = new swat.module.ldbbrowse.LdifViewer();
  ldifview.set({
               top: 130,
               left: 10,
               right: 10,
               bottom: 10
           });

  fsm.addObject("LdifView", ldifview);

  // Add the output area to the page
  page.add(ldifview);
};

qx.Proto._buildPageBrowse = function(module, page)
{
  var fsm = module.fsm;

  // Create a horizontal splitpane for tree (left) and table (right)
  var splitpane = new qx.ui.splitpane.HorizontalSplitPane("1*", "2*");
  splitpane.setEdge(0);

  // We need a vertical box layout for the tree and the buttons
  var vlayout = new qx.ui.layout.VerticalBoxLayout();
  vlayout.set({
               height: "100%",
               top: 5,
               left: 5,
               right: 5,
               bottom: 5,
               spacing: 10
           });

  // Create the tree and set its characteristics
  var tree = new qx.ui.treevirtual.TreeVirtual(["Browse"]);
  tree.set({
             backgroundColor: 255,
             border : qx.renderer.border.BorderPresets.getInstance().thinInset,
             overflow: "hidden",
             width: "100%",
             height: "1*",
             alwaysShowOpenCloseSymbol: true
           });

  // We've only got one column, so we don't need cell focus indication.
  tree.setCellFocusAttributes({ backgroundColor : "transparent" });

  // Get the data model
  var dataModel = tree.getDataModel();

  // Add the database file as the first node
  dataModel.addBranch(null, module.dbFile, false);

  // We're finished adding nodes.
  dataModel.setData();

  // Create event listeners
  tree.addEventListener("treeOpenWhileEmpty", fsm.eventListener, fsm);
  tree.addEventListener("changeSelection", fsm.eventListener, fsm);

  // We'll be receiving events on the tree object, so save its friendly name,
  // and cause the tree to be disabled during remote procedure calls.
  fsm.addObject("tree", tree, "swat.main.fsmUtils.disable_during_rpc");

  // Add the tree to the vlayout.
  vlayout.add(tree);

  // Add an horizonatl layout for the "New" and "Modify" buttons
  // We need a vertical box layout for the tree and the buttons
  var hlayout = new qx.ui.layout.HorizontalBoxLayout();
  hlayout.set({
               height: "auto",
               spacing: 10
           });

  // Add the "New" button
  this._newb = new qx.ui.form.Button("New");
  this._newb.addEventListener("execute", this._switchToNewrecord, this);

  // Add the button to the hlayout
  hlayout.add(this._newb);

  // Add the "New" button
  this._modb = new qx.ui.form.Button("Modify");
  this._modb.addEventListener("execute", this._switchToModrecord, this);

  // Add the button to the hlayout
  hlayout.add(this._modb);

  hlayout.add(new qx.ui.basic.HorizontalSpacer());

  // Add the "Delete" button
  this._delb = new qx.ui.form.Button("Delete");
  this._delb.addEventListener("execute", this._confirmDeleteRecord, this);

  // Add the button to the hlayout
  hlayout.add(this._delb);
  
  // Add the hlayout to the vlayout.
  vlayout.add(hlayout);

  //Add the left vlayout to the splitpane
  splitpane.addLeft(vlayout);

  // Create a simple table model
  var tableModel = new qx.ui.table.SimpleTableModel();
  tableModel.setColumns([ "Attribute", "Value" ]);

  tableModel.setColumnEditable(0, false);
  tableModel.setColumnEditable(1, false);
  fsm.addObject("tableModel:browse", tableModel);

  // Create a table
  this._table = new qx.ui.table.Table(tableModel);
  this._table.set({
                top: 10,
                left: 0,
                right: 0,
                bottom: 10,
                statusBarVisible: false,
                columnVisibilityButtonVisible: false
            });
  this._table.setColumnWidth(0, 180);
  this._table.setColumnWidth(1, 320);
  this._table.setMetaColumnCounts([1, -1]);
  fsm.addObject("table:browse", this._table);

  //table.setDisplay(false);

  // Add the table to the bottom portion of the splitpane
  splitpane.addRight(this._table);

  // Build the create/modify widget
  this._ldbmod = new swat.module.ldbbrowse.LdbModify(fsm);
  this._ldbmod.set({
                top: 10,
                left: 0,
                right: 0,
                bottom: 10
            });
  // Not displayed by default
  this._ldbmod.setDisplay(false);

  fsm.addObject("ldbmod", this._ldbmod);

  splitpane.addRight(this._ldbmod);

  // Add the first splitpane to the page
  page.add(splitpane);
};

qx.Proto._switchToNormal = function()
{
  this._table.setDisplay(true);
  this._ldbmod.setDisplay(false);
  this._newb.setEnabled(true);
  this._modb.setEnabled(true);
  this._delb.setEnabled(true);
};

qx.Proto._switchToNewrecord = function()
{
  this._table.setDisplay(false);
  this._ldbmod.setDisplay(true);
  this._newb.setEnabled(false);
  this._modb.setEnabled(false);
  this._delb.setEnabled(false);
  this._ldbmod.initNew(this._switchToNormal, this);
};

qx.Proto._switchToModrecord = function()
{
  this._table.setDisplay(false);
  this._ldbmod.setDisplay(true);
  this._newb.setEnabled(false);
  this._modb.setEnabled(false);
  this._delb.setEnabled(false);
  this._ldbmod.initMod(this._table.getTableModel(), this._switchToNormal, this);
};

qx.Proto._confirmDeleteRecord = function()
{
  
  //this._newb.setEnabled(false);
  //this._modb.setEnabled(false);
  //this._delb.setEnabled(false);
  this._ldbmod.showConfirmDelete();
};

qx.Proto._displayCommitResults = function(module, rpcRequest, type)
{
  var result = rpcRequest.getUserData("result");

  switch (type) {
  case "add":
    alert("Object successfully added!");
    break;

  case "modify":
    alert("Object successfully modified!");
    break;

  case "delete":
    alert("Object Successfully deleted!");
    break;
  }

  this._switchToNormal();

  //TODO: reload tree after add or delete

};

qx.Proto._displaySearchResults = function(module, rpcRequest)
{
  var fsm = module.fsm;

  // Obtain the ldif object
  var ldifview = fsm.getObject("LdifView");

  ldifview.reset();

  // Obtain the result object
  result = rpcRequest.getUserData("result").data;

  if (result && result["length"])
  {
    len = result["length"];
    for (var i = 0; i < result["length"]; i++)
    {
      var obj = result[i];
      if (typeof(obj) != "object")
      {
        alert("Found unexpected result, type " +
              typeof(obj) +
              ", " +
              obj +
              "\n");
        continue;
      }
      ldifview.appendObject(obj);
    }
  }
  else
  {
    alert("No results.");
  }
};


qx.Proto._displayTreeOpenResults = function(module, rpcRequest)
{
  var t;
  var child;
  var fsm = module.fsm;

  // Get the tree object
  var tree = fsm.getObject("tree");
  var dataModel = tree.getDataModel();

  // Obtain the result object
  var result = rpcRequest.getUserData("result").data;

  // We also need some of the original parameters passed to the request
  var parentNode = rpcRequest.getUserData("parentNode");
  var attributes = rpcRequest.getUserData("attributes");

  // Any children?
  if (! result || result["length"] == 0)
  {
    // Nope.  Remove parent's expand/contract button.
    dataModel.setState(parentNode.nodeId, { bHideOpenClose : true });
    dataModel.setData();
    return;
  }

  // base object, add naming contexts to the root
  if ((result.length == 1) &&
      ((result[0]["dn"] == "") ||
       (result[0]["dn"].toLowerCase() == "cn=rootdse")))
  {
    defnc = result[0]["defaultNamingContext"];

    // Build a tree row for the defaultNamingContext
    if (defnc)
    {
      dataModel.addBranch(parentNode.nodeId, defnc, false);
    }

    var ncs = result[0]["namingContexts"];

    // If it's multi-valued (type is an array) we have other naming contexts
    // to show
    if (typeof(ncs) == "object")
    {
      for (var i = 0; i < ncs.length; i++)
      {
        if (ncs[i] != defnc)
        {
          //skip default naming context
          dataModel.addBranch(parentNode.nodeId, ncs[i], false);
        }
      }
    }
  }
  else
  {
    for (var i = 0; i < result.length; i++)
    {
      var name;
  
      child = result[i];
  
      name = child["dn"].split(",")[0];
  
      // Build a standard tree row
      dataModel.addBranch(parentNode.nodeId, name, false);
    }
  }

  // Cause the tree changes to be rendered
  dataModel.setData();
};

qx.Proto._displayLdbmodBaseChanged = function(module, rpcRequest)
{
  var fsm = module.fsm;

  // Obtain the result object
  var result = rpcRequest.getUserData("result").data;

  // If we received an empty list, ...
  if (result == null)
  {
    // ... then ??
    return;
  }

  this._ldbmod.setBase(result[0]["dn"]);
};

qx.Proto._displayTreeSelectionChangedResults = function(module, rpcRequest)
{
  var fsm = module.fsm;

  // Obtain the result object
  var result = rpcRequest.getUserData("result").data;

  var tree = fsm.getObject("tree");
  var dataModel = tree.getDataModel();

  // If we received an empty list, ...
  if (result == null)
  {
    // ... then just clear the attribute/value table.
    dataModel.setData([ ]);
    return;
  }

  // Start with an empty table dataset
  var rowData = [ ];

  // The result contains a single object: attributes
  var attributes = result[0];

  // Track the maximum length of the attribute values
  var maxLen = 0;

  // For each attribute we received...
  for (var attr in attributes)
  {
    // If it's multi-valued (type is an array)...
    if (typeof(attributes[attr]) == "object")
    {
      // ... then add each value with same name
      var a = attributes[attr];
      for (var i = 0; i < a.length; i++)
      {
        if (a[i].length > maxLen)
        {
          maxLen = a[i].length;
        }
        rowData.push([ attr, a[i] ]);
      }
    }
    else    // single-valued
    {
      // ... add its name and value to the table dataset
      if (attributes[attr].length > maxLen)
      {
        maxLen = attributes[attr].length;
      }
      rowData.push([ attr, attributes[attr] ]);
    }
  }

  // Obtain the table and tableModel objects
  var table = fsm.getObject("table:browse");
  var tableModel = fsm.getObject("tableModel:browse");

  // Adjust the width of the value column based on maxLen
  table.setColumnWidth(1, maxLen * 7);

  // Add the dataset to the table
  tableModel.setData(rowData);
};


qx.Proto._clearAllFields = function(module, rpcRequest)
{
  // Obtain the result object
  var result = rpcRequest.getUserData("result").data;

  // Retrieve the database handle
  module.dbHandle = result;

  // In the future, when we support more than one database, we'll want to
  // clear all fields here.  For now, there's no need.
};



/**
 * Singleton Instance Getter
 */
qx.Class.getInstance = qx.lang.Function.returnInstance;