r1399@llin: dpavlin | 2007-10-31 11:19:39 +0100
[webpac2] / lib / WebPAC / Output / Webpacus.pm
1 package WebPAC::Output::Webpacus;
2
3 use warnings;
4 use strict;
5
6 use base qw/WebPAC::Common WebPAC::Output Class::Accessor/;
7 __PACKAGE__->mk_accessors(qw(
8         path
9         database
10 ));
11
12 use File::Path;
13 use Data::Dump qw/dump/;
14 use WebPAC::Common qw/force_array/;
15 use Carp qw/confess/;
16 use Cwd;
17
18 use Jifty;
19
20 =head1 NAME
21
22 WebPAC::Output::Webpacus - integrate WebPAC front-end with Jifty back-end
23
24 =head1 VERSION
25
26 Version 0.01
27
28 =cut
29
30 our $VERSION = '0.01';
31
32 =head1 SYNOPSIS
33
34 Does black magic to sync data between WebPAC and Webpacus, web front-end
35 implement in Jifty
36
37 =head1 FUNCTIONS
38
39 =head2 new
40
41  my $output = new WebPAC::Output::Webpacus({
42         path => '/path/to/Webpacus',
43         database => 'demo',
44  });
45
46 =head2 init
47
48  $output->init;
49
50 =cut
51
52 sub init {
53         my $self = shift;
54
55         my $log = $self->_get_logger;
56
57         foreach my $p (qw/path database/) {
58                 $log->logdie("need $p") unless ($self->$p);
59         }
60
61         my $path = $self->path;
62
63         $log->logdie("Webpacus path $path not found: $!") unless -d $path;
64
65         my $config_path = "$path/etc/config.yml";
66
67         $log->logdie("expected Webpacus config at $config_path: $!") unless -e $config_path;
68
69         $self->{fields} = {};
70
71 }
72
73
74 =head2 add
75
76 Adds one entry
77
78   $est->add( 42, $ds );
79
80 =cut
81
82 sub add {
83         my $self = shift;
84
85         my ( $id, $ds ) = @_;
86
87         my $log = $self->_get_logger;
88         $log->logdie("need id") unless defined $id;
89         $log->logdie("need ds") unless $ds;
90
91         $log->debug("id: $id ds = ",sub { dump($ds) });
92
93         my $hash = $self->ds_to_hash( $ds, 'sorted' ) || return;
94
95         foreach my $f ( keys %$hash ) {
96                 $self->{fields}->{$f}++;
97         }
98
99         return 1;
100 }
101
102 =head2 finish
103
104 Close index
105
106  $index->finish;
107
108 =cut
109
110 sub finish {
111         my $self = shift;
112
113         my $log = $self->_get_logger();
114
115         $log->info("syncing search fields");
116
117         my $fields = $self->{fields} || confess "no fields?";
118         my $path = $self->path || confess "no path?";
119
120         $log->debug("fields = ", sub { dump $fields });
121
122         $log->info("using Webpacus installation: $path");
123
124         my $webpac_dir = getcwd();
125
126         chdir $path || $log->logdie("can't chdir($path) $!");
127
128 #       push @INC, $path;
129         Jifty->new();
130         my $system_user = Webpacus::CurrentUser->superuser;
131         my $o = Webpacus::Model::Search->new(current_user => $system_user);
132
133         my $count = 0;
134
135         foreach my $field ( keys %$fields ) {
136                 $log->debug("adding search field: $field");
137                 $o->create( name => $field ); # || $log->logdie("can't add $field");
138                 $count++;
139         }
140
141         return $count;
142
143 }
144
145
146 =head1 AUTHOR
147
148 Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >>
149
150 =head1 COPYRIGHT & LICENSE
151
152 Copyright 2007 Dobrica Pavlinusic, All Rights Reserved.
153
154 This program is free software; you can redistribute it and/or modify it
155 under the same terms as Perl itself.
156
157 =cut
158
159 1;