From d226b34de58e31e33bcb06db2b04c0750e187e16 Mon Sep 17 00:00:00 2001 From: saurabhbhatia Date: Thu, 21 Nov 2013 16:08:42 +0800 Subject: [PATCH] Added Automatic Synchronize Conflicts: Gemfile app/controllers/admin/import_data_controller.rb vendor/built_in_modules/personal_conference/app/models/writing_conference.rb Conflicts: Gemfile app/controllers/admin/import_data_controller.rb --- Gemfile | 1 + .../admin/import_data_controller.rb | 73 ++++++++----------- config/schedule.rb | 20 +++++ lib/tasks/synchronize.rake | 12 +++ 4 files changed, 65 insertions(+), 41 deletions(-) mode change 100644 => 100755 Gemfile create mode 100644 config/schedule.rb create mode 100644 lib/tasks/synchronize.rake 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