package WebPAC::Output::Estraier;

use warnings;
use strict;

use base qw/WebPAC::Common/;

use HyperEstraier;
use Text::Iconv;
use Data::Dumper;

=head1 NAME

WebPAC::Output::Estraier - Create Hyper Estraier full text index

=head1 VERSION

Version 0.01

=cut

our $VERSION = '0.01';

=head1 SYNOPSIS

Create full text index using Hyper Estraier index from data with
type C<search>.

=head1 FUNCTIONS

=head2 new

Connect to Hyper Estraier index using HTTP

 my $est = new WebPAC::Output::Estraier(
 	url => 'http://localhost:1978/node/webpac2',
	user => 'admin',
	passwd => 'admin',
	database => 'demo',
	encoding => 'iso-8859-2',
 );

Options are:

=over 4

=item url

URI to C<estmaster> node

=item user

C<estmaster> user with sufficient rights

=item passwd

password for user

=item database

name of database from which data comes

=item encoding

character encoding of C<data_structure> if it's differenet than C<ISO-8859-2>
(and it probably is). This encoding will be converted to C<UTF-8> for
Hyper Estraier.

=back

Name of database will be used to form URI of documents in index.

=cut

sub new {
	my $class = shift;
        my $self = {@_};
        bless($self, $class);

	my $log = $self->_get_logger;

	foreach my $p (qw/url user passwd/) {
		$log->logdie("need $p") unless ($self->{$p});
	}

	$log->info("opening Hyper Estraier index $self->{'url'}");

	$self->{'db'} = HyperEstraier::Node->new($self->{'url'});
	$self->{'db'}->set_auth($self->{'user'}, $self->{'passwd'});

	my $encoding = $self->{'encoding'} || 'ISO-8859-2';
	$log->info("using encoding $encoding");

	$self->{'iconv'} = new Text::Iconv($encoding, 'UTF-8') or
		$log->die("can't create conversion from $encoding to UTF-8");

	$self ? return $self : return undef;
}


=head2 add

Adds one entry to database.

  $est->add(
  	id => 42,
	ds => $ds,
	type => 'display',
	url_prefix => 'database name',
	text => 'optional text from which snippet is created',
  );

This function will create  entries in index using following URI format:

  C<file:///database%20name/000>

Each tag in C<data_structure> with specified C<type> will create one
attribute and corresponding hidden text (used for search).

=cut

sub add {
	my $self = shift;

        my $args = {@_};

	my $log = $self->_get_logger;

	my $database = $self->{'database'} || $log->logconfess('no database in $self');
	$log->logconfess('need db in object') unless ($self->{'db'});

	foreach my $p (qw/id ds type/) {
		$log->logdie("need $p") unless ($args->{$p});
	}

	my $type = $args->{'type'};
	my $mfn = $args->{'id'};

	my $uri = "file:///$type/$database/$mfn";
	$log->debug("creating $uri");

	my $doc = HyperEstraier::Document->new;
	$doc->add_attr('@uri', $self->{'iconv'}->convert($uri) );

	$log->debug("ds = ", sub { Dumper($args->{'ds'}) } );

	# filter all tags which have type defined
	my @tags = grep {
		defined( $args->{'ds'}->{$_}->{$type} )
	} keys %{ $args->{'ds'} };

	$log->debug("tags = ", join(",", @tags));

	return unless (@tags);

	foreach my $tag (@tags) {

		my $vals = join(" ", @{ $args->{'ds'}->{$tag}->{$type} });

		$log->logconfess("no values for $tag/$type") unless ($vals);

		$vals = $self->{'iconv'}->convert( $vals ) or
			$log->die("can't convert '$vals' to UTF-8");

		$doc->add_attr( $tag, $vals );
		$doc->add_hidden_text( $vals );
	}

	my $text = $args->{'text'};
	if ( $text ) {
		$text = $self->{'iconv'}->convert( $text ) or
			$log->die("can't convert '$text' to UTF-8");
		$doc->add_text( $text );
	}

	$log->debug("adding ", sub { $doc->dump_draft } );
	$self->{'db'}->put_doc($doc) || $log->die("can't add document $uri to index");

	return 1;
}

=head1 AUTHOR

Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >>

=head1 COPYRIGHT & LICENSE

Copyright 2005 Dobrica Pavlinusic, All Rights Reserved.

This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.

=cut

1; # End of WebPAC::Output::Estraier
