#!/usr/bin/env ruby
#
# We find resources with a dc:title substring matching some string then
# query the RSS feed. We generate a 3rd result set by matching the first
# two result sets on their common columns.
#
# Dan Brickley <danbri@w3.org>
# todo:
#  - wrap SOAP query in Squish
#
# Usage:
#
# we load a couple of sql scripts which populate the db with data
# 
# 	psql test1 < examples.sql
# 	psql test1 < events.sql
#	./joiner.rb

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

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


#########################################################################
# Three queries. 
# The 1st represents a query we answer via a custom 'substring' API
# The 2nd is a normal Squish query (answered by a DBIDataService)
# The 3rd is implemented by merging results from 1st and 2nd via match()
# (note: only sq1 is fully used as a query)

sq1 = 'SELECT ?item, ?title, 
	WHERE 
	 (dc::title ?item ?title) 
	 (s::substring ?title "economics") 
	USING dc for http://purl.org/dc/elements/1.1/ 
	      s for http://www.w3.org/2001/12/rubyrdf/util/stringvocab'

sq2 = '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/ '

sq3 = '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)
	 (s::substring ?title "economics") 
	USING
	rss for http://purl.org/rss/1.0/
        s for http://www.w3.org/2001/12/rubyrdf/util/stringvocab
	ev for http://purl.org/rss/1.0/modules/event/
   	foaf for http://xmlns.com/foaf/0.1/ '

# parse queries and associate with their (initially empty) result sets:
#
pq1 = SquishQuery.new().parseFromText(sq1) 
results1=ResultSet.new(pq1)
pq2 = SquishQuery.new().parseFromText(sq2)
results2 = ResultSet.new(pq2)
pq3 = SquishQuery.new()
pq3.parseFromText(sq3)


## Query 1: items and titles where dc:title has a substring of ...
##
qs = 'economics'
dc_title='http://purl.org/dc/elements/1.1/title' 
service = DBIDataService.new(DBI_DRIVER,DBI_USER,DBI_PASS).defrag
service.addAllSuperProperties  	
matches = service.findPropertySubstring qs, dc_title
begin
  matches.each do |uri,value| 
    results1.push ResultRow.new( {'item'=> uri, 'title' => value } ) 
  end
rescue
    puts "Match failed. Be sure to load dataset(s)!"
end


## Query 2: events and their characteristics
## 
DBI.connect( DBI_DRIVER, DBI_USER, DBI_PASS) do | dbh |
  dbh.select_all( pq2.toSQLQuery  ) do | row | 
    results2.push ResultRow.new(row.to_h) 
  end
end


## Query 3 (merge 1 and 2): events whose dc:title matches the substring
##
results3 = results1.match(results2) # merge the two tables
### BROKEN COS I REMOVED query arg from match? pq3.vars needs adding?
results3.each do |hit|
  puts "#{hit.title}   From: #{hit.start} To: #{hit.end} Loc: #{hit.loc}"
  puts "URI: #{hit.item} Organised by: #{hit.org} Type: #{hit.etype}\n\n"
end



=begin

Sample output (for search constrained on dc:title substring 'economics'):

Computing in Economics and Finance (8th International Conference)   From: 2002-06-26 To: 2002-06-29 Loc: Aix en Provence, France
URI: http://pythie.cepremap.cnrs.fr/sce2002/ Organised by: Society for Computational Economics Type: Conference

5th International Business and Economics Conference   From: 2002-10-11 To: 2002-10-12 Loc: De Pere, USA 
URI: http://www.sncibec.org/ Organised by: St. Norbert College Type: Conference


=end
