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_retrieve_class",
99 api_name => "open-ils.actor.container.retrieve_by_class",
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
107 If bucketOwnerId is not defined, the authtoken is used as the
109 If requestor ID is different than bucketOwnerId, requestor must have
110 VIEW_CONTAINER permissions.
113 sub bucket_retrieve_class {
114 my( $self, $client, $authtoken, $userid, $class, $type ) = @_;
116 my( $staff, $user, $evt ) =
117 $apputils->checkses_requestor( $authtoken, $userid, 'VIEW_CONTAINER' );
120 $logger->debug("User " . $staff->id .
121 " retrieving buckets for user $userid [class=$class, type=$type]");
123 my $meth = $types{$class} . ".search_where.atomic";
127 $buckets = $apputils->simplereq( $svc,
128 $meth, { owner => $userid, btype => $type } );
130 $logger->debug("Grabbing buckets by class $class: $svc : $meth : {owner => $userid}");
131 $buckets = $apputils->simplereq( $svc, $meth, { owner => $userid } );
134 return _sort_buckets($buckets);
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
148 my( $self, $client, $authtoken, $class, $bucket ) = @_;
150 my( $staff, $target, $evt ) =
151 $apputils->checkses_requestor(
152 $authtoken, $bucket->owner, 'CREATE_CONTAINER' );
155 $logger->activity( "User " . $staff->id .
156 " creating a new continer for user " . $bucket->owner );
159 $logger->debug("Creating new container object: " . Dumper($bucket));
161 my $method = $types{$class} . ".create";
162 my $id = $apputils->simplereq( $svc, $method, $bucket );
164 $logger->debug("Creatined new container with id $id");
166 if(!$id) { throw OpenSRF::EX
167 ("Unable to create new bucket object"); }
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
184 my( $self, $client, $authtoken, $class, $bucketid ) = @_;
186 my( $bucket, $staff, $target, $evt );
188 ( $bucket, $evt ) = $apputils->fetch_container($bucketid, $class);
191 ( $staff, $target, $evt ) = $apputils->checkses_requestor(
192 $authtoken, $bucket->owner, 'DELETE_CONTAINER' );
195 $logger->activity( "User " . $staff->id .
196 " deleting continer $bucketid for user " . $bucket->owner );
198 my $method = $types{$class} . ".delete";
199 my $resp = $apputils->simplereq( $svc, $method, $bucketid );
201 throw OpenSRF::EX ("Unable to create new bucket object") unless $resp;
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)
214 my( $self, $client, $authtoken, $class, $item ) = @_;
215 my( $bucket, $staff, $target, $evt);
217 ( $bucket, $evt ) = $apputils->fetch_container($item->bucket, $class);
220 ( $staff, $target, $evt ) = $apputils->checkses_requestor(
221 $authtoken, $bucket->owner, 'CREATE_CONTAINER_ITEM' );
224 $logger->activity( "User " . $staff->id .
225 " creating continer item " . Dumper($item) . " for user " . $bucket->owner );
227 my $method = $types{$class} . "_item.create";
228 my $resp = $apputils->simplereq( $svc, $method, $item );
230 throw OpenSRF::EX ("Unable to create container item") unless $resp;
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)
244 my( $self, $client, $authtoken, $class, $itemid ) = @_;
245 my( $bucket, $item, $staff, $target, $evt);
248 ( $item, $evt ) = $apputils->fetch_container_item( $itemid, $class );
251 ( $bucket, $evt ) = $apputils->fetch_container($item->bucket, $class);
254 ( $staff, $target, $evt ) = $apputils->checkses_requestor(
255 $authtoken, $bucket->owner, 'DELETE_CONTAINER_ITEM' );
258 $logger->activity( "User " . $staff->id .
259 " deleting continer item $itemid for user " . $bucket->owner );
261 my $method = $types{$class} . "_item.delete";
262 my $resp = $apputils->simplereq( $svc, $method, $itemid );
264 throw OpenSRF::EX ("Unable to delete container item") unless $resp;
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",
275 my( $self, $client, $authtoken, $class, $containerId ) = @_;
276 my( $staff, $target, $container, $evt);
278 ( $container, $evt ) = $apputils->fetch_container($containerId, $class);
281 ( $staff, $target, $evt ) = $apputils->checkses_requestor(
282 $authtoken, $container->owner, 'DELETE_CONTAINER' );
285 $logger->activity("User " . $staff->id . " deleting full container $containerId");
287 my $meth = $types{$class};
288 my $items = $apputils->simplereq( $svc, "$meth"."_item.search.bucket.atomic", $containerId );
290 $self->item_delete( $client, $authtoken, $class, $_->id ) for @$items;
292 $meth = $types{$class} . ".delete";
293 return $apputils->simplereq( $svc, $meth, $containerId );