per-agent configuration of batch processors, #71837
[freeside.git] / torrus / perllib / Torrus / ServiceID.pm
1 #  Copyright (C) 2002  Stanislav Sinyagin
2 #
3 #  This program is free software; you can redistribute it and/or modify
4 #  it under the terms of the GNU General Public License as published by
5 #  the Free Software Foundation; either version 2 of the License, or
6 #  (at your option) any later version.
7 #
8 #  This program is distributed in the hope that it will be useful,
9 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
10 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 #  GNU General Public License for more details.
12 #
13 #  You should have received a copy of the GNU General Public License
14 #  along with this program; if not, write to the Free Software
15 #  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
16
17 # $Id: ServiceID.pm,v 1.1 2010-12-27 00:03:39 ivan Exp $
18 # Stanislav Sinyagin <ssinyagin@yahoo.com>
19
20 # Manage the properties assigned to Service IDs
21
22 package Torrus::ServiceID;
23
24 use Torrus::DB;
25 use Torrus::Log;
26
27 use strict;
28
29
30 sub new
31 {
32     my $self = {};
33     my $class = shift;
34     my %options = @_;
35     bless $self, $class;
36
37     my $writing = $options{'-WriteAccess'};
38
39     $self->{'db_params'} =
40         new Torrus::DB( 'serviceid_params',
41                         -Btree => 1,
42                         -WriteAccess => $writing );
43     defined( $self->{'db_params'} ) or return( undef );
44
45     $self->{'is_writing'} = $writing;
46
47     return $self;
48 }
49
50
51 sub DESTROY
52 {
53     my $self = shift;
54     Debug('Destroyed ServiceID object');
55     undef $self->{'db_params'};
56 }
57
58
59
60 sub idExists
61 {
62     my $self = shift;
63     my $serviceid = shift;
64     my $tree = shift;
65
66     if( defined($tree) )
67     {
68         return $self->{'db_params'}->searchList( 't:'.$tree, $serviceid );
69     }
70
71     return $self->{'db_params'}->searchList( 'a:', $serviceid );
72 }    
73     
74
75 sub add
76 {
77     my $self = shift;
78     my $serviceid = shift;
79     my $parameters = shift;
80
81     $self->{'db_params'}->addToList( 'a:', $serviceid );
82     
83     my $trees = $parameters->{'trees'};
84
85     foreach my $tree ( split(/\s*,\s*/o, $trees) )
86     {
87         $self->{'db_params'}->addToList( 't:'.$tree, $serviceid );
88     }
89
90     foreach my $param ( keys %{$parameters} )
91     {
92         my $val = $parameters->{$param};
93         
94         if( defined( $val ) and length( $val ) > 0 )
95         {
96             $self->{'db_params'}->put( 'p:'.$serviceid.':'.$param, $val );
97             $self->{'db_params'}->addToList( 'P:'.$serviceid, $param );
98         }
99     }
100 }
101
102
103 sub getParams
104 {
105     my $self = shift;
106     my $serviceid = shift;
107
108     my $ret = {};
109     my $plist = $self->{'db_params'}->get( 'P:'.$serviceid );
110     foreach my $param ( split(',', $plist ) )
111     {
112         $ret->{$param} =
113             $self->{'db_params'}->get( 'p:'.$serviceid.':'.$param );
114     }
115
116     return $ret;
117 }    
118
119
120 sub getAllForTree
121 {
122     my $self = shift;
123     my $tree = shift;
124
125     my $ret = [];
126     my $idlist = $self->{'db_params'}->get('t:'.$tree);
127     if( defined( $idlist ) )
128     {
129         push( @{$ret}, split( ',', $idlist ) );
130     }
131     return $ret;
132 }
133
134
135 sub cleanAllForTree
136 {
137     my $self = shift;
138     my $tree = shift;
139
140     my $idlist = $self->{'db_params'}->get('t:'.$tree);
141     if( defined( $idlist ) )
142     {
143         foreach my $serviceid ( split( ',', $idlist ) )
144         {
145             # A ServiceID may belong to several trees.
146             # delete it from all other trees.
147
148             my $srvTrees =
149                 $self->{'db_params'}->get( 'p:'.$serviceid.':trees' );
150             
151             foreach my $srvTree ( split(/\s*,\s*/o, $srvTrees) )
152             {
153                 if( $srvTree ne $tree )
154                 {
155                     $self->{'db_params'}->delFromList( 't:'.$srvTree,
156                                                        $serviceid );
157                 }
158             }            
159             
160             $self->{'db_params'}->delFromList( 'a:', $serviceid );
161             
162             my $plist = $self->{'db_params'}->get( 'P:'.$serviceid );
163
164             foreach my $param ( split(',', $plist ) )
165             {
166                 $self->{'db_params'}->del( 'p:'.$serviceid.':'.$param );
167             }
168
169             $self->{'db_params'}->del( 'P:'.$serviceid );
170             
171         }
172         $self->{'db_params'}->deleteList('t:'.$tree);
173     }
174 }
175
176             
177             
178             
179             
180
181 1;
182
183
184 # Local Variables:
185 # mode: perl
186 # indent-tabs-mode: nil
187 # perl-indent-level: 4
188 # End: