require "uri" require "net/http" namespace :fgu_sync do task :sync_plugins => :environment do MemberProfile.each do |mp| staff_id = mp.sid rescue nil # puts mp.id if !staff_id.nil? && staff_id != "" sync_fgu_journals(staff_id, mp) sync_fgu_conference(staff_id, mp) sync_fgu_books(staff_id, mp) sync_fgu_diploma(staff_id, mp) sync_fgu_experience(staff_id, mp) sync_fgu_profile(staff_id, mp) end end end def parse_date(d) (d.present? ? (Date.parse(d) rescue nil) : nil) end def camelize(str) str.split('_').map{|v| v.capitalize}.join(' ') end def fetch_key(data,key,role=nil,value_flag=false) begin attr_fields = (value_flag ? role.attribute_fields : data) case key when 'f_profession_list' tmp_key = 'Research Expertise' else tmp_key = camelize(key) end tmp = attr_fields.select{|v| v.title_translations[:en].upcase.include?(tmp_key.upcase)}.first value_flag ? data.where(:attribute_field_id => tmp.id).first : tmp rescue => e nil end end def sync_fgu_journals(staff_id, mp) puts "*********************************************" puts "Syncing journals for #{staff_id}" params_to_send = {"plugin" => "journal", "id" => staff_id} data = get_fgu_data(params_to_send) I18n.locale = :zh_tw if !data.nil? && data["success"] == true && data["data"].count > 0 puts "Data found." JournalPaper.where(:member_profile_id => mp.id).destroy_all data["data"].each_with_index do |dt, idx| rss2id = dt["journal_id"] pd = parse_date(dt["publication_date"]) paper_title = {"en" => dt["paper_title"],"zh_tw" => dt["paper_title"]} journal_title = {"en" => dt["journal_title"],"zh_tw" => dt["journal_title"]} from_to = dt["from_to"].split("-") from_to[1] = "" if !from_to[1].present? all_authors = [dt["author1"],dt["author2"],dt["author3"],dt["author4"]] all_authors.delete("") authors = {"en" => all_authors.join(", "), "zh_tw" => all_authors.join(", ")} note = dt["department"] + " " + dt["country"] + " " + dt["note"] if !dt["paper_level"].empty? level = JournalLevel.where(:title => dt["paper_level"]).first rescue nil if level.nil? level = JournalLevel.create(:key => "key_#{idx}", :title_translations => {"en" => dt["paper_level"], "zh_tw" => dt["paper_level"]}) end end if !dt["paper_type"].empty? type = JournalPaperType.where(:title => dt["paper_type"]).first rescue nil if type.nil? type = JournalPaperType.create(:key => "key_#{idx}", :title_translations => {"en" => dt["paper_type"], "zh_tw" => dt["paper_type"]}) end end jp = JournalPaper.where(:rss2_id => rss2id).first rescue nil jp_data = { :publication_date => pd, :rss2_id => rss2id, :year => (pd ? pd.strftime("%Y") : ""), :authors_translations => authors, :paper_title_translations => paper_title, :journal_title_translations => journal_title, :vol_no => dt["vol_no"], :issue_no => dt["issue_no"], :note => note, :form_to_start => from_to[0], :form_to_end => from_to[1] } if jp.nil? jp = JournalPaper.new(jp_data) puts "Saving new journal." else jp.update_attributes(jp_data) puts "Updating journal #{jp.id}." end jp_mps = MemberProfile.where("tmp_name.zh_tw" => {"$in" => all_authors}).to_a jp.member_profiles = jp_mps.size==0 ? [mp] : jp_mps jp.journal_levels << level if !level.nil? jp.journal_paper_type = type if !type.nil? jp.save end else puts "No data found." end puts "Sync complete for #{staff_id}" puts "*********************************************" end def sync_fgu_conference(staff_id, mp) puts "*********************************************" puts "Syncing conference for #{staff_id}" params_to_send = {"plugin" => "conference", "id" => staff_id} data = get_fgu_data(params_to_send) I18n.locale = :zh_tw if !data.nil? && data["success"] == true && data["data"].count > 0 puts "Data found." WritingConference.where(:member_profile_id => mp.id).destroy_all data["data"].each_with_index do |dt, idx| rss2id = dt["conf_id"] start_date = parse_date(dt["start_date"]) end_date = parse_date(dt["end_date"]) paper_title = {"en" => dt["paper_title"],"zh_tw" => dt["paper_title"]} conference_title = {"en" => dt["conference_name"],"zh_tw" => dt["conference_name"]} authors = [dt["author1"],dt["author2"],dt["author3"],dt["author4"]] authors.delete("") authors = {"en" => authors.join(", "), "zh_tw" => authors.join(", ")} note = dt["department"] + " " + dt["note"] cp = WritingConference.where(:rss2_id => rss2id).first rescue nil cp_data = {:period_start_date => start_date, :period_end_date => end_date, :rss2_id => rss2id, :year => (end_date ? end_date.strftime("%Y") : ""), :authors_translations => authors, :paper_title_translations => paper_title, :conference_title_translations => conference_title, :note => note, :location_translations => {"en" => dt["location"], "zh_tw" => dt["location"]}} if cp.nil? cp = WritingConference.new(cp_data) puts "Saving new conference." else cp.update_attributes(cp_data) puts "Updating conference #{cp.id}." end cp.member_profile = mp cp.save end else puts "No data found." end puts "Sync complete for #{staff_id}" puts "*********************************************" end def sync_fgu_books(staff_id, mp) puts "*********************************************" puts "Syncing book for #{staff_id}" params_to_send = {"plugin" => "book", "id" => staff_id} data = get_fgu_data(params_to_send) I18n.locale = :zh_tw if !data.nil? && data["success"] == true && data["data"].count > 0 puts "Data found." Book.where(:member_profile_id => mp.id).destroy_all data["data"].each_with_index do |dt, idx| rss2id = dt["book_id"] pd = parse_date(dt["date_of_publication"]) book_title = {"en" => dt["book_title"],"zh_tw" => dt["book_title"]} publisher = {"en" => dt["publisher"],"zh_tw" => dt["publisher"]} authors = [dt["author1"],dt["author2"],dt["author3"],dt["author4"]] authors.delete("") authors = {"en" => authors.join(", "), "zh_tw" => authors.join(", ")} note = dt["department"] + " " + dt["location"] + " " + dt["note"] if !dt["author_type"].empty? authortype = BookAuthorType.where(:title => dt["author_type"]).first rescue nil if authortype.nil? authortype = BookAuthorType.create(:title_translations => {"en" => dt["author_type"], "zh_tw" => dt["author_type"]}) end end if !dt["book_type"].empty? type = BookType.where(:title => dt["book_type"]).first rescue nil if type.nil? type = BookType.create(:title_translations => {"en" => dt["book_type"], "zh_tw" => dt["book_type"]}) end end book = Book.where(:rss2_id => rss2id).first rescue nil book_data = {:publish_date => pd, :rss2_id => rss2id, :year => (pd ? pd.strftime("%Y") : ""), :authors_translations => authors, :publisher_translations => publisher, :book_title_translations => book_title, :isbn => dt["isbn"], :note => note, :language => dt["language"], :member_profile => mp} if book.nil? book = Book.new(book_data) puts "Saving new book." else book.update_attributes(book_data) puts "Updating book #{book.id}." end book.book_author_types << authortype if !authortype.nil? book.book_type = type if !type.nil? book.member_profile = mp book.save end else puts "No data found." end puts "Sync complete for #{staff_id}" puts "*********************************************" end def sync_fgu_diploma(staff_id, mp) puts "*********************************************" puts "Syncing diplomas for #{staff_id}" params_to_send = {"plugin" => "diploma", "id" => staff_id} data = get_fgu_data(params_to_send) I18n.locale = :zh_tw if !data.nil? && data["success"] == true && data["data"].count > 0 puts "Data found." puts "Deleting old data" Diploma.where(:member_profile_id => mp.id).destroy_all data["data"].each_with_index do |dt, idx| start_date = parse_date(dt["start_date"]) end_date = parse_date(dt["end_date"]) school_name = {"en" => dt["school_name"], "zh_tw" => dt["school_name"]} department = {"en" => dt["department"], "zh_tw" => dt["department"]} degree = {"en" => dt["degree"], "zh_tw" => dt["degree"]} Diploma.create(:start_date => start_date, :end_date => end_date, :school_name_translations => school_name, :department_translations => department, :degree_translations => degree, :member_profile => mp) puts "Diploma created." end end puts "Syncing diplomas for #{staff_id} complete." puts "*********************************************" end def sync_fgu_experience(staff_id, mp) puts "*********************************************" puts "Syncing experiences for #{staff_id}" params_to_send = {"plugin" => "experience", "id" => staff_id} data = get_fgu_data(params_to_send) I18n.locale = :zh_tw if !data.nil? && data["success"] == true && data["data"].count > 0 puts "Data found." puts "Deleting old data" Experience.where(:member_profile_id => mp.id).destroy_all data["data"].each_with_index do |dt, idx| start_date = parse_date(dt["start_date"]) end_date = parse_date(dt["end_date"]) organization_title = {"en" => dt["organization_title"], "zh_tw" => dt["organization_title"]} job_title = {"en" => dt["job_title"], "zh_tw" => dt["job_title"]} if !dt["emp_type"].blank? type = ExperienceType.where(:title => dt["emp_type"]).first rescue nil if type.nil? type = ExperienceType.create(:title_translations => {"en" => dt["emp_type"], "zh_tw" => dt["emp_type"]}) end end exp = Experience.new(:start_date => start_date, :end_date => end_date, :organizationt_title_translations => organization_title, :job_title_translations => job_title, :member_profile => mp) exp.experience_type = type if !type.nil? exp.save puts "Exp created." end end puts "Syncing experiences for #{staff_id} complete." puts "*********************************************" end def sync_fgu_profile(staff_id, mp) puts "*********************************************" puts "Syncing profile for #{staff_id}" params_to_send = {"plugin" => "profile", "id" => staff_id} data = get_fgu_data(params_to_send) I18n.locale = :zh_tw if !data.nil? && data["success"] == true && data["data"].count > 0 all_role_ids = mp.role_ids all_role_status_ids = mp.role_status_ids data["data"].each do |dt| case dt["role"] when "職員" role = Role.where(:key => "staff").first when "教師" role = Role.where(:key => "teacher").first when nil role = Role.where(:key => "teacher").first end if !role.nil? # puts "Assigning role #{role.key}" case dt["job_status"] when "專任" role_status = role.role_statuses.select{|v| v.title_translations[:en].upcase == "Full-Time".upcase}.first when "兼任" role_status = role.role_statuses.select{|v| v.title_translations[:en].upcase == "Part-Time".upcase}.first else role_status = nil end if role.key == "teacher" # puts dt af = fetch_key(role.attribute_fields,"job_title") if !af.nil? options = af.option_list.values job_title = options.index(options.find{|x| x["zh_tw"] == dt["job_title"]}) # puts job_title.to_s if job_title.blank? job_title = af.option_list.keys.last.to_i + 1 af.option_list[job_title] = {"en" => dt["job_title"], "zh_tw" => dt["job_title"]} # puts af.option_list.to_s af.save else job_title = af.option_list.keys[job_title].to_i end end #saving job title av = fetch_key(mp.attribute_values,"job_title",role,true) if av.nil? field = fetch_key(role.attribute_fields,"job_title") if !field.nil? av = AttributeValue.new(:attribute_field_id => field.id, :member_profile_id => mp.id, :key => "job_title") end end if !av.nil? av.value= job_title av.save end #saving profession list av = fetch_key(mp.attribute_values,"f_profession_list",role,true) if av.nil? field = fetch_key(role.attribute_fields,"f_profession_list") if !field.nil? av = AttributeValue.new(:attribute_field_id => field.id, :member_profile_id => mp.id, :key => "f_profession_list") end end if !av.nil? av.value= {:en => dt["profession_list"], :zh_tw => dt["profession_list"]} av.save end #saving courses av = fetch_key(mp.attribute_values,"current_course",role,true) if av.nil? field = fetch_key(role.attribute_fields,"current_course") if !field.nil? av = AttributeValue.new(:attribute_field_id => field.id, :member_profile_id => mp.id) end end if !av.nil? course_html = "" if dt["courses"].blank? av.value= {:en => course_html, :zh_tw => course_html} av.save end dt["courses"].each do |course| course_url = course["course_url"] course_url.gsub!(/http:\/\/(.*\.fgu\.edu\.tw)/, 'https://\1') course_html += "#{course["course_id"]} #{course["course_name"]}
" av.value= {:en => course_html, :zh_tw => course_html} av.save end end end end mp.email = dt["email"] mp.office_tel = dt["office_tel"] mp.address_translations = {"en" => dt["address"], "zh_tw" => dt["address"]} mp.first_name_translations = {"en" => dt["name"], "zh_tw" => dt["name"]} if (!role.nil? && !all_role_ids.include?(role.id)) all_role_ids << role.id end if (!role_status.nil? && !all_role_status_ids.include?(role_status.id)) all_role_status_ids << role_status.id end end # puts [staff_id,all_role_ids.inspect] if !all_role_ids.blank? mp.role_ids = all_role_ids all_role_ids.each do |r_id| rr = Role.find(r_id) if !rr.member_profile_ids.include?(mp.id) rr.member_profile_ids << mp.id rr.save end end puts 'modify roles' end if !all_role_status_ids.blank? mp.role_status_ids = all_role_status_ids all_role_status_ids.each do |r_id| rr = RoleStatus.find(r_id) if !rr.member_profile_ids.include?(mp.id) rr.member_profile_ids << mp.id rr.save end end puts 'modify role_statuses' end mp.save end puts "Syncing profile for #{staff_id} complete." puts "*********************************************" end def get_fgu_data(params_to_send) uri = URI.parse(URI.encode("http://120.101.66.83/fgu/?plugin=#{params_to_send["plugin"]}&id=#{params_to_send["id"]}")) # uri = URI.parse("http://127.0.0.1/fgu/?plugin=#{params_to_send["plugin"]}&id=#{params_to_send["id"]}") http = Net::HTTP.new(uri.host, uri.port) request = Net::HTTP::Get.new(uri.request_uri) response = http.request(request) data = JSON.parse(response.body) rescue nil return data end end