This commit is contained in:
BoHung Chiu 2023-04-10 16:15:59 +08:00
parent 00b1ed264f
commit 917c2e661b
1 changed files with 70 additions and 56 deletions

View File

@ -41,16 +41,19 @@ module AsiaSyncPlugin
#待確認: 指導論文(可用個人研究製作) paper_data
def self.user_id(member)
user_id = member.email.sub(/@asia\.edu\.tw$/, '')
end
def self.localize_data(data)
in_use_locales = Site.first.in_use_locales rescue [:en,:zh_tw]
return in_use_locales.map{|locale| [locale, data] }.to_h
end
# 期刊論文
def self.sync_journal_papers(query_result, user)
puts "Journal Paper for user: #{user.user_name}"
def self.sync_journal_papers(query_result, member)
puts "Journal Paper for user: #{self.user_id(member)}"
journal_papers = query_result["journal"].to_a
if !query_result['emp'].blank?
JournalPaper.where(member_profile_id: user.member_profile_id,:rss2_id.ne=>nil).where(:rss2_id.nin=>journal_papers.map{|v| v["ukey"]}).destroy
JournalPaper.where(member_profile_id: member.id,:rss2_id.ne=>nil).where(:rss2_id.nin=>journal_papers.map{|v| v["ukey"]}).destroy
end
return if journal_papers.count == 0
journal_papers.each do |journal_paper|
@ -66,17 +69,17 @@ module AsiaSyncPlugin
journal_paper_type = JournalPaperType.create(:title_translations=>localize_data(journal_paper["J_class"]))
end
jp.journal_paper_type = journal_paper_type
jp.member_profile_id = user.member_profile_id
jp.member_profile_id = [member.id]
puts "Journal Paper #{jp.id} saved" if jp.save
end
end
# 研討會論文
def self.sync_conference_papers(query_result, user)
puts "Conference Paper for user: #{user.user_name}"
puts "Conference Paper for user: #{self.user_id(member)}"
conference_papers = query_result["conference"].to_a
if !query_result['emp'].blank?
WritingConference.where(member_profile_id: user.member_profile_id,:rss2_id.ne=>nil).where(:rss2_id.nin=>conference_papers.map{|v| v["ukey"]}).destroy
WritingConference.where(member_profile_id: member.id,:rss2_id.ne=>nil).where(:rss2_id.nin=>conference_papers.map{|v| v["ukey"]}).destroy
end
return if conference_papers.count == 0
conference_papers.each do |conference_paper|
@ -89,17 +92,17 @@ module AsiaSyncPlugin
wc.authors_translations = localize_data(conference_paper["author"])
wc.location_translations = localize_data(conference_paper["Sym_location"].strip)
wc.publication_date = DateTime.parse(conference_paper["Sym_YearMonth"].gsub('.','/')) rescue nil
wc.member_profile_id = user.member_profile_id
wc.member_profile_id = member.id
puts "conference paper #{wc.id} saved" if wc.save
end
end
# 研究計畫 Research Grants
def self.sync_projects(query_result, user)
puts "Projects(Research Grants) for user: #{user.user_name}"
puts "Projects(Research Grants) for user: #{self.user_id(member)}"
projects = query_result["research"].to_a
if !query_result['emp'].blank?
Project.where(member_profile_id: user.member_profile_id,:rss2_id.ne=>nil).where(:rss2_id.nin=>projects.map{|v| v["ukey"]}).destroy
Project.where(member_profile_id: member.id,:rss2_id.ne=>nil).where(:rss2_id.nin=>projects.map{|v| v["ukey"]}).destroy
end
return if projects.count == 0
projects.each do |project|
@ -114,18 +117,18 @@ module AsiaSyncPlugin
pj.period_start_date = period[0]
pj.period_end_date = period[1]
end
pj.member_profile_id = user.member_profile_id
pj.member_profile_id = member.id
puts "Project(Research Grants) #{pj.id} saved" if pj.save
end
end
# 專書 books
def self.sync_books(query_result, user)
puts "books for user: #{user.user_name}"
puts "books for user: #{self.user_id(member)}"
books = query_result["books"].to_a
books1 = query_result["books1"].to_a
if !query_result['emp'].blank?
Book.where(member_profile_id: user.member_profile_id,:rss2_id.ne=>nil).where(:rss2_id.nin=>(books + books1).map{|v| v["ukey"]}).destroy
Book.where(member_profile_id: member.id,:rss2_id.ne=>nil).where(:rss2_id.nin=>(books + books1).map{|v| v["ukey"]}).destroy
end
return if (books.count == 0 && books1.count == 0)
books.each do |book|
@ -145,7 +148,7 @@ module AsiaSyncPlugin
if bk.publish_date
bk.year = bk.publish_date.year
end
bk.member_profile_id = user.member_profile_id
bk.member_profile_id = member.id
puts "books #{bk.id} saved" if bk.save
end
books1.each do |book|
@ -159,16 +162,16 @@ module AsiaSyncPlugin
if !(book["Books_year"].blank?) #出版年度
bk.publish_date = DateTime.parse(book["Books_year"] + "/1") rescue nil
end
bk.member_profile_id = user.member_profile_id
bk.member_profile_id = member.id
puts "books #{bk.id} saved" if bk.save
end
end
#專利 Patents
def self.sync_patents(query_result, user)
puts "Patents for user: #{user.user_name}"
puts "Patents for user: #{self.user_id(member)}"
patents = query_result["patent"].to_a
if !query_result['emp'].blank?
Patent.where(member_profile_id: user.member_profile_id,:rss2_id.ne=>nil).where(:rss2_id.nin=>patents.map{|v| v["ukey"]}).destroy
Patent.where(member_profile_id: member.id,:rss2_id.ne=>nil).where(:rss2_id.nin=>patents.map{|v| v["ukey"]}).destroy
end
return if (patents.count == 0 )
patents.each do |patent|
@ -185,16 +188,16 @@ module AsiaSyncPlugin
pt.application_date = period[0]
pt.end_date = period[1]
end
pt.member_profile_id = user.member_profile_id
pt.member_profile_id = member.id
puts "Patent #{pt.id} saved" if pt.save
end
end
#獲獎(榮譽) Award
def self.sync_honors(query_result, user)
puts "Honors(Awards) for user: #{user.user_name}"
puts "Honors(Awards) for user: #{self.user_id(member)}"
honors = query_result["award"].to_a
if !query_result['emp'].blank?
Honor.where(member_profile_id: user.member_profile_id,:rss2_id.ne=>nil).where(:rss2_id.nin=>honors.map{|v| v["ukey"]}).destroy
Honor.where(member_profile_id: member.id,:rss2_id.ne=>nil).where(:rss2_id.nin=>honors.map{|v| v["ukey"]}).destroy
end
return if (honors.count == 0 )
honors.each do |honor|
@ -204,16 +207,16 @@ module AsiaSyncPlugin
hn.award_name_translations = localize_data(honor["Award_name"]) #獎項名稱
hn.awarding_unit_translations = localize_data(honor["Award_unit"].to_s.gsub("授獎單位:","").strip) #頒獎單位
hn.award_date = DateTime.parse(honor["Award_Date"].gsub('.','/')) rescue nil #獲獎日期
hn.member_profile_id = user.member_profile_id
hn.member_profile_id = member.id
puts "Honor(Award) #{hn.id} saved" if hn.save
end
end
#教師參與展演活動(展演) Show
def self.sync_activities(query_result, user)
puts "Activities(Shows) for user: #{user.user_name}"
puts "Activities(Shows) for user: #{self.user_id(member)}"
activities = query_result["show"].to_a
if !query_result['emp'].blank?
Activity.where(member_profile_id: user.member_profile_id,:rss2_id.ne=>nil).where(:rss2_id.nin=>activities.map{|v| v["ukey"]}).destroy
Activity.where(member_profile_id: member.id,:rss2_id.ne=>nil).where(:rss2_id.nin=>activities.map{|v| v["ukey"]}).destroy
end
return if (activities.count == 0 )
activities.each do |activity|
@ -227,16 +230,16 @@ module AsiaSyncPlugin
act.activity_start_date = period[0]
act.activity_end_date = period[1]
end
act.member_profile_id = user.member_profile_id
act.member_profile_id = member.id
puts "Activity(Show) #{act.id} saved" if act.save
end
end
#授課課程(教學資料) Course Information
def self.sync_courses(query_result, user)
puts "Courses(Course Information) for user: #{user.user_name}"
puts "Courses(Course Information) for user: #{self.user_id(member)}"
courses = query_result["cos_data"].to_a
if !query_result['emp'].blank?
Course.where(member_profile_id: user.member_profile_id,:rss2_id.ne=>nil).where(:rss2_id.nin=>courses.map{|v| v["ukey"]}).destroy
Course.where(member_profile_id: member.id,:rss2_id.ne=>nil).where(:rss2_id.nin=>courses.map{|v| v["ukey"]}).destroy
end
return if (courses.count == 0 )
courses.each do |course|
@ -268,16 +271,16 @@ module AsiaSyncPlugin
end
cs.course_code = course["cos_data"] #課程代碼 ex: "ES200023A"
cs.member_profile_id = user.member_profile_id
cs.member_profile_id = member.id
puts "Course(Course Information) #{cs.id} saved" if cs.save
end
end
#指導論文(個人研究) Thesis Advisor
def self.sync_researches(query_result, user)
puts "Researches(Thesis Advisor) for user: #{user.user_name}"
puts "Researches(Thesis Advisor) for user: #{self.user_id(member)}"
researches = query_result["paper_data"].to_a
if !query_result['emp'].blank?
Research.where(member_profile_id: user.member_profile_id,:rss2_id.ne=>nil).where(:rss2_id.nin=>researches.map{|v| v["ukey"]}).destroy
Research.where(member_profile_id: member.id,:rss2_id.ne=>nil).where(:rss2_id.nin=>researches.map{|v| v["ukey"]}).destroy
end
return if (researches.count == 0 )
researches.each do |research|
@ -290,16 +293,16 @@ module AsiaSyncPlugin
rs.authors_translations = localize_data(research["chi_name"]) #作者
rs.year = research["year"] #年度
rs.member_profile_id = user.member_profile_id
rs.member_profile_id = member.id
puts "Research(Thesis Advisor) #{rs.id} saved" if rs.save
end
end
#創作 Create design
def self.sync_create_designs(query_result, user)
puts "Create Designs for user: #{user.user_name}"
puts "Create Designs for user: #{self.user_id(member)}"
create_designs = query_result["create"].to_a
if !query_result['emp'].blank?
CreateDesign.where(member_profile_id: user.member_profile_id,:rss2_id.ne=>nil).where(:rss2_id.nin=>create_designs.map{|v| v["ukey"]}).destroy
CreateDesign.where(member_profile_id: member.id,:rss2_id.ne=>nil).where(:rss2_id.nin=>create_designs.map{|v| v["ukey"]}).destroy
end
return if (create_designs.count == 0 )
create_designs.each do |create_design|
@ -309,16 +312,16 @@ module AsiaSyncPlugin
cd.design_translations = localize_data(create_design["Create_name"].to_s.split("設計:")[1..-1].join("")) #設計
cd.create_location_translations = localize_data(create_design["Create_location"]) #地點
cd.publish_date = Date.parse(create_design["Create_sdate"].gsub(".","/")) rescue nil #系所(關鍵字)
cd.member_profile_id = user.member_profile_id
cd.member_profile_id = member.id
puts "Create Design #{cd.id} saved" if cd.save
end
end
#技術轉移
def self.sync_tec_transfers(query_result, user)
puts "Technology Transfers for user: #{user.user_name}"
puts "Technology Transfers for user: #{self.user_id(member)}"
tec_transfers = query_result["tec_transfer"].to_a
if !query_result['emp'].blank?
TecTransfer.where(member_profile_id: user.member_profile_id,:rss2_id.ne=>nil).where(:rss2_id.nin=>tec_transfers.map{|v| v["ukey"]}).destroy
TecTransfer.where(member_profile_id: member.id,:rss2_id.ne=>nil).where(:rss2_id.nin=>tec_transfers.map{|v| v["ukey"]}).destroy
end
return if (tec_transfers.count == 0 )
tec_transfers.each do |tec_transfer|
@ -328,21 +331,21 @@ module AsiaSyncPlugin
tt.patent_number = tec_transfer["ukey"]
tt.trans_name_translations = localize_data(tec_transfer["Trans_Data_NAME"]) #設計
tt.trans_unit_translations = localize_data(tec_transfer["Trans_Unit"]) #地點
if tec_transfer["period"].present? #展演期間
if tec_transfer["period"].present? #技轉期間
period = tec_transfer["period"].split('~').map{|date| Date.parse(date) rescue nil}
tt.period_start = period[0]
tt.period_end = period[1]
end
tt.member_profile_id = user.member_profile_id
tt.member_profile_id = member.id
puts "Technology Transfer #{tt.id} saved" if tt.save
end
end
#指導大專生計畫 College Student Participation in Research Projects
def self.sync_college_projects(query_result, user)
puts "College Projects for user: #{user.user_name}"
puts "College Projects for user: #{self.user_id(member)}"
college_projects = query_result["college_data"].to_a
if !query_result['emp'].blank?
CollegeProject.where(member_profile_id: user.member_profile_id,:rss2_id.ne=>nil).where(:rss2_id.nin=>college_projects.map{|v| v["ukey"]}).destroy
CollegeProject.where(member_profile_id: member.id,:rss2_id.ne=>nil).where(:rss2_id.nin=>college_projects.map{|v| v["ukey"]}).destroy
end
return if (college_projects.count == 0 )
college_projects.each do |college_project|
@ -355,7 +358,7 @@ module AsiaSyncPlugin
cp.advising_professor_translations = localize_data(college_tch_std[0]) #指導教授
cp.student_name_translations = localize_data(college_tch_std[1]) #學生姓名
cp.department_translations = localize_data(college_project["College_unit_nm"].strip)
cp.member_profile_id = user.member_profile_id
cp.member_profile_id = member.id
puts "College Project #{cp.id} saved" if cp.save
end
end
@ -364,28 +367,39 @@ namespace :sync_asia_personal_plugins do
desc 'sync_personal_plugins from Asia'
task :sync,[:user_names] => [:environment] do |task,args|
if args.user_names
users = User.where(:user_name.in=>YAML.load(args.user_names)).to_a
user_ids = YAML.load(args.user_names)
member_ids = (MemberProfile.where(:email.in=>user_ids.map{|s| s + '@asia.edu.tw' }).pluck(:id)
User.where(:user_name.in=>user_ids, :member_profile_id.nin=>member_ids).each do |u|
query_result = get_sync_data(u.user_name)
emp = query_result["emp"]
next if emp.blank?
member = u.member_profile
member.update(:email=>"#{u.user_name}@asia.edu.tw")
member_ids << member.id
end
members = MemberProfile.where(:id.in=>member_ids).to_a
else
users = User.all.to_a
members = MemberProfile.where(:email=>/@asia\.edu\.tw$/).to_a
end
users.each do |user|
puts "Fetching data for #{user.user_name}..."
query_result = get_sync_data(user.user_name)
members.each do |member|
user_id = AsiaSyncPlugin.user_id(member)
puts "Fetching data for #{user_id}..."
query_result = get_sync_data(user_id)
#puts ["query_result",query_result]
AsiaSyncPlugin.sync_journal_papers(query_result, user)
AsiaSyncPlugin.sync_books(query_result, user)
AsiaSyncPlugin.sync_journal_papers(query_result, member)
AsiaSyncPlugin.sync_books(query_result, member)
AsiaSyncPlugin.sync_conference_papers(query_result, user)
AsiaSyncPlugin.sync_projects(query_result, user)
AsiaSyncPlugin.sync_patents(query_result, user)
AsiaSyncPlugin.sync_honors(query_result, user)
AsiaSyncPlugin.sync_activities(query_result, user)
AsiaSyncPlugin.sync_courses(query_result, user)
AsiaSyncPlugin.sync_researches(query_result, user)
AsiaSyncPlugin.sync_create_designs(query_result, user)
AsiaSyncPlugin.sync_tec_transfers(query_result, user)
AsiaSyncPlugin.sync_college_projects(query_result, user)
AsiaSyncPlugin.sync_conference_papers(query_result, member)
AsiaSyncPlugin.sync_projects(query_result, member)
AsiaSyncPlugin.sync_patents(query_result, member)
AsiaSyncPlugin.sync_honors(query_result, member)
AsiaSyncPlugin.sync_activities(query_result, member)
AsiaSyncPlugin.sync_courses(query_result, member)
AsiaSyncPlugin.sync_researches(query_result, member)
AsiaSyncPlugin.sync_create_designs(query_result, member)
AsiaSyncPlugin.sync_tec_transfers(query_result, member)
AsiaSyncPlugin.sync_college_projects(query_result, member)
end
end
def net_http_get_response(uri,headers={})