diff --git a/app/builders/learning_object_builder.rb b/app/builders/learning_object_builder.rb index 9428e079637a53b7c2319fc4cb9167dba42f4f78..8b5dcc077c5455bcf15599e20bc2a07bf3f73151 100644 --- a/app/builders/learning_object_builder.rb +++ b/app/builders/learning_object_builder.rb @@ -18,7 +18,7 @@ # along with portalmec. If not, see <http://www.gnu.org/licenses/>. class LearningObjectBuilder - def self.build_from_dspace(item) + def self.build_from_dspace(item, publisher) lo = LearningObject.new( name: item.name, id_dspace: item.id, @@ -27,16 +27,18 @@ class LearningObjectBuilder ) lo.curator = lo.get_metadata_value_of('dc.curator') - institution = lo.get_metadata_value_of('dc.creator') - institution = 'Desconhecido' if institution.blank? - lo.publisher = Institution.where(name: institution).first_or_create + # institution = lo.get_metadata_value_of('dc.creator') + # institution = 'Desconhecido' if institution.blank? + # lo.publisher = Institution.where(name: institution).first_or_create + lo.publisher = publisher object_type = lo.get_metadata_value_of('dc.type') object_type = 'Desconhecido' if object_type.blank? lo.object_type = ObjectType.where(name: object_type).first_or_create - date = lo.get_metadata_value_of('dc.date.issued') - lo.published_at = Time.iso8601(date) unless date.nil? + # date = lo.get_metadata_value_of('dc.date.issued') + # lo.published_at = Time.iso8601(date) unless date.nil? + lo.published_at = Time.current code = lo.get_metadata_value_of('dc.language') code = 'pt' if code.blank? diff --git a/app/mailers/dspace_mailer.rb b/app/mailers/dspace_mailer.rb new file mode 100644 index 0000000000000000000000000000000000000000..9226aabf5116fbdb893e3c020f8de7e14b6a4024 --- /dev/null +++ b/app/mailers/dspace_mailer.rb @@ -0,0 +1,27 @@ + +# Copyright (C) 2015 Centro de Computacao Cientifica e Software Livre +# Departamento de Informatica - Universidade Federal do Parana +# +# This file is part of portalmec. +# +# portalmec is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# portalmec is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with portalmec. If not, see <http://www.gnu.org/licenses/>. + +class DspaceMailer < ApplicationMailer + default to: 'portalmec_tec@inf.ufpr.br' + + def dspace_info_updated(partner) + @partner = partner + mail(subject: "Dados para OAI harvest atualizados") + end +end diff --git a/app/models/learning_object.rb b/app/models/learning_object.rb index 9ecc54788ed67deb8f4908dab9c6fd9db689d4e1..547636e65042441f8d31476de95badba19596e55 100644 --- a/app/models/learning_object.rb +++ b/app/models/learning_object.rb @@ -143,6 +143,7 @@ class LearningObject < ApplicationRecord return thumbnail unless thumbnail.blank? return nil if attachments.blank? return default_attachment.thumbnail unless default_attachment.thumbnail.blank? + ThumbnailGenerateWorker.perform_async(default_attachment.id) attachments.each { |a| return a.thumbnail unless a.thumbnail.blank? } nil end diff --git a/app/models/user.rb b/app/models/user.rb index a8befa530f2b3f6cdace98f1372f7285289fee5e..015197f65bcf1e091123bb93ac0b8376decbaf67 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -88,6 +88,7 @@ class User < ApplicationRecord has_many :applications after_create :default_role + after_save :verify_dspace_info has_attached_file :avatar, styles: { medium: '300x300>', thumb: '60x60>' }, default_url: '' validates_attachment_content_type :avatar, content_type: ['image/jpg', 'image/jpeg', 'image/png', 'image/gif'] @@ -274,6 +275,12 @@ class User < ApplicationRecord ) end + def verify_dspace_info + if roles.include?(Role.find_by_name("partner")) && !(changed & ["dspace_url", "dspace_handle", "dspace_sets"]).empty? + DspaceMailer.dspace_info_updated(self).deliver_now + end + end + def activity_owner self end @@ -284,7 +291,7 @@ class User < ApplicationRecord def update_tracked_fields(request) super - # Change request.remote_ip to req.env["HTTP_X_REAL_IP"] in production + # Change request.remote_ip to request.env["HTTP_X_REAL_IP"] in production self.current_sign_in_ip = request.remote_ip end end diff --git a/app/services/dspace/learning_object_importer.rb b/app/services/dspace/learning_object_importer.rb index 1031196614c5063d63938a139ae0e6b19cc15cf4..5b200c69d7502611eb610f4a3a697d9e6dab02a8 100644 --- a/app/services/dspace/learning_object_importer.rb +++ b/app/services/dspace/learning_object_importer.rb @@ -19,8 +19,9 @@ class Dspace::LearningObjectImporter - def initialize(items) + def initialize(items, publisher) @items = items + @publisher = publisher end def import @@ -28,14 +29,14 @@ class Dspace::LearningObjectImporter @items.each do |item| next if item_exists? item - learning_object = LearningObjectBuilder.build_from_dspace(item) + learning_object = LearningObjectBuilder.build_from_dspace(item, @publisher) next if learning_object.blank? create_attachments(learning_object, item.bit_streams) items << learning_object yield(learning_object) if block_given? end - LearningObject.import items, recursive: true + LearningObject.import items, recursive: true unless items.empty? end private @@ -44,6 +45,7 @@ class Dspace::LearningObjectImporter bitstreams = JSON.parse bitstreams if bitstreams.class == String bitstreams.each do |bitstream| learning_object.attachments.new ::Dspace::AttachmentMapper.call(bitstream) + ThumbnailGenerateWorker.perform_async(learning_object.attachments.last) end end diff --git a/app/services/learning_object_publisher.rb b/app/services/learning_object_publisher.rb index 5b4554333f16e0b4d83fba9401fdca7b891829f4..4d1b439ee894915efc72effd817224deed32d863 100644 --- a/app/services/learning_object_publisher.rb +++ b/app/services/learning_object_publisher.rb @@ -51,7 +51,11 @@ class LearningObjectPublisher learning_object.state = LearningObject.states[:published] learning_object.published_at = Time.current - ThumbnailGenerateWorker.perform_async learning_object.id, learning_object.link if learning_object.link? + if learning_object.link? + att = LearningObject::Attachment.new + ThumbnailGenerateWorker.perform_async(att.id, learning_object.link) + end + # ThumbnailGenerateWorker.perform_async learning_object.id, learning_object.link if learning_object.link? learning_object.save end diff --git a/app/views/dspace_mailer/dspace_info_updated.erb b/app/views/dspace_mailer/dspace_info_updated.erb new file mode 100644 index 0000000000000000000000000000000000000000..a99129bae771d75b6493245cf9e1978bea1621c9 --- /dev/null +++ b/app/views/dspace_mailer/dspace_info_updated.erb @@ -0,0 +1,14 @@ +<!DOCTYPE html> +<html> + <head> + <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> + <style>/* Email styles need to be inline */</style> + </head> + <body> + <h2>ID do usuário: <%= @partner.id %></h1> + <h2>Nome do usuário: <%= @partner.name %></h1> + <h2>URL do DSpace: <%= @partner.dspace_url %></h1> + <h2>Handle: <%= @partner.dspace_url %></h1> + <h2>Sets: <%= @partner.dspace_sets.join(",") %></h1> + </body> +</html> diff --git a/lib/tasks/import/learning_objects.rake b/lib/tasks/import/learning_objects.rake index 0b9ae5583f74e64751ec058249810c320d493775..9b0947086efb3161a02f3fa00143c4f6951f7b46 100644 --- a/lib/tasks/import/learning_objects.rake +++ b/lib/tasks/import/learning_objects.rake @@ -17,9 +17,51 @@ # You should have received a copy of the GNU Affero General Public License # along with portalmec. If not, see <http://www.gnu.org/licenses/>. +require 'openssl' + namespace :import do desc 'import learning objects from dspace' + task :sync_learning_objects => :environment do + desc 'Synchronize Learning Objects with DSpace Items' + OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE + + # IDEA: create DSpace collections once dspace_sets are updated + # and then search for items based on the collection id + # TODO: Deal with already existing item/LO + # TODO: Deal with deleted items + params = { + q: "search.resourcetype:2", # AND location.coll:4 + fq: "dc.date.accessioned_dt:[NOW-1DAY TO NOW]", + wt: "json", + indent: "true" + } + uri = URI(DspaceService.link + "/solr/search/select") + + users = User.joins(:roles).where(roles: {name: "partner"}).where.not(dspace_url: [nil, ""], dspace_handle: [nil, ""]) + users.each do |user| + user_params = params + user_params[:q] += " AND handle: #{user.dspace_handle}/*" + + uri.query = URI.encode_www_form(user_params) + response = JSON.parse(Net::HTTP.get(uri)) + items = [] + response["response"]["docs"].each do |doc| + item = dspace_client.items.find( + id: doc["search.resourceid"], + expand: "metadata,bitstreams,parentCollection" + ) + if items.is_a? String + logger.warn "Received a string instead of item: #{item}" + next + end + items << item + end + importer = Dspace::LearningObjectImporter.new(items, user) + importer.import + end + end + task :learning_object, [:log] => [:environment] do |_t, args| desc 'Importing Learning Objects from Dspace items'