Boolean Search forign translation functionality
authorFredric T Parks <fparks@catalystitservices.com>
Thu, 13 Feb 2014 00:04:28 +0000 (16:04 -0800)
committerFredric T Parks <fparks@catalystitservices.com>
Thu, 13 Feb 2014 00:23:04 +0000 (16:23 -0800)
sql scripts should be run in the following order:
    XXXX.schema.boolean_keyword_table.sql
    XXXX.function.retrieve_keywords_by_locale.sql
    XXXX.data.seed_boolean_keyword_table.sql

Signed-off-by: Fredric T Parks <fparks@catalystitservices.com>
modified:   Open-ILS/src/perlmods/lib/OpenILS/Application/Storage/Publisher/metabib.pm
modified:   Open-ILS/src/perlmods/lib/OpenILS/WWW/EGCatLoader/Search.pm
new file:   Open-ILS/src/sql/Pg/upgrade/XXXX.data.seed_boolean_keyword_table.sql
new file:   Open-ILS/src/sql/Pg/upgrade/XXXX.function.retrieve_keywords_by_locale.sql
new file:   Open-ILS/src/sql/Pg/upgrade/XXXX.schema.boolean_keyword_table.sql

Open-ILS/src/perlmods/lib/OpenILS/Application/Storage/Publisher/metabib.pm
Open-ILS/src/perlmods/lib/OpenILS/WWW/EGCatLoader/Search.pm
Open-ILS/src/sql/Pg/upgrade/XXXX.data.seed_boolean_keyword_table.sql [new file with mode: 0644]
Open-ILS/src/sql/Pg/upgrade/XXXX.function.retrieve_keywords_by_locale.sql [new file with mode: 0644]
Open-ILS/src/sql/Pg/upgrade/XXXX.schema.boolean_keyword_table.sql [new file with mode: 0644]

index 1089cae..0e482cb 100644 (file)
@@ -7,6 +7,7 @@ use OpenILS::Utils::Fieldmapper;
 use OpenSRF::Utils::Logger qw/:level/;
 use OpenSRF::Utils::Cache;
 use OpenSRF::Utils::JSON;
+use OpenILS::Utils::CStoreEditor qw/:funcs/;
 use Data::Dumper;
 use Digest::MD5 qw/md5_hex/;
 
@@ -3206,10 +3207,12 @@ sub query_parser_fts_wrapper {
 
     #If this is a boolean search set the human readable operators
     #XXX hard coded to just use english for now. Needs to be able to take forign keywords
-    if($args{"_boolean"} == "true") {
-        $parser->operator("and", " and ");
-        $parser->operator("or", " or ");
-        $parser->operator("disallowed", " not ");
+    if(exists $args{"_boolean"}) {
+        $operators = retreave_boolean_keywords($args{"_boolean"});
+
+        foreach my $op (keys %{$operators}){
+            $parser->operator($op, $operators->{$op});
+        }
     }
 
     if (! scalar( keys %{$args{searches}} )) {
@@ -3325,6 +3328,40 @@ __PACKAGE__->register_method(
     cachable    => 1,
 );
 
+sub retreave_boolean_keywords {
+    my $cache = OpenSRF::Utils::Cache->new;
+    my $locale = shift;
+    my $operators;
+
+    #Reformat locale abreveation [en_us -> en-US]
+    $locale =~ s/(\w\w)_(\w\w)/$1\-\U$2\E/g;
+
+    #Check cache for localization valuse for the current locale
+    $operators = $cache->get_cache("boolsearchlocale.${locale}");
+
+    #unless there is a cached set of values load from disk and add to cache
+    unless (defined($operators)){
+        #there are no operators in the cache so load from the DB
+        $operators = {};
+        my $editor = new_editor;
+        my $results = $editor->json_query({from => ["config.retrieve_keywords_by_locale", $locale]});
+
+        foreach my $result (@{$results}){
+            $operators->{$result->{operator}} = $result->{keyword};
+        }
+
+        $cache->put_cache("boolsearchlocale.${locale}", $operators);
+    }
+
+    #if operators is still empty default to basic english keywords
+    if (!defined($operators)){
+        $operators->{"and"} = " and ";
+        $operators->{"or"} = " or ";
+        $operators->{"disallowed"} = " not ";
+    }
+
+    return $operators;
+}
 
 1;
 
index 2a8dfd1..c19e570 100644 (file)
@@ -400,7 +400,7 @@ sub load_rresults {
 
         #if this is a boolean search add flag so operators can be set later
         if($cgi->param("_boolean")) {
-            $args->{"_boolean"} = "true";
+            $args->{"_boolean"} = $ctx->{locale};
         }
 
         if ($tag_circs) {
diff --git a/Open-ILS/src/sql/Pg/upgrade/XXXX.data.seed_boolean_keyword_table.sql b/Open-ILS/src/sql/Pg/upgrade/XXXX.data.seed_boolean_keyword_table.sql
new file mode 100644 (file)
index 0000000..17af847
--- /dev/null
@@ -0,0 +1,7 @@
+INSERT INTO config.boolean_keyword (translation, operator, keyword)
+    VALUES ( 'en-US', 'and', ' and '),
+               ( 'en-US', 'or', ' or '),
+               ( 'en-US', 'disallowed', ' not '),
+               ( 'fr-CA', 'and', ' et '),
+               ( 'fr-CA', 'or', ' ou '),
+               ( 'fr-CA', 'disallowed', ' pas ');
diff --git a/Open-ILS/src/sql/Pg/upgrade/XXXX.function.retrieve_keywords_by_locale.sql b/Open-ILS/src/sql/Pg/upgrade/XXXX.function.retrieve_keywords_by_locale.sql
new file mode 100644 (file)
index 0000000..36df9a3
--- /dev/null
@@ -0,0 +1,11 @@
+CREATE OR REPLACE FUNCTION config.retrieve_keywords_by_locale(locale text) 
+  RETURNS TABLE( operator text, keyword text ) AS
+$BODY$
+BEGIN
+
+    RETURN QUERY
+    SELECT bk.operator, bk.keyword FROM config.boolean_keyword bk WHERE translation = locale;
+    RETURN;
+END;
+$BODY$
+LANGUAGE plpgsql;
diff --git a/Open-ILS/src/sql/Pg/upgrade/XXXX.schema.boolean_keyword_table.sql b/Open-ILS/src/sql/Pg/upgrade/XXXX.schema.boolean_keyword_table.sql
new file mode 100644 (file)
index 0000000..479cf93
--- /dev/null
@@ -0,0 +1,18 @@
+-- DROP TABLE config.boolean_keyword;
+
+CREATE TABLE config.boolean_keyword
+(
+  id serial NOT NULL,
+  translation text,
+  operator text,
+  keyword text,
+  CONSTRAINT boolean_keyword_pk PRIMARY KEY (id),
+  CONSTRAINT boolean_keyword_fk FOREIGN KEY (translation)
+      REFERENCES config.i18n_locale (code) MATCH SIMPLE
+      ON UPDATE NO ACTION ON DELETE NO ACTION
+)
+WITH (
+  OIDS=FALSE
+);
+ALTER TABLE config.boolean_keyword
+  OWNER TO evergreen;