Skip to content

Commit

Permalink
update the vocbench connector to create also the ontology if not exis…
Browse files Browse the repository at this point in the history
…tent
  • Loading branch information
syphax-bouazzouni committed Jul 5, 2024
1 parent 65d15a4 commit a6d560c
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 83 deletions.
49 changes: 6 additions & 43 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,40 +1,3 @@
GIT
remote: https://github.com/lifewatch-eric/ncbo_cron.git
revision: 2ff764e536e049220208e3cd6c0ee9f03366df17
branch: master
specs:
ncbo_cron (0.0.1)
dante
goo
google-analytics-data
google-apis-analytics_v3
mlanett-redis-lock
multi_json
ncbo_annotator
ontologies_linked_data
redis
rufus-scheduler (~> 2.0.24)

GIT
remote: https://github.com/lifewatch-eric/ontologies_linked_data.git
revision: 17edfcb470848152dec8939fc6f68f5ed00f83db
branch: master
specs:
ontologies_linked_data (0.0.1)
activesupport
bcrypt
goo
json
libxml-ruby
multi_json
oj
omni_logger
pony
rack
rack-test
rsolr
rubyzip

GIT
remote: https://github.com/ncbo/ncbo_ontology_recommender.git
revision: 013abea4af3b10910ec661dbb358a4b6cae198a4
Expand All @@ -48,7 +11,7 @@ GIT

GIT
remote: https://github.com/ontoportal-lirmm/goo.git
revision: b2a635fb1e8206e6e3010be4dbe033b47eb58481
revision: a95245b8c964431505ca6315907440996c59a00d
branch: development
specs:
goo (0.0.2)
Expand Down Expand Up @@ -77,7 +40,7 @@ GIT

GIT
remote: https://github.com/ontoportal-lirmm/ncbo_cron.git
revision: 6bb53a13f514a60513afe25e37c5c69475140452
revision: fabd04ef4fa37989d526fc6a7aa1e98830008dae
branch: master
specs:
ncbo_cron (0.0.1)
Expand All @@ -94,7 +57,7 @@ GIT

GIT
remote: https://github.com/ontoportal-lirmm/ontologies_linked_data.git
revision: a5b56a68e6dc8ecfc9db708d44350342dac38ce6
revision: 4cd56da111c2037bf0b606574e6b325bfa74a2f1
branch: development
specs:
ontologies_linked_data (0.0.1)
Expand Down Expand Up @@ -154,7 +117,7 @@ GEM
bcrypt_pbkdf (1.1.1-x86_64-darwin)
bigdecimal (1.4.2)
builder (3.3.0)
capistrano (3.19.0)
capistrano (3.19.1)
airbrussh (>= 1.0.0)
i18n
rake (>= 10.0.0)
Expand Down Expand Up @@ -293,7 +256,7 @@ GEM
method_source (1.1.0)
mime-types (3.5.2)
mime-types-data (~> 3.2015)
mime-types-data (3.2024.0604)
mime-types-data (3.2024.0702)
mini_mime (1.1.5)
minitest (4.7.5)
minitest-stub_any_instance (1.0.3)
Expand Down Expand Up @@ -431,7 +394,7 @@ GEM
strscan (3.1.0)
systemu (2.6.5)
temple (0.10.3)
tilt (2.3.0)
tilt (2.4.0)
timeout (0.4.1)
trailblazer-option (0.1.2)
tzinfo (2.0.6)
Expand Down
42 changes: 5 additions & 37 deletions controllers/ontologies_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,13 @@ class OntologiesController < ApplicationController
##
# Create an ontology
post do
create_ontology
create_ontology_with_params
end

##
# Create an ontology with constructed URL
put '/:acronym' do
create_ontology
create_ontology_with_params
end

##
Expand Down Expand Up @@ -185,45 +185,13 @@ def find_latest_submission
latest
end

def create_ontology
params ||= @params

# acronym must be well formed
params['acronym'] = params['acronym'].upcase # coerce new ontologies to upper case

