[ 'rubygems', 'sinatra', 'sinatra/url_for', 'opentox-ruby' ].each do |lib| require lib end QMRF_EDITOR_URI = "http://ortona.informatik.uni-freiburg.de/qmrfedit/OT_QMRFEditor.jnlp" # hack for as long as mysql lite is used def mysql_lite_retry( n_times=15 ) n_times.times do begin yield return rescue => ex LOGGER.warn "datamapper error, wait and retry : "+ex.message sleep(1+rand(3)) # wait 1-3 seconds end end yield # try a last time end require 'reach_reports/reach_persistance.rb' require 'reach_reports/reach_service.rb' require "lib/format_util.rb" def extract_type(params) raise OpenTox::BadRequestError.new "illegal type, neither QMRF nor QPRF: "+params[:type] unless params[:type] && params[:type] =~ /(?i)Q(M|P)RF/ params.delete("type") end get '/reach_report' do uri_list = url_for('/reach_report/QMRF', :full)+"\n"+url_for('/reach_report/QPRF', :full)+"\n" if request.env['HTTP_ACCEPT'] =~ /text\/html/ content_type "text/html" related_links = "All validations: "+url_for("/",:full)+"\n"+ "Validation reporting: "+url_for("/report",:full) description = "A list of all suported REACH reporting types." OpenTox.text_to_html uri_list,related_links,description, @subjectid else content_type "text/uri-list" uri_list end end get '/reach_report/:type' do type = extract_type(params) LOGGER.info "list all "+type+" reports" uris = ReachReports.list_reports(type,params[:model] || params[:model_uri]) if request.env['HTTP_ACCEPT'] =~ /text\/html/ content_type "text/html" related_links = "All REACH reporting types: "+url_for("/reach_report",:full) description = "A list of "+type+" reports." post_command = nil case type when /(?i)QMRF/ related_links += "\n"+ "OpenTox version of QMRF editor: "+QMRF_EDITOR_URI description += "\n"+ "To create a QMRF report use the POST method." post_command = OpenTox::PostCommand.new request.url,"Create QMRF report" post_command.attributes << OpenTox::PostAttribute.new("model_uri") when /(?i)QPRF/ #TODO end OpenTox.text_to_html uris,@subjectid,related_links,description,post_command else content_type "text/uri-list" uris end end post '/reach_report/:type' do type = extract_type(params) content_type "text/uri-list" LOGGER.info "creating "+type+" report "+params.inspect raise OpenTox::BadRequestError.new "model_uri missing" if type=~/(?i)QMRF/ and params[:model_uri]!=nil and params[:model_uri].to_s.size==0 #puts "creating "+type+" report "+params.inspect result_uri = ReachReports.create_report(type,params,@subjectid,request.env["rack.input"]) if result_uri and result_uri.task_uri? halt 202,result_uri+"\n" else result_uri+"\n" end end get '/reach_report/:type/:id' do type = extract_type(params) LOGGER.info "get "+type+" report with id '"+params[:id].to_s+"' "+request.env['HTTP_ACCEPT'].to_s+"'" rep = ReachReports.get_report(type, params[:id]) case request.env['HTTP_ACCEPT'].to_s when "application/rdf+xml" raise OpenTox::BadRequestError.new "application/rdf+xml not yet supported" owl = OpenTox::Owl.create(type+"Report",rep.report_uri) owl.set_data( rep.get_content.keys_to_rdf_format ) owl.rdf when "application/qmrf-xml" content_type "application/qmrf-xml" rep.to_xml #f = File.new("/home/martin/info_home/.public_html/qmrf.out.xml","w") #f.puts result when /text\/html/ content_type "text/html" related_links = "Open report in QMRF editor: "+rep.report_uri+"/editor"+"\n"+ "All "+type+" reports: "+url_for("/reach_report/"+type,:full) description = "A QMRF report." OpenTox.text_to_html rep.to_yaml,@subjectid,related_links,description when /application\/x-yaml|\*\/\*|^$/ # matches 'application/x-yaml', '*/*', '' content_type "application/x-yaml" rep.to_yaml else raise OpenTox::BadRequestError.new "MIME type '"+request.env['HTTP_ACCEPT'].to_s+"' not supported, valid Accept-Headers are \"application/rdf+xml\", \"application/x-yaml\", \"application/qmrf-xml\"." end end post '/reach_report/:type/:id' do type = extract_type(params) LOGGER.info "Post to "+type+" report with id "+params[:id].to_s+"' "+request.env['HTTP_ACCEPT'].to_s+"'" rep = ReachReports.get_report(type, params[:id]) input = request.env["rack.input"].read raise OpenTox::BadRequestError.new "no xml data specified" unless input && input.to_s.size>0 LOGGER.debug "size of posted data: "+input.to_s.size.to_s ReachReports::QmrfReport.from_xml(rep,input) #f = File.new("/home/martin/info_home/.public_html/qmrf.out.xml","w") #f.puts rep.to_xml end delete '/reach_report/:type/:id' do type = extract_type(params) LOGGER.info "delete "+type+" report with id '"+params[:id].to_s+"'" ReachReports.delete_report(type, params[:id], @subjectid) end #get '/reach_report/:type/:id/:section' do # # type = extract_type(params) # LOGGER.info "get "+type+" report section '"+params[:section].to_s+"', with id "+params[:id].to_s+"' "+request.env['HTTP_ACCEPT'].to_s+"'" # ReachReports.get_report(type, params[:id], params[:section]).to_yaml #end # #get '/reach_report/:type/:id/:section/:subsection' do # # type = extract_type(params) # LOGGER.info "get "+type+" report subsection '"+params[:subsection].to_s+"', section '"+params[:section].to_s+"', with id "+params[:id].to_s+"' "+request.env['HTTP_ACCEPT'].to_s+"'" # ReachReports.get_report(type, params[:id], params[:section], params[:subsection]).to_yaml #end get '/reach_report/:type/:id/editor' do type = extract_type(params) LOGGER.info "editor for "+type+" report with id '"+params[:id].to_s+"' "+params.inspect jnlp = < QMRF Editor www.opentox.org (Q)SAR Model Reporting Format Editor (Q)SAR Model Reporting Format Editor -x EOF jnlp.chomp! jnlp += File.join(url_for("/reach_report/QMRF",:full),params[:id]) if @subjectid.to_s.size>0 jnlp += < --subjectid= EOF jnlp.chomp! jnlp += @subjectid.to_s end jnlp += < -d http://opentox.informatik.uni-freiburg.de/qmrfedit/qmrf.dtd -t http://opentox.informatik.uni-freiburg.de/qmrfedit/verdana.ttf EOF content_type "application/x-java-jnlp-file" jnlp end