]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Storage/Publisher.pm
automatic generation of most "Publish" methods
[working/Evergreen.git] / Open-ILS / src / perlmods / OpenILS / Application / Storage / Publisher.pm
1 package OpenILS::Application::Storage::Publisher;
2 use base qw/OpenILS::Application::Storage/;
3 our $VERSION = 1;
4
5 use OpenILS::Utils::Fieldmapper;
6 use OpenILS::Application::Storage::CDBI;
7
8 #use OpenILS::Application::Storage::CDBI::actor;
9 #use OpenILS::Application::Storage::CDBI::asset;
10 #use OpenILS::Application::Storage::CDBI::biblio;
11 #use OpenILS::Application::Storage::CDBI::config;
12 #use OpenILS::Application::Storage::CDBI::metabib;
13
14 use OpenILS::Application::Storage::Publisher::actor;
15 #use OpenILS::Application::Storage::Publisher::asset;
16 use OpenILS::Application::Storage::Publisher::biblio;
17 use OpenILS::Application::Storage::Publisher::config;
18 use OpenILS::Application::Storage::Publisher::metabib;
19
20
21 for my $fmclass ( Fieldmapper->classes ) {
22         (my $cdbi = $fmclass) =~ s/^Fieldmapper:://o;
23         (my $class = $cdbi) =~ s/::.*//o;
24         (my $api_class = $cdbi) =~ s/::/./go;
25         my $registration_class = __PACKAGE__ . "::$class";
26         my $api_prefix = 'open-ils.storage.'.$api_class;
27
28         warn "\tfmclass => $fmclass\n\tclass => $class\n\tregclass => $registration_class\n\tprefix => $api_prefix\n\tcdbi => $cdbi\n\n";
29
30         # Create the create method
31         unless ( __PACKAGE__->is_registered( $api_prefix.'.create' ) ) {
32                 *{ $registration_class . '::create_node' } = sub {
33                         my $self = shift;
34                         my $client = shift;
35                         my $node = shift;
36
37                         my $success;
38                         try {
39                                 my $rec = $cdbi->create($node);
40                                 $success = $rec->id;
41                         } catch Error with {
42                                 $success = 0;
43                         };
44
45                         return $success;
46                 };
47                 $registration_class->register_method(
48                         api_name        => $api_prefix.'.create',
49                         method          => 'create_node',
50                         api_level       => 1,
51                 );
52         }
53
54         # Create the batch create method
55         unless ( __PACKAGE__->is_registered( $api_prefix.'.batch.create' ) ) {
56                 *{ $registration_class . '::create_node_batch' } = sub {
57                         my $self = shift;
58                         my $client = shift;
59                         my @nodes = @_;
60
61                         my $method = $self->method_lookup($api_prefix.'.create');
62
63                         my @success;
64                         while ( my $node = shift(@nodes) ) {
65                                 my ($res) = $method->run( $node ); 
66                                         push(@success, 1) if ($res >= 0);
67                         }
68
69                         my $insert_total = 0;
70                         $insert_total += $_ for (@success);
71
72                         return $insert_total;
73                 };
74                 $registration_class->register_method(
75                         api_name        => $api_prefix.'.batch.create',
76                         method          => 'create_node_batch',
77                         api_level       => 1,
78                 );
79         }
80
81         # Create the update method
82         unless ( __PACKAGE__->is_registered( $api_prefix.'.update' ) ) {
83                 *{ $registration_class . '::update_node' } = sub {
84                         my $self = shift;
85                         my $client = shift;
86                         my $node = shift;
87
88                         return $cdbi->update($node);
89                 };
90                 $registration_class->register_method(
91                         api_name        => $api_prefix.'.update',
92                         method          => 'update_node',
93                         api_level       => 1,
94                 );
95         }
96
97         # Create the batch update method
98         unless ( __PACKAGE__->is_registered( $api_prefix.'.batch.update' ) ) {
99                 *{ $registration_class . '::update_node_batch' } = sub {
100                         my $self = shift;
101                         my $client = shift;
102                         my @nodes = @_;
103
104                         my $method = $self->method_lookup($api_prefix.'.update');
105
106                         my @success;
107                         while ( my $node = shift(@nodes) ) {
108                                 my ($res) = $method->run( $node ); 
109                                         push(@success, $res) if ($res >= 0);
110                         }
111
112                         my $insert_total = 0;
113                         $insert_total += $_ for (@success);
114
115                         return $insert_total;
116                 };
117                 $registration_class->register_method(
118                         api_name        => $api_prefix.'.batch.update',
119                         method          => 'update_node_batch',
120                         api_level       => 1,
121                 );
122         }
123
124 }
125
126 1;