# ontology acronym must be unique
ont = Ontology.find(params['acronym']).first
if ont.nil?
ont = instance_from_params(Ontology, params)
else
error_msg = <<-ERR
Ontology already exists, see #{ont.id}
To add a new submission, POST to: /ontologies/#{params['acronym']}/submission.
To modify the resource, use PATCH.
ERR
error 409, error_msg
end

# ontology name must be unique
ont_names = Ontology.where.include(:name).to_a.map { |o| o.name }
if ont_names.include?(ont.name)
error 409, "Ontology name is already in use by another ontology."
end

def create_ontology_with_params
ont = create_ontology
if ont.valid?
ont.save
# Send an email to the administrator to warn him about the newly created ontology
begin
if !LinkedData.settings.admin_emails.nil? && !LinkedData.settings.admin_emails.empty?
LinkedData::Utils::Notifications.new_ontology(ont)
end
rescue Exception => e
end
reply 201, ont
else
error 422, ont.errors
end

reply 201, ont
end
end

Expand Down
11 changes: 10 additions & 1 deletion controllers/ontology_submissions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,16 @@ class OntologySubmissionsController < ApplicationController
# Create a new submission for an existing ontology
post do
ont = Ontology.find(params["acronym"]).include(Ontology.attributes).first
error 422, "You must provide a valid `acronym` to create a new submission" if ont.nil?
params["name"] ||= params["titles"]&.first&.dig('title')

if params["name"] && params["acronym"] && ont.nil?
params["hasOntologyLanguage"] = "SKOS"
params["administeredBy"] = [ current_user.username ]
ont = create_ontology
elsif ont.nil?
error 422, "You must provide a valid `acronym` to create a new submission"
end

reply 201, create_submission(ont)
end

Expand Down
38 changes: 38 additions & 0 deletions helpers/ontology_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,44 @@ module Helpers
module OntologyHelper
include Sinatra::Concerns::EcoPortalMetadataExporter

def create_ontology
params ||= @params

# acronym must be well formed
params['acronym'] = params['acronym'].upcase # coerce new ontologies to upper case

# ontology acronym must be unique
ont = Ontology.find(params['acronym']).first
if ont.nil?
ont = instance_from_params(Ontology, params)
else
error_msg = <<-ERR
Ontology already exists, see #{ont.id}
To add a new submission, POST to: /ontologies/#{params['acronym']}/submission.
To modify the resource, use PATCH.
ERR
error 409, error_msg
end

# ontology name must be unique
ont_names = Ontology.where.include(:name).to_a.map { |o| o.name }
if ont_names.include?(ont.name)
error 409, "Ontology name is already in use by another ontology."
end

if ont.valid?
ont.save
# Send an email to the administrator to warn him about the newly created ontology
begin
if !LinkedData.settings.admin_emails.nil? && !LinkedData.settings.admin_emails.empty?
LinkedData::Utils::Notifications.new_ontology(ont)
end
rescue Exception => e
end
end
ont
end

##
# Create a new OntologySubmission object based on the request data
def create_submission(ont)
Expand Down
10 changes: 8 additions & 2 deletions test/controllers/test_ontology_submissions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -271,10 +271,16 @@ def test_upload_eco_portal_metadata

2.times.each do |i|
header 'Authorization', "apikey token=#{user.apikey}"
post "/ontologies/#{@@acronym}/submissions", example_of_input, "CONTENT_TYPE" => "application/json"
post "/ontologies/SYPHAX_TEST_VOC/submissions", example_of_input, "CONTENT_TYPE" => "application/json"
assert_equal 201, last_response.status

ont = Ontology.find(@@acronym).first
ont = Ontology.find('SYPHAX_TEST_VOC').first

refute_nil ont

ont.bring_remaining

assert_equal ont.name, example_of_input["titles"].first["title"]

subs = ont.bring(:submissions).submissions

Expand Down

0 comments on commit a6d560c

Please sign in to comment.