From f59452ffc4ffc2e58c4370142db4dcf9791fa930 Mon Sep 17 00:00:00 2001 From: saurabhbhatia Date: Thu, 21 Nov 2013 17:20:47 +0800 Subject: [PATCH] both manual and automatic methods working Conflicts: config/routes.rb --- .../admin/import_data_controller.rb | 206 +++++++++++++++++- config/mongoid.yml | 6 +- config/routes.rb | 13 ++ lib/tasks/synchronize.rake | 6 +- 4 files changed, 222 insertions(+), 9 deletions(-) diff --git a/app/controllers/admin/import_data_controller.rb b/app/controllers/admin/import_data_controller.rb index 5c141aa3..da66662e 100644 --- a/app/controllers/admin/import_data_controller.rb +++ b/app/controllers/admin/import_data_controller.rb @@ -258,7 +258,207 @@ class Admin::ImportDataController < OrbitBackendController end end - def sync_conference_data +def sync_conference_data + @conference_data = WritingConference.where(create_user_id: "#{params[:user_id]}") + @conference_data.delete_all + @user = User.find("#{params[:user_id]}") + + # teachers.each do |hash| + if @user.sid.present? + ntu_seq = @user.sid + conference_xml = Nokogiri::XML( \ + open("http://ann.cc.ntu.edu.tw/Achv/xmlPaper.asp?Seq=#{ntu_seq}&type=C")) + #open("http://versatile.management.ntu.edu.tw/publication1/conference/#{ntuseq}.xml")) + @conference_papers = conference_xml.xpath("//Paper").map do |cp_node| + { + author: (cp_node>"Authors").text, + year: (cp_node>"PublishYear").text, + title: (cp_node>"PaperTitle").text, + conference: (cp_node>"PublishOn").text, + date: Date::MONTHNAMES[(cp_node>"PublishMonth").text.to_i], + location: "#{(cp_node>"Country").text} #{(cp_node>"location").text}", + conference_title: (cp_node>"PublishOn").text, + remarks: (cp_node>"Remarks").text + } + end + if @conference_papers.present? + @conference_papers.each do |b| + @conference_paper = WritingConference.new + I18n.locale = :zh_tw + @conference_paper.authors = b[:author] + if b[:title].blank? + @conference_paper.paper_title = "No Title Present" + else + @conference_paper.paper_title = b[:title] + end + if b[:conference_title].blank? + @conference_paper.conference_title = "No Title Present" + else + @conference_paper.conference_title = b[:conference_title] + end + + @conference_paper.note = b[:remarks] + I18n.locale = :en + @conference_paper.authors = b[:author] + if b[:title].blank? + @conference_paper.paper_title = "No Title Present" + else + @conference_paper.paper_title = b[:title] + end + if b[:conference_title].blank? + @conference_paper.conference_title = "No Title Present" + else + @conference_paper.conference_title = b[:conference_title] + end + @conference_paper.note = b[:remarks] + + @conference_paper.location = b[:location] + @conference_paper.year = b[:year] + @conference_paper.create_user_id = @user.id + + @conference_paper.save! + end + else + puts "No conference by Teacher" + end + end + respond_to do |format| + + format.html + format.json { render json: {"success"=>true}.to_json} + + end + end + + + def sync_journal_paper_data + @journal_data = WritingJournal.where(create_user_id: "#{params[:user_id]}") + @journal_data.delete_all + @user = User.find("#{params[:user_id]}") + + # teachers.each do |hash| + if @user.sid.present? + ntu_seq = @user.sid + paper_xml = Nokogiri::XML( \ + #open("http://versatile.management.ntu.edu.tw/publication1/journal/#{ntuseq}.xml")) + open("http://ann.cc.ntu.edu.tw/Achv/xmlPaper.asp?Seq=#{ntu_seq}&type=J")) + @journal_papers = paper_xml.xpath("//Paper").map do |paper_node| + { + author: (paper_node>"Authors").text, + year: (paper_node>"PublishYear").text, + title: (paper_node>"PaperTitle").text, + journal: (paper_node>"PublishOn").text, + volume:(paper_node>"Volume").text, + volumeno:(paper_node>"VolumeNo").text, + beginpage:(paper_node>"BeginPage").text, + endpage:(paper_node>"EndPage").text, + subgroup:(paper_node>"subgroup").text, + remarks: (paper_node>"Remarks").text, + cate: ((paper_node>"subgroup")>"Group").text + } + end + if @journal_papers.present? + @journal_papers.each do |b| + @journal_paper = WritingJournal.new + I18n.locale = :zh_tw + @journal_paper.authors = b[:author] + + if b[:title].blank? + @journal_paper.paper_title = "No Title Present" + else + @journal_paper.paper_title = b[:title] + end + + @journal_paper.journal_title = b[:journal] + @journal_paper.note = b[:remarks] + I18n.locale = :en + @journal_paper.authors = b[:author] + + if b[:title].blank? + @journal_paper.paper_title = "No Title Present" + else + @journal_paper.paper_title = b[:title] + end + + @journal_paper.journal_title = b[:journal] + @journal_paper.note = b[:remarks] + @journal_paper.year = b[:year] + @journal_paper.vol_no = b[:volumeno] + @journal_paper.form_to_start = b[:beginpage] + @journal_paper.form_to_end = b[:endpage] + + if !b[:cate].blank? + @level_type = JournalLevelType.where(:key => b[:cate]) + if @level_type.present? + @journal_paper.journal_level_type_ids = ["#{@level_type.first.id}"] + end + end + + @journal_paper.create_user_id = @user.id + @journal_paper.save! + end + end + + end + respond_to do |format| + + format.html + format.json { render json: {"success"=>true}.to_json} + + end + end + +def sync_book_data + @books_data = WritingBook.where(create_user_id: "#{params[:user_id]}") + @books_data.delete_all + @user = User.find("#{params[:user_id]}") + + # teachers.each do |hash| + if @user.sid.present? + ntu_seq = @user.sid + books_xml = Nokogiri::XML( \ + open("http://ann.cc.ntu.edu.tw/Achv/xmlBook.asp?Seq=#{ntu_seq}")) + @books = books_xml.xpath("//Book").map do |book_node| + { + author: (book_node>"Authors").text, + year: (book_node>"PublishYear").text, + title: (book_node>"DocTitle").text, + remarks: (book_node>"Remarks").text, + publisher: (book_node>"Publisher").text, + book_title: (book_node>"BookTitle").text + } + end + if @books.present? + @books.each do |b| + @book = WritingBook.new + I18n.locale = :zh_tw + @book.authors = b[:author] + @book.paper_title = b[:title] + @book.book_title = b[:book_title] + @book.note = b[:remarks] + @book.publisher = b[:publisher] + I18n.locale = :en + @book.authors = b[:author] + @book.paper_title = b[:title] + @book.book_title = b[:book_title] + @book.note = b[:remarks] + @book.publisher = b[:publisher] + @book.year = b[:year] + @book.create_user_id = @user.id + @book.save + end + end + end + respond_to do |format| + + format.html + format.json { render json: {"success"=>true}.to_json} + + end +end + + + def sync_conference_data_auto @user = User.all @user.each do |user| @conference_data = WritingConference.where(create_user_id: "#{user.id}") @@ -328,7 +528,7 @@ class Admin::ImportDataController < OrbitBackendController end -def sync_journal_paper_data +def sync_journal_paper_data_auto @user = User.all @user.each do |user| @journal_data = WritingJournal.where(create_user_id: "#{user_id}") @@ -402,7 +602,7 @@ def sync_journal_paper_data end end -def sync_book_data +def sync_book_data_auto @user = User.all @user.each do |user| @books_data = WritingBook.where(create_user_id: "#{user_id}") diff --git a/config/mongoid.yml b/config/mongoid.yml index 0b1b1cfc..e28c2e8a 100644 --- a/config/mongoid.yml +++ b/config/mongoid.yml @@ -8,13 +8,13 @@ defaults: &defaults development: <<: *defaults - database: orbit_site_new + database: ntu_mb_orbit_2_new test: <<: *defaults - database: orbit_site_new + database: ntu_mb_orbit_2_new # set these environment variables on your prod server production: @@ -24,4 +24,4 @@ production: # password: <%= ENV['MONGOID_PASSWORD'] %> # database: <%= ENV['MONGOID_DATABASE'] %> <<: *defaults - database: orbit_site_new + database: ntu_mb_orbit_2_new diff --git a/config/routes.rb b/config/routes.rb index 3599ab72..14a62ac3 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -218,6 +218,19 @@ Orbit::Application.routes.draw do get 'update_orbit' get 'restart_server' end + + get "import_data/get_teacher_data" + get "import_data/get_book_data" + get "import_data/get_conference_data" + get "import_data/get_journal_paper_data" + get "import_data/get_announcement_data" + get "import_data/get_page_data" + get "import_data/sync_book_data" + get "import_data/sync_journal_paper_data" + get "import_data/sync_conference_data" + get "import_data/sync_book_data_auto" + get "import_data/sync_journal_paper_data_auto" + get "import_data/sync_conference_data_auto" resources :tags do collection do diff --git a/lib/tasks/synchronize.rake b/lib/tasks/synchronize.rake index 8b5e58bd..15df7f6f 100644 --- a/lib/tasks/synchronize.rake +++ b/lib/tasks/synchronize.rake @@ -2,11 +2,11 @@ namespace :synchronize do desc "synchronize tasks" task :data => :environment do - p Admin::ImportDataController.new.sync_book_data + p Admin::ImportDataController.new.sync_book_data_auto puts "Book Data Synchronization Complete" - p Admin::ImportDataController.new.sync_journal_paper_data + p Admin::ImportDataController.new.sync_journal_paper_data_auto puts "Journal Paper Data Synchronization Complete" - p Admin::ImportDataController.new.sync_conference_data + p Admin::ImportDataController.new.sync_conference_data_auto puts "Conference Data Synchronization Complete" end end \ No newline at end of file