Perl Filter and Perl API
[idzebra-moved-to-github.git] / perl / lib / IDZebra / Repository.pm
1 #!/usr/bin/perl
2 # ============================================================================
3 # Zebra perl API header
4 # =============================================================================
5 use strict;
6 # ============================================================================
7 package IDZebra::Repository;
8 use IDZebra;
9 use IDZebra::Logger qw(:flags :calls);
10 use Carp;
11 use Scalar::Util qw(weaken);
12 1;
13
14
15 # -----------------------------------------------------------------------------
16 # Repository stuff
17 # -----------------------------------------------------------------------------
18
19 sub new {
20     my ($proto,$session,%args) = @_;
21     my $class = ref($proto) || $proto;
22     my $self = {};
23     $self->{session} = $session;
24     weaken ($self->{session});
25     $self->{rg} = IDZebra::recordGroup->new();
26     IDZebra::init_recordGroup($self->{rg});
27     bless ($self, $class);
28     unless ($self->_set_options(%args)) {
29         $self->_prepare;
30     }
31     return ($self);
32 }
33
34 sub modify {
35     my ($self,%args) = @_;
36     $self->_set_options(%args);
37 }
38
39 sub readConfig {
40     my ($self, $groupName, $ext) = @_;
41     if ($#_ > 0) { $self->{rg}{groupName} = $groupName;  }
42     $ext = "" unless ($ext);
43     IDZebra::res_get_recordGroup($self->{session}{zh}, $self->{rg}, $ext);
44     $self->_prepare();
45     print "recordType:",$self->{rg}{recordType},"\n";
46 }
47
48 sub _set_options {
49     my ($self, %args) = @_;
50     my $i = 0;
51     foreach my $key (keys(%args)) {
52         $self->{rg}{$key} = $args{$key};
53         $i++;
54     }    
55     if ($i > 0) {
56         $self->_prepare();
57     }
58     return ($i);
59 }
60
61 sub _prepare {
62     my ($self) = @_;
63
64     IDZebra::set_group($self->{session}{zh}, $self->{rg});
65
66     my $dbName;
67     unless ($dbName = $self->{rg}{databaseName}) {
68         $dbName = 'Default';
69     }
70     if (my $res = IDZebra::select_database($self->{session}{zh}, $dbName)) {
71         logf(LOG_FATAL, 
72              "Could not select database %s errCode=%d",
73              $self->{rg}{databaseName},
74              $self->{session}->errCode());
75         croak("Fatal error selecting database");
76     }
77 }
78
79 sub update {
80     my ($self, %args) = @_;
81     $self->_set_options(%args);
82     IDZebra::repository_update($self->{session}{zh});
83 }
84
85 sub delete {
86     my ($self, %args) = @_;
87     $self->_set_options(%args);
88     IDZebra::repository_delete($self->{session}{zh});
89 }
90
91 sub show {
92     my ($self, %args) = @_;
93     $self->_set_options(%args);
94     IDZebra::repository_show($self->{session}{zh});
95 }
96
97 sub update_record {
98     my ($self, $buf, $sysno, $match, $fname) = @_;
99
100     $sysno = 0 unless ($sysno > 0);
101     $match = "" unless ($match);
102     $fname = "<no file>" unless ($fname);
103
104     return(IDZebra::update_record($self->{session}{zh},
105                                  $self->{rg},
106                                  $sysno,$match,$fname,
107                                  $buf, -1)); 
108 }
109
110 sub delete_record {
111     my ($self, $buf, $sysno, $match, $fname) = @_;
112     
113     $sysno = 0 unless ($sysno > 0);
114     $match = "" unless ($match);
115     $fname = "<no file>" unless ($fname);
116
117     return(IDZebra::delete_record($self->{session}{zh},
118                                  $self->{rg},
119                                  $sysno,$match,$fname,
120                                  $buf, -1)); 
121 }
122
123 sub DESTROY {
124     my ($self) = @_;
125     print STDERR "Destroy repository\n";
126 }
127
128 __END__
129
130 =head1 NAME
131
132 IDZebra::Repository - 
133
134 =head1 SYNOPSIS
135
136 =head1 DESCRIPTION
137
138 =head1 COPYRIGHT
139
140 Fill in
141
142 =head1 AUTHOR
143
144 Peter Popovics, pop@technomat.hu
145
146 =head1 SEE ALSO
147
148 IDZebra, IDZebra::Data1, Zebra documentation
149
150 =cut