From 1bfd12be7dfdbda19a0f78bebd6a96b99b78cdea Mon Sep 17 00:00:00 2001 From: Giovanne Marcelo <gms15@inf.ufpr.br> Date: Wed, 16 Mar 2016 12:08:32 -0300 Subject: [PATCH] Adding institution importer --- app/services/institution_importer.rb | 36 +++++++++++++++++++++++ lib/tasks/import.rake | 43 ++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+) create mode 100644 app/services/institution_importer.rb diff --git a/app/services/institution_importer.rb b/app/services/institution_importer.rb new file mode 100644 index 000000000..32bda2383 --- /dev/null +++ b/app/services/institution_importer.rb @@ -0,0 +1,36 @@ +class InstitutionImporter + + attr_accessor :items + + def initialize + @names = [] + @learning_objects = [] + end + + def import + @items.each do |learning_object| + publisher = learning_object.get_metadata_value_of 'dc.creator' + if publisher.nil? + institution = Institution.where(name: "Banco Internacional de Objetos Educacionais").first_or_create + elsif !institution_exists?(publisher) + institution = Institution.new(name: publisher) + institution.save + @names << publisher + end + + learning_object.publisher = institution + learning_object.save + end + end + + private + + def institution_exists?(publisher) + exists = @names.include? publisher + unless exists + exists = Institution.exists?(name: publisher) + @names << publisher if exists + end + exists + end +end diff --git a/lib/tasks/import.rake b/lib/tasks/import.rake index 235f4e66d..4ca7a227e 100644 --- a/lib/tasks/import.rake +++ b/lib/tasks/import.rake @@ -45,6 +45,49 @@ namespace :import do logger.close end + task :institution, [:log] => [:environment] do |t, args| + desc "Create Publisher relations based on its metadata" + include Log::Logging + + args.with_defaults(:log => STDOUT) + + Log::Logging.logger = Log::DatabaseLogger.new(args.log) + logger.level = Log::DatabaseLogger::CREATE + + + # Quantity of items fetched on each iteration + limit = 500 + # Start point from where items will be fetched + offset = 0 + importer = InstitutionImporter.new + + loop do + logger.info " --> Creating Publisher relations from #{offset} to #{offset+limit}" + + begin + # Get learning_objects (from offset to offset+limit) + learning_objects = LearningObject.limit(limit).offset(offset) + + rescue => e + logger.warn "Database error, going to sleep" + logger.error e + # Sleeps for a while to wait database's recovery + sleep(10.seconds) + # Goes to next iteration to retry + next + else + # Terminate loop if there are no more learning_objects to import + break if learning_objects.empty? + + importer.items = learning_objects + importer.import + + end + offset += limit + end + logger.close + end + task :learning_objects, [:log] => [:environment] do |t, args| desc "Importing Learning Objects from Dspace items" -- GitLab