/*

  html views of the data from retrieve.pl


  output like:

  _Dereference_ yielded {label} {reltime}, expires {reltime}.  [Refresh]
  _Dereference_ yielded {label} {reltime}, EXPIRED {reltime},
      but _refresh failed_. [Retry]
  _Dereference_ yielded {label} {reltime}, EXPIRED {reltime},
      _refresh_ IN PROGRESS.
  _Dereference_ FAILED {reltime}.  [Retry]
  _Dereference_ IN PROGRESS_, began {reltime}.
  Dereference not yet attempted.

*/


/*
  
Dereference (_?_) not applicable for a tag: URI.
  (file:, mailto:, mid:, cid:, )
Dereference not currently implemented for the ftp: scheme.
          https:

Dereference: _The Foo Ontology_ (Retrieved 12 minutes ago, expires in 8
minutes.  Last modified 6 weeks ago.)  [reload now]
Dereference: _The Foo Ontology_ (Retrieved 12 minutes ago, expires in 8
minutes.  Last modified 6 weeks ago.)  [reload now]
 
   (_Retrieved_ ~a ago; scheduled (_?_) to be refreshed (_?_) in ~a.)
   [refresh now]

   (_Retrieved_ ~a ago; _REFRESH FAILED_ (500 internal server error)
   ~ago; retry scheduled in ~a.)
   [retry now]

Dereference FAILED_ (500 internal server error) ~ago; retry scheduled
in ~a.)  [retry now]


*/
:- ensure_loaded(common).
cvs_id('$Id: cache_views.pl,v 1.22 2005/04/14 22:17:40 sandro Exp $').

:- ensure_loaded(rdfpage).
:- ensure_loaded(webload).
:- debug(cache_view).

para_on_deref_status(Q, Focus, p(class=derefStatus, H)) :-
	rdfpage_uri(Focus),
 	!,
	H=[+deref(Q),
           ' not needed; page resides in this server.'].

para_on_deref_status(Q, Focus, p(class=derefStatus, H)) :-
        uri_scheme(Focus, Scheme),			
	no_deref(Scheme, Message),
	!,
	H=[+deref(Q),
           ' ', Message, ' for URI scheme "', Scheme, '".'].


para_on_deref_status(Q, Focus, p(class=derefStatus, H)) :-
        uri_scheme(Focus, Scheme),
	yes_deref(Scheme),
	!,
	debug(cache_view, 'normal http path on ~a', Focus),
	uri_stem(Focus, Stem),
	(   most_recent_retrieval_success(Stem, Success, SuccessTime)
	->  true
	;   Success=fail,
	    AttemptTime = 0
	),
	(   most_recent_retrieval_attempt(Stem, Attempt, AttemptTime)
	->  true
	;   Attempt=fail,
	    AttemptTime = 0
	),
	status_message(Q, Focus, 'Dereference', Stem,
		      Success, SuccessTime,
		      Attempt, AttemptTime,
		      H).

para_on_deref_status(Q, Focus, p(class=derefStatus, H)) :-
	uri_scheme(Focus, Scheme),
	H=[+deref(Q),
           ' not implemented for unknown URI scheme ("', Scheme, '").'].

status_message(Q, Focus, Label, Stem, fail, _SuccessTime, fail, _AttemptTime, H) :-
	H=[Label, ' not yet attempted.  ', +try_now(Q, Focus, 'Try Now.', Stem)].

status_message(Q, Focus, _Label, Stem, fail, _SuccessTime, Attempt, AttemptTime, H) :-
	rdfpage(Attempt, rdf:type, retr:'FailedRetrieval'),
	% rdfpage(Attempt, retr:endTime, literal(unixtime(T))),
	H=[ +deref(Q),
	    ' started ', +reltime(AttemptTime), ', ',
	    span(class=failed, 'failed'),
	    +details(Q, Attempt),
	    +try_now(Q, Focus, 'Try Again', Stem)
	  ].

status_message(Q, Focus, _Label, Stem, fail, _SuccessTime, Attempt, AttemptTime, H) :-
	H=[ +deref(Q),
	    ' started ', +reltime(AttemptTime), ' ',
	    span(class=inProgress, 'in progress.'),
	    +details(Q, Attempt),
	    ' (use browser\'s "refresh" to update.) ',
	    +try_now(Q, Focus, 'Refresh', Stem)
	  ].


status_message(Q, Focus, _Label, Stem, Success, SuccessTime, Attempt, AttemptTime, H) :-
	rdfpage(Success, retr:finalResource, Yield),
	% expiration(Success, _ExpireTime),
	(   AttemptTime > SuccessTime
	->  ExtraText = +status_message(Q, Focus, 'Refresh', Stem, fail, _, Attempt, AttemptTime)
	;   ExtraText = []
	),
	H=[ +deref(Q),
	    ' yielded ', +word(Q, Yield), ' ',
	    +reltime(SuccessTime),
	   % ', expiration ',
	   % +reltime(_ExpireTime), '.  ',
	    ExtraText,
	    +details(Q, Attempt),
	    +try_now(Q, Focus, 'Refresh', Stem)
	  ].

details(Q, Attempt, H) :-
	H = [' [', +link(Q, Attempt, details), '] '].

deref(_Q, 'Dereference').
      % +helplink(Q, dereference, 'Dereference')
	
no_deref(tag, 'not applicable').
no_deref(mailto, 'not applicable').
no_deref(mid, 'not applicable').
no_deref(cid, 'not applicable').
no_deref(ftp, 'not implemented').
no_deref(file, 'not implemented').

yes_deref(http).

/*try_now(_Q, Focus, Message, _URI, H) :-
	H =  span(form(method=get, action=basic,
		  input(type=hidden, name=focus, value=Focus),
		  % input(type=hidden, name=uri, value=URI),
		  input(type=hidden, name=refresh, value='1'),
		  input(type=submit, value=Message)
		 )).
*/


try_now(Q, _Focus, _Message, _URI, H) :-
	H =  span(' [', a(href= \self_uri(basic, Q, [refresh=1]),
			 'refresh data'), ']' ).


%try_now(_Q, _Focus, _Message, _URI, span(' ')).


/*


  para_on_deref_status([ sources([ 'http://www.w3.org/2004/03/retr/ont.rdf'
				   | _G1848
				   ]),
			   (focus = 'http://www.w3.org/2004/03/retr/RDFSource'),
			   (stacking = ''),
			   stacking([])
			 ],
			 'http://www.w3.org/2004/03/retr/RDFSource', H).

*/





