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 $logger->activity( "User " . $staff->id .
187 " creating a new container for user " . $bucket->owner );
190 $logger->debug("Creating new container object: " . Dumper($bucket));
192 my $method = $types{$class} . ".create";
193 my $id = $apputils->simplereq( $svc, $method, $bucket );
195 $logger->debug("Creatined new container with id $id");
197 if(!$id) { throw OpenSRF::EX
198 ("Unable to create new bucket object"); }
204 __PACKAGE__->register_method(
205 method => "bucket_delete",
206 api_name => "open-ils.actor.container.delete",
207 notes => <<" NOTES");
208 Deletes a bucket object. If requestor is different from
209 bucketOwner, requestor needs DELETE_CONTAINER permissions
210 PARAMS(authtoken, class, bucketId);
211 Returns the new bucket object
215 my( $self, $client, $authtoken, $class, $bucketid ) = @_;
217 my( $bucket, $staff, $target, $evt );
219 ( $bucket, $evt ) = $apputils->fetch_container($bucketid, $class);
222 ( $staff, $target, $evt ) = $apputils->checkses_requestor(
223 $authtoken, $bucket->owner, 'DELETE_CONTAINER' );
226 $logger->activity( "User " . $staff->id .
227 " deleting container $bucketid for user " . $bucket->owner );
229 my $method = $types{$class} . ".delete";
230 my $resp = $apputils->simplereq( $svc, $method, $bucketid );
232 throw OpenSRF::EX ("Unable to create new bucket object") unless $resp;
237 __PACKAGE__->register_method(
238 method => "item_create",
239 api_name => "open-ils.actor.container.item.create",
240 notes => <<" NOTES");
241 PARAMS(authtoken, class, item)
245 my( $self, $client, $authtoken, $class, $item ) = @_;
246 my( $bucket, $staff, $target, $evt);
248 ( $bucket, $evt ) = $apputils->fetch_container($item->bucket, $class);
251 ( $staff, $target, $evt ) = $apputils->checkses_requestor(
252 $authtoken, $bucket->owner, 'CREATE_CONTAINER_ITEM' );
255 $logger->activity( "User " . $staff->id .
256 " creating container item for bucket " . $item->bucket . " and user " . $bucket->owner );
258 my $method = $types{$class} . "_item.create";
259 my $resp = $apputils->simplereq( $svc, $method, $item );
261 return $U->DB_UPDATE_FAILED($item) unless $resp;
267 __PACKAGE__->register_method(
268 method => "item_delete",
269 api_name => "open-ils.actor.container.item.delete",
270 notes => <<" NOTES");
271 PARAMS(authtoken, class, itemId)
275 my( $self, $client, $authtoken, $class, $itemid ) = @_;
276 my( $bucket, $item, $staff, $target, $evt);
279 ( $item, $evt ) = $apputils->fetch_container_item( $itemid, $class );
282 ( $bucket, $evt ) = $apputils->fetch_container($item->bucket, $class);
285 ( $staff, $target, $evt ) = $apputils->checkses_requestor(
286 $authtoken, $bucket->owner, 'DELETE_CONTAINER_ITEM' );
289 $logger->activity( "User " . $staff->id .
290 " deleting continer item $itemid for user " . $bucket->owner );
292 my $method = $types{$class} . "_item.delete";
293 my $resp = $apputils->simplereq( $svc, $method, $itemid );
295 throw OpenSRF::EX ("Unable to delete container item") unless $resp;
299 __PACKAGE__->register_method(
300 method => 'full_delete',
301 api_name => 'open-ils.actor.container.full_delete',
302 notes => "Complety removes a container including all attached items",
306 my( $self, $client, $authtoken, $class, $containerId ) = @_;
307 my( $staff, $target, $container, $evt);
309 ( $container, $evt ) = $apputils->fetch_container($containerId, $class);
312 ( $staff, $target, $evt ) = $apputils->checkses_requestor(
313 $authtoken, $container->owner, 'DELETE_CONTAINER' );
316 $logger->activity("User " . $staff->id . " deleting full container $containerId");
318 my $meth = $types{$class};
319 my $items = $apputils->simplereq( $svc, "$meth"."_item.search.bucket.atomic", $containerId );
321 $self->item_delete( $client, $authtoken, $class, $_->id ) for @$items;
323 $meth = $types{$class} . ".delete";
324 return $apputils->simplereq( $svc, $meth, $containerId );
327 __PACKAGE__->register_method(
328 method => 'container_update',
329 api_name => 'open-ils.actor.container.update',
331 Updates the given container item.
332 @param authtoken The login session key
333 @param class The container class
334 @param container The container item
335 @return true on success, 0 on no update, Event on error
339 sub container_update {
340 my( $self, $conn, $authtoken, $class, $container ) = @_;
342 my( $staff, $target, $dbcontainer, $evt);
344 ( $dbcontainer, $evt ) = $apputils->fetch_container($container->id, $class);
347 ( $staff, $target, $evt ) = $apputils->checkses_requestor(
348 $authtoken, $dbcontainer->owner, 'UPDATE_CONTAINER' );
351 $logger->activity("User " . $staff->id . " updating container ". $container->id);
353 my $meth = $types{$class}.".update";
354 return $U->storagereq($meth, $container);