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' ) || next; |
103 |
|
104 |
foreach my $f ( keys %$hash ) { |
105 |
$hash->{$f} = join(' ', @{ $hash->{$f} }) if ref($hash->{$f}) eq 'ARRAY'; |
106 |
} |
107 |
|
108 |
$log->debug("data: ", sub { dump( $hash ) }); |
109 |
|
110 |
my ( $m_id, $msg ) = $self->{_model}->load_or_create( %$hash ); |
111 |
$log->debug("ID: $m_id $msg"); |
112 |
|
113 |
push @{ $stat->{$msg} }, $m_id; |
114 |
|
115 |
return 1; |
116 |
} |
117 |
|
118 |
=head2 finish |
119 |
|
120 |
$output->finish; |
121 |
|
122 |
=cut |
123 |
|
124 |
sub finish { |
125 |
my $self = shift; |
126 |
|
127 |
my $log = $self->_get_logger; |
128 |
$log->debug("stats: ", dump( $stat )); |
129 |
|
130 |
return 1; |
131 |
} |
132 |
|
133 |
=head1 AUTHOR |
134 |
|
135 |
Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >> |
136 |
|
137 |
=head1 COPYRIGHT & LICENSE |
138 |
|
139 |
Copyright 2007 Dobrica Pavlinusic, All Rights Reserved. |
140 |
|
141 |
This program is free software; you can redistribute it and/or modify it |
142 |
under the same terms as Perl itself. |
143 |
|
144 |
=cut |
145 |
|
146 |
1; |