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";
12 my $logger = "OpenSRF::Utils::Logger";
14 sub initialize { return 1; }
16 my $svc = 'open-ils.storage';
17 my $meth = 'open-ils.storage.direct.container';
19 $types{'biblio'} = "$meth.biblio_record_entry_bucket";
20 $types{'callnumber'} = "$meth.call_number_bucket";
21 $types{'copy'} = "$meth.copy_bucket";
22 $types{'user'} = "$meth.user_bucket";
27 return $buckets unless ($buckets && $buckets->[0]);
28 return [ sort { $a->name cmp $b->name } @$buckets ];
31 __PACKAGE__->register_method(
32 method => "bucket_retrieve_all",
33 api_name => "open-ils.actor.container.all.retrieve_by_user",
35 Retrieves all un-fleshed buckets assigned to given user
36 PARAMS(authtoken, bucketOwnerId)
37 If requestor ID is different than bucketOwnerId, requestor must have
38 VIEW_CONTAINER permissions.
41 sub bucket_retrieve_all {
42 my($self, $client, $authtoken, $userid) = @_;
44 my( $staff, $evt ) = $apputils->checkses($authtoken);
47 my( $user, $e ) = $apputils->checkrequestor( $staff, $userid, 'VIEW_CONTAINER');
50 $logger->debug("User " . $staff->id .
51 " retrieving all buckets for user $userid");
55 $buckets{$_} = $apputils->simplereq(
56 $svc, $types{$_} . ".search.owner.atomic", $userid ) for keys %types;
61 __PACKAGE__->register_method(
62 method => "bucket_flesh",
63 api_name => "open-ils.actor.container.flesh",
66 Fleshes a bucket by id
67 PARAMS(authtoken, bucketClass, bucketId)
68 bucketclasss include biblio, callnumber, copy, and user.
69 bucketclass defaults to biblio.
70 If requestor ID is different than bucketOwnerId, requestor must have
71 VIEW_CONTAINER permissions.
76 my($self, $client, $authtoken, $class, $bucket) = @_;
78 my( $staff, $evt ) = $apputils->checkses($authtoken);
81 $logger->debug("User " . $staff->id . " retrieving bucket $bucket");
83 my $meth = $types{$class};
85 my $bkt = $apputils->simplereq( $svc, "$meth.retrieve", $bucket );
86 if(!$bkt) {return undef};
89 my( $user, $e ) = $apputils->checkrequestor( $staff, $bkt->owner, 'VIEW_CONTAINER' );
93 $bkt->items( $apputils->simplereq( $svc,
94 "$meth"."_item.search.bucket.atomic", $bucket ) );
100 __PACKAGE__->register_method(
101 method => "bucket_flesh_public",
102 api_name => "open-ils.actor.container.public.flesh",
104 notes => <<" NOTES");
105 Fleshes a bucket by id
106 PARAMS(authtoken, bucketClass, bucketId)
107 bucketclasss include biblio, callnumber, copy, and user.
108 bucketclass defaults to biblio.
109 If requestor ID is different than bucketOwnerId, requestor must have
110 VIEW_CONTAINER permissions.
113 sub bucket_flesh_public {
115 my($self, $client, $class, $bucket) = @_;
117 my $meth = $types{$class};
118 my $bkt = $apputils->simplereq( $svc, "$meth.retrieve", $bucket );
119 return undef unless ($bkt and $bkt->pub);
121 $bkt->items( $apputils->simplereq( $svc,
122 "$meth"."_item.search.bucket.atomic", $bucket ) );
128 __PACKAGE__->register_method(
129 method => "bucket_retrieve_class",
130 api_name => "open-ils.actor.container.retrieve_by_class",
132 notes => <<" NOTES");
133 Retrieves all un-fleshed buckets by class assigned to given user
134 PARAMS(authtoken, bucketOwnerId, class [, type])
135 class can be one of "biblio", "callnumber", "copy", "user"
136 The optional "type" parameter allows you to limit the search by
138 If bucketOwnerId is not defined, the authtoken is used as the
140 If requestor ID is different than bucketOwnerId, requestor must have
141 VIEW_CONTAINER permissions.
144 sub bucket_retrieve_class {
145 my( $self, $client, $authtoken, $userid, $class, $type ) = @_;
147 my( $staff, $user, $evt ) =
148 $apputils->checkses_requestor( $authtoken, $userid, 'VIEW_CONTAINER' );
151 $logger->debug("User " . $staff->id .
152 " retrieving buckets for user $userid [class=$class, type=$type]");
154 my $meth = $types{$class} . ".search_where.atomic";
158 $buckets = $apputils->simplereq( $svc,
159 $meth, { owner => $userid, btype => $type } );
161 $logger->debug("Grabbing buckets by class $class: $svc : $meth : {owner => $userid}");
162 $buckets = $apputils->simplereq( $svc, $meth, { owner => $userid } );
165 return _sort_buckets($buckets);
168 __PACKAGE__->register_method(
169 method => "bucket_create",
170 api_name => "open-ils.actor.container.create",
171 notes => <<" NOTES");
172 Creates a new bucket object. If requestor is different from
173 bucketOwner, requestor needs CREATE_CONTAINER permissions
174 PARAMS(authtoken, bucketObject);
175 Returns the new bucket object
179 my( $self, $client, $authtoken, $class, $bucket ) = @_;
181 my( $staff, $target, $evt ) =
182 $apputils->checkses_requestor(
183 $authtoken, $bucket->owner, 'CREATE_CONTAINER' );
186 if( $staff->id eq $target->id ) {
187 $evt = $U->check_perms($target->id, $target->home_ou, 'CREATE_MY_CONTAINER');
191 $logger->activity( "User " . $staff->id .
192 " creating a new container for user " . $bucket->owner );
195 $logger->debug("Creating new container object: " . Dumper($bucket));
197 my $method = $types{$class} . ".create";
198 my $id = $apputils->simplereq( $svc, $method, $bucket );
200 $logger->debug("Creatined new container with id $id");
202 if(!$id) { throw OpenSRF::EX
203 ("Unable to create new bucket object"); }
209 __PACKAGE__->register_method(
210 method => "bucket_delete",
211 api_name => "open-ils.actor.container.delete",
212 notes => <<" NOTES");
213 Deletes a bucket object. If requestor is different from
214 bucketOwner, requestor needs DELETE_CONTAINER permissions
215 PARAMS(authtoken, class, bucketId);
216 Returns the new bucket object
220 my( $self, $client, $authtoken, $class, $bucketid ) = @_;
222 my( $bucket, $staff, $target, $evt );
224 ( $bucket, $evt ) = $apputils->fetch_container($bucketid, $class);
227 ( $staff, $target, $evt ) = $apputils->checkses_requestor(
228 $authtoken, $bucket->owner, 'DELETE_CONTAINER' );
231 $logger->activity( "User " . $staff->id .
232 " deleting container $bucketid for user " . $bucket->owner );
234 my $method = $types{$class} . ".delete";
235 my $resp = $apputils->simplereq( $svc, $method, $bucketid );
237 throw OpenSRF::EX ("Unable to create new bucket object") unless $resp;
242 __PACKAGE__->register_method(
243 method => "item_create",
244 api_name => "open-ils.actor.container.item.create",
245 notes => <<" NOTES");
246 PARAMS(authtoken, class, item)
250 my( $self, $client, $authtoken, $class, $item ) = @_;
251 my( $bucket, $staff, $target, $evt);
253 ( $bucket, $evt ) = $apputils->fetch_container($item->bucket, $class);
256 ( $staff, $target, $evt ) = $apputils->checkses_requestor(
257 $authtoken, $bucket->owner, 'CREATE_CONTAINER_ITEM' );
260 $logger->activity( "User " . $staff->id .
261 " creating container item for bucket " . $item->bucket . " and user " . $bucket->owner );
263 my $method = $types{$class} . "_item.create";
264 my $resp = $apputils->simplereq( $svc, $method, $item );
266 return $U->DB_UPDATE_FAILED($item) unless $resp;
272 __PACKAGE__->register_method(
273 method => "item_delete",
274 api_name => "open-ils.actor.container.item.delete",
275 notes => <<" NOTES");
276 PARAMS(authtoken, class, itemId)
280 my( $self, $client, $authtoken, $class, $itemid ) = @_;
281 my( $bucket, $item, $staff, $target, $evt);
284 ( $item, $evt ) = $apputils->fetch_container_item( $itemid, $class );
287 ( $bucket, $evt ) = $apputils->fetch_container($item->bucket, $class);
290 ( $staff, $target, $evt ) = $apputils->checkses_requestor(
291 $authtoken, $bucket->owner, 'DELETE_CONTAINER_ITEM' );
294 $logger->activity( "User " . $staff->id .
295 " deleting continer item $itemid for user " . $bucket->owner );
297 my $method = $types{$class} . "_item.delete";
298 my $resp = $apputils->simplereq( $svc, $method, $itemid );
300 throw OpenSRF::EX ("Unable to delete container item") unless $resp;
304 __PACKAGE__->register_method(
305 method => 'full_delete',
306 api_name => 'open-ils.actor.container.full_delete',
307 notes => "Complety removes a container including all attached items",
311 my( $self, $client, $authtoken, $class, $containerId ) = @_;
312 my( $staff, $target, $container, $evt);
314 ( $container, $evt ) = $apputils->fetch_container($containerId, $class);
317 ( $staff, $target, $evt ) = $apputils->checkses_requestor(
318 $authtoken, $container->owner, 'DELETE_CONTAINER' );
321 $logger->activity("User " . $staff->id . " deleting full container $containerId");
323 my $meth = $types{$class};
324 my $items = $apputils->simplereq( $svc, "$meth"."_item.search.bucket.atomic", $containerId );
326 $self->item_delete( $client, $authtoken, $class, $_->id ) for @$items;
328 $meth = $types{$class} . ".delete";
329 return $apputils->simplereq( $svc, $meth, $containerId );
332 __PACKAGE__->register_method(
333 method => 'container_update',
334 api_name => 'open-ils.actor.container.update',
336 Updates the given container item.
337 @param authtoken The login session key
338 @param class The container class
339 @param container The container item
340 @return true on success, 0 on no update, Event on error
344 sub container_update {
345 my( $self, $conn, $authtoken, $class, $container ) = @_;
347 my( $staff, $target, $dbcontainer, $evt);
349 ( $dbcontainer, $evt ) = $apputils->fetch_container($container->id, $class);
352 ( $staff, $target, $evt ) = $apputils->checkses_requestor(
353 $authtoken, $dbcontainer->owner, 'UPDATE_CONTAINER' );
356 $logger->activity("User " . $staff->id . " updating container ". $container->id);
358 my $meth = $types{$class}.".update";
359 return $U->storagereq($meth, $container);