Skip to content
Snippets Groups Projects
Commit 923eba4f authored by Israel Barreto Sant'Anna's avatar Israel Barreto Sant'Anna
Browse files

Fixed language error when importing. Fixed attachment link for ORE bitstreams

parent 2a6f3f11
No related branches found
No related tags found
No related merge requests found
...@@ -75,7 +75,7 @@ class LearningObjectBuilder ...@@ -75,7 +75,7 @@ class LearningObjectBuilder
language = Language.where(code: code).first language = Language.where(code: code).first
language = Language.where(code: "Outro").first if language.nil? language = Language.where(code: "Outro").first if language.nil?
# language = Language.create(name: code, code: code) if language.nil? # language = Language.create(name: code, code: code) if language.nil?
lo.language = language lo.language << language
lo.author = lo.get_metadata_values_of('dc.contributor.author').join(', ') lo.author = lo.get_metadata_values_of('dc.contributor.author').join(', ')
lo.author = publisher.name if lo.author.blank? lo.author = publisher.name if lo.author.blank?
...@@ -83,7 +83,7 @@ class LearningObjectBuilder ...@@ -83,7 +83,7 @@ class LearningObjectBuilder
lo.link = lo.get_metadata_value_of('dc.relation.uri') lo.link = lo.get_metadata_value_of('dc.relation.uri')
lo.state = LearningObject.states[:published] # lo.state = LearningObject.states[:published]
lo lo
end end
......
...@@ -29,7 +29,7 @@ ...@@ -29,7 +29,7 @@
# #
class Language < ApplicationRecord class Language < ApplicationRecord
has_many :learning_objects has_and_belongs_to_many :learning_objects
validates_uniqueness_of :code validates_uniqueness_of :code
validates_presence_of :name, :code validates_presence_of :name, :code
......
...@@ -81,7 +81,8 @@ class LearningObject < ApplicationRecord ...@@ -81,7 +81,8 @@ class LearningObject < ApplicationRecord
belongs_to :attachment, class_name: 'LearningObject::Attachment' belongs_to :attachment, class_name: 'LearningObject::Attachment'
has_one :submission, dependent: :destroy has_one :submission, dependent: :destroy
validates :name, :publisher, :object_type, :language, :author, unless: :draft?, presence: true validates :name, :publisher, :object_type, :author, unless: :draft?, presence: true
validates :language, unless: :draft?, :length => { :minimum => 1 }
validates :id_dspace, presence: true, uniqueness: true, unless: :published? validates :id_dspace, presence: true, uniqueness: true, unless: :published?
before_destroy :delete_index before_destroy :delete_index
...@@ -154,8 +155,8 @@ class LearningObject < ApplicationRecord ...@@ -154,8 +155,8 @@ class LearningObject < ApplicationRecord
at = attachments.where("name NOT LIKE '%.torrent' AND bundle_name = 'ORIGINAL'") at = attachments.where("name NOT LIKE '%.torrent' AND bundle_name = 'ORIGINAL'")
# at = attachments.first if at.nil? # at = attachments.first if at.nil?
unless at.blank? unless at.blank?
update(attachment: at.first) update(attachment: at.first)
return attachment return attachment
end end
end end
nil nil
......
...@@ -50,6 +50,7 @@ class LearningObject::Attachment < ApplicationRecord ...@@ -50,6 +50,7 @@ class LearningObject::Attachment < ApplicationRecord
def retrieve_url def retrieve_url
return retrieve_link if bundle_name == "ORE"
"#{DspaceService.link}/rest#{retrieve_link}" "#{DspaceService.link}/rest#{retrieve_link}"
end end
......
...@@ -19,38 +19,82 @@ ...@@ -19,38 +19,82 @@
class Dspace::LearningObjectImporter class Dspace::LearningObjectImporter
def initialize(items, publisher) def initialize(items, publisher, dspace_client)
@items = items @items = items
@publisher = publisher @publisher = publisher
@dspace_client = dspace_client
end end
def import def import
items = [] learning_objects = []
languages = []
@items.each do |item| @items.each do |item|
next if item_exists? item next if item_exists? item
learning_object = LearningObjectBuilder.build_from_dspace(item, @publisher) learning_object = LearningObjectBuilder.build_from_dspace(item, @publisher)
next if learning_object.blank? next if learning_object.blank?
create_attachments(learning_object, item.bit_streams) create_attachments(learning_object, item.bit_streams)
items << learning_object learning_objects << learning_object
languages << learning_object.language
yield(learning_object) if block_given? yield(learning_object) if block_given?
end end
LearningObject.import items, recursive: true unless items.empty? unless learning_objects.empty?
result = LearningObject.import learning_objects, recursive: true
result.failed_instances.each do |failure|
p "IMPORT ERRORS:"
p failure.errors
end
learning_objects.each_with_index do |lo, i|
lo.language = languages[i]
lo.state = LearningObject.states[:published]
lo.save!
end
end
end end
private private
def create_attachments(learning_object, bitstreams) def create_attachments(learning_object, bitstreams)
bitstreams = JSON.parse bitstreams if bitstreams.class == String bitstreams = JSON.parse bitstreams if bitstreams.class == String
has_thumbnail = false
bitstreams.each do |bitstream| bitstreams.each do |bitstream|
learning_object.attachments.new ::Dspace::AttachmentMapper.call(bitstream) att = learning_object.attachments.new(::Dspace::AttachmentMapper.call(bitstream))
ThumbnailGenerateWorker.perform_async(learning_object.attachments.last) att.retrieve_link = original_link(att) if att.bundle_name == "ORE"
if !has_thumbnail
# if attachment.bundle_name == "THUMBNAIL"
# get_thumbnail(att)
# else
ThumbnailGenerateWorker.perform_async(att)
has_thumbnail = true
end
end end
end end
def original_link(attachment)
file = @dspace_client.bitstreams.retrieve(id: attachment.id_dspace).open
xml = Hash.from_xml(file.read)
link = xml["entry"]["link"].select {|i| i["href"].include? "bitstream"}[0]["href"]
File.unlink(file) if File.exist?(file.path)
link
end
def item_exists?(item) def item_exists?(item)
LearningObject.where(id_dspace: item.id).exists? LearningObject.where(id_dspace: item.id).exists?
end end
# def get_thumbnail(attachment)
# file = @dspace_client.bitstreams.retrieve(id: attachment.id_dspace).open
#
# thumbnail_service = ThumbnailService.new([Thumbnail::Strategies::ImageThumbnailGenerator.new])
# thumbnail = thumbnail_service.create_thumbnail(file)
# attachment.update(thumbnail: thumbnail)
# end
# def update(learning_object_og, new_learning_object)
# changes = new_learning_object.dup.delete_if {|k, v| learning_object_og[k] == v}.merge!(learning_object_og.dup.delete_if {|k,v| new_learning_object.has_key?(k)})
# end
end end
...@@ -26,8 +26,6 @@ namespace :import do ...@@ -26,8 +26,6 @@ namespace :import do
desc 'Synchronize Learning Objects with DSpace Items' desc 'Synchronize Learning Objects with DSpace Items'
OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE 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 already existing item/LO
# TODO: Deal with deleted items # TODO: Deal with deleted items
params = { params = {
...@@ -57,12 +55,10 @@ namespace :import do ...@@ -57,12 +55,10 @@ namespace :import do
logger.warn "Received a string instead of item: #{item}" logger.warn "Received a string instead of item: #{item}"
next next
end end
items << item items << item
end end
importer = Dspace::LearningObjectImporter.new(items, user) importer = Dspace::LearningObjectImporter.new(items, user, dspace_client)
importer.import importer.import
end end
end end
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment