diff options
Diffstat (limited to 'report/report_persistance.rb')
-rwxr-xr-x[-rw-r--r--] | report/report_persistance.rb | 183 |
1 files changed, 129 insertions, 54 deletions
diff --git a/report/report_persistance.rb b/report/report_persistance.rb index 46a014e..df4930c 100644..100755 --- a/report/report_persistance.rb +++ b/report/report_persistance.rb @@ -1,5 +1,6 @@ REPORT_DIR = File.join(Dir.pwd,'/reports') +require "lib/format_util.rb" # = Reports::ReportPersistance # @@ -51,7 +52,7 @@ class Reports::ReportPersistance # call-seq: # delete_report(type, id) => boolean # - def delete_report(type, id) + def delete_report(type, id, subjectid=nil) raise "not implemented" end @@ -69,7 +70,6 @@ end class Reports::FileReportPersistance < Reports::ReportPersistance def initialize() - raise "pls specify report-directory (:reports -> :report_dir) in config file" unless @@config[:reports] and @@config[:reports][:report_dir] 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 @@ -100,11 +100,11 @@ class Reports::FileReportPersistance < Reports::ReportPersistance 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 Reports::NotFound.new("resource not found, resource: '"+resource.to_s+"', type:'"+type.to_s+"', id:'"+id.to_s+"'") unless File.exist?(file_path) + 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) + def delete_report(type, id, subjectid=nil) report_dir = report_directory(type, id) raise_report_not_found(type, id) unless File.directory?(report_dir) @@ -162,7 +162,7 @@ class Reports::FileReportPersistance < Reports::ReportPersistance private def raise_report_not_found(type, id) - raise Reports::NotFound.new("report not found, type:'"+type.to_s+"', id:'"+id.to_s+"'") + raise OpenTox::NotFoundError.new("report not found, type:'"+type.to_s+"', id:'"+id.to_s+"'") end def type_directory(type) @@ -181,72 +181,101 @@ end module Reports - class ReportData < ActiveRecord::Base - include Lib::RDFProvider + #class ReportData < ActiveRecord::Base +# serialize :validation_uris +# serialize :crossvalidation_uris +# serialize :algorithm_uris +# serialize :model_uris +# alias_attribute :date, :created_at + + class ReportData + include DataMapper::Resource - def get_content_as_hash - map = {} - map[:created_at] = created_at - map[:report_uri] = report_uri - map[:report_type] = report_type - map[:validation_uris] = validation_uris - map[:crossvalidation_uris] = crossvalidation_uris - map[:algorithm_uris] = algorithm_uris - map[:model_uris] = model_uris - map + property :id, Serial + property :report_type, String, :length => 255 + property :created_at, DateTime + property :validation_uris, Object + property :crossvalidation_uris, Object + property :model_uris, Object + property :algorithm_uris, Object + + attr_accessor :subjectid + + after :save, :check_policy + private + def check_policy + OpenTox::Authorization.check_policy(report_uri, subjectid) end - def rdf_title - "ValidationReport" + public + def date + created_at end - def uri - report_uri + def report_uri + raise "no id" if self.id==nil + Reports::ReportService.instance.get_uri(self.report_type, self.id) end - LITERALS = [ :created_at, :report_type ] - LITERAL_NAMES = {:created_at => OT["date"] } - OBJECT_PROPERTIES = { :crossvalidation_uris => OT['reportCrossvalidation'], :algorithm_uris => OT['reportAlgorithm'], - :validation_uris => OT['reportValidation'], :model_uris => OT['reportModel'] } - OBJECTS = { :crossvalidation_uris => OT['Crossvalidation'], :algorithm_uris => OT['Algorithm'], - :validation_uris => OT['Validation'], :model_uris => OT['Model'] } - CLASSES = {} - IGNORE = [ :id, :report_uri ] + 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 - serialize :validation_uris - serialize :crossvalidation_uris - serialize :algorithm_uris - serialize :model_uris + 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) + def new_report(report_content, type, meta_data, uri_provider, subjectid=nil) raise "report meta data missing" unless meta_data report = ReportData.new(meta_data) - report.save #to set id - report.attributes = { :report_type => type, :report_uri => uri_provider.get_uri(type, report.id) } + report.subjectid = subjectid + report.report_type = type report.save new_report_with_id(report_content, type, report.id) end - def list_reports(type, filter_params=nil) - #QMRF-STUB - return "1" if type == ReportFactory::RT_QMRF + def list_reports(type, filter_params={}) + filter_params["report_type"]=type unless filter_params.has_key?("report_type") + #ReportData.find_like(filter_params).delete_if{|r| r.report_type!=type}.collect{ |r| r.id } + + filter_params = Lib::DataMapperUtil.check_params(ReportData, filter_params) + # unfortunately, datamapper does not allow searching in Objects + # do filtering for list = Object params manually + list_params = {} + [:validation_uris, :crossvalidation_uris, :algorithm_uris, :model_uris].each do |l| + list_params[l] = filter_params.delete(l) if filter_params.has_key?(l) + end - filter_params = {} unless filter_params - filter_params.each{ |k,v| raise Reports::BadRequest.new("no report-attribute: "+k.to_s) unless ReportData.column_names.include?(k.gsub(/_like$/,"")) } - filter_params[:report_type] = type - ReportData.find(:all, :conditions => filter_params).collect{ |r| r.id } + reports = ReportData.all(filter_params).delete_if{|r| r.report_type!=type} + list_params.each do |k,v| + reports = reports.delete_if{ |r| !r.send(k).include?(v) } + end + reports.collect{ |r| r.id } end def get_report(type, id, format, force_formating, params) - begin - report = ReportData.find(:first, :conditions => {:id => id, :report_type => type}) - rescue ActiveRecord::RecordNotFound - raise Reports::NotFound.new("Report with id='"+id.to_s+"' and type='"+type.to_s+"' not found.") - end + report = ReportData.first({:id => id, :report_type => type}) + raise OpenTox::NotFoundError.new("Report with id='"+id.to_s+"' and type='"+type.to_s+"' not found.") unless report +# 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" @@ -258,14 +287,60 @@ module Reports end end - def delete_report(type, id) - begin - report = ReportData.find(:first, :conditions => {:id => id, :report_type => type}) - rescue ActiveRecord::RecordNotFound - raise Reports::NotFound.new("Report with id='"+id.to_s+"' and type='"+type.to_s+"' not found.") + 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.first({:id => id, :report_type => type}) + raise OpenTox::NotFoundError.new("Report with id='"+id.to_s+"' and type='"+type.to_s+"' not found.") unless report + report.destroy + 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 - ReportData.delete(id) super end end end + +Reports::ReportData.auto_upgrade! +Reports::ReportData.raise_on_save_failure = true + +#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.halt 400,err +# 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 |