Class: Csm::Resource::Generator::Org

Inherits:
Object
  • Object
show all
Defined in:
lib/csm/resource/generator/org.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeOrg



15
16
# File 'lib/csm/resource/generator/org.rb', line 15

def initialize
end

Class Method Details

.desc_firstObject



7
8
9
# File 'lib/csm/resource/generator/org.rb', line 7

def self.desc_first
   'org [prefix] [information] [output file]'
end

.desc_secondObject



11
12
13
# File 'lib/csm/resource/generator/org.rb', line 11

def self.desc_second
   'Create xml files for `fscsm_org create`'
end

.long_descObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/csm/resource/generator/org.rb', line 69

def self.long_desc
   a = "Outputs the XML that contains a hierarchical organization data for `fscsm_org import`\n[sample args]\n org orgSample 1:3:3\n   -> orgSample000/orgSample000-000/orgSample000-000-000\n                                   /orgSample000-000-001\n                                   /orgSample000-000-002\n                  /orgSample000-001/orgSample000-001-000\n                                   /orgSample000-001-001\n                                   /orgSample000-001-002\n                  /orgSample000-002/orgSample000-002-000\n                                   /orgSample000-002-001\n                                   /orgSample000-002-002\n\n"
   a.split("\n").map{|s| "  #{s}"}.join("\n")
end

Instance Method Details

#create_top_rank(org_id) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/csm/resource/generator/org.rb', line 18

def create_top_rank(org_id)
   o = OrgCreate.new
   o.org_id = org_id
   o.parent_org_id = "!root"
   o.rank = 0
   o.comment = "/#{org_id}"
   o
end

#run(prefix, information, output) ⇒ Object



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
# File 'lib/csm/resource/generator/org.rb', line 27

def run(prefix, information, output)
   xml_file = File::join(Dir::pwd, output)
   STDOUT.puts "Outputs the specified file:`#{xml_file}`.\nIs it ok? [Yn]"
   typed = STDIN.gets
   exit if typed.upcase == "N"
   
   data = []
   begin
      
      OrgCreate.rank_array = information.split(":").map{|s|s.to_i}
      
      0.upto(OrgCreate::rank_array[0].to_i - 1) do |i|
         o = create_top_rank("#{prefix}-#{"%03d" % i}")
         data << o
         
         o.generate_children_recursive(data)
         
      end
      
      data.sort! do |d1, d2| 
         (d1.rank <=> d2.rank).nonzero? || (d1.org_id <=> d2.org_id)
      end
      
      File::open(xml_file, 'w') do |xml|
         xml.puts %|<?xml version="1.0" encoding="UTF-8"?>|
         xml.puts %|<orgs>|
         data.each do |d|
            xml.puts d.to_xml
         end
         xml.puts %|</orgs>|
      end
      
      STDOUT.puts "Output done."
      
   rescue
      STDERR.puts $!
      STDERR.puts $!.backtrace.join("\n")
      
   end
   
end