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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
|
REPORT_DIR = File.join(Dir.pwd,'/reports')
require "lib/format_util.rb"
# = Reports::ReportPersistance
#
# service that stores reports (Reports::ReportConent), and provides access in various formats
#
class Reports::ReportPersistance
# lists all stored report ID-s of a certain type
#
# call-seq:
# list_reports(type) => Array
#
def list_reports(type, filter_params)
raise "not implemented"
end
# stores content of a report (Reports::ReportContent) and returns id
#
# call-seq:
# new_report(report_content) => string
#
def new_report(report_content)
raise "not implemented"
end
# returns a already created report (file path on server) in a certain format (converts to this format if it does not exist yet)
#
# call-seq:
# get_report(type, id, format) => string
#
def get_report(type, id, format, force_formating, params)
raise "not implemented"
end
# returns file path on server of a resource (i.e. image) of a report
#
# call-seq:
# get_report_resource(type, id, resource) => string
#
def get_report_resource(type, id, resource)
raise "not implemented"
end
# deletes a report
# * returns true if deleting successfull
# * returns false if report not found
# * raises exception if error occurs
#
# call-seq:
# delete_report(type, id) => boolean
#
def delete_report(type, id, subjectid=nil)
raise "not implemented"
end
# raises exception if not valid id format
def check_report_id_format(id)
raise "not implemented"
end
end
# = Reports::FileReportPersistance
#
# type of Reports::ReportPersistance, stores reports in file-system, see Reports::ReportPersistance for API documentation
#
class Reports::FileReportPersistance < Reports::ReportPersistance
def initialize()
FileUtils.mkdir REPORT_DIR unless File.directory?(REPORT_DIR)
raise "report cannot be found nor created" unless File.directory?(REPORT_DIR)
LOGGER.debug "reports are stored in "+REPORT_DIR
end
def list_reports(type, filter_params=nil)
raise "filter params not supported" if filter_params
(Dir.new(type_directory(type)).entries - [".", ".."]).sort{|x,y| x.to_i <=> y.to_i}
end
def get_report(type, id, format, force_formating, params)
report_dir = report_directory(type, id)
raise_report_not_found(type, id) unless File.directory?(report_dir)
filename = "report."+Reports::ReportFormat.get_filename_extension(format)
file_path = report_dir+"/"+filename
return file_path if File.exist?(file_path) && !force_formating
Reports::ReportFormat.format_report(report_dir, "report.xml", filename, format, force_formating, params)
raise "formated file not found '"+file_path+"'" unless File.exist?(file_path)
return file_path
end
def get_report_resource(type, id, resource)
report_dir = report_directory(type, id)
raise_report_not_found(type, id) unless File.directory?(report_dir)
file_path = report_dir+"/"+resource.to_s
raise OpenTox::NotFoundError.new("resource not found, resource: '"+resource.to_s+"', type:'"+type.to_s+"', id:'"+id.to_s+"'") unless File.exist?(file_path)
return file_path
end
def delete_report(type, id, subjectid=nil)
report_dir = report_directory(type, id)
raise_report_not_found(type, id) unless File.directory?(report_dir)
entries = (Dir.new(report_dir).entries-[".", ".."]).collect{|f| report_dir+"/"+f.to_s}
FileUtils.rm(entries)
FileUtils.rmdir report_dir
raise "could not delete report directory '"+report_dir+"'" if File.directory?(report_dir)
return true
end
def check_report_id_format(id)
raise "not valid report id format" unless id.to_s =~ /[0-9]+/
end
def new_report(report_content, type, meta_data=nil, uri_provider=nil)
new_report_with_id(report_content, type)
end
protected
def new_report_with_id(report_content, type, force_id=nil)
LOGGER.debug "storing new report of type "+type.to_s
type_dir = type_directory(type)
raise "type dir '"+type_dir+"' cannot be found nor created" unless File.directory?(type_dir)
if (force_id==nil)
id = 1
while File.exist?( type_dir+"/"+id.to_s )
id += 1
end
else
raise "report with id '"+force_id.to_s+"' already exists, file system not consistent with db" if File.exist?( type_dir+"/"+force_id.to_s )
id = force_id
end
report_dir = type_dir+"/"+id.to_s
FileUtils.mkdir(report_dir)
raise "report dir '"+report_dir+"' cannot be created" unless File.directory?(report_dir)
xml_filename = report_dir+"/report.xml"
xml_file = File.new(xml_filename, "w")
report_content.xml_report.write_to(xml_file, id)
xml_file.close
if (report_content.tmp_files)
report_content.tmp_files.each do |k,v|
tmp_filename = report_dir+"/"+k
raise "tmp-file '"+tmp_filename.to_s+"' already exists" if File.exist?(tmp_filename)
raise "tmp-file '"+v.to_s+"' not found" unless File.exist?(v)
FileUtils.mv(v.to_s,tmp_filename)
raise "could not move tmp-file to '"+tmp_filename.to_s+"'" unless File.exist?(tmp_filename)
end
end
return id
end
private
def raise_report_not_found(type, id)
raise OpenTox::NotFoundError.new("report not found, type:'"+type.to_s+"', id:'"+id.to_s+"'")
end
def type_directory(type)
dir = REPORT_DIR+"/"+type
FileUtils.mkdir dir.to_s unless (File.directory?(dir))
return dir
end
def report_directory(type, id)
type_dir = type_directory(type)
raise "type dir '"+type_dir+"' cannot be found nor created" unless File.directory?(type_dir)
return type_dir+"/"+id.to_s
end
end
module Reports
#class ReportData < ActiveRecord::Base
# serialize :validation_uris
# serialize :crossvalidation_uris
# serialize :algorithm_uris
# serialize :model_uris
# alias_attribute :date, :created_at
class ReportData < Ohm::Model
attribute :report_type
attribute :date
attribute :validation_uris
attribute :crossvalidation_uris
attribute :model_uris
attribute :algorithm_uris
index :report_type
index :validation_uris
index :crossvalidation_uris
index :algorithm_uris
attr_accessor :subjectid
def self.create(params={})
params[:date] = Time.new
super params
end
def save
super
OpenTox::Authorization.check_policy(report_uri, subjectid)
end
def report_uri
raise "no id" if self.id==nil
Reports::ReportService.instance.get_uri(self.report_type, self.id)
end
def get_content_as_hash
map = {}
[ :date, :report_type, :validation_uris, :crossvalidation_uris,
:algorithm_uris, :model_uris ].each do |p|
map[p] = self.send(p)
end
map
end
def to_yaml
get_content_as_hash.keys_to_rdf_format.keys_to_owl_uris.to_yaml
end
def to_rdf
s = OpenTox::Serializer::Owl.new
s.add_resource(report_uri,OT.Report,get_content_as_hash.keys_to_rdf_format.keys_to_owl_uris)
s.to_rdfxml
end
end
class ExtendedFileReportPersistance < FileReportPersistance
def new_report(report_content, type, meta_data, uri_provider, subjectid=nil)
raise "report meta data missing" unless meta_data
meta_data[:report_type] = type
report = ReportData.create(meta_data)
report.subjectid = subjectid
OpenTox::Authorization.check_policy(report.report_uri, subjectid)
new_report_with_id(report_content, type, report.id)
end
def list_reports(type, filter_params={})
filter_params[:report_type] = type
LOGGER.debug "find reports for params: "+filter_params.inspect
reports = Lib::OhmUtil.find( ReportData, filter_params )
reports.collect{ |r| r.id }
end
def get_report(type, id, format, force_formating, params)
report = ReportData[id]
raise OpenTox::NotFoundError.new("Report with id='"+id.to_s+"' and type='"+type.to_s+"' not found.") if
report==nil or report.report_type!=type
# begin
# report = ReportData.find(:first, :conditions => {:id => id, :report_type => type})
# rescue ActiveRecord::RecordNotFound
# raise OpenTox::NotFoundError.new("Report with id='"+id.to_s+"' and type='"+type.to_s+"' not found.")
# end
case format
when "application/rdf+xml"
report.to_rdf
when "application/x-yaml"
report.to_yaml
else
super
end
end
def delete_report(type, id, subjectid=nil)
# begin
# report = ReportData.find(:first, :conditions => {:id => id, :report_type => type})
# rescue ActiveRecord::RecordNotFound
# raise OpenTox::NotFoundError.new("Report with id='"+id.to_s+"' and type='"+type.to_s+"' not found.")
# end
# ReportData.delete(id)
report = ReportData[id]
raise OpenTox::NotFoundError.new("Report with id='"+id.to_s+"' and type='"+type.to_s+"' not found.") if
report==nil || report.report_type!=type
report.delete
if (subjectid)
begin
res = OpenTox::Authorization.delete_policies_from_uri(report.report_uri, subjectid)
LOGGER.debug "Deleted validation policy: #{res}"
rescue
LOGGER.warn "Policy delete error for validation: #{report.report_uri}"
end
end
super
end
end
end
#module Reports
# def self.check_filter_params(model, filter_params)
# prop_names = model.properties.collect{|p| p.name.to_s}
# filter_params.keys.each do |k|
# key = k.to_s
# unless prop_names.include?(key)
# key = key.from_rdf_format
# unless prop_names.include?(key)
# key = key+"_uri"
# unless prop_names.include?(key)
# key = key+"s"
# unless prop_names.include?(key)
# err = "no attribute found: '"+k.to_s+"'"
# if $sinatra
# $sinatra.raise OpenTox::BadRequestError.newerr
# else
# raise err
# end
# end
# end
# end
# end
# filter_params[key] = filter_params.delete(k)
# end
# filter_params
# end
#
# def ReportData.all( params )
# super Reports.check_filter_params( ReportData, params )
# end
#end
|