diff --git a/Gemfile b/Gemfile old mode 100644 new mode 100755 index 80d2db1c..1d524caf --- a/Gemfile +++ b/Gemfile @@ -20,6 +20,7 @@ gem "acts_as_unvlogable" gem 'youtube_it' gem 'gotcha' gem "geocoder" +gem 'whenever', :require => false # gem "memcached", "~> 1.4.3" # gem "memcache-client" diff --git a/app/controllers/admin/import_data_controller.rb b/app/controllers/admin/import_data_controller.rb index c09121a0..5c141aa3 100644 --- a/app/controllers/admin/import_data_controller.rb +++ b/app/controllers/admin/import_data_controller.rb @@ -76,8 +76,8 @@ class Admin::ImportDataController < OrbitBackendController teachers = User.all teachers.each do |hash| - if hash.sid.present? - ntu_seq = hash.sid + if hash.ntu_seq.present? + ntu_seq = hash.ntu_seq 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| @@ -125,8 +125,8 @@ class Admin::ImportDataController < OrbitBackendController teachers = User.all teachers.each do |hash| - if hash.sid.present? - ntu_seq = hash.sid + if hash.ntu_seq.present? + ntu_seq = hash.ntu_seq 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")) @@ -196,8 +196,8 @@ class Admin::ImportDataController < OrbitBackendController teachers = User.all teachers.each do |hash| - if hash.sid.present? - ntu_seq = hash.sid + if hash.ntu_seq.present? + ntu_seq = hash.ntu_seq 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")) @@ -259,13 +259,15 @@ class Admin::ImportDataController < OrbitBackendController end def sync_conference_data - @conference_data = WritingConference.where(create_user_id: "#{params[:user_id]}") + @user = User.all + @user.each do |user| + @conference_data = WritingConference.where(create_user_id: "#{user.id}") @conference_data.delete_all - @user = User.find("#{params[:user_id]}") + # @user = User.find("#{params[:user_id]}") # teachers.each do |hash| - if @user.sid.present? - ntu_seq = @user.sid + 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")) @@ -314,7 +316,7 @@ class Admin::ImportDataController < OrbitBackendController @conference_paper.location = b[:location] @conference_paper.year = b[:year] - @conference_paper.create_user_id = @user.id + @conference_paper.create_user_id = user.id @conference_paper.save! end @@ -322,23 +324,20 @@ class Admin::ImportDataController < OrbitBackendController puts "No conference by Teacher" end end - respond_to do |format| - - format.html - format.json { render json: {"success"=>true}.to_json} - - end + end end - def sync_journal_paper_data - @journal_data = WritingJournal.where(create_user_id: "#{params[:user_id]}") +def sync_journal_paper_data + @user = User.all + @user.each do |user| + @journal_data = WritingJournal.where(create_user_id: "#{user_id}") @journal_data.delete_all - @user = User.find("#{params[:user_id]}") + # @user = User.find("#{params[:user_id]}") # teachers.each do |hash| - if @user.sid.present? - ntu_seq = @user.sid + 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")) @@ -394,28 +393,25 @@ class Admin::ImportDataController < OrbitBackendController end end - @journal_paper.create_user_id = @user.id + @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 + end +end def sync_book_data - @books_data = WritingBook.where(create_user_id: "#{params[:user_id]}") + @user = User.all + @user.each do |user| + @books_data = WritingBook.where(create_user_id: "#{user_id}") @books_data.delete_all - @user = User.find("#{params[:user_id]}") + # @user = User.find("#{params[:user_id]}") # teachers.each do |hash| - if @user.sid.present? - ntu_seq = @user.sid + 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| @@ -444,18 +440,13 @@ def sync_book_data @book.note = b[:remarks] @book.publisher = b[:publisher] @book.year = b[:year] - @book.create_user_id = @user.id + @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 +end def get_announcement_data diff --git a/config/schedule.rb b/config/schedule.rb new file mode 100644 index 00000000..bfefe33b --- /dev/null +++ b/config/schedule.rb @@ -0,0 +1,20 @@ +# Use this file to easily define all of your cron jobs. +# +# It's helpful, but not entirely necessary to understand cron before proceeding. +# http://en.wikipedia.org/wiki/Cron + +# Example: +# +# set :output, "/path/to/my/cron_log.log" +# + every 1.day, :at => '11:00 pm' do +# command "/usr/bin/some_great_command" +# runner "MyModel.some_method" + rake "rake synchronize:data" + end +# +# every 4.days do +# runner "AnotherModel.prune_old_records" +# end + +# Learn more: http://github.com/javan/whenever diff --git a/lib/tasks/synchronize.rake b/lib/tasks/synchronize.rake new file mode 100644 index 00000000..8b5e58bd --- /dev/null +++ b/lib/tasks/synchronize.rake @@ -0,0 +1,12 @@ +# encoding: utf-8 +namespace :synchronize do + desc "synchronize tasks" + task :data => :environment do + p Admin::ImportDataController.new.sync_book_data + puts "Book Data Synchronization Complete" + p Admin::ImportDataController.new.sync_journal_paper_data + puts "Journal Paper Data Synchronization Complete" + p Admin::ImportDataController.new.sync_conference_data + puts "Conference Data Synchronization Complete" + end +end \ No newline at end of file