diff --git a/app/controllers/learning_objects_controller.rb b/app/controllers/learning_objects_controller.rb index a230f0950f0d4eea3d7cc0eaa5629c4e40300ccd..7573b7e24c27d66c89d8ebd986190b5c6247e147 100644 --- a/app/controllers/learning_objects_controller.rb +++ b/app/controllers/learning_objects_controller.rb @@ -3,7 +3,7 @@ class LearningObjectsController < ApplicationController after_action :increment_learning_object_views, only: [:show] before_action :authenticate_user!, except: [:index, :show] before_action :set_complaint_messages, only: :show - + before_action :set_dspace_collection, :create_repositories, :process_params, :get_selected_subjects, only: :create # GET /learning_objects # GET /learning_objects.json def index @@ -30,22 +30,18 @@ class LearningObjectsController < ApplicationController # POST /learning_objects # POST /learning_objects.json def create - # Create struct with id of the collection - collection_struct = Struct.new(:id) - collection = collection_struct.new('4') - repo = Dspace::Client.instance.repository.collection_repository - strategy = DSpaceRest::Strategies::Uploads::CurlStrategy.new(repo.rest_client.url,repo.rest_client.options[:headers][:rest_dspace_token]) - - # Manage the attributes necessary to create a new Item - args = {} - args['name'] = learning_object_params[:name] - args['type'] = learning_object_params[:type] - args['last_modified'] = Time.now.to_s - args['metadata'] = [] - params[:learning_object][:date_created] = Time.now - params[:learning_object][:date_available] = Time.now - params[:learning_object][:type] = get_file_type params[:learning_object][:file] - lo = DSpaceRest::Item.new(args) + strategy = DSpaceRest::Strategies::Uploads::CurlStrategy.new( + @col_repository.rest_client.url, + @col_repository.rest_client.options[:headers][:rest_dspace_token] + ) + + # Create the item inside Dspace + lo = DSpaceRest::Item.new({ + "name"=> learning_object_params[:name], + "type"=> learning_object_params[:type], + "last_modified"=> Time.now.to_s, + "metadata"=> [] + }) dspace_keys = get_dspace_metadata_names("invert") params[:learning_object].each do |k,v| unless dspace_keys[k].nil? @@ -58,30 +54,22 @@ class LearningObjectsController < ApplicationController end end end - response = repo.create_item_for(collection, lo) - # Now upload the file to the create item in DSpace + response = @col_repository.create_item_for(@collection, lo) + + + # Now upload the file to the created item in DSpace file = params[:learning_object][:file] bitstream_response = {} - repo = Dspace::Client.instance.repository.item_repository - dspace_bitstream_response = repo.create_bitstream_for(response, file.tempfile.path, strategy).as_json + dspace_bitstream_response = @item_repository.create_bitstream_for(response, file.tempfile.path, strategy).as_json dspace_bitstream_response.each do |v,k| bitstream_response[v.camelize(:lower)] = k end - # repo = Dspace::Client.instance.repository.bitstream_repository - # bitstream_response["name"] = file.original_filename - # bitstream_response["format"] = file.content_type - # repo.update(DSpaceRest::Bitstream.new bitstream_response) # Create the object inside OrientDB - subjects = [] - params[:learning_object][:subjects].each do |subject| - subjects << subject_repository.find_by_name(subject) - end - @learning_object = LearningObject.new(learning_object_params) @learning_object.type = get_file_type file @learning_object.id_dspace = response.id - @learning_object.subjects = subjects + @learning_object.subjects = @subjects @learning_object.created_at = Time.now @learning_object.last_modified = Time.now @learning_object.publisher = current_user @@ -157,6 +145,31 @@ class LearningObjectsController < ApplicationController private + def get_selected_subjects + @subjects = [] + params[:learning_object][:subjects].each do |subject| + @subjects << subject_repository.find_by_name(subject) + end + end + + def process_params + params[:learning_object][:date_created] = Time.now + params[:learning_object][:date_available] = Time.now + params[:learning_object][:type] = get_file_type params[:learning_object][:file] + end + + def create_repositories + @col_repository = Dspace::Client.instance.repository.collection_repository + @item_repository = Dspace::Client.instance.repository.item_repository + end + + def set_dspace_collection + # create struct with the id of the collection + # Avoiding an unecessary request to the DSpace + collection_struct = Struct.new(:id) + @collection = collection_struct.new('4') + end + def get_file_type(file) type = file.content_type.split('/').first case type @@ -173,10 +186,6 @@ class LearningObjectsController < ApplicationController def get_dspace_metadata_names(invert=nil) h = { - - - - "dc.contributor.author" => "author", "dc.date.accessioned" => "date_created", "dc.date.available" => "date_available", @@ -184,21 +193,6 @@ class LearningObjectsController < ApplicationController "dc.title" => "name", "dc.type" => "type", "dc.subject.category" => "subjects" - # dc.identifier.uri - # "dc.description" => "description", - # "dc.source" => "source", - # "dc.date.issued" => "date", - # "dc.rights.license" => "license", - # "dc.rights.holder" => "copyright", - # "dc.location.country" => "country", - # "dc.date.accessioned" => "date_created", - # "dc.date.available" => "date_available", - # "dc.subject.keyword" => "keywords", - # "dc.date.issued" => "date_issued", - # "dc.date.submitted" => "date_submitted", - # "dc.publisher" => "publisher" - # TODO: dc.audience.mediator - } if (invert != nil) return h.invert