class Csm::Resource::Generator::Org

Public Class Methods

desc_first() click to toggle source
# File lib/csm/resource/generator/org.rb, line 7
def self.desc_first
   'org [prefix] [information] [output file]'
end
desc_second() click to toggle source
# File lib/csm/resource/generator/org.rb, line 11
def self.desc_second
   'Create xml files for `fscsm_org create`'
end
long_desc() click to toggle source
# File lib/csm/resource/generator/org.rb, line 69
      def self.long_desc
         a = <<~USAGE
Outputs the XML that contains a hierarchical organization data for `fscsm_org import`
[sample args]
 org orgSample 1:3:3
         -> orgSample000/orgSample000-000/orgSample000-000-000
                                         /orgSample000-000-001
                                         /orgSample000-000-002
                        /orgSample000-001/orgSample000-001-000
                                         /orgSample000-001-001
                                         /orgSample000-001-002
                        /orgSample000-002/orgSample000-002-000
                                         /orgSample000-002-001
                                         /orgSample000-002-002

USAGE
         a.split("\n").map{|s| "  #{s}"}.join("\n")
      end
new() click to toggle source
# File lib/csm/resource/generator/org.rb, line 15
def initialize
end

Public Instance Methods

create_top_rank(org_id) click to toggle source
# 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) click to toggle source
# 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