#!/usr/local/bin/perl -w

# $Id: search_germplasm,v 1.2 2006/02/02 17:20:58 liya Exp $

=head1 NAME

germplasm_display

=head1 DESCRIPTION

This script is for searching and display germplasms

=cut

# -------------------------------------------------------

use strict;
use Apache::Request;
use Template;
use Tie::IxHash;

use Gramene::Page;
use Gramene::Config;
use Gramene::Gene::GeneDB;

use constant DETAIL_TEMPLATE  => 'germplasm_detail.tmpl';
use constant ERROR_TEMPLATE   => 'gene_error.tmpl';

my $apr         = Apache::Request->new( Apache->request );
my $page        = Gramene::Page->new($apr);
my $config      = Gramene::Config->new;
my $gene_config = $config->get('genes');

my ( $template, $html, $db, $pager );

my %args = $apr->args;
	
eval {

    $template = Template->new(
        {
            INCLUDE_PATH => $gene_config->{'template_dir'},
            PRE_CHOMP    => 1,
            POST_CHOMP   => 1,
            TRIM         => 1,

        }
    );

    $db = Gramene::Gene::GeneDB->new();
    $db->connect_to_db();

    # for germplasm detail display
    my $id = $apr->param('id');
    my $germplasm_id = $id if $id;
    
    my $acc = $apr->param('acc');
    $germplasm_id = $db->get_germplasm_id($acc) if $acc;

    my $germplasm_obj; 

    # ---------------------------------------------------------------
    # germplasm detail search
    #

    if ($germplasm_id) {

	$germplasm_obj = $db->get_germplasm_info($germplasm_id);
    }

    #
    # template processing
    #
    if ($germplasm_obj) {    # germplasm germplasm detail display

        $template->process(
            DETAIL_TEMPLATE,
            {
                gramene_page => $page,
                apr          => $apr,
                germplasm         => $germplasm_obj,
                title        => "Summary for Germplasm &quot;"
                  . $germplasm_obj->{'accession'}
                  . "&quot;"
            },
            \$html
          )
          or $html = $template->error;
    }
};

if ( my $err = $@ ) {
    if ($template) {
        $template->process(
            ERROR_TEMPLATE,
            {
                gramene_page  => $page,
                error_message => $err,
            },
            \$html
          )
          or $html = $template->error;
    }else {
        $html = "Error: $err";
    }
}

$apr->content_type('text/html');
$apr->send_http_header;
$apr->print($html);

# ------------------------------------------------------------

=pod

=head1 AUTHORS

Liya Ren E<lt>ren@cshl.eduE<gt>

=cut

