# encoding : utf-8 # Usage =============== # # ARGV[0] = folder path of xml files # ARGV[1] = folder path of files # #====================== require 'mongo' require 'nokogiri' include Mongo CSV_PATH = ARGV[0] FILE_PATH = ARGV[1] FIELDS = %w[title file createdate modifydate createuser modifyuser] DB_BASE_NAME = "production" TABLE = { "0" => ["gaTaco", "ntuga"], "1" => ["fdLai", "fdhome"], "5" => ["prLin", "property01"], "3" => ["gsTien", "ckh", "general01", "lynn"], "10" => ["cmChen", "construction01", "allen", "enAmeliaxu", "energy"], # "2" => ["caLin", "cashier01"], "6" => ["pcAmyok", "purchasing01", "purchasing02"], "7" => ["fmHsyu", "ntufss"], "4" => ["soChang", "social01"], "8" => ["meJune", "medicine01"], "9" => ["plKoa", "police"] } which_site = {} data = [] Dir.foreach(ARGV[0]) do |file| if file =~ /\w\.\w/ doc = Nokogiri::XML(File.open(File.join(ARGV[0], file))) doc.xpath("//row").each { |row| xml_fields = row.children.map{ |row| [row.name, row.content] } fields = FIELDS.inject([]) do |fields, field| xml_fields.each do |xf| if xf[0].include?(field) fields << xf[1] break end end fields end data << fields + [File.basename(file, '.xml')] } data.each do |d| title, file, c_time, m_time, creator, modifier, category = d if file =~ /\w\.\w/ file = File.join(File.join(ARGV[1], file)) site = "" TABLE.each do |key, value| if value.include? modifier site = key break elsif value.include? creator site = key break end end site = "0" if site.empty? if which_site[site] if which_site[site][category] which_site[site][category] += [[title, file, c_time, m_time]] else which_site[site][category] = [[title, file, c_time, m_time]] end else which_site[site] = {category => [[title, file, c_time, m_time]]} end end end end end def save_category archive_file_category, in_category category = { title: {"zh_tw" => in_category, "en" => in_category}, key: "import_#{category}" } archive_file_category.save(category) end def save_file_multiples archive_file_multiples, grid ,archive_id,title, file, c_time, m_time a_file = File.open(file) rescue nil if a_file filename = File.basename(file) archive_file = { file_title: {"zh_tw" => title, "en" => title }, choose_lang: ["zh_tw", "en"], file: filename, archive_file_id: archive_id, created_at: c_time, updated_at: m_time } a_file_id = archive_file_multiples.save(archive_file) grid.put(a_file, filename: "assets/archive_file_multiple/file/#{a_file_id}/#{filename}") end end def save_archive archive_files, category_id archive = { title: {"zh_tw" => " ", "en" => " "}, archive_file_category_id: category_id, is_top: false, is_hot: false, is_hidden: false, created_at: c_time, updated_at: m_time } archive_files.save(archive) end which_site.each do |site, categories| db = MongoClient.new("localhost", 27017).db("#{DB_BASE_NAME}_#{site}") categories.each do |category, data| archive_file_category = db.collection('archive_file_categories') category_id = save_category archive_file_category, category archive_files = db.collection('archive_files') archive_id = save_archive archive_files, category_id grid = Grid.new(db) archive_file_multiples = db.collection('archive_file_multiples') file_ids = [] data.each do |file| save_file_multiples(archive_file_multiples, grid ,archive_id , *file) end end end