r1534@llin: dpavlin | 2007-11-04 17:20:48 +0100
authorDobrica Pavlinusic <dpavlin@rot13.org>
Sun, 4 Nov 2007 16:20:55 +0000 (16:20 +0000)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Sun, 4 Nov 2007 16:20:55 +0000 (16:20 +0000)
 sync changes in WebPAC with latest model in Webpacus

git-svn-id: svn+ssh://mjesec/home/dpavlin/svn/webpac2/trunk@997 07558da8-63fa-0310-ba24-9fe276d99e06

lib/WebPAC/Output/Webpacus.pm

index 1a2bffe..1a88752 100644 (file)
@@ -25,7 +25,7 @@ WebPAC::Output::Webpacus - integrate WebPAC front-end with Jifty back-end
 
 =cut
 
-our $VERSION = '0.02';
+our $VERSION = '0.03';
 
 =head1 SYNOPSIS
 
@@ -126,28 +126,63 @@ sub finish {
 
        $log->debug("fields = ", sub { dump $fields });
 
+       $log->info("init Jifty");
        my $path = $self->path || confess "no path?";
        my $webpac_dir = getcwd();
        chdir $path || $log->logdie("can't chdir($path) $!");
+       Jifty->new();
 
        my $affected = 0;
 
        foreach my $type ( $self->consume_outputs ) {
                next unless defined $fields->{$type};
-               $affected += $self->_sync_field( $fields->{$type} );
+               $affected += $self->_sync_field(
+                       $self->database, $type, $fields->{$type}
+               );
        }
 
+
+
+       my $glue_path = "$path/lib/Webpacus/Webpac.pm";
+
+       $log->debug("creating clue class Webpacus::Webpac at $glue_path");
+
+       my $glue = <<"_END_OF_GLUE_";
+package Webpacus::Webpac;
+
+=head1 NAME
+
+Webpacus::Webpac - configuration exported from WebPAC
+
+=cut
+
+use strict;
+use warnings;
+
+sub index_path { '/data/webpac2/var/kinosearch/' };
+
+1;
+_END_OF_GLUE_
+
+       $log->debug("glue source:\n$glue");
+
+       write_file( $glue_path, $glue ) || $log->logdie("can't create $glue_path: $!");
+
        return $affected;
 };
 
 sub _sync_field {
        my $self = shift;
 
-       my $field_hash = shift || confess "no field?";
+       my ( $database, $type, $field_hash ) = @_;
+
        my $path = $self->path || confess "no path?";
 
        my $log = $self->_get_logger();
 
+       my $model = 'Webpacus::Model::' . ucfirst($type);
+       $log->info("sync $model");
+
        $log->debug("field_hash = ",sub { dump($field_hash) });
 
        my @field_names = keys %$field_hash;
@@ -157,28 +192,30 @@ sub _sync_field {
                return;
        }
 
-       $log->info("syncing search fields: ", join(", ", @field_names));
+       $log->info("syncing $database $type fields: ", join(", ", @field_names));
 
-#      push @INC, $path;
-       Jifty->new();
        my $system_user = Webpacus::CurrentUser->superuser;
-       my $o = Webpacus::Model::Search->new(current_user => $system_user);
+       my $o = $model->new(current_user => $system_user);
 
        my ( $count, $new, $updated ) = ( 0, 0, 0 );
 
        foreach my $field ( @field_names ) {
                my $items = $field_hash->{$field} || confess "no field?";
 
-               my ( $id, $msg ) = $o->load_by_cols( name => $field );
+               my ( $id, $msg ) = $o->load_by_cols(
+                       name => $field,
+                       from_database => $database,
+               );
 
                if ( $id ) {
                        $o->set_items( $items );
-                       $log->debug("updated search field: $field [$items] ID: $id $msg");
+                       $log->debug("updated $database $type field: $field [$items] ID: $id $msg");
                        $updated++;
                } else {
-                       $log->debug("adding search field: $field [$items] $msg");
+                       $log->debug("adding $database $type field: $field [$items] $msg");
                        $o->create(
                                name => $field,
+                               from_database => $database,
                                items => $items,
                        );
                        $new++;
@@ -187,32 +224,7 @@ sub _sync_field {
                $count++;
        }
 
-       $log->info("synced $count search fields with Webpacus ($new new/$updated updated) at $path");
-
-       my $glue_path = "$path/lib/Webpacus/Webpac.pm";
-
-       $log->debug("creating clue class Webpacus::Webpac at $glue_path");
-
-       my $glue = <<"_END_OF_GLUE_";
-package Webpacus::Webpac;
-
-=head1 NAME
-
-Webpacus::Webpac - configuration exported from WebPAC
-
-=cut
-
-use strict;
-use warnings;
-
-sub index_path { '/data/webpac2/var/kinosearch/webpacus' };
-
-1;
-_END_OF_GLUE_
-
-       $log->debug("glue source:\n$glue");
-
-       write_file( $glue_path, $glue ) || $log->logdie("can't create $glue_path: $!");
+       $log->info("synced $count fields (",join(", ", @field_names),") from $database with Webpacus ($new new/$updated updated) at $path");
 
        return $count;