/
miq_ae_class_controller.rb
2585 lines (2398 loc) · 95.9 KB
/
miq_ae_class_controller.rb
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
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
require "rexml/document"
class MiqAeClassController < ApplicationController
include MiqAeClassHelper
include AutomateTreeHelper
before_action :check_privileges
before_action :get_session_data
after_action :cleanup_action
after_action :set_session_data
# GET /automation_classes
# GET /automation_classes.xml
def index
redirect_to :action => 'explorer'
end
def change_tab
# resetting flash array so messages don't get displayed when tab is changed
@flash_array = []
@explorer = true
@record = @ae_class = MiqAeClass.find_by_id(from_cid(x_node.split('-').last))
@sb[:active_tab] = params[:tab_id]
c_tb = build_toolbar(center_toolbar_filename)
case params[:tab_id]
when "instances"
div_suffix = "_class_instances"
when "methods"
div_suffix = "_class_methods"
when "props"
div_suffix = "_class_props"
when "schema"
div_suffix = "_class_fields"
end
render :update do |page| # Use JS to update the display
page.replace("flash_msg_div#{div_suffix}", :partial => "layouts/flash_msg", :locals => {:div_num => div_suffix})
page << javascript_pf_toolbar_reload('center_tb', c_tb)
page << "miqSparkle(false);"
end
end
AE_X_BUTTON_ALLOWED_ACTIONS = {
'instance_fields_edit' => :edit_instance,
'method_inputs_edit' => :edit_mehod,
'miq_ae_class_copy' => :copy_objects,
'miq_ae_class_edit' => :edit_class,
'miq_ae_class_delete' => :deleteclasses,
'miq_ae_class_new' => :new,
'miq_ae_domain_delete' => :delete_domain,
'miq_ae_domain_edit' => :edit_domain,
'miq_ae_domain_lock' => :domain_lock,
'miq_ae_domain_unlock' => :domain_unlock,
'miq_ae_domain_new' => :new_domain,
'miq_ae_domain_priority_edit' => :domains_priority_edit,
'miq_ae_field_edit' => :edit_fields,
'miq_ae_field_seq' => :fields_seq_edit,
'miq_ae_instance_copy' => :copy_objects,
'miq_ae_instance_delete' => :deleteinstances,
'miq_ae_instance_edit' => :edit_instance,
'miq_ae_instance_new' => :new_instance,
'miq_ae_item_edit' => :edit_item,
'miq_ae_method_copy' => :copy_objects,
'miq_ae_method_delete' => :deletemethods,
'miq_ae_method_edit' => :edit_method,
'miq_ae_method_new' => :new_method,
'miq_ae_namespace_delete' => :delete_ns,
'miq_ae_namespace_edit' => :edit_ns,
'miq_ae_namespace_new' => :new_ns,
}.freeze
def x_button
@sb[:action] = action = params[:pressed]
raise ActionController::RoutingError, _("Invalid button action.") unless
AE_X_BUTTON_ALLOWED_ACTIONS.key?(action)
send(AE_X_BUTTON_ALLOWED_ACTIONS[action])
end
def explorer
@trees = []
@sb[:action] = nil
@sb[:open_tree_nodes] ||= [] # Create array to keep open tree nodes (only for autoload trees)
@explorer = true
# don't need right bottom cell
@collapse_c_cell = true
@breadcrumbs = []
bc_name = "Explorer"
bc_name += " (filtered)" if @filters && (!@filters[:tags].blank? || !@filters[:cats].blank?)
drop_breadcrumb(:name => bc_name, :url => "/miq_ae_class/explorer")
@lastaction = "replace_right_cell"
@accords = []
self.x_active_tree = :ae_tree
tree = build_ae_tree
@trees << tree
@accords << {:name => "datastores", :title => "Datastore", :container => "ae_accord", :image => "folder"}
@sb[:active_accord] = :ae
@sb[:active_tab] ||= "namespaces"
self.x_node ||= "root"
@right_cell_text ||= "Datastore"
get_node_info(x_node)
render :layout => "application"
end
def set_right_cell_text(id, rec = nil)
nodes = id.split('-')
case nodes[0]
when "root"
txt = "Datastore"
@sb[:namespace_path] = ""
when "aec"
txt = ui_lookup(:model => "MiqAeClass")
@sb[:namespace_path] = rec.fqname
when "aei"
txt = ui_lookup(:model => "MiqAeInstance")
updated_by = rec.updated_by ? " by #{rec.updated_by}" : ""
@sb[:namespace_path] = rec.fqname
@right_cell_text = txt +
" [" + get_rec_name(rec) + " - Updated " + format_timezone(rec.created_on, Time.zone, "gtl") +
updated_by + "]"
when "aem"
txt = ui_lookup(:model => "MiqAeMethod")
updated_by = rec.updated_by ? " by #{rec.updated_by}" : ""
@sb[:namespace_path] = rec.fqname
@right_cell_text = txt + " [" + get_rec_name(rec) +
" - Updated " + format_timezone(rec.created_on, Time.zone, "gtl") +
updated_by + "]"
when "aen"
txt = ui_lookup(:model => rec.domain? ? "MiqAeDomain" : "MiqAeNamespace")
@sb[:namespace_path] = rec.fqname
end
@sb[:namespace_path].gsub!(/\//, " / ") if @sb[:namespace_path]
@right_cell_text = "#{txt} \
#{_("\"%s\"") % get_rec_name(rec)}" unless %w(root aei aem).include?(nodes[0])
end
def expand_toggle
render :update do |page| # Use RJS to update the display
if @sb[:squash_state]
@sb[:squash_state] = false
page << javascript_show("inputs_div")
page << "$('#exp_collapse_img').prop('src' ,'/images/toolbars/squashed-false.png');"
page << "$('#exp_collapse_img').prop('title', 'Hide Input Parameters');"
page << "$('#exp_collapse_img').prop('alt', 'Hide Input Parameters');"
else
@sb[:squash_state] = true
page << javascript_hide("inputs_div")
page << "$('#exp_collapse_img').prop('src', '/images/toolbars/squashed-true.png');"
page << "$('#exp_collapse_img').prop('title', 'Show Input Parameters');"
page << "$('#exp_collapse_img').prop('alt', 'Show Input Parameters');"
end
end
end
# reset node to root node when previously viewed item no longer exists
def set_root_node
self.x_node = "root"
get_node_info(x_node)
end
def get_node_info(node)
id = valid_active_node(node).split('-')
@sb[:row_selected] = nil if params[:action] == "tree_select"
case id[0]
when "aec"
get_class_node_info(id)
when "aei"
get_instance_node_info(id)
when "aem"
get_method_node_info(id)
when "aen"
@record = MiqAeNamespace.find_by_id(from_cid(id[1]))
# need to set record as Domain record if it's a domain, editable_domains, enabled_domains,
# visible domains methods returns list of Domains, need this for toolbars to hide/disable correct records.
@record = MiqAeDomain.find_by_id(from_cid(id[1])) if @record.domain?
@version_message = domain_version_message(@record) if @record.domain?
if @record.nil?
set_root_node
else
@records = []
# Add Namespaces under a namespace
details = @record.ae_namespaces
@records += details.flatten.sort_by { |d| [d.display_name.to_s, d.name.to_s] }
# Add classes under a namespace
details_cls = @record.ae_classes
unless details_cls.nil?
@records += details_cls.flatten.sort_by { |d| [d.display_name.to_s, d.name.to_s] }
end
@combo_xml = build_type_options
@dtype_combo_xml = build_dtype_options
@sb[:active_tab] = "details"
set_right_cell_text(x_node, @record)
end
else
@grid_data = User.current_tenant.visible_domains
add_all_domains_version_message(@grid_data)
@record = nil
@right_cell_text = "Datastore"
@sb[:active_tab] = "namespaces"
set_right_cell_text(x_node)
end
x_history_add_item(:id => x_node, :text => @right_cell_text)
end
def domain_version_message(domain)
version = domain.version
available_version = domain.available_version
return if version.nil? || available_version.nil?
_("%s domain: Current version - %s, Available version - %s") % [domain.name, version, available_version] if version != available_version
end
def add_all_domains_version_message(domains)
@version_messages = domains.collect { |dom| domain_version_message(dom) }.compact
end
# Tree node selected in explorer
def tree_select
@explorer = true
@lastaction = "explorer"
self.x_active_tree = params[:tree] if params[:tree]
self.x_node = params[:id]
@sb[:action] = nil
replace_right_cell
end
# Check for parent nodes missing from ae tree and return them if any
def open_parent_nodes(record)
nodes = record.fqname.split("/")
parents = []
nodes.each_with_index do |_, i|
if i == nodes.length - 1
selected_node = x_node.split("-")
parents.push(record.ae_class) if %w(aei aem).include?(selected_node[0])
self.x_node = "#{selected_node[0]}-#{to_cid(record.id)}"
parents.push(record)
else
ns = MiqAeNamespace.find_by_fqname(nodes[0..i].join("/"))
parents.push(ns) if ns
end
end
build_and_add_nodes(parents)
end
def build_and_add_nodes(parents)
existing_node = find_existing_node(parents)
return nil if existing_node.nil?
children = TreeBuilder.tree_add_child_nodes(@sb, x_tree[:klass_name], existing_node)
# set x_node after building tree nodes so parent node of new nodes can be selected in the tree.
unless params[:action] == "x_show"
if @record.kind_of?(MiqAeClass)
self.x_node = "aen-#{to_cid(@record.namespace_id)}"
else
self.x_node = "aec-#{to_cid(@record.class_id)}"
end
end
{:key => existing_node, :children => children}
end
def find_existing_node(parents)
existing_node = nil
# Go up thru the parents and find the highest level unopened, mark all as opened along the way
unless parents.empty? || # Skip if no parents or parent already open
x_tree[:open_nodes].include?(x_build_node_id(parents.last))
parents.reverse_each do |p|
p_node = x_build_node_id(p)
if x_tree[:open_nodes].include?(p_node)
return p_node
else
x_tree[:open_nodes].push(p_node)
existing_node = p_node
end
end
end
existing_node
end
def replace_right_cell(replace_trees = [])
@explorer = true
# FIXME: is the following line needed?
# replace_trees = @replace_trees if @replace_trees #get_node_info might set this
nodes = x_node.split('-')
@in_a_form = @in_a_form_fields = @in_a_form_props = false if params[:button] == "cancel" ||
(["save", "add"].include?(params[:button]) && replace_trees)
add_nodes = open_parent_nodes(@record) if params[:button] == "copy" ||
params[:action] == "x_show"
get_node_info(x_node) if !@in_a_form && @button != "reset"
ae_tree = build_ae_tree if replace_trees
c_tb = build_toolbar(center_toolbar_filename) unless @in_a_form
h_tb = build_toolbar("x_history_tb")
presenter = ExplorerPresenter.new(
:active_tree => x_active_tree,
)
if add_nodes
# remove any existing nodes before adding child nodes to avoid duplication
presenter[:remove_nodes] = true
presenter[:add_nodes] = add_nodes
end
r = proc { |opts| render_to_string(opts) }
presenter[:right_cell_text] = @right_cell_text
# Build hash of trees to replace and optional new node to be selected
replace_trees.each do |t|
presenter[:replace_partials]["#{t}_tree_div".to_sym] = r[
:partial => 'shared/tree',
:locals => {:tree => ae_tree,
:name => ae_tree.name.to_s
}
]
end
if @sb[:action] == "miq_ae_field_seq"
if @flash_array
replace_partial_div = :flash_msg_div_fields_seq
replace_partial_div_num = "_fields_seq"
end
update_partial_div = :class_fields_div
update_partial = "fields_seq_form"
elsif @sb[:action] == "miq_ae_domain_priority_edit"
if @flash_array
replace_partial_div = :flash_msg_div_domains_priority
replace_partial_div_num = "_domains_priority"
end
update_partial_div = :ns_list_div
update_partial = "domains_priority_form"
elsif MIQ_AE_COPY_ACTIONS.include?(@sb[:action])
if @flash_array
replace_partial_div = :flash_msg_div_copy
replace_partial_div_num = "_copy"
end
update_partial_div = :main_div
update_partial = "copy_objects_form"
else
if @sb[:action] == "miq_ae_class_edit"
@sb[:active_tab] = 'props'
else
@sb[:active_tab] ||= 'instances'
end
update_partial_div = :main_div
update_partial = "all_tabs"
end
presenter[:replace_partials][replace_partial_div] = r[
:partial => "layouts/flash_msg",
:locals => {:div_num => replace_partial_div_num}
] if replace_partial_div
presenter[:update_partials][update_partial_div] = r[:partial => update_partial] if update_partial
if @in_a_form
action_url = create_action_url(nodes.first)
# incase it was hidden for summary screen, and incase there were no records on show_list
presenter[:set_visible_elements][:paging_div] = true
presenter[:set_visible_elements][:form_buttons_div] = true
presenter[:update_partials][:form_buttons_div] = r[
:partial => "layouts/x_edit_buttons",
:locals => {
:record_id => @edit[:rec_id],
:action_url => action_url,
:copy_button => action_url == "copy_objects",
:multi_record => @sb[:action] == "miq_ae_domain_priority_edit",
:serialize => @sb[:active_tab] == 'methods',
}
]
else
# incase it was hidden for summary screen, and incase there were no records on show_list
presenter[:set_visible_elements][:paging_div] = false
presenter[:set_visible_elements][:form_buttons_div] = false
end
presenter[:lock_unlock_trees][x_active_tree] = @in_a_form && @edit
if @record.kind_of?(MiqAeMethod) && !@in_a_form
presenter[:set_visible_elements][:params_div] = !@record.inputs.blank?
end
presenter[:clear_gtl_list_grid] = @gtl_type && @gtl_type != 'list'
# Rebuild the toolbars
presenter[:reload_toolbars][:history] = h_tb
if c_tb.present?
presenter[:set_visible_elements][:toolbar] = true
presenter[:reload_toolbars][:center] = c_tb
else
presenter[:set_visible_elements][:toolbar] = false
end
if @record && !@in_a_form
presenter[:record_id] = @record.id
else
presenter[:record_id] = @edit && @edit[:rec_id] && @in_a_form ? @edit[:rec_id] : nil
end
presenter[:osf_node] = x_node
presenter[:extra_js] << "miqButtons('#{@changed ? 'show' : 'hide'}');"
# Render the JS responses to update the explorer screen
render :js => presenter.to_html
end
def build_type_options
MiqAeField.available_aetypes.collect { |t| [t.titleize, t, {"data-icon" => "product product-#{t}"}] }
end
def build_dtype_options
MiqAeField.available_datatypes_for_ui.collect { |t| [t.titleize, t, {"data-icon" => "product product-#{t}"}] }
end
def set_cls(cls)
case cls.to_s.split("::").last
when "MiqAeClass"
cls = "aec"
glyphicon = "product product-ae_class"
when "MiqAeNamespace"
cls = "aen"
glyphicon = "product product-ae_namespace"
when "MiqAeInstance"
cls = "aei"
glyphicon = "product product-ae_instance"
when "MiqAeField"
cls = "Field"
glyphicon = "product product-ae_field"
when "MiqAeMethod"
cls = "aem"
glyphicon = "product product-ae_method"
end
return cls, glyphicon
end
def build_details_grid(view, mode = true)
xml = REXML::Document.load("")
xml << REXML::XMLDecl.new(1.0, "UTF-8")
# Create root element
root = xml.add_element("rows")
# Build the header row
head = root.add_element("head")
header = ""
new_column = head.add_element("column", "type" => "ch", "width" => 25, "align" => "center") # Checkbox column
new_column = head.add_element("column", "width" => "30", "align" => "left", "sort" => "na")
new_column.add_attribute("type", 'ro')
new_column.text = header
new_column = head.add_element("column", "width" => "*", "align" => "left", "sort" => "na")
new_column.add_attribute("type", 'ro')
new_column.text = header
# passing in mode, don't need to sort records for namaspace node, it will be passed in sorted order, need to show Namesaces first and then Classes
records =
if mode
view.flatten.sort_by { |v| [v.display_name.to_s, v.name.to_s] }
else
view
end
records.each do |kids|
cls, glyphicon = set_cls(kids.class)
rec_name = get_rec_name(kids)
if rec_name
rec_name = rec_name.gsub(/\n/, "\\n")
rec_name = rec_name.gsub(/\t/, "\\t")
rec_name = rec_name.tr('"', "'")
rec_name = CGI.escapeHTML(rec_name)
rec_name = rec_name.gsub(/\\/, "\")
end
srow = root.add_element("row", "id" => "#{cls}-#{to_cid(kids.id)}", "style" => "border-bottom: 1px solid #CCCCCC;color:black; text-align: center")
srow.add_element("cell").text = "0" # Checkbox column unchecked
srow.add_element("cell", "image" => "blank.png", "title" => "#{cls}", "style" => "border-bottom: 1px solid #CCCCCC;text-align: left;height:28px;").text = REXML::CData.new("<ul class='icons list-unstyled'><li><span class='#{glyphicon}' alt='#{cls}' title='#{cls}'></span></li></ul>")
srow.add_element("cell", "image" => "blank.png", "title" => "#{rec_name}", "style" => "border-bottom: 1px solid #CCCCCC;text-align: left;height:28px;").text = rec_name
end
xml.to_s
end
def edit_item
item = find_checked_items
@sb[:row_selected] = item[0]
if @sb[:row_selected].split('-')[0] == "aec"
edit_class
else
edit_ns
end
end
def edit_class
assert_privileges("miq_ae_class_edit")
if params[:pressed] == "miq_ae_item_edit" # came from Namespace details screen
id = @sb[:row_selected].split('-')
@ae_class = MiqAeClass.find(from_cid(id[1]))
else
@ae_class = MiqAeClass.find(params[:id].to_s)
end
set_form_vars
# have to get name and set node info, to load multiple tabs correctly
# rec_name = get_rec_name(@ae_class)
# get_node_info("aec-#{to_cid(@ae_class.id)}")
@in_a_form = true
@in_a_form_props = true
session[:changed] = @changed = false
replace_right_cell
end
def edit_fields
assert_privileges("miq_ae_field_edit")
if params[:pressed] == "miq_ae_item_edit" # came from Namespace details screen
id = @sb[:row_selected].split('-')
@ae_class = MiqAeClass.find(from_cid(id[1]))
else
@ae_class = MiqAeClass.find(params[:id].to_s)
end
fields_set_form_vars
@in_a_form = true
@in_a_form_fields = true
session[:changed] = @changed = false
replace_right_cell
end
def edit_domain
assert_privileges("miq_ae_domain_edit")
edit_domain_or_namespace
end
def edit_ns
assert_privileges("miq_ae_namespace_edit")
edit_domain_or_namespace
end
def edit_instance
assert_privileges("miq_ae_instance_edit")
obj = find_checked_items
if !obj.blank?
@sb[:row_selected] = obj[0]
id = @sb[:row_selected].split('-')
else
id = x_node.split('-')
end
initial_setup_for_instances_form_vars(from_cid(id[1]))
set_instances_form_vars
@in_a_form = true
session[:changed] = @changed = false
replace_right_cell
end
def edit_method
assert_privileges("miq_ae_method_edit")
obj = find_checked_items
if !obj.blank?
@sb[:row_selected] = obj[0]
id = @sb[:row_selected].split('-')
else
id = x_node.split('-')
end
@ae_method = MiqAeMethod.find(from_cid(id[1]))
set_method_form_vars
@in_a_form = true
session[:changed] = @changed = false
replace_right_cell
end
# Set form variables for edit
def set_instances_form_vars
session[:inst_data] = {}
@edit = {
:ae_inst_id => @ae_inst.id,
:ae_class_id => @ae_class.id,
:rec_id => @ae_inst.id || nil,
:key => "aeinst_edit__#{@ae_inst.id || "new"}",
:new => {}
}
@edit[:new][:ae_inst] = {}
instance_column_names.each do |fld|
@edit[:new][:ae_inst][fld] = @ae_inst.send(fld)
end
@edit[:new][:ae_values] = @ae_values.collect do |ae_value|
value_column_names.each_with_object({}) do |fld, hash|
hash[fld] = ae_value.send(fld)
end
end
@edit[:new][:ae_fields] = @ae_class.ae_fields.collect do |ae_field|
field_column_names.each_with_object({}) do |fld, hash|
hash[fld] = ae_field.send(fld)
end
end
@edit[:current] = copy_hash(@edit[:new])
@right_cell_text = @edit[:rec_id].nil? ?
_("Adding a new %s") % ui_lookup(:model => "MiqAeInstance") :
_("Editing %{model} \"%{name}\"") % {:model => ui_lookup(:model => "MiqAeInstance"), :name => @ae_inst.name}
session[:edit] = @edit
end
# AJAX driven routine to check for changes in ANY field on the form
def form_instance_field_changed
return unless load_edit("aeinst_edit__#{params[:id]}", "replace_cell__explorer")
get_instances_form_vars
render :update do |page| # Use JS to update the display
@changed = (@edit[:current] != @edit[:new])
page << javascript_for_miq_button_visibility(@changed)
end
end
def update_instance
assert_privileges("miq_ae_instance_edit")
return unless load_edit("aeinst_edit__#{params[:id]}", "replace_cell__explorer")
get_instances_form_vars
@changed = (@edit[:new] != @edit[:current])
case params[:button]
when "cancel"
session[:edit] = nil # clean out the saved info
add_flash(_("Edit of %{model} \"%{name}\" was cancelled by the user") % {:model => ui_lookup(:model => "MiqAeInstance"), :name => @ae_inst.name})
@in_a_form = false
replace_right_cell
when "save"
if @edit[:new][:ae_inst]["name"].blank?
add_flash(_("%s is required") % "Name", :error)
end
if @flash_array
render :update do |page|
if @sb[:row_selected]
page.replace("flash_msg_div_class_instances", :partial => "layouts/flash_msg", :locals => {:div_num => "_class_instances"})
else
page.replace("flash_msg_div_instance_fields", :partial => "layouts/flash_msg", :locals => {:div_num => "_instance_fields"})
end
end
return
end
set_instances_record_vars(@ae_inst) # Set the instance record variables, but don't save
# Update the @ae_inst.ae_values directly because of update bug in RAILS
# When saving a parent, the childrens updates are not getting saved
set_instances_value_vars(@ae_values, @ae_inst) # Set the instance record variables, but don't save
begin
MiqAeInstance.transaction do
@ae_inst.ae_values.each { |v| v.value = nil if v.value == "" }
@ae_inst.save!
end # end of transaction
rescue StandardError => bang
add_flash(_("Error during '%s': ") % "save" << bang.message, :error)
@in_a_form = true
flash_validation_errors(@ae_inst)
render :update do |page|
if @sb[:row_selected]
page.replace("flash_msg_div_class_instances", :partial => "layouts/flash_msg", :locals => {:div_num => "_class_instances"})
else
page.replace("flash_msg_div_instance_fields", :partial => "layouts/flash_msg", :locals => {:div_num => "_instance_fields"})
end
end
else
AuditEvent.success(build_saved_audit(@ae_class, @edit))
session[:edit] = nil # clean out the saved info
@in_a_form = false
add_flash(_("%{model} \"%{name}\" was saved") % {:model => ui_lookup(:model => "MiqAeInstance"), :name => @ae_inst.name})
replace_right_cell([:ae])
return
end
when "reset"
set_instances_form_vars
add_flash(_("All changes have been reset"), :warning)
@in_a_form = true
@button = "reset"
replace_right_cell
end
end
def create_instance
assert_privileges("miq_ae_instance_new")
case params[:button]
when "cancel"
session[:edit] = nil # clean out the saved info
add_flash(_("Add of new %s was cancelled by the user") % ui_lookup(:model => "MiqAeInstance"))
@in_a_form = false
replace_right_cell
when "add"
return unless load_edit("aeinst_edit__new", "replace_cell__explorer")
get_instances_form_vars
if @edit[:new][:ae_inst]["name"].blank?
add_flash(_("%s is required") % "Name", :error)
end
if @flash_array
render :update do |page|
page.replace("flash_msg_div_class_instances", :partial => "layouts/flash_msg", :locals => {:div_num => "_class_instances"})
end
return
end
add_aeinst = MiqAeInstance.new
set_instances_record_vars(add_aeinst) # Set the instance record variables, but don't save
set_instances_value_vars(@ae_values) # Set the instance value record variables, but don't save
begin
MiqAeInstance.transaction do
add_aeinst.ae_values = @ae_values
add_aeinst.ae_values.each { |v| v.value = nil if v.value == "" }
add_aeinst.save!
end # end of transaction
rescue StandardError => bang
add_flash(_("Error during '%s': ") % "add" << bang.message, :error)
@in_a_form = true
flash_validation_errors(add_aeinst)
render :update do |page|
page.replace("flash_msg_div_class_instances", :partial => "layouts/flash_msg", :locals => {:div_num => "_class_instances"})
end
else
AuditEvent.success(build_created_audit(add_aeinst, @edit))
add_flash(_("%{model} \"%{name}\" was added") % {:model => ui_lookup(:model => "MiqAeInstance"), :name => add_aeinst.name})
@in_a_form = false
replace_right_cell([:ae])
return
end
end
end
# Set form variables for edit
def set_form_vars
@in_a_form_props = true
session[:field_data] = {}
@edit = {}
session[:edit] = {}
@edit[:ae_class_id] = @ae_class.id
@edit[:new] = {}
@edit[:current] = {}
@edit[:new_field] = {}
@edit[:rec_id] = @ae_class.id || nil
@edit[:key] = "aeclass_edit__#{@ae_class.id || "new"}"
@edit[:new][:name] = @ae_class.name
@edit[:new][:display_name] = @ae_class.display_name
@edit[:new][:description] = @ae_class.description
@edit[:new][:namespace] = @ae_class.namespace
@edit[:new][:inherits] = @ae_class.inherits
@edit[:inherits_from] = MiqAeClass.all.collect { |c| [c.fqname, c.fqname] }
@edit[:current] = @edit[:new].dup
@right_cell_text = @edit[:rec_id].nil? ?
_("Adding a new %s") % ui_lookup(:model => "Class") :
_("Editing %{model} \"%{name}\"") % {:model => ui_lookup(:model => "Class"), :name => @ae_class.name}
session[:edit] = @edit
@in_a_form = true
end
# Set form variables for edit
def fields_set_form_vars
@in_a_form_fields = true
session[:field_data] = {}
@edit = {
:ae_class_id => @ae_class.id,
:rec_id => @ae_class.id,
:new_field => {},
:key => "aefields_edit__#{@ae_class.id || "new"}",
:fields_to_delete => []
}
@edit[:new] = {
:datatypes => build_dtype_options, # setting dtype combo for adding a new field
:aetypes => build_type_options # setting aetype combo for adding a new field
}
@edit[:new][:fields] = @ae_class.ae_fields.sort_by { |a| [a.priority.to_i] }.collect do |fld|
field_attributes.each_with_object({}) do |column, hash|
hash[column] = fld.send(column)
end
end
# combo to show existing fields
@combo_xml = build_type_options
# passing in fields because that's how many combo boxes we need
@dtype_combo_xml = build_dtype_options
@edit[:current] = copy_hash(@edit[:new])
@right_cell_text = @edit[:rec_id].nil? ?
_("Adding a new %s") % ui_lookup(:model => "Class Schema") :
_("Editing %{model} \"%{name}\"") % {:model => ui_lookup(:model => "Class Schema"), :name => @ae_class.name}
session[:edit] = @edit
end
# Set form variables for edit
def set_method_form_vars
session[:field_data] = {}
@ae_class = ae_class_for_instance_or_method(@ae_method)
@edit = {}
session[:edit] = {}
@edit[:ae_method_id] = @ae_method.id
@edit[:fields_to_delete] = []
@edit[:new] = {}
@edit[:new_field] = {}
@edit[:ae_class_id] = @ae_class.id
@edit[:rec_id] = @ae_method.id || nil
@edit[:key] = "aemethod_edit__#{@ae_method.id || "new"}"
@sb[:form_vars_set] = true
@sb[:squash_state] ||= true
@edit[:new][:name] = @ae_method.name
@edit[:new][:display_name] = @ae_method.display_name
@edit[:new][:scope] = "instance"
@edit[:new][:language] = "ruby"
@edit[:new][:available_locations] = MiqAeMethod.available_locations
@edit[:new][:location] = @ae_method.location.nil? ? "inline" : @ae_method.location
@edit[:new][:data] = @ae_method.data.to_s
if @edit[:new][:location] == "inline" && !@ae_method.data
@edit[:new][:data] = MiqAeMethod.default_method_text
end
@edit[:default_verify_status] = @edit[:new][:location] == "inline" && @edit[:new][:data] && @edit[:new][:data] != ""
@edit[:new][:fields] = @ae_method.inputs.collect do |input|
method_input_column_names.each_with_object({}) do |column, hash|
hash[column] = input.send(column)
end
end
@edit[:new][:available_datatypes] = MiqAeField.available_datatypes_for_ui
@edit[:current] = copy_hash(@edit[:new])
@right_cell_text = @edit[:rec_id].nil? ?
_("Adding a new %s") % ui_lookup(:model => "MiqAeMethod") :
_("Editing %{model} \"%{name}\"") % {:model => ui_lookup(:model => "MiqAeMethod"), :name => @ae_method.name}
session[:log_depot_default_verify_status] = false
session[:edit] = @edit
session[:changed] = @changed = false
end
def ae_class_for_instance_or_method(record)
record.id ? record.ae_class : MiqAeClass.find_by_id(from_cid(x_node.split("-").last))
end
def validate_method_data
return unless load_edit("aemethod_edit__#{params[:id]}", "replace_cell__explorer")
@edit[:new][:data] = params[:cls_method_data] if params[:cls_method_data]
@edit[:new][:data] = params[:method_data] if params[:method_data]
res = MiqAeMethod.validate_syntax(@edit[:new][:data])
line = 0
if !res
add_flash(_("Data validated successfully"))
else
res.each do |err|
line = err[0] if line == 0
add_flash(_("Error on line %{line_num}: %{err_txt}") % {:line_num => err[0], :err_txt => err[1]}, :error)
end
end
render :update do |page|
page << "if (miqDomElementExists('cls_method_data')){"
page.replace("flash_msg_div_class_methods", :partial => "layouts/flash_msg", :locals => {:div_num => "_class_methods"})
page << "var ta = document.getElementById('cls_method_data');"
page << "} else {"
page.replace("flash_msg_div_method_inputs", :partial => "layouts/flash_msg", :locals => {:div_num => "_method_inputs"})
page << "var ta = document.getElementById('method_data');"
page << "}"
page << "var lineHeight = ta.clientHeight / ta.rows;"
page << "ta.scrollTop = (#{line.to_i}-1) * lineHeight;"
if line > 0
if @sb[:row_selected]
page << "$('#cls_method_data_lines').scrollTop(ta.scrollTop);"
page << "$('#cls_method_data').scrollTop(ta.scrollTop);"
else
page << "$('#method_data_lines').scrollTop(ta.scrollTop);"
page << "$('#method_data').scrollTop(ta.scrollTop);"
end
end
end
end
# AJAX driven routine to check for changes in ANY field on the form
def form_field_changed
return unless load_edit("aeclass_edit__#{params[:id]}", "replace_cell__explorer")
get_form_vars
@changed = (@edit[:new] != @edit[:current])
render :update do |page| # Use JS to update the display
page.replace_html(@refresh_div, :partial => @refresh_partial) if @refresh_div
page << javascript_for_miq_button_visibility(@changed)
end
end
# AJAX driven routine to check for changes in ANY field on the form
def fields_form_field_changed
return unless load_edit("aefields_edit__#{params[:id]}", "replace_cell__explorer")
fields_get_form_vars
@changed = (@edit[:new] != @edit[:current])
render :update do |page| # Use JS to update the display
page.replace_html(@refresh_div, :partial => @refresh_partial) if @refresh_div
unless ["up", "down"].include?(params[:button])
if params[:field_datatype] == "password"
page << javascript_hide("field_default_value")
page << javascript_show("field_password_value")
page << "$('#field_password_value').val('');"
elsif params[:field_datatype]
page << javascript_hide("field_password_value")
page << javascript_show("field_default_value")
page << "$('#field_default_value').val('');"
end
params.keys.each do |field|
if field.to_s.starts_with?("fields_datatype")
f = field.split('fields_datatype')
def_field = "fields_default_value_" << f[1].to_s
pwd_field = "fields_password_value_" << f[1].to_s
if @edit[:new][:fields][f[1].to_i]['datatype'] == "password"
page << javascript_hide(def_field)
page << javascript_show(pwd_field)
page << "$('##{pwd_field}').val('');"
@edit[:new][:fields][f[1].to_i]['default_value'] = nil
else
page << javascript_hide(pwd_field)
page << javascript_show(def_field)
page << "$('##{def_field}').val('');"
@edit[:new][:fields][f[1].to_i]['default_value'] = nil
end
end
end
end
page << javascript_for_miq_button_visibility_changed(@changed)
end
end
# AJAX driven routine to check for changes in ANY field on the form
def form_method_field_changed
if !@sb[:form_vars_set] # workaround to prevent an error that happens when IE sends a transaction form form even after save button is clicked when there is text_area in the form
render :nothing => true
else
return unless load_edit("aemethod_edit__#{params[:id]}", "replace_cell__explorer")
@prev_location = @edit[:new][:location]
get_method_form_vars
if row_selected_in_grid?
@refresh_div = "class_methods_div"
@refresh_partial = "class_methods"
@field_name = "cls_method"
else
@refresh_div = "method_inputs_div"
@refresh_partial = "method_inputs"
@field_name = "method"
end
if @edit[:current][:location] == "inline" && @edit[:current][:data]
@edit[:method_prev_data] = @edit[:current][:data]
end
if @edit[:new][:location] == "inline" && !params[:cls_method_data] && !params[:method_data] && !params[:transOne]
if !@edit[:method_prev_data]
@edit[:new][:data] = MiqAeMethod.default_method_text
else
@edit[:new][:data] = @edit[:method_prev_data]
end
elsif params[:cls_method_location] || params[:method_location] # reset data if location is changed
@edit[:new][:data] = ""
end
@changed = (@edit[:new] != @edit[:current])
@edit[:default_verify_status] = @edit[:new][:location] == "inline" && @edit[:new][:data] && @edit[:new][:data] != ""
render :update do |page| # Use JS to update the display
page.replace_html(@refresh_div, :partial => @refresh_partial) if @refresh_div && @prev_location != @edit[:new][:location]
# page.replace_html("hider_1", :partial=>"method_data", :locals=>{:field_name=>@field_name}) if @prev_location != @edit[:new][:location]
if params[:cls_field_datatype]
if session[:field_data][:datatype] == "password"
page << javascript_hide("cls_field_default_value")
page << javascript_show("cls_field_password_value")
page << "$('#cls_field_password_value').val('');"
else
page << javascript_hide("cls_field_password_value")
page << javascript_show("cls_field_default_value")
page << "$('#cls_field_default_value').val('');"
end
end
if params[:method_field_datatype]
if session[:field_data][:datatype] == "password"
page << javascript_hide("method_field_default_value")
page << javascript_show("method_field_password_value")
page << "$('#method_field_password_value').val('');"
else
page << javascript_hide("method_field_password_value")
page << javascript_show("method_field_default_value")
page << "$('#method_field_default_value').val('');"
end
end
params.keys.each do |field|
if field.to_s.starts_with?("cls_fields_datatype_")
f = field.split('cls_fields_datatype_')
def_field = "cls_fields_value_" << f[1].to_s
pwd_field = "cls_fields_password_value_" << f[1].to_s
elsif field.to_s.starts_with?("fields_datatype_")
f = field.split('fields_datatype_')
def_field = "fields_value_" << f[1].to_s
pwd_field = "fields_password_value_" << f[1].to_s
end
if f
if @edit[:new][:fields][f[1].to_i]['datatype'] == "password"
page << javascript_hide(def_field)
page << javascript_show(pwd_field)
page << "$('##{pwd_field}').val('');"
@edit[:new][:fields][f[1].to_i]['default_value'] = nil
else
page << javascript_hide(pwd_field)
page << javascript_show(def_field)
page << "$('##{def_field}').val('');"
@edit[:new][:fields][f[1].to_i]['default_value'] = nil
end
end
end
if @edit[:default_verify_status] != session[:log_depot_default_verify_status]
session[:log_depot_default_verify_status] = @edit[:default_verify_status]
if @edit[:default_verify_status]
page << "miqValidateButtons('show', 'default_');"
else
page << "miqValidateButtons('hide', 'default_');"
end
end
page << javascript_for_miq_button_visibility_changed(@changed)
end
end
end