Skip to content

Commit

Permalink
Add publish Investigation and study.
Browse files Browse the repository at this point in the history
  • Loading branch information
MasoudAbedi committed Jul 9, 2024
1 parent 37b98fe commit 52132af
Show file tree
Hide file tree
Showing 7 changed files with 420 additions and 79 deletions.
150 changes: 150 additions & 0 deletions app/controllers/investigations_controller.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'nfdi4health/csh_client'
class InvestigationsController < ApplicationController

include Seek::IndexPager
Expand Down Expand Up @@ -143,6 +144,155 @@ def update
end
end

def publish_to_csh
@investigation = Investigation.find(params[:id])
password = Seek::Config.n4h_password
url = Seek::Config.n4h_url.blank? ? nil : Seek::Config.n4h_url
authorization_url = Seek::Config.n4h_authorization_url.blank? ? nil : Seek::Config.n4h_authorization_url
username = Seek::Config.n4h_username.blank? ? nil : Seek::Config.n4h_username
url_publish = Seek::Config.n4h_publish_url.blank? ? nil : Seek::Config.n4h_publish_url

data_investigation = Nfdi4Health::Preparation_json.new
transforming_api_data = data_investigation.transforming_api(Investigation.find(params[:id]), InvestigationSerializer, 'investigation')

begin
endpoints = Nfdi4Health::Client.new()
endpoints.send_transforming_api(transforming_api_data.to_json, url)
rescue RestClient::ExceptionWithResponse => e
flash[:error] = if e.response
"HTTP Status: #{e.response.code} - #{e.response.body}"
else
"RestClient::ExceptionWithResponse occurred without a response: #{e.message}"
end
respond_to do |format|
format.html { redirect_to(@investigation) }
format.rdf { render template: 'rdf/show' }
format.json { render json: { error: flash[:error] }, status: :unprocessable_entity }
end

return
rescue RestClient::RequestTimeout => e
flash[:error] = 'Request Timeout: The server took too long to respond.'
respond_to do |format|
format.html { redirect_to(@investigation) }
format.rdf { render template: 'rdf/show' }
format.json { render json: { error: flash[:error] }, status: :unprocessable_entity }
end
return
rescue SocketError => e
flash[:error] = 'Network Error: Please check your internet connection.'
respond_to do |format|
format.html { redirect_to(@investigation) }
format.rdf { render template: 'rdf/show' }
format.json { render json: { error: flash[:error] }, status: :unprocessable_entity }
end
return
rescue StandardError => e
flash[:error] = "An unexpected error occurred: #{e.message}"
respond_to do |format|
format.html { redirect_to(@investigation) }
format.rdf { render template: 'rdf/show' }
format.json { render json: { error: flash[:error] }, status: :unprocessable_entity }
end
return
end

sender_investigation_merged=data_investigation.header(endpoints, @current_user.to_json,current_person)
begin
endpoints.get_token(authorization_url,username,password)
rescue RestClient::ExceptionWithResponse => e
flash[:error] = endpoints.handle_restclient_error(e,'get_token')
respond_to do |format|
format.html { redirect_to(@investigation) }
format.rdf { render template: 'rdf/show' }
format.json { render json: { error: flash[:error] }, status: :unprocessable_entity }
end
return
rescue RestClient::RequestTimeout
flash[:error] = 'Request Timeout: The server took too long to respond.'
respond_to do |format|
format.html { redirect_to(@investigation) }
format.rdf { render template: 'rdf/show' }
format.json { render json: { error: flash[:error] }, status: :unprocessable_entity }
end
return
rescue SocketError
flash[:error] = 'Network Error: Please check your internet connection.'
respond_to do |format|
format.html { redirect_to(@investigation) }
format.rdf { render template: 'rdf/show' }
format.json { render json: { error: flash[:error] }, status: :unprocessable_entity }
end
return
rescue StandardError => e
flash[:error] = "An unexpected error occurred: #{e.message}"
respond_to do |format|
format.html { redirect_to(@investigation) }
format.rdf { render template: 'rdf/show' }
format.json { render json: { error: flash[:error] }, status: :unprocessable_entity }
end
return
end
token_respond_hash = JSON.parse(endpoints.to_json)
access_token = token_respond_hash['token']
access_token_hash = JSON.parse(access_token)
one_time_token = access_token_hash['access_token']
begin
endpoints.publish_csh(sender_investigation_merged.to_json,url_publish,one_time_token)
rescue RestClient::ExceptionWithResponse => e
flash[:error] = endpoints.handle_restclient_error(e,'publish_csh')
respond_to do |format|
format.html { redirect_to(@investigation) }
format.rdf { render template: 'rdf/show' }
format.json { render json: { error: flash[:error] }, status: :unprocessable_entity }
end
return
rescue RestClient::RequestTimeout
flash[:error] = 'Request Timeout: The server took too long to respond.'
respond_to do |format|
format.html { redirect_to(@investigation) }
format.rdf { render template: 'rdf/show' }
format.json { render json: { error: flash[:error] }, status: :unprocessable_entity }
end
return
rescue SocketError
flash[:error] = 'Network Error: Please check your internet connection.'
respond_to do |format|
format.html { redirect_to(@investigation) }
format.rdf { render template: 'rdf/show' }
format.json { render json: { error: flash[:error] }, status: :unprocessable_entity }
end
return
rescue StandardError => e
flash[:error] = "An unexpected error occurred: #{e.message}"
respond_to do |format|
format.html { redirect_to(@investigation) }
format.rdf { render template: 'rdf/show' }
format.json { render json: { error: flash[:error] }, status: :unprocessable_entity }
end
return
end
if !JSON.parse(JSON.parse(endpoints.to_json)['endpoint'])['resource'].nil?
identifier = JSON.parse(JSON.parse(endpoints.to_json)['endpoint'])['resource']['identifier']
if !sender_investigation_merged['resource']['identifier'].nil?
flash[:notice] ="#{t('investigation')} was successfully updated with ID #{identifier}."
else
flash[:notice] ="#{t('investigation')} was successfully published with ID #{identifier}."
em = @investigation.extended_metadata
jem = JSON.parse(em.json_metadata)
jem['Resource_identifier_Investigation'] = identifier
em.update_column(:json_metadata, jem.to_json)
end
else
flash[:notice] = JSON.parse(JSON.parse(endpoints.to_json)['endpoint'])
end

respond_to do |format|
format.html { redirect_to(@investigation) }
format.rdf { render template: 'rdf/show' }
format.json { render json: @investigation, include: [params[:include]] }
end
end


private
Expand Down
89 changes: 15 additions & 74 deletions app/controllers/projects_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -542,71 +542,28 @@ def order_investigations

def publish_to_csh
@project = Project.find(params[:id])
project_attributes_json = ProjectSerializer.new(@project).to_json

@user_current = current_person
special_characters = {
'\\u0026' => '__AMP__',
'\\u003c' => '__LT__',
'\\u003e' => '__GT__',
'\\u0022' => '__QUOTE__'
}

# Replace special characters with placeholders
special_characters.each do |char, placeholder|
project_attributes_json.gsub!(char, placeholder)
end
project_attributes_json_parse = JSON.parse(project_attributes_json)

meta_data = JSON.parse(@project.to_json(only:[:created_at,:updated_at, :uuid]))
meta_data = meta_data.transform_keys { |key| key == 'created_at' ? 'created' : key }
meta_data = meta_data.transform_keys { |key| key == 'updated_at' ? 'modified' : key }
meta_data['api_version'] = ActiveModel::Serializer.config.api_version.to_s
meta_data['base_url'] = Seek::Config.site_base_host
meta_data = {meta: meta_data}

selected_keys = %w[project_administrators pals asset_housekeepers asset_gatekeepers organisms human_diseases people
institutions programmes investigations studies assays data_files project_relationships_json_files
file_templates placeholders models sops publications presentations events documents workflows
collections]
project_relationships_json_parse = project_attributes_json_parse.select { |key, _| selected_keys.include?(key) }
project_relationships_json = {relationships: project_relationships_json_parse}

selected_keys.each do |item|
project_attributes_json_parse.delete(item)
end
project_attributes_json = {attributes: project_attributes_json_parse}

attributes_selected_json_parse = project_attributes_json.merge(project_relationships_json)
attributes_selected_json_parse = attributes_selected_json_parse.merge(meta_data)
attributes_selected_json_parse['id'] = @project.id.to_s
attributes_selected_json_parse['type'] = 'projects'
attributes_selected_json_parse['jsonapi'] = { 'version' => '1.0' }
attributes_selected_json_parse['links'] = { 'self' => "/projects/#{@project.id}"}

user_attributes_selected_json_parse = {data: attributes_selected_json_parse}



password = Seek::Config.n4h_password
url = Seek::Config.n4h_url.blank? ? nil : Seek::Config.n4h_url
authorization_url = Seek::Config.n4h_authorization_url.blank? ? nil : Seek::Config.n4h_authorization_url
username = Seek::Config.n4h_username.blank? ? nil : Seek::Config.n4h_username
url_publish = Seek::Config.n4h_publish_url.blank? ? nil : Seek::Config.n4h_publish_url

data_project = Nfdi4Health::Preparation_json.new
transforming_api_data = data_project.transforming_api(Project.find(params[:id]), ProjectSerializer, 'Projects')
begin
endpoints = Nfdi4Health::Client.new()
endpoints.send_transforming_api(user_attributes_selected_json_parse.to_json, url)
endpoints.send_transforming_api(transforming_api_data.to_json, url)
rescue RestClient::ExceptionWithResponse => e
flash[:error] = if e.response
"HTTP Status: #{e.response.code} - #{e.response.body}"
else
"RestClient::ExceptionWithResponse occurred without a response: #{e.message}"
end
respond_to do |format|
format.html { redirect_to(@project) }
respond_to do |format|
format.html { redirect_to(@project) }
format.rdf { render template: 'rdf/show' }
format.json { render json: { error: flash[:error] }, status: :unprocessable_entity }
end
end

return
rescue RestClient::RequestTimeout => e
Expand Down Expand Up @@ -634,43 +591,27 @@ def publish_to_csh
end
return
end
# Convert placeholders back to special characters in the final JSON
final_json_string = endpoints.to_json
special_characters.each do |char, placeholder|
final_json_string.gsub!(placeholder, char)
end

project_transformed_hash = JSON.parse(JSON.parse(final_json_string)['transformed'])



project_transformed_update_hash = project_transformed_hash#{data: project_transformed_hash }
current_person_json = current_person.to_json
current_person_json_parsed = JSON.parse(current_person_json)
selected_keys = ['first_name', 'last_name','email']
current_person_json_parsed_filtered = current_person_json_parsed.select { |key, _| selected_keys.include?(key) }
sender_part = {sender: current_person_json_parsed_filtered }
sender_project_merged = sender_part.merge(project_transformed_update_hash)
sender_project_merged=data_project.header(endpoints, @current_user.to_json,current_person)
begin
endpoints.get_token(authorization_url,username,password)
rescue RestClient::ExceptionWithResponse => e
flash[:error] = endpoints.handle_restclient_error(e,"get_token")
flash[:error] = endpoints.handle_restclient_error(e,'get_token')
respond_to do |format|
format.html { redirect_to(@project) }
format.rdf { render template: 'rdf/show' }
format.json { render json: { error: flash[:error] }, status: :unprocessable_entity }
end
return
rescue RestClient::RequestTimeout
flash[:error] = "Request Timeout: The server took too long to respond."
flash[:error] = 'Request Timeout: The server took too long to respond.'
respond_to do |format|
format.html { redirect_to(@project) }
format.rdf { render template: 'rdf/show' }
format.json { render json: { error: flash[:error] }, status: :unprocessable_entity }
end
return
rescue SocketError
flash[:error] = "Network Error: Please check your internet connection."
flash[:error] = 'Network Error: Please check your internet connection.'
respond_to do |format|
format.html { redirect_to(@project) }
format.rdf { render template: 'rdf/show' }
Expand All @@ -693,23 +634,23 @@ def publish_to_csh
begin
endpoints.publish_csh(sender_project_merged.to_json,url_publish,one_time_token)
rescue RestClient::ExceptionWithResponse => e
flash[:error] = endpoints.handle_restclient_error(e,"publish_csh")
flash[:error] = endpoints.handle_restclient_error(e,'publish_csh')
respond_to do |format|
format.html { redirect_to(@project) }
format.rdf { render template: 'rdf/show' }
format.json { render json: { error: flash[:error] }, status: :unprocessable_entity }
end
return
rescue RestClient::RequestTimeout
flash[:error] = "Request Timeout: The server took too long to respond."
flash[:error] = 'Request Timeout: The server took too long to respond.'
respond_to do |format|
format.html { redirect_to(@project) }
format.rdf { render template: 'rdf/show' }
format.json { render json: { error: flash[:error] }, status: :unprocessable_entity }
end
return
rescue SocketError
flash[:error] = "Network Error: Please check your internet connection."
flash[:error] = 'Network Error: Please check your internet connection.'
respond_to do |format|
format.html { redirect_to(@project) }
format.rdf { render template: 'rdf/show' }
Expand All @@ -727,7 +668,7 @@ def publish_to_csh
end
if !JSON.parse(JSON.parse(endpoints.to_json)['endpoint'])['resource'].nil?
identifier = JSON.parse(JSON.parse(endpoints.to_json)['endpoint'])['resource']['identifier']
if !project_transformed_update_hash['resource']['identifier'].nil?
if !sender_project_merged['resource']['identifier'].nil?
flash[:notice] ="#{t('project')} was successfully updated with ID #{identifier}."
else
flash[:notice] ="#{t('project')} was successfully published with ID #{identifier}."
Expand Down
Loading

0 comments on commit 52132af

Please sign in to comment.