summaryrefslogtreecommitdiff
path: root/reach_reports/reach_application.rb
blob: b380c92bfbf2c7561f6d951c86122e7146b54d6d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269

[ '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 = <<EOF
<?xml version ="1.0" encoding="utf-8"?>                                                                                                                                                                     
<jnlp spec="1.0+" codebase="http://opentox.informatik.uni-freiburg.de/" href="qmrfedit/OT_QMRFEditor.jnlp" >                                                                                               
<information>                                                                                                                                                                                               
<title>QMRF Editor</title>                                                                                                                                                                                  
<vendor>www.opentox.org</vendor>                                                                                                                                                                            
<description>(Q)SAR Model Reporting Format Editor</description>
<description kind="short">(Q)SAR Model Reporting Format Editor</description>
<icon href="qmrfedit/OTLogo.png" />
</information>
<resources>
<j2se version="1.6+" java-vm-args="-Xincgc"/>

<jar href="qmrfedit/OT_QMRFEditor.jar" download="eager" main="true"/>
<jar href="qmrfedit/OT_QMRFEditor_lib/xercesImpl.jar" download="eager"/>
<jar href="qmrfedit/OT_QMRFEditor_lib/itext-1.4.5.jar" download="lazy"/>
<jar href="qmrfedit/OT_QMRFEditor_lib/poi-3.0.jar" download="lazy"/>
<jar href="qmrfedit/OT_QMRFEditor_lib/poi-contrib.jar" download="lazy"/>
<jar href="qmrfedit/OT_QMRFEditor_lib/poi-scratchpad.jar" download="lazy"/>
<jar href="qmrfedit/OT_QMRFEditor_lib/commons-lang-2.3.jar" download="lazy"/>
<jar href="qmrfedit/OT_QMRFEditor_lib/cdk-applications.jar" download="lazy" />
<jar href="qmrfedit/OT_QMRFEditor_lib/cdk-builder3d.jar" download="lazy" />
<jar href="qmrfedit/OT_QMRFEditor_lib/cdk-charges.jar" download="lazy" />
<jar href="qmrfedit/OT_QMRFEditor_lib/cdk-core.jar" download="lazy" />
<jar href="qmrfedit/OT_QMRFEditor_lib/cdk-datadebug.jar" download="lazy" />
<jar href="qmrfedit/OT_QMRFEditor_lib/cdk-data.jar" download="lazy" />
<jar href="qmrfedit/OT_QMRFEditor_lib/cdk-experimental.jar" download="lazy" />
<jar href="qmrfedit/OT_QMRFEditor_lib/cdk-extra.jar" download="lazy" />
<jar href="qmrfedit/OT_QMRFEditor_lib/cdk-forcefield.jar" download="lazy" />
<jar href="qmrfedit/OT_QMRFEditor_lib/cdk-interfaces.jar" download="lazy" />
<jar href="qmrfedit/OT_QMRFEditor_lib/cdk-io.jar" download="lazy" />
<jar href="qmrfedit/OT_QMRFEditor_lib/cdk-jchempaint.applet.jar" download="lazy" />
<jar href="qmrfedit/OT_QMRFEditor_lib/cdk-jchempaint.application.jar" download="lazy" />
<jar href="qmrfedit/OT_QMRFEditor_lib/cdk-jchempaint.jar" download="lazy" />
<jar href="qmrfedit/OT_QMRFEditor_lib/cdk-libio-cml.jar" download="lazy" />
<jar href="qmrfedit/OT_QMRFEditor_lib/cdk-libio-weka.jar" download="lazy" />
<jar href="qmrfedit/OT_QMRFEditor_lib/cdk-nonotify.jar" download="lazy" />
<jar href="qmrfedit/OT_QMRFEditor_lib/cdk-pdb-cml.jar" download="lazy" />
<jar href="qmrfedit/OT_QMRFEditor_lib/cdk-pdb.jar" download="lazy" />
<jar href="qmrfedit/OT_QMRFEditor_lib/cdk-qsar-cml.jar" download="lazy" />
<jar href="qmrfedit/OT_QMRFEditor_lib/cdk-qsar.jar" download="lazy" />
<jar href="qmrfedit/OT_QMRFEditor_lib/cdk-qsar-pdb.jar" download="lazy" />
<jar href="qmrfedit/OT_QMRFEditor_lib/commons-cli-1.0.jar" download="lazy" />
<jar href="qmrfedit/OT_QMRFEditor_lib/commons-io-1.1.jar" download="lazy" />
<jar href="qmrfedit/OT_QMRFEditor_lib/commons-logging-1.0.4.jar" download="lazy" />
<jar href="qmrfedit/OT_QMRFEditor_lib/commons-codec-1.3.jar" download="eager" />
<jar href="qmrfedit/OT_QMRFEditor_lib/fop.jar" download="lazy" />
<jar href="qmrfedit/OT_QMRFEditor_lib/jai_codec.jar" download="lazy" />
<jar href="qmrfedit/OT_QMRFEditor_lib/jai_core.jar" download="lazy" />
<jar href="qmrfedit/OT_QMRFEditor_lib/jgrapht-0.6.0.jar" download="lazy" />
<jar href="qmrfedit/OT_QMRFEditor_lib/jh.jar" download="lazy" />
<jar href="qmrfedit/OT_QMRFEditor_lib/l2fprod-common-all.jar" download="lazy" />
<jar href="qmrfedit/OT_QMRFEditor_lib/libfonts-0.1.4.jar" download="lazy" />
<jar href="qmrfedit/OT_QMRFEditor_lib/log4j-1.2.8.jar" download="lazy" />
<jar href="qmrfedit/OT_QMRFEditor_lib/log4j.jar" download="lazy" />
<jar href="qmrfedit/OT_QMRFEditor_lib/mysql-connector-java-5.0.5-bin.jar" download="lazy" />
<jar href="qmrfedit/OT_QMRFEditor_lib/naming-factory-dbcp.jar" download="lazy" />
<jar href="qmrfedit/OT_QMRFEditor_lib/naming-factory.jar" download="lazy" />
<jar href="qmrfedit/OT_QMRFEditor_lib/naming-resources.jar" download="lazy" />
<jar href="qmrfedit/OT_QMRFEditor_lib/opsin-big-0.1.0.jar" download="lazy" />
<jar href="qmrfedit/OT_QMRFEditor_lib/org.restlet.jar" download="lazy" />
<jar href="qmrfedit/OT_QMRFEditor_lib/swing-layout-1.0.jar" download="lazy" />
<jar href="qmrfedit/OT_QMRFEditor_lib/xmlgraphics-commons-1.1.jar" download="lazy" />
<jar href="qmrfedit/OT_QMRFEditor_lib/xom-1.1b2.jar" download="lazy" />
<jar href="qmrfedit/OT_QMRFEditor_lib/xom-1.1.jar" download="lazy" />


</resources>
<application-desc main-class="ambit.applications.qmrf.QMRFEditor">
<argument>-x 
EOF
  jnlp.chomp!
  jnlp += File.join(url_for("/reach_report/QMRF",:full),params[:id])

  if @subjectid.to_s.size>0
    jnlp += <<EOF 
</argument>
<argument>--subjectid=
EOF
    jnlp.chomp!
    jnlp += @subjectid.to_s
  end

  jnlp += <<EOF 
</argument>
<argument>-d http://opentox.informatik.uni-freiburg.de/qmrfedit/qmrf.dtd</argument>
<argument>-t http://opentox.informatik.uni-freiburg.de/qmrfedit/verdana.ttf</argument>

</application-desc> 
<security>
    <all-permissions/>
</security>
</jnlp>
EOF
  
  content_type "application/x-java-jnlp-file"
  jnlp
end