summaryrefslogtreecommitdiff
path: root/swat/apps/samba/utils/ldbbrowse.html
blob: 81b74c44d2b71c62743286d01d3d3b2b54f4486d (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
721
722
723
724
<html>
<head>
  <script type="text/javascript">
     /*
      * Copyright:
      *   (C) 2006 by Derrell Lipman
      *       All rights reserved
      *
      * License:
      *   LGPL 2.1: http://creativecommons.org/licenses/LGPL/2.1/
      */
  </script>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>ldbbrowse</title>
  <link type="text/css" rel="stylesheet" href="../../resource/css/layout-samba.css"/>
  <!--[if IE]>
  <link
   type="text/css" rel="stylesheet" href="../../resource/css/layout-samba_ie.css"/>
  <![endif]-->
  <script type="text/javascript" src="../../script/qx.js"></script>
</head>
<body>
  <script type="text/javascript" src="../../script/layout-samba.js"></script>

<div style="position: absolute; background-color: transparent; right:0; top:0; z-index: 1000000001;">
     <img src="../../../images/logo.png">
</div>
<div id="demoHead">
     ldbbrowse
</div>


<script type="text/javascript">

// All of our variables which must be in a global scope will be part of this
// object
globals = new Object();

// Name of database to use
globals.dbFile = "sam.ldb";

// No database is yet open
globals.dbHandle = null;
        

function setAppearances()
{
    // 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;
    }
}

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

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

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

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

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

    // Build the search page
    var searchResultsTable = buildPageSearch(tabViewPage_Search);

    // Provide access to the search results table
    tabView_.searchResultTable = searchResultsTable;

    // Build the browse page
    buildPageBrowse(tabViewPage_Browse);

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

    // Add the tabview to the document
    clientDocument.add(tabView_);

    // Give 'em what we built!
    return tabView_;
}

