--- /dev/null
+package WebPAC::Output::Webpacus;
+
+use warnings;
+use strict;
+
+use base qw/WebPAC::Common WebPAC::Output Class::Accessor/;
+__PACKAGE__->mk_accessors(qw(
+ path
+ database
+));
+
+use File::Path;
+use Data::Dump qw/dump/;
+use WebPAC::Common qw/force_array/;
+use Carp qw/confess/;
+use Cwd;
+
+use Jifty;
+
+=head1 NAME
+
+WebPAC::Output::Webpacus - integrate WebPAC front-end with Jifty back-end
+
+=head1 VERSION
+
+Version 0.01
+
+=cut
+
+our $VERSION = '0.01';
+
+=head1 SYNOPSIS
+
+Does black magic to sync data between WebPAC and Webpacus, web front-end
+implement in Jifty
+
+=head1 FUNCTIONS
+
+=head2 new
+
+ my $output = new WebPAC::Output::Webpacus({
+ path => '/path/to/Webpacus',
+ database => 'demo',
+ });
+
+=head2 init
+
+ $output->init;
+
+=cut
+
+sub init {
+ my $self = shift;
+
+ my $log = $self->_get_logger;
+
+ foreach my $p (qw/path database/) {
+ $log->logdie("need $p") unless ($self->$p);
+ }
+
+ my $path = $self->path;
+
+ $log->logdie("Webpacus path $path not found: $!") unless -d $path;
+
+ my $config_path = "$path/etc/config.yml";
+
+ $log->logdie("expected Webpacus config at $config_path: $!") unless -e $config_path;
+
+ $self->{fields} = {};
+
+}
+
+
+=head2 add
+
+Adds one entry
+
+ $est->add( 42, $ds );
+
+=cut
+
+sub add {
+ my $self = shift;
+
+ my ( $id, $ds ) = @_;
+
+ my $log = $self->_get_logger;
+ $log->logdie("need id") unless defined $id;
+ $log->logdie("need ds") unless $ds;
+
+ $log->debug("id: $id ds = ",sub { dump($ds) });
+
+ my $hash = $self->ds_to_hash( $ds, 'sorted' ) || return;
+
+ foreach my $f ( keys %$hash ) {
+ $self->{fields}->{$f}++;
+ }
+
+ return 1;
+}
+
+=head2 finish
+
+Close index
+
+ $index->finish;
+
+=cut
+
+sub finish {
+ my $self = shift;
+
+ my $log = $self->_get_logger();
+
+ $log->info("syncing search fields");
+
+ my $fields = $self->{fields} || confess "no fields?";
+ my $path = $self->path || confess "no path?";
+
+ $log->debug("fields = ", sub { dump $fields });
+
+ $log->info("using Webpacus installation: $path");
+
+ my $webpac_dir = getcwd();
+
+ chdir $path || $log->logdie("can't chdir($path) $!");
+
+# push @INC, $path;
+ Jifty->new();
+ my $system_user = Webpacus::CurrentUser->superuser;
+ my $o = Webpacus::Model::Search->new(current_user => $system_user);
+
+ my $count = 0;
+
+ foreach my $field ( keys %$fields ) {
+ $log->debug("adding search field: $field");
+ $o->create( name => $field ); # || $log->logdie("can't add $field");
+ $count++;
+ }
+
+ return $count;
+
+}
+
+
+=head1 AUTHOR
+
+Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >>
+
+=head1 COPYRIGHT & LICENSE
+
+Copyright 2007 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;
--- /dev/null
+#!/usr/bin/perl -w
+
+use Test::More tests => 10;
+use Test::Exception;
+use Cwd qw/abs_path/;
+use Data::Dump qw/dump/;
+use blib;
+use strict;
+
+BEGIN {
+use_ok( 'WebPAC::Output::Webpacus' );
+}
+
+my $debug = shift @ARGV;
+
+ok(my $abs_path = abs_path($0), "abs_path");
+$abs_path =~ s#/[^/]*$#/#; #
+diag "abs_path: $abs_path";
+my $path = "$abs_path/sorted/";
+
+ok(my $out = new WebPAC::Output::Webpacus({
+ path => '/data/Webpacus2/',
+ database => 'test',
+ clean => 1,
+ debug => $debug,
+}), "new");
+
+ok( $out->init, 'init' );
+
+my $ds = {
+ 'Source' => {
+ 'name' => 'Izvor: ',
+ 'sorted' => [ 'foo' ]
+ },
+ 'ID' => {
+ 'sorted' => 'id',
+ },
+ 'Array' => {
+ 'sorted' => [ qw/a1 a2 s3 a4 a5/ ],
+ },
+};
+
+throws_ok { $out->add( ) } qr/need id/, 'add without params';
+throws_ok { $out->add( 42 ) } qr/need ds/, 'add without ds';
+
+ok( $out->add( 42, $ds ), 'add 42' );
+
+ok( $out->add( 99, { foo => { sorted => 'bar' } } ), 'add 99' );
+
+ok( $out->add( 100, { foo => { sorted => [ qw/foo bar baz/ ] } } ), 'add 100' );
+
+diag "path: ",$out->path;
+
+ok( $out->finish, 'finish' );
+