#!/usr/bin/env ruby
#
# We find resources with a dc:title substring matching some string
# ...then query the RSS feed.

require '../squish'
require '../../basicrdf'
require 'dbi'

DBI_DRIVER = 'DBI:Pg:test1'
DBI_USER = 'danbri'
DBI_PASS=''

qs = 'economics'
dc_title='http://purl.org/dc/elements/1.1/title' 

service = DBIDataService.new(DBI_DRIVER,DBI_USER,DBI_PASS)
service.defrag 			# smush together nodes we know represent same thing

service.addAllSuperProperties  	# add in a few implied properties
				# we should know rss:title-rdfs:subPropertyOf->dc:title

matches = service.findPropertySubstring qs, dc_title
		# find nodes with a dc_title substring matching our query string
		# matches.each do |v,k| puts "dc:title matched: in '#{k}' id: #{v} " end

# query the event descriptions:
#
sq = 'SELECT ?item, ?title, ?etype, ?org, ?loc, ?start, ?end, 
	WHERE 
	(rss::title ?item ?title) 
	(ev::type ?item ?etype)
	(ev::organizer ?item ?org)
        (ev::location ?item ?loc)
	(ev::startdate ?item ?start)
	(ev::enddate ?item ?end)
	USING
	rss for http://purl.org/rss/1.0/
	ev for http://purl.org/rss/1.0/modules/event/
   	foaf for http://xmlns.com/foaf/0.1/ '

  query = SquishQuery.new()
  DBI.connect( DBI_DRIVER, DBI_USER, DBI_PASS) do | dbh |

  dbh.select_all( query.parseFromText(sq).toSQLQuery  ) do | row |  
    r = ResultRow.new(row) 

    next unless (matches[r.item] != nil) # results subset, needs better api
 
    puts "#############################################################\n\n"

    puts "#{r.title}   From: #{r.start} To: #{r.end} Loc: #{r.loc}"
    puts "URI: #{r.item} Organised by: #{r.org} Type: #{r.etype} \n\n"

   end
end



