]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Actor/Container.pm
more bookbag fun
[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 sub _sort_buckets {
25         my $buckets = shift;
26         return $buckets unless ($buckets && $buckets->[0]);
27         return [ sort { $a->name cmp $b->name } @$buckets ];
28 }
29
30 __PACKAGE__->register_method(
31         method  => "bucket_retrieve_all",
32         api_name        => "open-ils.actor.container.all.retrieve_by_user",
33         notes           => <<"  NOTES");
34                 Retrieves all un-fleshed buckets assigned to given user 
35                 PARAMS(authtoken, bucketOwnerId)
36                 If requestor ID is different than bucketOwnerId, requestor must have
37                 VIEW_CONTAINER permissions.
38         NOTES
39
40 sub bucket_retrieve_all {
41         my($self, $client, $authtoken, $userid) = @_;
42
43         my( $staff, $evt ) = $apputils->checkses($authtoken);
44         return $evt if $evt;
45
46         my( $user, $e ) = $apputils->checkrequestor( $staff, $userid, 'VIEW_CONTAINER');
47         return $e if $e;
48
49         $logger->debug("User " . $staff->id . 
50                 " retrieving all buckets for user $userid");
51
52         my %buckets;
53
54         $buckets{$_} = $apputils->simplereq( 
55                 $svc, $types{$_} . ".search.owner.atomic", $userid ) for keys %types;
56
57         return \%buckets;
58 }
59
60 __PACKAGE__->register_method(
61         method  => "bucket_flesh",
62         api_name        => "open-ils.actor.container.flesh",
63         argc            => 3, 
64         notes           => <<"  NOTES");
65                 Fleshes a bucket by id
66                 PARAMS(authtoken, bucketClass, bucketId)
67                 bucketclasss include biblio, callnumber, copy, and user.  
68                 bucketclass defaults to biblio.
69                 If requestor ID is different than bucketOwnerId, requestor must have
70                 VIEW_CONTAINER permissions.
71         NOTES
72
73 sub bucket_flesh {
74
75         my($self, $client, $authtoken, $class, $bucket) = @_;
76
77         my( $staff, $evt ) = $apputils->checkses($authtoken);
78         return $evt if $evt;
79
80         $logger->debug("User " . $staff->id . " retrieving bucket $bucket");
81
82         my $meth = $types{$class};
83
84         my $bkt = $apputils->simplereq( $svc, "$meth.retrieve", $bucket );
85         if(!$bkt) {return undef};
86
87         my( $user, $e ) = $apputils->checkrequestor( $staff, $bkt->owner, 'VIEW_CONTAINER' );
88         return $e if $e;
89
90         $bkt->items( $apputils->simplereq( $svc,
91                 "$meth"."_item.search.bucket.atomic", $bucket ) );
92
93         return $bkt;
94 }
95
96
97 __PACKAGE__->register_method(
98         method  => "bucket_retrieve_class",
99         api_name        => "open-ils.actor.container.retrieve_by_class",
100         argc            => 3, 
101         notes           => <<"  NOTES");
102                 Retrieves all un-fleshed buckets by class assigned to given user 
103                 PARAMS(authtoken, bucketOwnerId, class [, type])
104                 class can be one of "biblio", "callnumber", "copy", "user"
105                 The optional "type" parameter allows you to limit the search by 
106                 bucket type.  
107                 If bucketOwnerId is not defined, the authtoken is used as the
108                 bucket owner.
109                 If requestor ID is different than bucketOwnerId, requestor must have
110                 VIEW_CONTAINER permissions.
111         NOTES
112
113 sub bucket_retrieve_class {
114         my( $self, $client, $authtoken, $userid, $class, $type ) = @_;
115
116         my( $staff, $user, $evt ) = 
117                 $apputils->checkses_requestor( $authtoken, $userid, 'VIEW_CONTAINER' );
118         return $evt if $evt;
119
120         $logger->debug("User " . $staff->id . 
121                 " retrieving buckets for user $userid [class=$class, type=$type]");
122
123         my $meth = $types{$class} . ".search_where.atomic";
124         my $buckets;
125
126         if( $type ) {
127                 $buckets = $apputils->simplereq( $svc, 
128                         $meth, { owner => $userid, btype => $type } );
129         } else {
130                 $logger->debug("Grabbing buckets by class $class: $svc : $meth :  {owner => $userid}");
131                 $buckets = $apputils->simplereq( $svc, $meth, { owner => $userid } );
132         }
133
134         return _sort_buckets($buckets);
135 }
136
137 __PACKAGE__->register_method(
138         method  => "bucket_create",
139         api_name        => "open-ils.actor.container.create",
140         notes           => <<"  NOTES");
141                 Creates a new bucket object.  If requestor is different from
142                 bucketOwner, requestor needs CREATE_CONTAINER permissions
143                 PARAMS(authtoken, bucketObject);
144                 Returns the new bucket object
145         NOTES
146
147 sub bucket_create {
148         my( $self, $client, $authtoken, $class, $bucket ) = @_;
149
150         my( $staff, $target, $evt ) = 
151                 $apputils->checkses_requestor( 
152                         $authtoken, $bucket->owner, 'CREATE_CONTAINER' );
153         return $evt if $evt;
154
155         $logger->activity( "User " . $staff->id . 
156                 " creating a new continer for user " . $bucket->owner );
157
158         $bucket->clear_id;
159         $logger->debug("Creating new container object: " . Dumper($bucket));
160
161         my $method = $types{$class} . ".create";
162         my $id = $apputils->simplereq( $svc, $method, $bucket );
163
164         $logger->debug("Creatined new container with id $id");
165
166         if(!$id) { throw OpenSRF::EX 
167                 ("Unable to create new bucket object"); }
168
169         return $id;
170 }
171
172
173 __PACKAGE__->register_method(
174         method  => "bucket_delete",
175         api_name        => "open-ils.actor.container.delete",
176         notes           => <<"  NOTES");
177                 Deletes a bucket object.  If requestor is different from
178                 bucketOwner, requestor needs DELETE_CONTAINER permissions
179                 PARAMS(authtoken, class, bucketId);
180                 Returns the new bucket object
181         NOTES
182
183 sub bucket_delete {
184         my( $self, $client, $authtoken, $class, $bucketid ) = @_;
185
186         my( $bucket, $staff, $target, $evt );
187
188         ( $bucket, $evt ) = $apputils->fetch_container($bucketid, $class);
189         return $evt if $evt;
190
191         ( $staff, $target, $evt ) = $apputils->checkses_requestor( 
192                 $authtoken, $bucket->owner, 'DELETE_CONTAINER' );
193         return $evt if $evt;
194
195         $logger->activity( "User " . $staff->id . 
196                 " deleting continer $bucketid for user " . $bucket->owner );
197
198         my $method = $types{$class} . ".delete";
199         my $resp = $apputils->simplereq( $svc, $method, $bucketid );
200
201         throw OpenSRF::EX ("Unable to create new bucket object") unless $resp;
202         return $resp;
203 }
204
205
206 __PACKAGE__->register_method(
207         method  => "item_create",
208         api_name        => "open-ils.actor.container.item.create",
209         notes           => <<"  NOTES");
210                 PARAMS(authtoken, class, item)
211         NOTES
212
213 sub item_create {
214         my( $self, $client, $authtoken, $class, $item ) = @_;
215         my( $bucket, $staff, $target, $evt);
216
217         ( $bucket, $evt ) = $apputils->fetch_container($item->bucket, $class);
218         return $evt if $evt;
219
220         ( $staff, $target, $evt ) = $apputils->checkses_requestor( 
221                 $authtoken, $bucket->owner, 'CREATE_CONTAINER_ITEM' );
222         return $evt if $evt;
223
224         $logger->activity( "User " . $staff->id . 
225                 " creating continer item  " . Dumper($item) . " for user " . $bucket->owner );
226
227         my $method = $types{$class} . "_item.create";
228         my $resp = $apputils->simplereq( $svc, $method, $item );
229
230         throw OpenSRF::EX ("Unable to create container item") unless $resp;
231         return $resp;
232 }
233
234
235
236 __PACKAGE__->register_method(
237         method  => "item_delete",
238         api_name        => "open-ils.actor.container.item.delete",
239         notes           => <<"  NOTES");
240                 PARAMS(authtoken, class, itemId)
241         NOTES
242
243 sub item_delete {
244         my( $self, $client, $authtoken, $class, $itemid ) = @_;
245         my( $bucket, $item, $staff, $target, $evt);
246
247         
248         ( $item, $evt ) = $apputils->fetch_container_item( $itemid, $class );
249         return $evt if $evt;
250
251         ( $bucket, $evt ) = $apputils->fetch_container($item->bucket, $class);
252         return $evt if $evt;
253
254         ( $staff, $target, $evt ) = $apputils->checkses_requestor( 
255                 $authtoken, $bucket->owner, 'DELETE_CONTAINER_ITEM' );
256         return $evt if $evt;
257
258         $logger->activity( "User " . $staff->id . 
259                 " deleting continer item  $itemid for user " . $bucket->owner );
260
261         my $method = $types{$class} . "_item.delete";
262         my $resp = $apputils->simplereq( $svc, $method, $itemid );
263
264         throw OpenSRF::EX ("Unable to delete container item") unless $resp;
265         return $resp;
266 }
267
268 __PACKAGE__->register_method(
269         method  => 'full_delete',
270         api_name        => 'open-ils.actor.container.full_delete',
271         notes           => "Complety removes a container including all attached items",
272 );      
273
274 sub full_delete {
275         my( $self, $client, $authtoken, $class, $containerId ) = @_;
276         my( $staff, $target, $container, $evt);
277
278         ( $container, $evt ) = $apputils->fetch_container($containerId, $class);
279         return $evt if $evt;
280
281         ( $staff, $target, $evt ) = $apputils->checkses_requestor( 
282                 $authtoken, $container->owner, 'DELETE_CONTAINER' );
283         return $evt if $evt;
284
285         $logger->activity("User " . $staff->id . " deleting full container $containerId");
286
287         my $meth = $types{$class};
288         my $items = $apputils->simplereq( $svc, "$meth"."_item.search.bucket.atomic", $containerId );
289
290         $self->item_delete( $client, $authtoken, $class, $_->id ) for @$items;
291
292         $meth = $types{$class} . ".delete";
293         return $apputils->simplereq( $svc, $meth, $containerId );
294 }
295
296
297
298 1;
299
300