1 package OpenILS::Application::Actor::Container;
2 use base 'OpenSRF::Application';
3 use strict; use warnings;
4 use OpenILS::Application::AppUtils;
7 use OpenSRF::EX qw(:try);
8 use OpenILS::Utils::Fieldmapper;
10 my $apputils = "OpenILS::Application::AppUtils";
11 my $logger = "OpenSRF::Utils::Logger";
13 sub initialize { return 1; }
15 my $svc = 'open-ils.storage';
16 my $meth = 'open-ils.storage.direct.container';
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";
26 return $buckets unless ($buckets && $buckets->[0]);
27 return [ sort { $a->name cmp $b->name } @$buckets ];
30 __PACKAGE__->register_method(
31 method => "bucket_retrieve_all",
32 api_name => "open-ils.actor.container.all.retrieve_by_user",
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.
40 sub bucket_retrieve_all {
41 my($self, $client, $authtoken, $userid) = @_;
43 my( $staff, $evt ) = $apputils->checkses($authtoken);
46 my( $user, $e ) = $apputils->checkrequestor( $staff, $userid, 'VIEW_CONTAINER');
49 $logger->debug("User " . $staff->id .
50 " retrieving all buckets for user $userid");
54 $buckets{$_} = $apputils->simplereq(
55 $svc, $types{$_} . ".search.owner.atomic", $userid ) for keys %types;
60 __PACKAGE__->register_method(
61 method => "bucket_flesh",
62 api_name => "open-ils.actor.container.flesh",
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.
75 my($self, $client, $authtoken, $class, $bucket) = @_;
77 my( $staff, $evt ) = $apputils->checkses($authtoken);
80 $logger->debug("User " . $staff->id . " retrieving bucket $bucket");
82 my $meth = $types{$class};
84 my $bkt = $apputils->simplereq( $svc, "$meth.retrieve", $bucket );
85 if(!$bkt) {return undef};
87 my( $user, $e ) = $apputils->checkrequestor( $staff, $bkt->owner, 'VIEW_CONTAINER' );
90 $bkt->items( $apputils->simplereq( $svc,
91 "$meth"."_item.search.bucket.atomic", $bucket ) );
97 __PACKAGE__->register_method(
98 method => "bucket_flesh_public",
99 api_name => "open-ils.actor.container.public.flesh",
101 notes => <<" NOTES");
102 Fleshes a bucket by id
103 PARAMS(authtoken, bucketClass, bucketId)
104 bucketclasss include biblio, callnumber, copy, and user.
105 bucketclass defaults to biblio.
106 If requestor ID is different than bucketOwnerId, requestor must have
107 VIEW_CONTAINER permissions.
110 sub bucket_flesh_public {
112 my($self, $client, $class, $bucket) = @_;
114 my $meth = $types{$class};
115 my $bkt = $apputils->simplereq( $svc, "$meth.retrieve", $bucket );
116 return undef unless ($bkt and $bkt->public);
118 $bkt->items( $apputils->simplereq( $svc,
119 "$meth"."_item.search.bucket.atomic", $bucket ) );
125 __PACKAGE__->register_method(
126 method => "bucket_retrieve_class",
127 api_name => "open-ils.actor.container.retrieve_by_class",
129 notes => <<" NOTES");
130 Retrieves all un-fleshed buckets by class assigned to given user
131 PARAMS(authtoken, bucketOwnerId, class [, type])
132 class can be one of "biblio", "callnumber", "copy", "user"
133 The optional "type" parameter allows you to limit the search by
135 If bucketOwnerId is not defined, the authtoken is used as the
137 If requestor ID is different than bucketOwnerId, requestor must have
138 VIEW_CONTAINER permissions.
141 sub bucket_retrieve_class {
142 my( $self, $client, $authtoken, $userid, $class, $type ) = @_;
144 my( $staff, $user, $evt ) =
145 $apputils->checkses_requestor( $authtoken, $userid, 'VIEW_CONTAINER' );
148 $logger->debug("User " . $staff->id .
149 " retrieving buckets for user $userid [class=$class, type=$type]");
151 my $meth = $types{$class} . ".search_where.atomic";
155 $buckets = $apputils->simplereq( $svc,
156 $meth, { owner => $userid, btype => $type } );
158 $logger->debug("Grabbing buckets by class $class: $svc : $meth : {owner => $userid}");
159 $buckets = $apputils->simplereq( $svc, $meth, { owner => $userid } );
162 return _sort_buckets($buckets);
165 __PACKAGE__->register_method(
166 method => "bucket_create",
167 api_name => "open-ils.actor.container.create",
168 notes => <<" NOTES");
169 Creates a new bucket object. If requestor is different from
170 bucketOwner, requestor needs CREATE_CONTAINER permissions
171 PARAMS(authtoken, bucketObject);
172 Returns the new bucket object
176 my( $self, $client, $authtoken, $class, $bucket ) = @_;
178 my( $staff, $target, $evt ) =
179 $apputils->checkses_requestor(
180 $authtoken, $bucket->owner, 'CREATE_CONTAINER' );
183 $logger->activity( "User " . $staff->id .
184 " creating a new continer for user " . $bucket->owner );
187 $logger->debug("Creating new container object: " . Dumper($bucket));
189 my $method = $types{$class} . ".create";
190 my $id = $apputils->simplereq( $svc, $method, $bucket );
192 $logger->debug("Creatined new container with id $id");
194 if(!$id) { throw OpenSRF::EX
195 ("Unable to create new bucket object"); }
201 __PACKAGE__->register_method(
202 method => "bucket_delete",
203 api_name => "open-ils.actor.container.delete",
204 notes => <<" NOTES");
205 Deletes a bucket object. If requestor is different from
206 bucketOwner, requestor needs DELETE_CONTAINER permissions
207 PARAMS(authtoken, class, bucketId);
208 Returns the new bucket object
212 my( $self, $client, $authtoken, $class, $bucketid ) = @_;
214 my( $bucket, $staff, $target, $evt );
216 ( $bucket, $evt ) = $apputils->fetch_container($bucketid, $class);
219 ( $staff, $target, $evt ) = $apputils->checkses_requestor(
220 $authtoken, $bucket->owner, 'DELETE_CONTAINER' );
223 $logger->activity( "User " . $staff->id .
224 " deleting continer $bucketid for user " . $bucket->owner );
226 my $method = $types{$class} . ".delete";
227 my $resp = $apputils->simplereq( $svc, $method, $bucketid );
229 throw OpenSRF::EX ("Unable to create new bucket object") unless $resp;
234 __PACKAGE__->register_method(
235 method => "item_create",
236 api_name => "open-ils.actor.container.item.create",
237 notes => <<" NOTES");
238 PARAMS(authtoken, class, item)
242 my( $self, $client, $authtoken, $class, $item ) = @_;
243 my( $bucket, $staff, $target, $evt);
245 ( $bucket, $evt ) = $apputils->fetch_container($item->bucket, $class);
248 ( $staff, $target, $evt ) = $apputils->checkses_requestor(
249 $authtoken, $bucket->owner, 'CREATE_CONTAINER_ITEM' );
252 $logger->activity( "User " . $staff->id .
253 " creating continer item " . Dumper($item) . " for user " . $bucket->owner );
255 my $method = $types{$class} . "_item.create";
256 my $resp = $apputils->simplereq( $svc, $method, $item );
258 throw OpenSRF::EX ("Unable to create container item") unless $resp;
264 __PACKAGE__->register_method(
265 method => "item_delete",
266 api_name => "open-ils.actor.container.item.delete",
267 notes => <<" NOTES");
268 PARAMS(authtoken, class, itemId)
272 my( $self, $client, $authtoken, $class, $itemid ) = @_;
273 my( $bucket, $item, $staff, $target, $evt);
276 ( $item, $evt ) = $apputils->fetch_container_item( $itemid, $class );
279 ( $bucket, $evt ) = $apputils->fetch_container($item->bucket, $class);
282 ( $staff, $target, $evt ) = $apputils->checkses_requestor(
283 $authtoken, $bucket->owner, 'DELETE_CONTAINER_ITEM' );
286 $logger->activity( "User " . $staff->id .
287 " deleting continer item $itemid for user " . $bucket->owner );
289 my $method = $types{$class} . "_item.delete";
290 my $resp = $apputils->simplereq( $svc, $method, $itemid );
292 throw OpenSRF::EX ("Unable to delete container item") unless $resp;
296 __PACKAGE__->register_method(
297 method => 'full_delete',
298 api_name => 'open-ils.actor.container.full_delete',
299 notes => "Complety removes a container including all attached items",
303 my( $self, $client, $authtoken, $class, $containerId ) = @_;
304 my( $staff, $target, $container, $evt);
306 ( $container, $evt ) = $apputils->fetch_container($containerId, $class);
309 ( $staff, $target, $evt ) = $apputils->checkses_requestor(
310 $authtoken, $container->owner, 'DELETE_CONTAINER' );
313 $logger->activity("User " . $staff->id . " deleting full container $containerId");
315 my $meth = $types{$class};
316 my $items = $apputils->simplereq( $svc, "$meth"."_item.search.bucket.atomic", $containerId );
318 $self->item_delete( $client, $authtoken, $class, $_->id ) for @$items;
320 $meth = $types{$class} . ".delete";
321 return $apputils->simplereq( $svc, $meth, $containerId );