#!@@_perl_root_@@/bin/perl.exe 
package ParagraphUI;

##########################################################
#                                                        #
# dictyBase Extension of ParagraphUI                     #
#                                                        #
##########################################################




##########################################################
#                                                        #
# was using XML::XSLT, but switched to Sablotron         #
#  because XML::XSLT was unacceptably incomplete         #
#                                                        #
##########################################################

use ParagraphUI_base;

BEGIN { %ParagraphUI:: = %ParagraphUI_base:: }

use XML::Sablotron;
use XML::XPath;
use Cwd;

use DictyBaseConfig;
use dicty::Paragraph::Paragraph;

use lib "/usr/local/dicty/www_dictybase/db/lib/dictyBase/Objects/";
use ConfigPathdictyBase;
use Locus;

our $configPath= ConfigPathdictyBase->new;


###############################################################################
#
#  Note: these xml routines probably take up a lot of overhead.  They
#  are made for single use on a page. If you find yourself using
#  these functions more than once on a page, consider makeing singleton
#  out of the object you are parsing, that way it only gets created once
#
###############################################################################


#
#  accepts ref to xml text, name of xsl template returns html text
#  was using XML::XSLT, but this was an unacceptably incomplete implementation
#
###########################################################################
sub xml2html {
###########################################################################
    my ($self, $xml, $templateName) = @_;

    my $result;
    my $xslDir  = $configPath->dictyBaseXSL;

    open (XSL, "${xslDir}${templateName}");
    undef $/;
    my $xsl = <XSL>;
    close XSL;

    XML::Sablotron::ProcessStrings($xsl, ${$xml}, $result);

    return $result;
}


#
# dictyBase version accepts either HTML text or a DOM object and applys an xslt
#  style sheet to it
#
###########################################################################
sub para2References {
###########################################################################

   my ($self, $xml) = @_;
   my $xp      = XML::XPath->new( xml => ${$xml} );
 
   my @reflist = $xp->find('//reference[     not( @pmid  = preceding::reference/@pmid  ) 
                                         and not( @reference_no = preceding::reference/@reference_no )  ]')->get_nodelist;
   
   my $references = "";

   my $refno = 1;
   my @bgcolor = ("#FFFFFF", "#F5F5F5");

   my %references;

   foreach my $record (@reflist) {
   
      my $reference_no   = $record->find('@reference_no')->string_value;
      my $pubmed_id      = $record->find('@pmid')->string_value;

      my $refObj;

      eval {
         if ($reference_no) {
            $refObj = Reference->new(dbh          => $dbh,
                                     reference_no => $reference_no);
         }
         elsif ($pubmed_id) {

            $refObj = Reference->new(dbh    => $dbh,
                                     pubmed => $pubmed_id);
            $reference_no = $refObj->reference_no();
         }

         my $currBgcolor = (($refno % 2) == 1) ? $bgcolor[1] : $bgcolor[0];

         $references .= Tr(td({bgcolor=>$currBgcolor,
                        valign=>"top",
                        align=>"left"},
                       "$refno)"),
                    td({bgcolor=>$currBgcolor},
                       a(
                         {name=>"$reference_no"}
                        ).
                       $refObj->formatedCitation()
                      )
                   );

      };
      if ($@) {
         my $citation;
         if ($pubmed_id) {
             $citation .= a({-href=>$configUrl->reference_popup( $configUrl->pubmed_url($pubmed_id)  )},
                         img({-src=>$configUrl->dictyBaseImages."pubmedrefsml.gif",
                              -border=>'0',
                              -alt=>"Pubmed Entry"}))."&nbsp;&nbsp;";
         }
         else {
            $citation = "Sorry, an error occured while fetching this citation";
         }

         $references .= Tr(td({bgcolor=>$currBgcolor,
                        valign=>"top",
                        align=>"left"},
                       "$refno)"),
                    td({bgcolor=>$currBgcolor},
                       a(
                         {name=>"$reference_no"}
                        ).
                        $record->string_value.br.
                        $citation
                      )
                   );

      }
   $refno++;
   }
   return $references;
}


###########################################################################
sub displayParagraph {
###########################################################################
    my ($self, %args) = @_;

    my $outputString = "";

    my $locusNo = $args{'locusNo'};
    my $dictyBaseObject = $args{'dictyBaseObject'};
    $dbh = $args{'dbh'};
    my $paragraphObj = $args{'paragraphObj'};

    if (defined $dictyBaseObject) {
	if (!defined $dictyBaseObject->locusNo) {
	    return;
	}
	else {
	    $locusNo = $dictyBaseObject->locusNo;
	}

	if (!defined $dbh) {
	    $dbh = $dictyBaseObject->dbh();
	}
    }

    my $paragraph;
    eval {
	if (!defined $paragraphObj) {
	    my $locusObj = Locus->new(dbh=>$dbh, 
					     locus_no=>$locusNo
					    );
         $paragraph = new dicty::Paragraph::Paragraph( -paragraph_no => $locusObj->paragraph_no() ) if $locusObj->paragraph_no();
	}
	else {
	    $paragraph = new dicty::Paragraph::Paragraph( -paragraph_no => $paragraphObj->paragraph_no() );
         return undef unless $paragraph;
	}
    };
    if ($@) {
     die ($@);
    }

    return undef unless $paragraph;
    my $paraXML = $paragraph->to_string;
   #
   # parse text to retun DOM object
   #
    my $paragraphText = $self->xml2html( \$paraXML,"paragraph.xsl" );

    my $paragraph = (a({name=>"summaryParagraph"}) . 
		     b("Summary: ") . "\n" .
		     $paragraphText . "\n"
		    );

#    # modification information:
#    my $mod_date = (b("Created: ") . "\n" .
#		      $paragraphObj->getDateWritten()
#		    . " " . $paragraphObj->getWrittenBy()
#		    . br . "\n" 
#		   );
#
#    $mod_date .= (b("Modified: ") . "\n" .
#		  $paragraphObj->getLastModifiedDate()
#		  . " " # . $paragraphObj->getLastModifiedBy()
#		  . br . "\n" 
#		  . br . "\n\n"
#		 );

    # load references from the database

   $references = $self->para2References( \$paraXML );
   $references = (b("Summary References:") . 
		  table({border=>0},
			$references
		       )
		 );

    $outputString .= (
		     $paragraph . p . "\n" 
#		     . $mod_date . p . "\n"
		     . $references . "\n"
		    );

    return $outputString;
}

########################################################################
1;
########################################################################

