1 |
package WebPAC::Output::Jifty; |
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 |
model |
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 |
use File::Slurp; |
18 |
|
19 |
use Jifty; |
20 |
|
21 |
=head1 NAME |
22 |
|
23 |
WebPAC::Output::Jifty - fill Jifty model from WebPAC data |
24 |
|
25 |
=cut |
26 |
|
27 |
our $VERSION = '0.01'; |
28 |
|
29 |
=head1 SYNOPSIS |
30 |
|
31 |
This is simple output which will fill one Jifty model with data |
32 |
|
33 |
=head1 FUNCTIONS |
34 |
|
35 |
=head2 new |
36 |
|
37 |
my $output = new WebPAC::Output::Jifty({ |
38 |
path => '/path/to/Jifty', |
39 |
model => 'Webpacus::Model::UFO', |
40 |
}); |
41 |
|
42 |
=head2 init |
43 |
|
44 |
$output->init; |
45 |
|
46 |
=cut |
47 |
|
48 |
sub init { |
49 |
my $self = shift; |
50 |
|
51 |
my $log = $self->_get_logger; |
52 |
|
53 |
foreach my $p (qw/path model/) { |
54 |
$log->logdie("need $p") unless ($self->$p); |
55 |
} |
56 |
|
57 |
my $path = $self->path; |
58 |
|
59 |
$log->logdie("Jifty path $path not found: $!") unless -d $path; |
60 |
|
61 |
my $config_path = "$path/etc/config.yml"; |
62 |
|
63 |
$log->logdie("expected Jifty config at $config_path: $!") unless -e $config_path; |
64 |
|
65 |
my $model = $self->model || confess "no model?"; |
66 |
|
67 |
$log->info("init Jifty at $path using model $model"); |
68 |
my $webpac_dir = getcwd(); |
69 |
chdir $path || $log->logdie("can't chdir($path): $!"); |
70 |
Jifty->new(); |
71 |
chdir $webpac_dir || $log->logdie("can't chdir($webpac_dir): $!"); |
72 |
|
73 |
my $system_user = Webpacus::CurrentUser->superuser; |
74 |
$self->{_model} = $model->new(current_user => $system_user); |
75 |
|
76 |
} |
77 |
|
78 |
|
79 |
=head2 add |
80 |
|
81 |
Adds one entry |
82 |
|
83 |
$output->add( 42, $ds ); |
84 |
|
85 |
=cut |
86 |
|
87 |
my $stat; |
88 |
|
89 |
sub add { |
90 |
my $self = shift; |
91 |
|
92 |
my ( $id, $ds ) = @_; |
93 |
|
94 |
my $log = $self->_get_logger; |
95 |
$log->logdie("need id") unless defined $id; |
96 |
$log->logdie("need ds") unless $ds; |
97 |
|
98 |
$log->debug("id: $id ds = ",sub { dump($ds) }); |
99 |
|
100 |
my $stat; |
101 |
|
102 |
my $hash = $self->ds_to_hash( $ds, 'display', single_values => 1 ) || next; |
103 |
|
104 |
$log->debug("data: ", sub { dump( $hash ) }); |
105 |
|
106 |
my ( $m_id, $msg ) = $self->{_model}->load_or_create( %$hash ); |
107 |
$log->debug("ID: $m_id $msg"); |
108 |
|
109 |
push @{ $stat->{$msg} }, $m_id; |
110 |
|
111 |
return 1; |
112 |
} |
113 |
|
114 |
=head2 finish |
115 |
|
116 |
$output->finish; |
117 |
|
118 |
=cut |
119 |
|
120 |
sub finish { |
121 |
my $self = shift; |
122 |
|
123 |
my $log = $self->_get_logger; |
124 |
$log->debug("stats: ", dump( $stat )); |
125 |
|
126 |
return 1; |
127 |
} |
128 |
|
129 |
=head1 AUTHOR |
130 |
|
131 |
Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >> |
132 |
|
133 |
=head1 COPYRIGHT & LICENSE |
134 |
|
135 |
Copyright 2007 Dobrica Pavlinusic, All Rights Reserved. |
136 |
|
137 |
This program is free software; you can redistribute it and/or modify it |
138 |
under the same terms as Perl itself. |
139 |
|
140 |
=cut |
141 |
|
142 |
1; |