function buildPageSearch(page)
{
    // We need a vertical box layout for the various input fields
    var vlayout = new qx.ui.layout.VerticalBoxLayout();
    vlayout.setWidth("100%");

    // We need a horizontal box layout for the search combo box and its label
    var hlayout = new qx.ui.layout.HorizontalBoxLayout();
    hlayout.setWidth("100%");
    hlayout.setHeight(25);

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

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

    // Create a combo box for entry of the search expression
    var search = new qx.ui.form.ComboBox();
    search.getField().setWidth("100%");
    search.setEditable(true);
    
    // Add the combo box to the horizontal layout
    hlayout.add(search);

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

    // We need a horizontal box layout for the base combo box and its label
    hlayout = new qx.ui.layout.HorizontalBoxLayout();
    hlayout.setWidth("100%");
    hlayout.setHeight(25);

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

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

    // Create a combo box for entry of the search expression
    var base = new qx.ui.form.ComboBox();
    base.getField().setWidth("100%");
    base.setEditable(true);
    
    // Add the combo box to the horizontal layout
    hlayout.add(base);

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

    // We need a horizontal box layout for scope radio buttons
    hlayout = new qx.ui.layout.HorizontalBoxLayout();
    hlayout.setWidth("100%");
    hlayout.setHeight(25);

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

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

    // Create a radio button for each scope
    var rbDefault = new qx.ui.form.RadioButton("Default",   "default");
    var rbBase    = new qx.ui.form.RadioButton("Base",      "base");
    var rbOne     = new qx.ui.form.RadioButton("One Level", "one");
    var rbSubtree = new qx.ui.form.RadioButton("Subtree",   "subtree");

    // Use a default of "Default"
    rbBase.setChecked(true);

    // Add the radio buttons to the horizontal layout
    hlayout.add(rbDefault, rbBase, rbOne, rbSubtree);

    // Group the radio buttons so only one in the group may be selected
    var scope = new qx.manager.selection.RadioManager("scope",
                                                      [
                                                          rbDefault,
                                                          rbBase,
                                                          rbOne,
                                                          rbSubtree
                                                      ]);
    
    // Right-justify the 'Find' button
    var spacer = new qx.ui.basic.HorizontalSpacer;
    hlayout.add(spacer);

    // Create the 'Find' button
    var find = new qx.ui.form.Button('Find');
    hlayout.add(find);

    // Add the Find button line to the vertical layout
    vlayout.add(hlayout);

    // We'll be setting url and service upon execute; no need to do it now.
    var rpc = new qx.io.remote.Rpc();
    rpc.setTimeout(60000);
    var mycall = null;

    find.addEventListener("execute", function()
    {
        // Set the URL and Service
        rpc.setUrl("/services/");
        rpc.setServiceName("samba.ldb");
        rpc.setCrossDomain(false);

        find.setEnabled(false);
        mycall = rpc.callAsync(function(result, ex, id)
        {
            mycall = null;
            if (ex == null)
            {
                var rowData = [];

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

                for (var i = 0; i < result.length; i++)
                {
                    var o = result[i];
                    if (typeof(o) != "object")
                    {
                        alert("Found unexpected result, type " +
                              typeof(o) +
                              ", " +
                              o +
                              "\n");
                        continue;
                    }
                    for (var field in o)
                    {
                        // skip dn and distinguishedName fields;
                        // they're shown in each row anyway.
                        if (field == "dn" || field == "distinguishedName")
                        {
                            continue;
                        }

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

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

                    // Tell the table to use the new data
                    tableModel.setData(rowData);
                }
            }
            else
            {
                alert("Async(" + id + ") exception: " + ex);
            }
            find.setEnabled(true);
        },
        "search",                       // method
        globals.dbHandle,               // database handle
        search.getValue(),              // search expression
        base.getValue(),                // baseDN
        scope.getSelected().getValue(), // scope
        [ "*" ]);                       // attributes
    });

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

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

    tableModel.setColumnEditable(0, false);
    tableModel.setColumnEditable(1, false);
    tableModel.setColumnEditable(2, false);

    // Create a table
    var table = new qx.ui.table.Table(tableModel);
    with (table)
    {
      set({
              top: 80,
              left: 0,
              right: 0,
              bottom: 10,
              statusBarVisible: false,
              columnVisibilityButtonVisible: false
          });
      setColumnWidth(0, 300);
      setColumnWidth(1, 180);
      setColumnWidth(2, 240);
      setMetaColumnCounts([ 1, -1 ]); // h-scroll attribute and value together
    };

    page.add(table);

    return table;
}

function buildPageBrowse(page)
{
    /*
     * Reset the default of always showing the plus/minus symbol.  The
     * default is 'false'.  We want to always display it for each folder
     * (and then stop displaying it if we determine upon open that there
     * are no contents).
     */
    var constructor = qx.OO.classes["qx.ui.treefullcontrol.TreeFolder"];
    qx.Proto = constructor.prototype;
    qx.OO.changeProperty(
        {
            name         : "alwaysShowPlusMinusSymbol",
            type         : qx.constant.Type.BOOLEAN,
            defaultValue : true
        });

    // Create a vertical splitpane for tree (top) and table (bottom)
    var splitpane = new qx.ui.splitpane.VerticalSplitPane("1*", "2*");
    splitpane.setEdge(0);

    // Create a tree row structure for the tree root
    var trs = qx.ui.treefullcontrol.TreeRowStructure.getInstance().standard(globals.dbFile);

    // Create the tree and set its characteristics
    var tree = new qx.ui.treefullcontrol.Tree(trs);
    tree.set(
        {
            backgroundColor: 255,
            border: qx.renderer.border.BorderPresets.getInstance().inset,
            overflow: "auto",
            height: null,
            top: 10,
            left: 0,
            right: 0,
            bottom: 10,
            open: false
        });

    var addChildren = function(parent, children, retrieve)
    {
        var t;
        var trs;
        var child;

        // Any children?
        if (! children || children["length"] == 0)
        {
            // Nope.  Allow parent's expand/contract button to be removed
            parent.setAlwaysShowPlusMinusSymbol(false);
            return;
        }

        for (i = 0; i < children.length; i++)
        {
            var name;

            child = children[i];

            // Determine name for new tree row.  If first level, use entire
            // DN.  Otherwise, strip off first additional component.
            if (retrieve == "defaultNamingContext")
            {
                name = child["defaultNamingContext"];
            }
            else
            {
                name = child["dn"].split(",")[0];
            }

            // Build a standard tree row
            trs = qx.ui.treefullcontrol.TreeRowStructure.getInstance().standard(name);

            // This row is a "folder" (it can have children)
            t = new qx.ui.treefullcontrol.TreeFolder(trs);

            // Add this row to its parent
            parent.add(t);
        }

        // 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();
    }

    // Prepare to issue RPC calls
    var rpc = new qx.io.remote.Rpc();
    rpc.setTimeout(60000);
    rpc.setUrl("/services/");
    rpc.setServiceName("samba.ldb");
    rpc.setCrossDomain(false);

    /*
     * All subtrees will use this root node's event listeners.  Create an
     * event listener for an open while empty.
     */
    tree.addEventListener(
        qx.constant.Event.TREEOPENWHILEEMPTY,
        function(e)
        {
            var parent = e.getData();
            var hierarchy = parent.getHierarchy(new Array());

            parent.debug("Requesting children...");

            // Strip off the root node
            hierarchy.shift();

            // Determine the children.  Differs depending on root or otherwise
            var attributes;
            var scope;
            var baseDN;
            
            // If parent is the root...
            if (parent == tree)
            {
                // ... then we want the defaultNamingContext, ...
                attributes = "defaultNamingContext";

                // ... and we want only base scope
                scope = "base";

                // ... and an empty base DN
                baseDN = "";
            }
            else
            {
                // otherwise, retrieve the DN,
                attributes = "dn";

                // ... and we want one level of scope
                scope = "one";

                // ... and base DN is the parent
                baseDN = hierarchy.reverse().join(",");
            }

            mycall = rpc.callAsync(function(result, ex, id)
            {
                mycall = null;
                if (ex == null)
                {
                    parent.debug("Children obtained.  Rendering...");
                    addChildren(parent, result, attributes);
                    parent.debug("Rendering complete.");
                }
                else
                {
                    alert("Async(" + id + ") exception: " + ex);
                }
            },
            "search",                       // method
            globals.dbHandle,               // database handle
            "(objectclass=*)",              // search expression
            baseDN,                         // baseDN
            scope,                          // scope
            [ attributes ]);                // attributes
        });

    /*
     * All subtrees will use this root node's event listeners.  Create an
     * event listener for selection changed, to populate attribute/value table
     */
    tree.getManager().addEventListener(
        qx.constant.Event.CHANGESELECTION,
        function(e)
        {
            var element = e.getData()[0];
            var hierarchy = element.getHierarchy(new Array());

            // Strip off the root node
            hierarchy.shift();

            // Determine the children.  Differs depending on root or otherwise
            var attributes;
            var scope;
            var baseDN;
            
            // If element is the root...
            if (element == tree)
            {
                // ... then just clear out the attribute/value table.
                tableModel.setData([]);
                return;
            }

            // We want all attributes
            attributes = "*";

            // We want the attributes only for the current element
            scope = "base";

            // Base DN is the current element
            baseDN = hierarchy.reverse().join(",");

            mycall = rpc.callAsync(function(result, ex, id)
            {
                mycall = null;
                if (ex == null)
                {
                    // If we received an empty list, ...
                    if (result == null)
                    {
                        // ... then just clear the attribute/value table.
                        tableModel.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] ]);
                        }
                    }

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

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

                    // 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();
                }
                else
                {
                    alert("Async(" + id + ") exception: " + ex);
                }
            },
            "search",                       // method
            globals.dbHandle,               // database handle
            "(objectclass=*)",              // search expression
            baseDN,                         // baseDN
            scope,                          // scope
            [ attributes ]);                // attributes
        });




    // Add the tree to the page.
    splitpane.addTop(tree);

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

    tableModel.setColumnEditable(0, false);
    tableModel.setColumnEditable(1, false);

    // Create a table
    var table = new qx.ui.table.Table(tableModel);
    with (table) {
      set({
              top: 10,
              left: 0,
              right: 0,
              bottom: 10,
              statusBarVisible: false,
              columnVisibilityButtonVisible: false
          });
      setColumnWidth(0, 200);
      setColumnWidth(1, 440);
      setMetaColumnCounts([1, -1]);
    };

    // Add the table to the bottom portion of the splitpane
    splitpane.addBottom(table);

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

