add sync ntnu personal data task
This commit is contained in:
parent
b518a8ad29
commit
3009fba380
|
@ -1,4 +1,452 @@
|
|||
# desc "Explaining what the task does"
|
||||
# task :sync_ntnu_personal_data do
|
||||
# # Task goes here
|
||||
# end
|
||||
namespace :sync_personal_plugins do
|
||||
desc 'sync_personal_plugins from NTNU'
|
||||
task :sync => [:environment] do
|
||||
# User info include ntnu_account and member_profile_id
|
||||
sybase_client = TinyTds::Client.new username: ENV['USER_NAME'],
|
||||
password: ENV['PASSWORD'],
|
||||
host: ENV['NTNU_DB_HOST'],
|
||||
port: ENV['NTNU_DB_PORT'].to_i,
|
||||
tds_version: ENV['TDS_VERSION'],
|
||||
database: ENV['DB_NAME']
|
||||
|
||||
if !sybase_client.active?
|
||||
puts 'Connection is not active.'
|
||||
return
|
||||
end
|
||||
|
||||
# Query data and store them to local memory
|
||||
tds_result = sybase_client.execute 'SELECT * FROM VIEW_RESULTS_0001'
|
||||
query_result = tds_result.entries
|
||||
|
||||
# Close the NTNU SyBase connection
|
||||
tds_result.cancel
|
||||
sybase_client.close
|
||||
|
||||
# List available account data
|
||||
available_accounts = Set.new
|
||||
query_result.each do |data|
|
||||
available_accounts.add data["ER_ACCOUNT"]
|
||||
end
|
||||
|
||||
# Remove Old JournalPaper, xxx, ...
|
||||
JournalPaper.destroy_all
|
||||
WritingConference.destroy_all
|
||||
Project.destroy_all
|
||||
Book.destroy_all
|
||||
|
||||
# Create book types
|
||||
define_book_types
|
||||
|
||||
puts ENV['USER_NAME']
|
||||
User.each do |user|
|
||||
next if !available_accounts.include? user.user_name
|
||||
# Make sure sync methods don't mutate the query_result
|
||||
sync_journal_papers(query_result, user)
|
||||
sync_conference_papers(query_result, user)
|
||||
sync_projects(query_result, user)
|
||||
sync_academic_treatise_books(query_result, user)
|
||||
sync_academic_book_chapters(query_result, user)
|
||||
sync_officially_published_university_textbooks(query_result, user)
|
||||
sync_other_authored_works(query_result, user)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# 期刊論文
|
||||
def sync_journal_papers(query_result, user)
|
||||
puts "Journal Paper for user: #{user.user_name}"
|
||||
journal_papers = query_result.select { |data| data['ER_ACCOUNT'] == user.user_name && data['EI_SN'] == '20100421092921000' }
|
||||
journal_papers.each do |journal_paper|
|
||||
j_data = JSON.parse(journal_paper['JDATA'])
|
||||
jp = JournalPaper.new
|
||||
publication_year = nil
|
||||
publication_month = nil
|
||||
apa_title = nil
|
||||
invalid_apa_title = [nil, '', '必填欄位', '無', 'n/a', 'non']
|
||||
j_data['data'].each do |data|
|
||||
case data['eifSn']
|
||||
when '20140313173512173' # APA 論文格式
|
||||
apa_title = data['data'].first['v']
|
||||
when '20100430145309101' # 發表卷數
|
||||
jp.vol_no = data['data'].first['v']
|
||||
when '20100430145700542' # 出版年
|
||||
publication_year = data['data'].first['v']
|
||||
jp.year = publication_year
|
||||
when '20101119123019886' # 出版月
|
||||
publication_month = data['data'].first['v']
|
||||
when '20100430135842804' # 論文名稱
|
||||
jp.paper_title_translations = { 'zh_tw' => data['data'].first['v'], 'en' => data['data'].first['v'] }
|
||||
when '20100430135853270' # 期刊名稱
|
||||
jp.journal_title_translations = { 'zh_tw' => data['data'].first['v'], 'en' => data['data'].first['v'] }
|
||||
else
|
||||
puts "useless data #{data['eifSn']}"
|
||||
end
|
||||
end
|
||||
|
||||
if !invalid_apa_title.include? apa_title
|
||||
jp.delete
|
||||
jp = JournalPaper.new
|
||||
jp.paper_title_translations = { 'zh_tw' => apa_title, 'en' => apa_title }
|
||||
jp.year = publication_year
|
||||
end
|
||||
|
||||
jp.publication_date = "#{publication_year}/#{publication_month}/01"
|
||||
jp.member_profile_id = user.member_profile_id
|
||||
puts "journal paper #{jp.id} saved" if jp.save
|
||||
end
|
||||
end
|
||||
|
||||
# 研討會論文
|
||||
def sync_conference_papers(query_result, user)
|
||||
puts "Conference Paper for user: #{user.user_name}"
|
||||
conference_papers = query_result.select { |data| data['ER_ACCOUNT'] == user.user_name && data['EI_SN'] == '20100504082700357' }
|
||||
conference_papers.each do |conference_paper|
|
||||
j_data = JSON.parse(conference_paper['JDATA'])
|
||||
wc = WritingConference.new
|
||||
apa_title = nil
|
||||
invalid_apa_title = [nil, '', '必填欄位', '無', 'n/a', 'non']
|
||||
|
||||
j_data['data'].each do |data|
|
||||
case data['eifSn']
|
||||
when '20100504082730625' # 論文名稱
|
||||
wc.paper_title_translations = { 'zh_tw' => data['data'].first['v'], 'en' => data['data'].first['v'] }
|
||||
when '20100504082900819' # 會議名稱
|
||||
wc.conference_title_translations = { 'zh_tw' => data['data'].first['v'], 'en' => data['data'].first['v'] }
|
||||
when '20100504082910474' # 會議舉辦地點
|
||||
wc.location_translations = { 'zh_tw' => data['data'].first['v'], 'en' => data['data'].first['v'] }
|
||||
when '20110926150114944' # 主辦單位、支持者
|
||||
if !invalid_apa_title.include? data['data'].first['v']
|
||||
wc.sponsor_translations = { 'zh_tw' => data['data'].first['v'], 'en' => data['data'].first['v'] }
|
||||
end
|
||||
when '20100504082950644' # 使用語言
|
||||
wc.language = data['data'].first['v']
|
||||
when '20101012151243807' # 會議開始日期
|
||||
wc.period_start_date = data['data'].first['v']
|
||||
wc.year = wc.period_start_date.year
|
||||
when '20101012151318602' # 會議結束日期
|
||||
wc.period_end_date = data['data'].first['v']
|
||||
#when '20190109101534767' # 合著之國際學者姓名與機構
|
||||
# wc.authors = data['data'].first['v']
|
||||
when '20140313173831962' # 完整引文內容
|
||||
apa_title = data['data'].first['v']
|
||||
else
|
||||
puts "useless data #{data['eifSn']}"
|
||||
end
|
||||
end
|
||||
|
||||
if !invalid_apa_title.include? apa_title
|
||||
wc.delete
|
||||
wc = WritingConference.new
|
||||
wc.paper_title_translations = { 'zh_tw' => apa_title, 'en' => apa_title }
|
||||
end
|
||||
|
||||
wc.member_profile_id = user.member_profile_id
|
||||
puts "conference paper #{wc.id} saved" if wc.save
|
||||
end
|
||||
end
|
||||
|
||||
# 研究計畫
|
||||
def sync_projects(query_result, user)
|
||||
puts "Projects for user: #{user.user_name}"
|
||||
projects = query_result.select { |data| data['ER_ACCOUNT'] == user.user_name && data['EI_SN'] == '20101113165022345' }
|
||||
projects.each do |project|
|
||||
j_data = JSON.parse(project['JDATA'])
|
||||
pj = Project.new
|
||||
apa_title = nil
|
||||
invalid_values = [nil, '', '必填欄位', '無', 'n/a', 'non']
|
||||
|
||||
j_data['data'].each do |data|
|
||||
case data['eifSn']
|
||||
when '20101113165900450' # 計畫開始日期
|
||||
temp_date = Date.strptime(data['data'].first['v'], '%Y/%m/%d') rescue nil
|
||||
if temp_date.nil?
|
||||
pj.period_start_date = nil
|
||||
pj.year = nil
|
||||
else
|
||||
pj.period_start_date = temp_date
|
||||
pj.year = temp_date.year
|
||||
end
|
||||
when '20101113170021839' # 計畫結束日期
|
||||
temp_date = Date.strptime(data['data'].first['v'], '%Y/%m/%d') rescue nil
|
||||
if temp_date.nil?
|
||||
pj.period_end_date = nil
|
||||
pj.year = nil
|
||||
else
|
||||
pj.period_end_date = temp_date
|
||||
pj.year = temp_date.year
|
||||
end
|
||||
when '20101113165829608' # 計畫名稱
|
||||
pj.project_title_translations = { 'zh_tw' => data['data'].first['v'], 'en' => data['data'].first['v'] }
|
||||
when '20101113165400610' # 計畫委託單位
|
||||
pj.unit_translations = { 'zh_tw' => data['data'].first['v'], 'en' => data['data'].first['v'] }
|
||||
when '20101113170139811' # 參與計畫身份別
|
||||
pj.job_title_translations = { 'zh_tw' => data['data'].first['v'], 'en' => data['data'].first['v'] }
|
||||
when '20101113165807383' # 計畫編號,放到 note
|
||||
if !invalid_values.include? data['data'].first['v']
|
||||
pj.note = data['data'].first['v']
|
||||
end
|
||||
when '20101120133553203' # 協同執行計畫單位名稱
|
||||
if !invalid_values.include? data['data'].first['v']
|
||||
pj.participator_translations = { 'zh_tw' => data['data'].first['v'], 'en' => data['data'].first['v'] }
|
||||
end
|
||||
else
|
||||
puts "useless data #{data['eifSn']}"
|
||||
end
|
||||
end
|
||||
|
||||
pj.member_profile_id = user.member_profile_id
|
||||
puts "project #{pj.id} saved" if pj.save
|
||||
end
|
||||
end
|
||||
|
||||
# 學術論述專書, academic treatise books
|
||||
def sync_academic_treatise_books(query_result, user)
|
||||
puts "Academic treatise books for user: #{user.user_name}"
|
||||
book_type = BookType.where('title.en' => 'academic treatise books')
|
||||
books = query_result.select { |data| data['ER_ACCOUNT'] == user.user_name && data['EI_SN'] == '20101113154035616' }
|
||||
|
||||
return if books.empty?
|
||||
|
||||
books.each do |book|
|
||||
j_data = JSON.parse(book['JDATA'])
|
||||
bk = Book.new
|
||||
book_title = nil
|
||||
book_edition = nil
|
||||
publication_month = nil
|
||||
publication_year = nil
|
||||
publication_contry = nil
|
||||
publisher = nil
|
||||
invalid_values = [nil, '', '必填欄位', '無', 'n/a', 'non']
|
||||
|
||||
j_data['data'].each do |data|
|
||||
case data['eifSn']
|
||||
when '20101113155146878' # 出版年
|
||||
publication_year = data['data'].first['v']
|
||||
bk.year = data['data'].first['v']
|
||||
when '20101119131606987' # 出版月
|
||||
publication_month = data['data'].first['v']
|
||||
when '20101113154705945' # 專書名稱
|
||||
book_title = data['data'].first['v']
|
||||
when '20101117093151912' # 版(刷)次
|
||||
book_edition = data['data'].first['v']
|
||||
book_edition = 1 if invalid_values.include? book_edition
|
||||
when '20101113162540244' # 發行國家
|
||||
publication_contry = data['data'].first['v']
|
||||
when '20101113162459663' # 出版機關
|
||||
publisher = data['data'].first['v']
|
||||
when '20101113155314433' # ISBN 編號
|
||||
bk.isbn = data['data'].first['v']
|
||||
bk.isbn = '' if invalid_values.include? bk.isbn
|
||||
else
|
||||
puts "useless data #{data['eifSn']}"
|
||||
end
|
||||
end
|
||||
|
||||
full_title = book_title + "(#{book_edition})"
|
||||
bk.book_title_translations = { 'zh_tw' => full_title, 'en' => full_title }
|
||||
full_publisher = "#{publication_contry} : #{publisher}"
|
||||
bk.publisher_translations = { 'zh_tw' => full_publisher, 'en' => full_publisher }
|
||||
bk.publication_date = "#{publication_year}/#{publication_month}/01"
|
||||
bk.member_profile_id = user.member_profile_id
|
||||
bk.book_type_id = book_type.entries.first.id
|
||||
puts "academic treatise books #{bk.id} saved" if bk.save
|
||||
end
|
||||
end
|
||||
|
||||
# 學術專書單篇(章), academic book chapters
|
||||
def sync_academic_book_chapters(query_result, user)
|
||||
puts "Academic book chapters for user: #{user.user_name}"
|
||||
book_type = BookType.where('title.en' => 'academic book chapters')
|
||||
books = query_result.select { |data| data['ER_ACCOUNT'] == user.user_name && data['EI_SN'] == '20101113154120441' }
|
||||
|
||||
return if books.empty?
|
||||
|
||||
books.each do |book|
|
||||
j_data = JSON.parse(book['JDATA'])
|
||||
bk = Book.new
|
||||
book_title = nil
|
||||
chapter_title = nil
|
||||
chapter_pages = nil
|
||||
publication_month = nil
|
||||
publication_year = nil
|
||||
publication_contry = nil
|
||||
publisher = nil
|
||||
invalid_values = [nil, '', '必填欄位', '無', 'n/a', 'non']
|
||||
|
||||
j_data['data'].each do |data|
|
||||
case data['eifSn']
|
||||
when '20101113164018594' # 出版年
|
||||
publication_year = data['data'].first['v']
|
||||
bk.year = data['data'].first['v']
|
||||
when '20101120085632652' # 出版月
|
||||
publication_month = data['data'].first['v']
|
||||
when '20101113161735848' # 專書名稱
|
||||
book_title = data['data'].first['v']
|
||||
when '20101117100427736' # 篇(章)名稱
|
||||
chapter_title = data['data'].first['v']
|
||||
when '20111020085257865' # 篇(章)頁碼
|
||||
chapter_pages = data['data'].first['v']
|
||||
chapter_pages = 'pp ? - ?' if invalid_values.include? chapter_pages
|
||||
bk.pages = chapter_pages
|
||||
when '20101113164115903' # 發行國家
|
||||
publication_contry = data['data'].first['v']
|
||||
when '20101113164042969' # 出版機關
|
||||
publisher = data['data'].first['v']
|
||||
when '20101113162003149' # ISBN 編號
|
||||
bk.isbn = data['data'].first['v']
|
||||
bk.isbn = '' if invalid_values.include? bk.isbn
|
||||
else
|
||||
puts "useless data #{data['eifSn']}"
|
||||
end
|
||||
end
|
||||
|
||||
full_title = "#{chapter_title}。#{book_title}"
|
||||
bk.book_title_translations = { 'zh_tw' => full_title, 'en' => full_title }
|
||||
full_publisher = "#{publication_contry} : #{publisher}"
|
||||
bk.publisher_translations = { 'zh_tw' => full_publisher, 'en' => full_publisher }
|
||||
bk.publication_date = "#{publication_year}/#{publication_month}/01"
|
||||
bk.member_profile_id = user.member_profile_id
|
||||
bk.book_type_id = book_type.entries.first.id
|
||||
puts "academic treatise books #{bk.id} saved" if bk.save
|
||||
end
|
||||
end
|
||||
|
||||
# 正式出版之大專院校教科書, officially published university textbooks
|
||||
def sync_officially_published_university_textbooks(query_result, user)
|
||||
puts "Officially published university textbooks for user: #{user.user_name}"
|
||||
book_type = BookType.where('title.en' => 'officially published university textbooks')
|
||||
books = query_result.select { |data| data['ER_ACCOUNT'] == user.user_name && data['EI_SN'] == '20101117102418434' }
|
||||
|
||||
return if books.empty?
|
||||
|
||||
books.each do |book|
|
||||
j_data = JSON.parse(book['JDATA'])
|
||||
bk = Book.new
|
||||
book_title = nil
|
||||
book_edition = nil
|
||||
publication_month = nil
|
||||
publication_year = nil
|
||||
publication_contry = nil
|
||||
publisher = nil
|
||||
invalid_values = [nil, '', '必填欄位', '無', 'n/a', 'non']
|
||||
|
||||
j_data['data'].each do |data|
|
||||
case data['eifSn']
|
||||
when '20101117102419161' # 出版年
|
||||
publication_year = data['data'].first['v']
|
||||
bk.year = data['data'].first['v']
|
||||
when '20101120082104670' # 出版月
|
||||
publication_month = data['data'].first['v']
|
||||
when '20101117102418916' # 教科書名稱
|
||||
book_title = data['data'].first['v']
|
||||
when '20101117102419281' # 版(刷)次
|
||||
book_edition = data['data'].first['v']
|
||||
book_edition = 1 if invalid_values.include? book_edition
|
||||
when '20101117102420949' # 發行國家
|
||||
publication_contry = data['data'].first['v']
|
||||
when '20101117102419390' # 出版機關
|
||||
publisher = data['data'].first['v']
|
||||
when '20101117102421533' # ISBN 編號
|
||||
bk.isbn = data['data'].first['v']
|
||||
bk.isbn = '' if invalid_values.include? bk.isbn
|
||||
else
|
||||
puts "useless data #{data['eifSn']}"
|
||||
end
|
||||
end
|
||||
|
||||
full_title = "#{book_title} #{book_edition}"
|
||||
bk.book_title_translations = { 'zh_tw' => full_title, 'en' => full_title }
|
||||
full_publisher = "#{publication_contry} : #{publisher}"
|
||||
bk.publisher_translations = { 'zh_tw' => full_publisher, 'en' => full_publisher }
|
||||
bk.publication_date = "#{publication_year}/#{publication_month}/01"
|
||||
bk.member_profile_id = user.member_profile_id
|
||||
bk.book_type_id = book_type.entries.first.id
|
||||
puts "officially published university textbooks #{bk.id} saved" if bk.save
|
||||
end
|
||||
end
|
||||
|
||||
# 其他著作, other authored works
|
||||
def sync_other_authored_works(query_result, user)
|
||||
puts "Other authored works for user: #{user.user_name}"
|
||||
book_type = BookType.where('title.en' => 'other authored works')
|
||||
books = query_result.select { |data| data['ER_ACCOUNT'] == user.user_name && data['EI_SN'] == '20101117111154107' }
|
||||
|
||||
return if books.empty?
|
||||
|
||||
books.each do |book|
|
||||
j_data = JSON.parse(book['JDATA'])
|
||||
bk = Book.new
|
||||
book_title = nil
|
||||
book_edition = nil
|
||||
publication_month = nil
|
||||
publication_year = nil
|
||||
publication_contry = nil
|
||||
publisher = nil
|
||||
invalid_values = [nil, '', '必填欄位', '無', 'n/a', 'non']
|
||||
|
||||
j_data['data'].each do |data|
|
||||
case data['eifSn']
|
||||
when '20101117111154862' # 出版年
|
||||
publication_year = data['data'].first['v']
|
||||
bk.year = data['data'].first['v']
|
||||
when '20101120091544594' # 出版月
|
||||
publication_month = data['data'].first['v']
|
||||
when '20101117111154623' # 書名
|
||||
book_title = data['data'].first['v']
|
||||
when '20101117111154977' # 版(刷)次
|
||||
book_edition = data['data'].first['v']
|
||||
book_edition = 1 if invalid_values.include? book_edition
|
||||
when '20101117111156631' # 發行國家
|
||||
publication_contry = data['data'].first['v']
|
||||
when '20101117111154746' # 出版機關
|
||||
publisher = data['data'].first['v']
|
||||
when '20101117111157224' # ISBN 編號
|
||||
bk.isbn = data['data'].first['v']
|
||||
bk.isbn = '' if invalid_values.include? bk.isbn
|
||||
else
|
||||
puts "useless data #{data['eifSn']}"
|
||||
end
|
||||
end
|
||||
|
||||
full_title = book_title + "(#{book_edition})"
|
||||
bk.book_title_translations = { 'zh_tw' => full_title, 'en' => full_title }
|
||||
full_publisher = "#{publication_contry} : #{publisher}"
|
||||
bk.publisher_translations = { 'zh_tw' => full_publisher, 'en' => full_publisher }
|
||||
bk.publication_date = "#{publication_year}/#{publication_month}/01"
|
||||
bk.member_profile_id = user.member_profile_id
|
||||
bk.book_type_id = book_type.entries.first.id
|
||||
puts "other authored works #{bk.id} saved" if bk.save
|
||||
end
|
||||
end
|
||||
|
||||
# 建立預設著作類別
|
||||
def define_book_types
|
||||
|
||||
# 學術論述專書, academic treatise books
|
||||
if BookType.where('title.en' => 'academic treatise books').empty?
|
||||
bt_1 = BookType.new
|
||||
bt_1.title_translations = { en: 'academic treatise books', zh_tw: '學術論述專書' }
|
||||
puts '著作類別:學術論述專書, 建立成功' if bt_1.save
|
||||
end
|
||||
|
||||
# 學術專書單篇(章), academic book chapters
|
||||
if BookType.where('title.en' => 'academic book chapters').empty?
|
||||
bt_2 = BookType.new
|
||||
bt_2.title_translations = { en: 'academic book chapters', zh_tw: '學術專書單篇(章)' }
|
||||
puts '著作類別:學術專書單篇(章), 建立成功' if bt_2.save
|
||||
end
|
||||
|
||||
# 正式出版之大專院校教科書, officially published university textbooks
|
||||
if BookType.where('title.en' => 'officially published university textbooks').empty?
|
||||
bt_3 = BookType.new
|
||||
bt_3.title_translations = { en: 'officially published university textbooks', zh_tw: '正式出版之大專院校教科書' }
|
||||
puts '著作類別:正式出版之大專院校教科書, 建立成功' if bt_3.save
|
||||
end
|
||||
|
||||
# 其他著作, other authored works
|
||||
if BookType.where('title.en' => 'other authored works').empty?
|
||||
bt_4 = BookType.new
|
||||
bt_4.title_translations = { en: 'other authored works', zh_tw: '其他著作' }
|
||||
puts '著作類別:其他著作, 建立成功' if bt_4.save
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue