Skip to content

Commit

Permalink
update publish button code
Browse files Browse the repository at this point in the history
  • Loading branch information
ReneHaensel committed May 2, 2024
1 parent ef6fc95 commit 0e059dc
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 22 deletions.
75 changes: 55 additions & 20 deletions app/controllers/projects_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
require 'nfdi4health/csh_client'
require 'seek/util'


class ProjectsController < ApplicationController
include Seek::IndexPager
include CommonSweepers
Expand Down Expand Up @@ -36,7 +37,7 @@ class ProjectsController < ApplicationController
before_action :validate_message_log_for_create, only: [:administer_create_project_request, :respond_create_project_request]
before_action :validate_message_log_for_import, only: [:administer_import_project_request, :respond_import_project_request]
before_action :parse_message_log_details, only: [:administer_create_project_request, :administer_import_project_request]
before_action :check_message_log_programme_permissions, only: [:administer_create_project_request, :administer_import_project_request,
before_action :check_message_log_programme_permissions, only: [:administer_create_project_request, :administer_import_project_request,
:respond_create_project_request, :respond_import_project_request], if: Proc.new{Seek::Config.programmes_enabled}

skip_before_action :project_membership_required
Expand All @@ -61,7 +62,7 @@ def project_creation_requests
@requests = ProjectCreationMessageLog.pending_requests.select do |r|
r.can_respond_project_creation_request?(current_user)
end

respond_to do |format|
format.html
end
Expand Down Expand Up @@ -420,7 +421,7 @@ def asset_report
format.html { render template: 'projects/asset_report/report' }
end
end

# GET /projects/1
def show
respond_to do |format|
Expand Down Expand Up @@ -541,16 +542,53 @@ def order_investigations

def publish_to_csh
@project = Project.find(params[:id])
data_project = render json: @project, include: [params[:include]]
data_project_parsed = JSON.parse(data_project)
data = @project.to_json
#host = Seek::Config.site_base_host
#url = host + '/projects'
project_attributes_json= ProjectSerializer.new(@project).to_json #Project.find(params[:id])

@user_current=current_person
user_json=@user_current.to_json
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 = ["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}

for item in selected_keys do
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.to_s}

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#"https://csh.nfdi4health.de/api/resource/"
endpoints = Nfdi4Health::Client.new(authorization_url)
endpoints.send_transforming_api(data_project_parsed.to_json,url)
endpoints.send_transforming_api(user_attributes_selected_json_parse.to_json,url)
aaa

project_transformed_hash=JSON.parse(JSON.parse(endpoints.to_json)['transformed'])
project_transformed_update_hash = project_transformed_hash#{data: project_transformed_hash }
Expand All @@ -561,7 +599,6 @@ def publish_to_csh
sender_part = {sender: current_person_json_parsed_filtered }
sender_project_merged=sender_part.merge(project_transformed_update_hash)


endpoints.get_token(authorization_url,username,password)
token_respond_hash=JSON.parse(endpoints.to_json)
access_token=token_respond_hash['token']
Expand All @@ -570,25 +607,23 @@ def publish_to_csh
endpoints.publish_csh(sender_project_merged.to_json,url_publish,one_time_token)
identifier=JSON.parse(JSON.parse(endpoints.to_json)["endpoint"])["resource"]["identifier"]

#@project.extended_metadata.data.update(Resource_identifier_Project: identifier)
#@project.extended_metadata.data.set_attribute_value(:Resource_identifier_Project, identifier)



# adding the {sender: ...} to endpoint
# getting the token with csh_username, csh_password from auth_url
#

# with token from csh

#respond_to do |format|
#flash[:notice] = "#{t('project')} was successfully published."


#respond_to do |format|
# flash[:notice] = "#{t('project')} was successfully published."
# format.html { redirect_to(@project) }
#end


flash[:notice] = "#{t('project')} was successfully published with ID #{identifier}."
respond_to do |format|
flash[:notice] = "#{t('project')} was successfully published."
format.html { render(params[:only_content] ? { layout: false } : {})} # show.html.erb
#@project.reload
#flash[:notice] = "#{t('project')} was successfully published."
format.html { redirect_to(@project) }
#format.html { render(params[:only_content] ? { layout: false } : {})} # show.html.erb
format.rdf { render template: 'rdf/show' }
format.json { render json: @project, include: [params[:include]] }
end
Expand Down Expand Up @@ -676,7 +711,7 @@ def populate_from_spreadsheet
format.html { redirect_to project_path(@project) }
end
end

def admin_members
respond_with(@project)
end
Expand Down
10 changes: 8 additions & 2 deletions lib/nfdi4health/csh_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,23 @@ def initialize(endpoint)


end

def publish_csh(project_transformed,url,token)
content_length = project_transformed.bytesize
headers = { content_type: 'application/json',Content_Length: content_length ,Host: 'csh.nfdi4health.de', Authorization: "Bearer " + token
}

@endpoint = RestClient::Request.execute(method: :post, url: url, payload: project_transformed, headers: headers)
end

def send_transforming_api(project,url)
@transformed = RestClient::Request.execute(method: :post, url: url,payload: project, headers: { content_type: :json, accept: :json })
#raise project.inspect
@transformed = RestClient::Request.execute(method: :post, url: url,payload: project, headers: { content_type: :json, accept: :json }).body
#@endpoint['publish'].post(project, content_type: 'application/json')
end



def get_token(url,user,password)
headers = {
content_type: 'application/x-www-form-urlencoded'
Expand All @@ -40,4 +46,4 @@ def get_token(url,user,password)
end

end
end
end

0 comments on commit 0e059dc

Please sign in to comment.