qx.core.Init.getInstance().defineMain(
    function()
    {
        // Enable JSON-RPC debugging
        qx.Settings.setCustomOfClass("qx.io.Json", "enableDebug", true);

        if (false)
        {
            // We'd like all table columns to do "live resize". Unfortunately,
            // it's too slow.  Maybe someone wants to work on it...
            var constructor = qx.OO.classes["qx.ui.table.TablePaneScroller"];
            qx.Proto = constructor.prototype;
            qx.OO.changeProperty(
                {
                    name         : "liveResize",
                    type         : qx.constant.Type.BOOLEAN,
                    defaultValue : true
                });
        }

        // Set appearances for this application
        setAppearances();

        // Get the client document
        var clientDocument = qx.ui.core.ClientDocument.getInstance();

        // Create the tabs and their windows, and attach to client document
        var tabView = setupTabs(clientDocument);

        // Open a database connection.  Uses the dangerous sync request.
        var rpc = new qx.io.remote.Rpc();
        rpc.setTimeout(60000);
        rpc.setUrl("/services/");
        rpc.setServiceName("samba.ldb");
        rpc.setCrossDomain(false);

        try
        {
            // Database handle
            globals.dbHandle = rpc.callSync("connect", globals.dbFile);
        }
        catch (ex)
        {
            alert("Sync exception: " + ex);
        }
    });
/*
 * Local Variables:
 * mode: c
 * End:
 */
</script>

</body>
</html>