summaryrefslogtreecommitdiffstats
path: root/install/ui/entity.js
blob: 7e600e4e7990fb06b6d6e0d275621a18584fadd9 (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
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
/*jsl:import ipa.js */
/*jsl:import navigation.js */

/*  Authors:
 *    Pavel Zuna <pzuna@redhat.com>
 *    Endi S. Dewata <edewata@redhat.com>
 *    Adam Young <ayoung@redhat.com>
 *
 * Copyright (C) 2010-2011 Red Hat
 * see file 'COPYING' for use and warranty information
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/* REQUIRES: ipa.js, details.js, search.js, add.js */

IPA.facet = function (spec) {

    spec = spec || {};

    var that = {};

    that.display_class = spec.display_class || 'entity-facet';
    that.name = spec.name;
    that.label = spec.label;
    that.title = spec.title || that.label;
    that._entity_name = spec.entity_name;

    that.dialogs = $.ordered_map();

    // facet group name
    that.facet_group = spec.facet_group;

    that.__defineGetter__('entity_name', function() {
        return that._entity_name;
    });

    that.__defineSetter__('entity_name', function(entity_name) {
        that._entity_name = entity_name;
    });

    that.get_dialog = function(name) {
        return that.dialogs.get(name);
    };

    that.dialog = function(dialog) {
        that.dialogs.put(dialog.name, dialog);
        return that;
    };

    that.init = function() {

        var dialogs = that.dialogs.values;
        for (var i=0; i<dialogs.length; i++){
            var dialog = dialogs[i];
            dialog.entity_name = that._entity_name;
            dialog.init();
        }
    };

    that.create = function(container) {

        that.container = container;

        that.header = $('<div/>', {
            'class': 'facet-header'
        }).appendTo(container);
        that.create_header(that.header);

        that.content = $('<div/>', {
            'class': 'facet-content'
        }).appendTo(container);
        that.create_content(that.content);
    };

    that.create_header = function(container) {

        that.title_container = $('<div/>', {
            'class': 'facet-title'
        }).appendTo(container);

        $('<h1/>').append(IPA.create_network_spinner()).appendTo(that.title_container);

        that.set_title(container, that.title);

        that.controls = $('<div/>', {
            'class': 'facet-controls'
        }).appendTo(container);
    };

    that.create_content = function(container) {
    };

    that.set_title = function(container, title) {
        var element = $('h1', that.title_container);
        element.html(title);
    };

    that.setup = function(container) {
        that.container = container;
    };

    that.show = function() {
        that.container.css('display', 'block');
    };

    that.hide = function() {
        that.container.css('display', 'none');
    };

    that.load = function() {
    };

    that.is_dirty = function() {
        return false;
    };

    that.get_content = function() {
        return $('.content', that.container);
    };

    that.report_error = function(error_thrown){
        var details = $('.details', that.container).empty();
        details.append('<p>Error: '+error_thrown.name+'</p>');
        details.append('<p>'+error_thrown.message+'</p>');
    };

    that.redirect_on_error = function(){
        var current_entity = that.entity;
        while (current_entity.containing_entity){
            current_entity = current_entity.containing_entity;
        }
        IPA.nav.show_page(
            current_entity.name,
            that.entity.redirect_facet);
    };

    var redirect_errors =
        ["IPA Error 4001"];

    that.on_error = function(xhr, text_status, error_thrown) {

        /*If the error is in talking to the server, don't attempt to redirect,
          as there is nothing any other facet can do either. */
        if (that.entity.redirect_facet )
        {
            for (var i =0; i <  redirect_errors.length; i += 1){
                if (error_thrown.name ===  redirect_errors[i]){
                    that.redirect_on_error();
                    return;
                }
            }
        }
        that.report_error(error_thrown);
    };


    // methods that should be invoked by subclasses
    that.facet_init = that.init;
    that.facet_create_header = that.create_header;
    that.facet_create_content = that.create_content;
    that.facet_setup = that.setup;
    that.facet_show = that.show;
    that.facet_hide = that.hide;

    return that;
};

IPA.table_facet = function(spec) {

    spec = spec || {};

    var that = IPA.facet(spec);

    that.managed_entity_name = spec.managed_entity_name || that.entity_name;

    that.columns = $.ordered_map();

    that.get_columns = function() {
        return that.columns.values;
    };

    that.get_column = function(name) {
        return that.columns.get(name);
    };

    that.add_column = function(column) {
        column.entity_name = that.managed_entity_name;
        that.columns.put(column.name, column);
    };

    that.create_column = function(spec) {
        var column;
        if (spec instanceof Object) {
            var factory = spec.factory || IPA.column;
            column = factory(spec);
        } else {
            column = IPA.column({ name: spec });
        }
        that.add_column(column);
        return column;
    };

    that.column = function(spec){
        that.create_column(spec);
        return that;
    };

    var columns = spec.columns || [];
    for (var i=0; i<columns.length; i++) {
        that.create_column(columns[i]);
    }

    return that;
};

IPA.facet_group = function(spec) {

    spec = spec || {};

    var that = {};

    that.name = spec.name;
    that.label = spec.label;

    that.facets = $.ordered_map();

    that.add_facet = function(facet) {
        that.facets.put(facet.name, facet);
    };

    that.get_facet = function(name) {
        return that.facets.get(name);
    };

    return that;
};

IPA.entity = function (spec) {

    spec = spec || {};

    var that = {};
    that.metadata = spec.metadata;
    that.name = spec.name;
    that.label = spec.label || spec.metadata.label || spec.name;
    that.title = spec.title || that.label;

    that.header = spec.header || IPA.entity_header({entity: that});

    that.dialogs = $.ordered_map();

    that.facets = $.ordered_map();
    that.facet_groups = $.ordered_map();

    // current facet
    that.facet_name = null;

    that.redirect_facet = spec.redirect_facet;
    that.containing_entity = null;
    that.get_dialog = function(name) {
        return that.dialogs.get(name);
    };

    that.add_dialog = function(dialog) {
        return that.dialog(dialog);
    };

    that.dialog = function(dialog) {
        dialog.entity_name = that.name;
        that.dialogs.put(dialog.name, dialog);
        return that;
    };

    that.add_facet_group = function(facet_group) {
        that.facet_groups.put(facet_group.name, facet_group);
    };

    that.get_facet_group = function(name) {
        return that.facet_groups.get(name);
    };

    that.remove_facet_groups = function() {
        that.facet_groups.empty();
    };

    that.get_facet = function(name) {
        if (name === 'default') {
            // return the first facet in the first facet group
            var facet_groups = that.facet_groups.values;
            for (var i=0; i<facet_groups.length; i++) {
                var facet_group = facet_groups[i];
                var facets = facet_group.facets.values;
                if (!facets.length) continue;
                return facets[0];
            }

            return that.facets.values[0];
        }

        return that.facets.get(name);
    };

    that.add_facet = function(facet) {
        facet.entity_name = that.name;
        that.facets.put(facet.name, facet);

        if (facet.facet_group) {
            var facet_group = that.get_facet_group(facet.facet_group);
            if (facet_group) {
                facet_group.add_facet(facet);
            }
        }

        return that;
    };

    that.init = function() {

        var facets = that.facets.values;
        for (var i=0; i<facets.length; i++) {
            var facet = facets[i];
            facet.entity = that;
            facet.init();
        }

        var dialogs = that.dialogs.values;
        for (var j=0; j<dialogs.length; j++) {
            dialogs[j].init();
        }
    };

    that.create = function(container) {
        var entity_header = $('<div/>', {
            'class': 'entity-header'
        }).appendTo(container);
        that.header.create(entity_header);

        that.content = $('<div/>', {
            'class': 'entity-content'
        }).appendTo(container);
    };

    that.setup = function(container) {

        var prev_facet = that.facet;

        IPA.current_entity = that;
        var facet_name = IPA.current_facet(that);

        that.facet = that.get_facet(facet_name);
        if (!that.facet) return;

        if (IPA.entity_name == that.name) {
            if (that.facet_name == that.facet.name) {
                if (that.facet.new_key && (!that.facet.new_key())) return;
            } else {
                that.facet_name = that.facet.name;
            }
        } else {
            IPA.entity_name = that.name;
        }

        if (prev_facet) {
            prev_facet.hide();
        }

        var facet_container = $('.facet[name="'+that.facet.name+'"]', that.content);
        if (!facet_container.length) {
            facet_container = $('<div/>', {
                name: that.facet.name,
                'class': 'facet'
            }).appendTo(that.content);

            that.facet.create(facet_container);
            that.facet.setup(facet_container);
        }

        that.facet.show();
        that.header.select_tab();
        that.facet.refresh();
    };

    that.get_primary_key_prefix = function() {
        var pkey = [];
        var current_entity = that;
        current_entity = current_entity.containing_entity;
        while(current_entity !== null){
            pkey.unshift($.bbq.getState(current_entity.name+'-pkey'));
            current_entity = current_entity.containing_entity;
        }
        return pkey;
    };

    /*gets the primary key for trhe current entity out of the URL parameters */
    that.get_primary_key = function() {
        var pkey = that.get_primary_key_prefix();
        var current_entity = that;
        pkey.unshift($.bbq.getState(current_entity.name+'-pkey'));
        return pkey;
    };


    that.entity_init = that.init;

    return that;
};

IPA.current_facet = function(entity) {
    var facet_name = $.bbq.getState(entity.name+'-facet');
    var facets = entity.facets.values;
    if (!facet_name  && facets.length) {
        facet_name = facets[0].name;
    }
    return facet_name;
};

IPA.nested_tab_labels = {};

IPA.get_nested_tab_label = function(entity_name){

    if (!IPA.nested_tab_labels[entity_name]){
        IPA.nested_tab_labels[entity_name] = "LABEL";

    }
    return IPA.nested_tab_labels[entity_name];

};

/*Returns the entity requested, as well as:
  any nested tabs underneath it or
  its parent tab and the others nested at the same level*/

IPA.nested_tabs = function(entity_name) {

    var siblings = [];
    var i;
    var i2;
    var nested_entities;
    var sub_i;
    var sub_tab;

    var key = entity_name;
    function push_sibling(sibling){
        siblings.push (sibling);
        IPA.nested_tab_labels[key] = sub_tab;
    }


    if (!IPA.nav.tabs) {
        siblings.push(entity_name);
        return siblings;
    }

    for (var top_i = 0; top_i < IPA.nav.tabs.length; top_i++) {
        var top_tab = IPA.nav.tabs[top_i];
        for (sub_i = 0; sub_i < top_tab.children.length; sub_i++) {
            sub_tab = top_tab.children[sub_i];
            nested_entities = sub_tab.children;
            if (sub_tab.name === entity_name){
                push_sibling(entity_name);
            }
            if (sub_tab.children){
                for (i = 0; i < nested_entities.length; i += 1){
                    if (sub_tab.name === entity_name){
                        push_sibling(nested_entities[i].name);
                    }else{
                        if (nested_entities[i].name === entity_name){
                            push_sibling(sub_tab.name);
                            for (i2 = 0; i2 < nested_entities.length; i2 += 1){
                                key = nested_entities[i].name;
                                push_sibling(nested_entities[i2].name);
                            }
                        }
                    }
                }
            }
        }
    }

    return siblings;
};

IPA.selected_icon = '<span class="ipa-icon">&#x25B6;</span>';
IPA.back_icon = '<span class="ipa-icon">&#x25C0;</span>';

IPA.entity_header = function(spec) {

    spec = spec || {};

    var that = {};
    that.entity = spec.entity;

    that.select_tab = function() {
        $(that.facet_tabs).find('a').removeClass('selected');
        var facet_name = $.bbq.getState(that.entity.name+'-facet');

        if (!facet_name || facet_name === 'default') {
            that.facet_tabs.find('a:first').addClass('selected');
        } else {
            that.facet_tabs.find('a#' + facet_name ).addClass('selected');
        }
    };

    that.set_pkey = function(value) {

        if (value) {
            var breadcrumb = [];
            var current_entity = IPA.current_entity.containing_entity;

            while(current_entity){
                breadcrumb.unshift($('<a/>',{
                    text:$.bbq.getState(current_entity.name+'-pkey'),
                    title: current_entity.name,
                    click: function() {
                        var entity = IPA.get_entity((this.title));
                        IPA.nav.show_page(entity.name, 'default');
                        $('a', that.facet_tabs).removeClass('selected');
                        return false;
                    }
                }));

                current_entity = current_entity.containing_entity;
            }

            that.title_container.empty();
            var h3 =  $('<h3/>').appendTo(that.title_container);

            h3.empty();
            h3.append(IPA.current_entity.title);
            h3.append(': ');

            for (var i = 0; i < breadcrumb.length; i+=1){
                h3.append(breadcrumb[i]);
                h3.append(' > ');

            }
            h3.append(
                $('<span/>', {
                    'class': 'entity-pkey',
                    text:value
                }));
        } else {
            that.title_container.empty();
            var span =  $('<h3/>',{
                text:IPA.current_entity.metadata.label
            }).appendTo(that.title_container);
        }
    };

    that.create_facet_link = function(container, other_facet) {

        var li = $('<li/>', {
            title: other_facet.name,
            click: function() {
                if (li.hasClass('entity-facet-disabled')) {
                    return false;
                }

                var pkey = $.bbq.getState(that.entity.name+'-pkey');
                IPA.nav.show_page(that.entity.name, other_facet.name, pkey);

                return false;
            }
        }).appendTo(container);

        $('<a/>', {
            text: other_facet.label,
            id: other_facet.name
        }).appendTo(li);
    };

    that.create_facet_group = function(container, facet_group) {

        var section = $('<span/>', {
            'class': 'facet-group'
        }).appendTo(container);

        $('<div/>', {
            'class': 'facet-group-name',
            text: facet_group.label
        }).appendTo(section);

        var ul = $('<ul/>', {
            'class': 'facet-tab'
        }).appendTo(section);

        var facets = facet_group.facets.values;
        for (var i=0; i<facets.length; i++) {
            var facet = facets[i];
            that.create_facet_link(ul, facet);
        }
    };

    that.create = function(container) {

        that.title_container = $('<div/>', {
            'class': 'entity-title'
        }).appendTo(container);

        var title_text = $('<h3/>', {
            text: that.entity.metadata.label
        }).appendTo(that.title_container);

        that.pkey = $('<span/>').appendTo(title_text);

        that.pkey.append(': ');
        that.pkey.append($('<span/>', {
            'class': 'entity-pkey'
        }));

        var search_bar = $('<div/>', {
            'class': 'entity-search'
        }).appendTo(container);

        that.back_link = $('<span/>', {
            'class': 'back-link',
            click: function() {
                if ($(this).hasClass('entity-facet-disabled')) {
                    return false;
                }

                var current_entity = that.entity;
                while(current_entity.containing_entity){
                    current_entity = current_entity.containing_entity;
                }

                IPA.nav.show_page(current_entity.name, 'search');
                $('a', that.facet_tabs).removeClass('selected');
                return false;
            }
        }).appendTo(search_bar);

        that.back_link.append(IPA.back_icon);
        that.back_link.append('  ');
        that.back_link.append(IPA.messages.buttons.back_to_list);

        that.facet_tabs = $('<div/>', {
            'class': 'entity-tabs'
        }).appendTo(container);

        var facet_groups = that.entity.facet_groups.values;
        for (var i=0; i<facet_groups.length; i++) {
            var facet_group = facet_groups[i];
            if (facet_group.facets.length) {
                that.create_facet_group(that.facet_tabs, facet_group);
            }
        }
    };

    return that;
};

IPA.entity_builder = function(){

    var that = {};

    var entity = null;
    var facet_group = null;
    var facet = null;
    var section = null;

    that.entity = function(spec) {
        var factory = IPA.entity;
        if (spec instanceof Object) {
            factory = spec.factory || IPA.entity;
        } else {
            spec = { name: spec };
        }

        spec.metadata = spec.metadata || IPA.metadata.objects[spec.name];
        if (!spec.metadata) {
            throw "Entity not supported by server.";
        }

        entity = factory(spec);

        that.facet_groups([
            'member',
            'memberindirect',
            'settings',
            'memberof',
            'memberofindirect',
            'managedby'
        ]);

        return that;
    };

    that.facet_group = function(spec) {
        if (spec instanceof Object) {
            var factory = spec.factory || IPA.facet_group;
            facet_group = factory(spec);
        } else {
            facet_group = IPA.facet_group({ name: spec });
        }

        if (!facet_group.label) {
            var relationships = IPA.metadata.objects[entity.name].relationships;
            if (relationships) {
                var relationship = relationships[facet_group.name];
                if (relationship) {
                    facet_group.label = relationship[0];
                }
            }
        }

        if (!facet_group.label) {
            facet_group.label = IPA.messages.facet_groups[facet_group.name];
        }

        entity.add_facet_group(facet_group);

        return that;
    };

    that.facet_groups = function(specs) {

        entity.remove_facet_groups();

        for (var i=0; i<specs.length; i++) {
            that.facet_group(specs[i]);
        }

        return that;
    };

    that.facet = function(spec) {
        spec.entity_name  = entity.name;
        facet = spec.factory(spec);
        entity.add_facet(facet);
        return that;
    };

    that.search_facet = function(spec) {

        spec.entity_name = entity.name;
        spec.title = spec.title || spec.label;
        spec.label = spec.label || IPA.messages.facets.search;

        var factory = spec.factory || IPA.search_facet;
        facet = factory(spec);
        entity.add_facet(facet);
        add_redirect_info();

        return that;
    };

    that.nested_search_facet = function(spec) {

        spec.entity_name = entity.name;
        spec.label = spec.label || IPA.messages.facets.search;

        var factory = spec.factory || IPA.nested_search_facet;
        facet = factory(spec);
        entity.add_facet(facet);

        return that;
    };

    that.details_facet = function(spec) {

        var sections = spec.sections;
        spec.sections = null;
        spec.entity_name = entity.name;
        spec.title = spec.title || spec.label || IPA.messages.details.settings;
        spec.label = spec.label || IPA.messages.facets.details;

        var factory = spec.factory || IPA.details_facet;
        facet = factory(spec);
        entity.add_facet(facet);

        if (sections) {
            for (var i=0; i<sections.length; i++) {
                that.section(sections[i]);
            }
        }

        return that;
    };

    that.association_facet = function(spec) {

        spec.entity_name = entity.name;

        var index = spec.name.indexOf('_');
        spec.attribute_member = spec.attribute_member ||
            spec.name.substring(0, index);
        spec.other_entity = spec.other_entity ||
            spec.name.substring(index+1);

        spec.facet_group = spec.facet_group ||
            spec.attribute_member;

        if (spec.facet_group == 'memberindirect' ||
            spec.facet_group == 'memberofindirect') {

            var length = spec.attribute_member.length;
            var direct_attribute_member = spec.attribute_member.substring(0, length-8);
            var direct_facet_name = direct_attribute_member+'_'+spec.other_entity;

            facet = entity.get_facet(direct_facet_name);

            if (facet) { // merge into previously created direct facet
                facet.indirect_attribute_member = spec.attribute_member;
                return that;

            } else {
                spec.read_only = true;
            }
        }

        spec.label = spec.label ||
            (IPA.metadata.objects[spec.other_entity] ?
             IPA.metadata.objects[spec.other_entity].label : spec.other_entity);

        if (!spec.title) {
            if (spec.facet_group == 'member' ||
                spec.facet_group == 'memberindirect') {
                spec.title = IPA.messages.association.member;
            } else if (spec.facet_group == 'memberof' ||
                       spec.facet_group == 'memberofindirect') {
                spec.title = IPA.messages.association.memberof;
            }
        }

        var factory = spec.factory || IPA.association_facet;
        facet = factory(spec);
        entity.add_facet(facet);

        return that;
    };

    that.standard_association_facets = function(spec) {

        spec = spec || {};

        var direct_associations = [];
        var indirect_associations = [];

        for (var association in entity.metadata.attribute_members) {
            if (association == 'memberindirect' ||
                association == 'memberofindirect') {
                indirect_associations.push(association);
            } else {
                direct_associations.push(association);
            }
        }

        // make sure direct facets are created first
        var attribute_members = direct_associations.concat(indirect_associations);

        for (var i=0; i<attribute_members.length; i++) {
            var attribute_member = attribute_members[i];
            var other_entities = entity.metadata.attribute_members[attribute_member];

            for (var j=0; j<other_entities.length; j++) {

                var other_entity = other_entities[j];
                var association_name = attribute_member+'_'+other_entity;

                var facet = entity.get_facet(association_name);
                if (facet) continue;

                var tmp_spec = $.extend({}, spec);
                tmp_spec.name = association_name;

                that.association_facet(tmp_spec);
            }
        }

        return that;
    };

    that.section = function(spec) {
        spec.entity_name = entity.name;

        if (!spec.label) {
            var obj_messages = IPA.messages.objects[entity.name];
            spec.label = obj_messages[spec.name];
        }

        if (spec.factory) {
            section = spec.factory(spec);
        } else {
            section = IPA.details_list_section(spec);
        }
        facet.add_section(section);
        var fields = spec.fields;
        if (fields) {
            for (var i=0; i<fields.length; i++) {
                var field_spec = fields[i];
                var field;

                if (field_spec instanceof Object) {
                    field_spec.entity_name = entity.name;
                    var factory = field_spec.factory || IPA.text_widget;
                    field = factory(field_spec);
                } else {
                    field = IPA.text_widget({
                        name: field_spec,
                        entity_name: entity.name
                    });
                }
                section.add_field(field);
            }
        }
    };

    function add_redirect_info(facet_name){
        if (!entity.redirect_facet){
            entity.redirect_facet = 'search';
        }
    }

    that.containing_entity = function(entity_name) {
        add_redirect_info();
        entity.containing_entity = IPA.get_entity(entity_name);
        return that;
    };

    that.dialog = function(spec) {
        var dialog;
        if (spec instanceof Object) {
            var factory = spec.factory || IPA.dialog;
            dialog = factory(spec);
        } else {
            dialog = IPA.dialog({ name: spec });
        }
        entity.dialog(dialog);
        return that;
    };

    that.adder_dialog = function(spec) {
        spec.factory = spec.factory || IPA.add_dialog;
        spec.name = spec.name || 'add';

        if (!spec.title) {
            var messages = IPA.messages.objects[entity.name];
            if (messages) {
                spec.title = messages.add;
            }
        }

        return that.dialog(spec);
    };

    that.build = function(){
        var item = entity;
        entity = null;
        return item;
    };

    return that;
};