:- ensure_loaded(common).
cvs_id('$Id: ontology_page_view.pl,v 1.4 2004/10/13 19:35:06 sandro Exp $').
/*

   Generate a page which is good HTML to serve at the
   ontology namespace address for HashURI ontologies.

   We use  <a name="fragmentId"></a>  in the HTML to get
   the user's browser to show the right part of the
   page.   We don't put any text in there, or use "id",
   because we want to reduce the risk of someone using
   that URI to talk about a peice of HTML, perhaps in
   an annotation.


  TODO:
      show ontology metadata (title, author, license, etc)
      allow markup in text fields
      show more than one comment/description, if present
      show extra (unexpected) triples somehow
      figure out what h2 should link to
      for classess show what it is domain/range of
  
*/

ontology_page_view_reply(Request, ReqId) :-
	member(path('/ontology_page_view'), Request),
	memberchk(search(QueryParms), Request),
	memberchk(focus=Focus, QueryParms),
	write_page(ReqId, +ontology_page_view(Focus), '').

% we'll want a different/custom-style view [diff title!]
% for hosting

ontology_page_view(Focus, H) :-

	% find all the terms coming from here, which start with its address

	% cribbed from terms_in_source/3 (in source_views).
	debug(opv, 'Focus: ~q', [Focus]),
	atom_concat(Focus, '#', Prefix),
	findall(Term,
		(  term_source(Term, Focus),
		   atom_concat(Prefix, _Fragment, Term)
		),
		DupTermsTerms 
	       ),
	list_to_set(DupTermsTerms, Terms),
	sort(Terms, SortedTerms),
	
	%sublist(is_class(Q), Terms, Classes),
	%sublist(is_property(Q), Terms, Properties),
	%sublist(neither(Q), Terms, OtherTerms),
	%   quick hack -- we want the MOST RECENT one, really, no?
	rdf(Ret, retr:finalResource, Focus),     % @@ rdf/3 !
	rdf(Ret, retr:graph, Source),            % @@ rdf/3 !
        Q=[sources([Source])],
	maplist(ontology_term_view(Q), Terms, Blocks),
 	H=[+ontology_term_index(Q, SortedTerms, Prefix) | Blocks].

ontology_term_index(Q, Terms, Prefix, H) :-
	H = div(class=ontologyTermIndex,
		id='__index',
		h2('Term Index'),
		+thing_list([link_predicate(mlp(Prefix)) | Q], Terms)
	       ).

mlp(Prefix, Focus, Link) :-
	atom_concat(Prefix, Fragment, Focus),
	atom_concat('#', Fragment, Link).

ontology_term_view(Q, Term, H) :-
	rdf_split_url(_NS, Name, Term),
	gensym(Name, Name2),
	H = div(id=Name2, 
		div(class=fragtag, a(name=Name, [])),
		+xpara(Q, Term, Name)
	       ).



xpara(Q, Focus, _Name, HTree) :-
	comment(Q, Focus, SummaryText),
	findall(Property,
		(   interesting_property(P),
		    rdf_global_id(P, PG),
		    rdfq(Q, Focus, PG, V),
		    Property=tr(th(+word(Q, PG)), td(+word(Q, V)))
		),
		PropertiesU),
	sort(PropertiesU, Properties),
	uri_escape(Focus, QuotedFocus),
	atom_concat('http://www.w3.org/2004/ontaria/basic?focus=',
		    QuotedFocus, OntariaLink),
	HTree = div(class=ontologyTerm,
    		    %div(class=ontologyTermQName,
		%	a(href=Focus, tt("ns:", Name)) %,
		%	% a(href=yyy, 'Meta')
		%       ),

    		    div(class=linkToOntaria,
			a(href=OntariaLink, 'View in Ontaria')
		       ),
		    div(class=ontologyTermURL,a(href=Focus, tt(Focus))
		       ),
		    h2(class=summaryTitle,
		       +word(Q, Focus),
		       span(class=summaryTitleComment,
			    +leaf_classes(Q, Focus))),
		    table(class=ptable4, Properties),
		    p(class=desc, SummaryText),
		    p(class=returnLink, a(href='#__index', 'Return to Index'))
		   ).

interesting_property(rdfs:range).
interesting_property(rdfs:domain).
interesting_property(rdfs:subClassOf).
interesting_property(rdfs:subPropertyOf).
interesting_property(rdfs:seeAlso).
interesting_property(rdfs:isDefinedBy).
% interesting_property(_).

