ML methods to create sessions and do the searching/bucketing
authorLebbeous Fogle-Weekley <lebbeous@esilibrary.com>
Fri, 17 Aug 2012 19:40:09 +0000 (15:40 -0400)
committerLebbeous Fogle-Weekley <lebbeous@esilibrary.com>
Fri, 21 Sep 2012 15:06:57 +0000 (11:06 -0400)
We can't use PCRUD to create url_verify.session objects because a) you
couldn't trust the creator field if we allowed that, and b) the
container foreign key has a not-null constraint, so you have to create
that first, and you can't do that with PCRUD.

I've removed the C, U and D perms for PCRUD for url_verify.session, but
I left the R in case we wind up using that.

Signed-off-by: Lebbeous Fogle-Weekley <lebbeous@esilibrary.com>
Open-ILS/examples/fm_IDL.xml
Open-ILS/src/perlmods/lib/OpenILS/Application/URLVerify.pm

index 2521678..d685b20 100644 (file)
@@ -9359,10 +9359,7 @@ SELECT  usr,
 
         <permacrud xmlns="http://open-ils.org/spec/opensrf/IDL/permacrud/v1">
             <actions>
-                <create permission="ADMIN_URL_VERIFY" context_field="owning_lib"/>
-                <retrieve permission="ADMIN_URL_VERIFY" context_field="owning_lib"/>
-                <update permission="ADMIN_URL_VERIFY" context_field="owning_lib"/>
-                <delete permission="ADMIN_URL_VERIFY" context_field="owning_lib"/>
+                <retrieve permission="VERIFY_URL" context_field="owning_lib"/>
             </actions>
         </permacrud>
 
index 4e86dcd..0d6df0c 100644 (file)
@@ -1,4 +1,7 @@
 package OpenILS::Application::URLVerify;
+
+# For code searchability, I'm telling you this is the "link checker."
+
 use base qw/OpenILS::Application/;
 use strict; use warnings;
 use OpenSRF::Utils::Logger qw(:logger);
@@ -51,6 +54,9 @@ __PACKAGE__->register_method(
     }
 );
 
+# "validate_session" sounds like something to do with authentication, but it
+# actually means for a given session, validate all the URLs associated with
+# that session.
 sub validate_session {
     my ($self, $client, $auth, $session_id, $url_ids, $options) = @_;
     $options ||= {};
@@ -225,12 +231,14 @@ sub validate_session {
     };
 }
 
-# retrieves the URL domains and sorts them into buckets
+# retrieves the URL domains and sorts them into buckets*
 # Iterates over the buckets and fires the multi-session call
 # the main drawback to this domain sorting approach is that
 # any domain used a lot more than the others will be the
 # only domain standing after the others are exhausted, which
 # means it will take a beating at the end of the batch.
+#
+# * local data structures, not container.* buckets
 sub sort_and_fire_domains {
     my ($e, $auth, $attempt, $url_ids, $multises) = @_;
 
@@ -584,4 +592,114 @@ sub verify_one_url {
 }
 
 
+__PACKAGE__->register_method(
+    method => "create_session",
+    api_name => "open-ils.url_verify.create_session",
+    signature => {
+        desc => q/Create a URL verify session. Also automatically create and
+            link a container./,
+        params => [
+            {desc => "Authentication token", type => "string"},
+            {desc => "session name", type => "string"},
+            {desc => "QueryParser search", type => "string"},
+            {desc => "owning_lib (defaults to ws_ou)", type => "number"},
+        ],
+        return => {desc => "ID of new session or event on error", type => "number"}
+    }
+);
+
+sub create_session {
+    my ($self, $client, $auth, $name, $search, $owning_lib) = @_;
+
+    my $e = new_editor(authtoken => $auth, xact => 1);
+    return $e->die_event unless $e->checkauth;
+
+    $owning_lib ||= $e->requestor->ws_ou;
+    return $e->die_event unless $e->allowed("VERIFY_URL", $owning_lib);
+
+    my $session = Fieldmapper::url_verify::session->new;
+    $session->name($name);
+    $session->owning_lib($owning_lib);
+    $session->creator($e->requestor->id);
+    $session->search($search);
+
+    my $container = Fieldmapper::container::biblio_record_entry_bucket->new;
+    $container->btype("url_verify");
+    $container->owner($e->requestor->id);
+    $container->name($name);
+    $container->description("Automatically generated");
+
+    $e->create_container_biblio_record_entry_bucket($container) or
+        return $e->die_event;
+
+    $session->container($e->data->id);
+    $e->create_url_verify_session($session) or
+        return $e->die_event;
+
+    $e->commit or return $e->die_event;
+
+    return $e->data->id;
+}
+
+
+__PACKAGE__->register_method(
+    method => "session_perform_search",
+    api_name => "open-ils.url_verify.session_perform_search",
+    signature => {
+        desc => q/
+            Perform the search contained in the session, populating the linked bucket
+        /,
+        params => [
+            {desc => "Authentication token", type => "string"},
+            {desc => "url_verify.session id", type => "number"},
+        ],
+        return => {desc => "1 for success, event on failure", type => "number"}
+    }
+);
+
+# XXX TODO send progress updates?
+# XXX TODO either blow away old search results or refuse to start (consider)
+sub session_perform_search {
+    my ($self, $client, $auth, $ses_id) = @_;
+
+    my $e = new_editor(authtoken => $auth);
+    return $e->die_event unless $e->checkauth;
+
+    my $session = $e->retrieve_url_verify_session(int($ses_id));
+
+    return $e->die_event unless
+        $session and $e->allowed("VERIFY_URL", $session->owning_lib);
+
+    # XXX TODO, if the following search takes too long, the caller of our method
+    # can time out
+    my $search_results = $U->simplereq(
+        "open-ils.search", "open-ils.search.biblio.multiclass.query.staff",
+        {}, $session->search
+    );
+
+    return new OpenILS::Event("UNKNOWN") unless $search_results;
+    return $search_results if $U->is_event($search_results);
+
+    $e->xact_begin;
+
+    # Make and save a bucket item for each search result.
+
+    # remember, from search, each ID is nested in its own array ref
+    my $pos = 0;
+    foreach my $wrapper (@{$search_results->{ids}}) {
+        my $bucket_item =
+            Fieldmapper::container::biblio_record_entry_bucket_item->new;
+
+        $bucket_item->bucket($session->container);
+        $bucket_item->target_biblio_record_entry(pop @$wrapper);
+        $bucket_item->pos($pos++); # we don't actually care
+
+        $e->create_container_biblio_record_entry_bucket_item($bucket_item) or
+            return $e->die_event;
+    }
+    $e->xact_commit;
+
+    return 1;
+}
+
 1;