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};
88 my( $user, $e ) = $apputils->checkrequestor( $staff, $bkt->owner, 'VIEW_CONTAINER' );
91 $bkt->items( $apputils->simplereq( $svc,
92 "$meth"."_item.search.bucket.atomic", $bucket ) );
98 __PACKAGE__->register_method(
99 method => "bucket_flesh_public",
100 api_name => "open-ils.actor.container.public.flesh",
102 notes => <<" NOTES");
103 Fleshes a bucket by id
104 PARAMS(authtoken, bucketClass, bucketId)
105 bucketclasss include biblio, callnumber, copy, and user.
106 bucketclass defaults to biblio.
107 If requestor ID is different than bucketOwnerId, requestor must have
108 VIEW_CONTAINER permissions.
111 sub bucket_flesh_public {
113 my($self, $client, $class, $bucket) = @_;
115 my $meth = $types{$class};
116 my $bkt = $apputils->simplereq( $svc, "$meth.retrieve", $bucket );
117 return undef unless ($bkt and $bkt->pub);
119 $bkt->items( $apputils->simplereq( $svc,
120 "$meth"."_item.search.bucket.atomic", $bucket ) );
126 __PACKAGE__->register_method(
127 method => "bucket_retrieve_class",
128 api_name => "open-ils.actor.container.retrieve_by_class",
130 notes => <<" NOTES");
131 Retrieves all un-fleshed buckets by class assigned to given user
132 PARAMS(authtoken, bucketOwnerId, class [, type])
133 class can be one of "biblio", "callnumber", "copy", "user"
134 The optional "type" parameter allows you to limit the search by
136 If bucketOwnerId is not defined, the authtoken is used as the
138 If requestor ID is different than bucketOwnerId, requestor must have
139 VIEW_CONTAINER permissions.
142 sub bucket_retrieve_class {
143 my( $self, $client, $authtoken, $userid, $class, $type ) = @_;
145 my( $staff, $user, $evt ) =
146 $apputils->checkses_requestor( $authtoken, $userid, 'VIEW_CONTAINER' );
149 $logger->debug("User " . $staff->id .
150 " retrieving buckets for user $userid [class=$class, type=$type]");
152 my $meth = $types{$class} . ".search_where.atomic";
156 $buckets = $apputils->simplereq( $svc,
157 $meth, { owner => $userid, btype => $type } );
159 $logger->debug("Grabbing buckets by class $class: $svc : $meth : {owner => $userid}");
160 $buckets = $apputils->simplereq( $svc, $meth, { owner => $userid } );
163 return _sort_buckets($buckets);
166 __PACKAGE__->register_method(
167 method => "bucket_create",
168 api_name => "open-ils.actor.container.create",
169 notes => <<" NOTES");
170 Creates a new bucket object. If requestor is different from
171 bucketOwner, requestor needs CREATE_CONTAINER permissions
172 PARAMS(authtoken, bucketObject);
173 Returns the new bucket object
177 my( $self, $client, $authtoken, $class, $bucket ) = @_;
179 my( $staff, $target, $evt ) =
180 $apputils->checkses_requestor(
181 $authtoken, $bucket->owner, 'CREATE_CONTAINER' );
184 $logger->activity( "User " . $staff->id .
185 " creating a new continer for user " . $bucket->owner );
188 $logger->debug("Creating new container object: " . Dumper($bucket));
190 my $method = $types{$class} . ".create";
191 my $id = $apputils->simplereq( $svc, $method, $bucket );
193 $logger->debug("Creatined new container with id $id");
195 if(!$id) { throw OpenSRF::EX
196 ("Unable to create new bucket object"); }
202 __PACKAGE__->register_method(
203 method => "bucket_delete",
204 api_name => "open-ils.actor.container.delete",
205 notes => <<" NOTES");
206 Deletes a bucket object. If requestor is different from
207 bucketOwner, requestor needs DELETE_CONTAINER permissions
208 PARAMS(authtoken, class, bucketId);
209 Returns the new bucket object
213 my( $self, $client, $authtoken, $class, $bucketid ) = @_;
215 my( $bucket, $staff, $target, $evt );
217 ( $bucket, $evt ) = $apputils->fetch_container($bucketid, $class);
220 ( $staff, $target, $evt ) = $apputils->checkses_requestor(
221 $authtoken, $bucket->owner, 'DELETE_CONTAINER' );
224 $logger->activity( "User " . $staff->id .
225 " deleting continer $bucketid for user " . $bucket->owner );
227 my $method = $types{$class} . ".delete";
228 my $resp = $apputils->simplereq( $svc, $method, $bucketid );
230 throw OpenSRF::EX ("Unable to create new bucket object") unless $resp;
235 __PACKAGE__->register_method(
236 method => "item_create",
237 api_name => "open-ils.actor.container.item.create",
238 notes => <<" NOTES");
239 PARAMS(authtoken, class, item)
243 my( $self, $client, $authtoken, $class, $item ) = @_;
244 my( $bucket, $staff, $target, $evt);
246 ( $bucket, $evt ) = $apputils->fetch_container($item->bucket, $class);
249 ( $staff, $target, $evt ) = $apputils->checkses_requestor(
250 $authtoken, $bucket->owner, 'CREATE_CONTAINER_ITEM' );
253 $logger->activity( "User " . $staff->id .
254 " creating continer item " . Dumper($item) . " for user " . $bucket->owner );
256 my $method = $types{$class} . "_item.create";
257 my $resp = $apputils->simplereq( $svc, $method, $item );
259 return $U->DB_UPDATE_FAILED($item) unless $resp;
265 __PACKAGE__->register_method(
266 method => "item_delete",
267 api_name => "open-ils.actor.container.item.delete",
268 notes => <<" NOTES");
269 PARAMS(authtoken, class, itemId)
273 my( $self, $client, $authtoken, $class, $itemid ) = @_;
274 my( $bucket, $item, $staff, $target, $evt);
277 ( $item, $evt ) = $apputils->fetch_container_item( $itemid, $class );
280 ( $bucket, $evt ) = $apputils->fetch_container($item->bucket, $class);
283 ( $staff, $target, $evt ) = $apputils->checkses_requestor(
284 $authtoken, $bucket->owner, 'DELETE_CONTAINER_ITEM' );
287 $logger->activity( "User " . $staff->id .
288 " deleting continer item $itemid for user " . $bucket->owner );
290 my $method = $types{$class} . "_item.delete";
291 my $resp = $apputils->simplereq( $svc, $method, $itemid );
293 throw OpenSRF::EX ("Unable to delete container item") unless $resp;
297 __PACKAGE__->register_method(
298 method => 'full_delete',
299 api_name => 'open-ils.actor.container.full_delete',
300 notes => "Complety removes a container including all attached items",
304 my( $self, $client, $authtoken, $class, $containerId ) = @_;
305 my( $staff, $target, $container, $evt);
307 ( $container, $evt ) = $apputils->fetch_container($containerId, $class);
310 ( $staff, $target, $evt ) = $apputils->checkses_requestor(
311 $authtoken, $container->owner, 'DELETE_CONTAINER' );
314 $logger->activity("User " . $staff->id . " deleting full container $containerId");
316 my $meth = $types{$class};
317 my $items = $apputils->simplereq( $svc, "$meth"."_item.search.bucket.atomic", $containerId );
319 $self->item_delete( $client, $authtoken, $class, $_->id ) for @$items;
321 $meth = $types{$class} . ".delete";
322 return $apputils->simplereq( $svc, $meth, $containerId );