]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Actor/Container.pm
more container creation work
[Evergreen.git] / Open-ILS / src / perlmods / OpenILS / Application / Actor / Container.pm
1 package OpenILS::Application::Actor::Container;
2 use base 'OpenSRF::Application';
3 use strict; use warnings;
4 use OpenILS::Application::AppUtils;
5 use OpenILS::Perm;
6 use Data::Dumper;
7 use OpenSRF::EX qw(:try);
8 use OpenILS::Utils::Fieldmapper;
9
10 my $apputils = "OpenILS::Application::AppUtils";
11 my $logger = "OpenSRF::Utils::Logger";
12
13 sub initialize { return 1; }
14
15 my $svc = 'open-ils.storage';
16 my $meth = 'open-ils.storage.direct.container';
17 my %types;
18 $types{'biblio'} = "$meth.biblio_record_entry_bucket";
19 $types{'callnumber'} = "$meth.call_number_bucket";
20 $types{'copy'} = "$meth.copy_bucket";
21 $types{'user'} = "$meth.user_bucket";
22 my $event;
23
24 __PACKAGE__->register_method(
25         method  => "bucket_retrieve_all",
26         api_name        => "open-ils.actor.container.bucket.all.retrieve_by_user",
27         notes           => <<"  NOTES");
28                 Retrieves all un-fleshed buckets assigned to given user 
29                 PARAMS(authtoken, bucketOwnerId)
30                 If requestor ID is different than bucketOwnerId, requestor must have
31                 VIEW_CONTAINER permissions.
32         NOTES
33
34 sub bucket_retrieve_all {
35         my($self, $client, $authtoken, $userid) = @_;
36
37         my( $staff, $evt ) = $apputils->checkses($authtoken);
38         return $evt if $evt;
39
40         my( $user, $e ) = $apputils->checkrequestor( $staff, $userid, 'VIEW_CONTAINER');
41         return $e if $e;
42
43         $logger->debug("User " . $staff->id . 
44                 " retrieving all buckets for user $userid");
45
46         my %buckets;
47
48         $buckets{$_} = $apputils->simplereq( 
49                 $svc, $types{$_} . ".search.owner.atomic", $userid ) for keys %types;
50
51         return \%buckets;
52 }
53
54 __PACKAGE__->register_method(
55         method  => "bucket_flesh",
56         api_name        => "open-ils.actor.container.bucket.flesh",
57         argc            => 3, 
58         notes           => <<"  NOTES");
59                 Fleshes a bucket by id
60                 PARAMS(authtoken, bucketClass, bucketId)
61                 bucketclasss include biblio, callnumber, copy, and user.  
62                 bucketclass defaults to biblio.
63                 If requestor ID is different than bucketOwnerId, requestor must have
64                 VIEW_CONTAINER permissions.
65         NOTES
66
67 sub bucket_flesh {
68
69         my($self, $client, $authtoken, $class, $bucket) = @_;
70
71         my( $staff, $evt ) = $apputils->checkses($authtoken);
72         return $evt if $evt;
73
74         $logger->debug("User " . $staff->id . " retrieving bucket $bucket");
75
76         my $meth = $types{$class};
77
78         my $bkt = $apputils->simplereq( $svc, "$meth.retrieve", $bucket );
79         if(!$bkt) {return undef};
80
81         my( $user, $e ) = $apputils->checkrequestor( $staff, $bkt->owner, 'VIEW_CONTAINER' );
82         return $e if $e;
83
84         $bkt->items( $apputils->simplereq( $svc,
85                 "$meth"."_item.search.bucket.atomic", $bucket ) );
86
87         return $bkt;
88 }
89
90
91 __PACKAGE__->register_method(
92         method  => "bucket_retrieve_class",
93         api_name        => "open-ils.actor.container.bucket.retrieve_by_class",
94         argc            => 3, 
95         notes           => <<"  NOTES");
96                 Retrieves all un-fleshed buckets by class assigned to given user 
97                 PARAMS(authtoken, bucketOwnerId, class [, type])
98                 class can be one of "biblio", "callnumber", "copy", "user"
99                 The optional "type" parameter allows you to limit the search by 
100                 bucket type.  
101                 If bucketOwnerId is not defined, the authtoken is used as the
102                 bucket owner.
103                 If requestor ID is different than bucketOwnerId, requestor must have
104                 VIEW_CONTAINER permissions.
105         NOTES
106
107 sub bucket_retrieve_class {
108         my( $self, $client, $authtoken, $userid, $class, $type ) = @_;
109
110         my( $staff, $user, $evt );
111         ($staff, $evt) = $apputils->checkses($authtoken);
112         return $evt if $evt;
113
114         ($user, $evt) = $apputils->checkrequestor($staff, $userid, 'VIEW_CONTAINER');
115         return $evt if $evt;
116
117         $logger->debug("User " . $staff->id . 
118                 " retrieving buckets for user $userid [class=$class, type=$type]");
119
120         my $meth = $types{$class} . ".search_where.atomic";
121         my $buckets;
122
123         if( $type ) {
124                 $buckets = $apputils->simplereq( $svc, 
125                         $meth, { owner => $userid, btype => $type } );
126         } else {
127                 $logger->debug("Grabbing buckets by class $class: $svc : $meth :  {owner => $userid}");
128                 $buckets = $apputils->simplereq( $svc, $meth, { owner => $userid } );
129         }
130
131         return $buckets;
132 }
133
134 __PACKAGE__->register_method(
135         method  => "bucket_create",
136         api_name        => "open-ils.actor.container.bucket.create",
137         notes           => <<"  NOTES");
138                 Creates a new bucket object.  If requestor is different from
139                 bucketOwner, requestor needs CREATE_CONTAINER permissions
140                 PARAMS(authtoken, bucketObject);
141                 Returns the new bucket object
142         NOTES
143
144 sub bucket_create {
145         my( $self, $client, $authtoken, $class, $bucket ) = @_;
146
147         my( $staff, $target, $evt ) = 
148                 $apputils->checkses_requestor( 
149                         $authtoken, $bucket->owner, 'CREATE_CONTAINER' );
150         return $evt if $evt;
151
152         $logger->activity( "User " . $staff->id . 
153                 " creating a new continer for user " . $bucket->owner );
154
155         $logger->debug("Creating new container object: " . Dumper($bucket));
156
157         my $method = $types{$class} . ".create";
158         my $id = $apputils->simplreq( $svc, $method, $bucket );
159
160         $logger->debug("Creatined new container with id $id");
161
162         if(!$id) { throw OpenSRF::EX 
163                 ("Unable to create new bucket object"); }
164
165         return $id;
166 }
167
168
169 __PACKAGE__->register_method(
170         method  => "bucket_delete",
171         api_name        => "open-ils.actor.container.bucket.delete",
172         notes           => <<"  NOTES");
173                 Deletes a bucket object.  If requestor is different from
174                 bucketOwner, requestor needs DELETE_CONTAINER permissions
175                 PARAMS(authtoken, class, bucketId);
176                 Returns the new bucket object
177         NOTES
178
179 sub bucket_delete {
180         my( $self, $client, $authtoken, $class, $bucketid ) = @_;
181
182         my $bucket = $apputils->simplereq( 
183                 $svc, $types{$class} . ".retrieve", $bucketid );
184
185         if(!$bucket) {
186                 return OpenILS::Event->new('CONTAINER_NOT_FOUND');
187         }
188
189         my( $staff, $target, $evt ) = $apputils->checkses_requestor( 
190                 $authtoken, $bucket->owner, 'DELETE_CONTAINER' );
191         return $evt if $evt;
192
193         $logger->activity( "User " . $staff->id . 
194                 " deleting continer $bucketid for user " . $bucket->owner );
195
196         my $method = $types{$class} . ".delete";
197         my $resp = $apputils->simplreq( $svc, $method, $bucketid );
198
199         if(!$resp) { throw OpenSRF::EX 
200                 ("Unable to create new bucket object"); }
201         return $resp;
202
203 }
204
205
206
207 1;
208
209