Alert addresses for patron registration : DB/IDL
authorBill Erickson <berick@esilibrary.com>
Tue, 6 Dec 2011 21:09:16 +0000 (16:09 -0500)
committerThomas Berezansky <tsbere@mvlc.org>
Wed, 14 Dec 2011 15:10:39 +0000 (10:10 -0500)
DB and IDL components to support configuration of alert addresses.  The
target use case for alert addresses is in the patron registration
interface.  When a patron's address matches an alert address, the staff
should be notified.

Alert address fields are treated as regular expressions.

Signed-off-by: Bill Erickson <berick@esilibrary.com>
Signed-off-by: Thomas Berezansky <tsbere@mvlc.org>
Open-ILS/examples/fm_IDL.xml
Open-ILS/src/sql/Pg/005.schema.actors.sql
Open-ILS/src/sql/Pg/999.functions.global.sql
Open-ILS/src/sql/Pg/upgrade/XXXX.schema.address-alert.sql [new file with mode: 0644]

index ec1cac6..ae72e81 100644 (file)
@@ -3043,6 +3043,36 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
                        <link field="replaces" reltype="has_a" key="id" map="" class="aua"/>
                </links>
        </class>
+       <class id="aal" controller="open-ils.cstore open-ils.pcrud" oils_obj:fieldmapper="actor::address_alert" oils_persist:tablename="actor.address_alert" reporter:label="Address Alert">
+               <fields oils_persist:primary="id" oils_persist:sequence="actor.address_alert_id_seq">
+                       <field reporter:label="Address Alert ID" name="id" reporter:datatype="id" />
+                       <field reporter:label="Owner" name="owner" reporter:datatype="org_unit" oils_obj:required="true"/>
+                       <field reporter:label="Active" name="active" reporter:datatype="bool"/>
+                       <field reporter:label="Match All Fields" name="match_all" reporter:datatype="bool" />
+                       <field reporter:label="Alert Message" name="alert_message" reporter:datatype="text" oils_obj:required="true"/>
+                       <field reporter:label="Street (1)" name="street1"  reporter:datatype="text"/>
+                       <field reporter:label="Street (2)" name="street2"  reporter:datatype="text"/>
+                       <field reporter:label="City" name="city"  reporter:datatype="text"/>
+                       <field reporter:label="County" name="county"  reporter:datatype="text"/>
+                       <field reporter:label="State" name="state"  reporter:datatype="text"/>
+                       <field reporter:label="Country" name="country"  reporter:datatype="text"/>
+                       <field reporter:label="Postal Code" name="post_code" reporter:datatype="text"/>
+                       <field reporter:label="Mailing Address" name="mailing_address" reporter:datatype="bool"/>
+                       <field reporter:label="Billing Address" name="billing_address" reporter:datatype="bool"/>
+               </fields>
+               <links>
+                       <link field="owner" reltype="has_a" key="id" map="" class="aou"/>
+               </links>
+               <permacrud xmlns="http://open-ils.org/spec/opensrf/IDL/permacrud/v1">
+                       <actions>
+                               <create   context_field='owner' permission="ADMIN_ADDRESS_ALERT"/>
+                               <retrieve context_field='owner' permission="ADMIN_ADDRESS_ALERT VIEW_ADDRESS_ALERT CREATE_USER"/>
+                               <update   context_field='owner' permission="ADMIN_ADDRESS_ALERT"/>
+                               <delete   context_field='owner' permission="ADMIN_ADDRESS_ALERT"/>
+                       </actions>
+               </permacrud>
+       </class>
+
 
        <class id="auss" controller="open-ils.cstore open-ils.pcrud" oils_obj:fieldmapper="actor::usr_saved_search" oils_persist:tablename="actor.usr_saved_search" reporter:label="User Saved Search">
                <fields oils_persist:primary="id" oils_persist:sequence="actor.usr_saved_search_id_seq">
index 3f8fb92..3a9f841 100644 (file)
@@ -605,4 +605,21 @@ CREATE TABLE actor.usr_saved_search (
        CONSTRAINT name_once_per_user UNIQUE (owner, name)
 );
 
+CREATE TABLE actor.address_alert (
+    id              SERIAL  PRIMARY KEY,
+    owner           INT     NOT NULL REFERENCES actor.org_unit (id) DEFERRABLE INITIALLY DEFERRED,
+    active          BOOL    NOT NULL DEFAULT TRUE,
+    match_all       BOOL    NOT NULL DEFAULT TRUE,
+    alert_message   TEXT    NOT NULL,
+    street1         TEXT,
+    street2         TEXT,
+    city            TEXT,
+    county          TEXT,
+    state           TEXT,
+    country         TEXT,
+    post_code       TEXT,
+    mailing_address BOOL    NOT NULL DEFAULT FALSE,
+    billing_address BOOL    NOT NULL DEFAULT FALSE
+);
+
 COMMIT;
index 4eff147..9885876 100644 (file)
@@ -2035,3 +2035,53 @@ for my $key ( keys %map ) {
 return $default;
 
 $f$ LANGUAGE PLPERLU;
+
+CREATE OR REPLACE FUNCTION actor.address_alert_matches (
+        org_unit INT, 
+        street1 TEXT, 
+        street2 TEXT, 
+        city TEXT, 
+        county TEXT, 
+        state TEXT, 
+        country TEXT, 
+        post_code TEXT,
+        mailing_address BOOL DEFAULT FALSE,
+        billing_address BOOL DEFAULT FALSE
+    ) RETURNS SETOF actor.address_alert AS $$
+
+SELECT *
+FROM actor.address_alert
+WHERE
+    active
+    AND owner IN (SELECT id FROM actor.org_unit_ancestors($1)) 
+    AND (
+        (NOT mailing_address AND NOT billing_address)
+        OR (mailing_address AND $9)
+        OR (billing_address AND $10)
+    )
+    AND (
+            (
+                match_all
+                AND COALESCE($2, '') ~* COALESCE(street1,   '.*')
+                AND COALESCE($3, '') ~* COALESCE(street2,   '.*')
+                AND COALESCE($4, '') ~* COALESCE(city,      '.*')
+                AND COALESCE($5, '') ~* COALESCE(county,    '.*')
+                AND COALESCE($6, '') ~* COALESCE(state,     '.*')
+                AND COALESCE($7, '') ~* COALESCE(country,   '.*')
+                AND COALESCE($8, '') ~* COALESCE(post_code, '.*')
+            ) OR (
+                NOT match_all 
+                AND (  
+                       $2 ~* street1
+                    OR $3 ~* street2
+                    OR $4 ~* city
+                    OR $5 ~* county
+                    OR $6 ~* state
+                    OR $7 ~* country
+                    OR $8 ~* post_code
+                )
+            )
+        )
+    ORDER BY actor.org_unit_proximity(owner, $1)
+$$ LANGUAGE SQL;
+
diff --git a/Open-ILS/src/sql/Pg/upgrade/XXXX.schema.address-alert.sql b/Open-ILS/src/sql/Pg/upgrade/XXXX.schema.address-alert.sql
new file mode 100644 (file)
index 0000000..aed3dce
--- /dev/null
@@ -0,0 +1,79 @@
+-- Evergreen DB patch XXXX.schema.address-alert.sql
+--
+BEGIN;
+
+-- check whether patch can be applied
+SELECT evergreen.upgrade_deps_block_check('XXXX', :eg_version);
+
+CREATE TABLE actor.address_alert (
+    id              SERIAL  PRIMARY KEY,
+    owner           INT     NOT NULL REFERENCES actor.org_unit (id) DEFERRABLE INITIALLY DEFERRED,
+    active          BOOL    NOT NULL DEFAULT TRUE,
+    match_all       BOOL    NOT NULL DEFAULT TRUE,
+    alert_message   TEXT    NOT NULL,
+    street1         TEXT,
+    street2         TEXT,
+    city            TEXT,
+    county          TEXT,
+    state           TEXT,
+    country         TEXT,
+    post_code       TEXT,
+    mailing_address BOOL    NOT NULL DEFAULT FALSE,
+    billing_address BOOL    NOT NULL DEFAULT FALSE
+);
+
+CREATE OR REPLACE FUNCTION actor.address_alert_matches (
+        org_unit INT, 
+        street1 TEXT, 
+        street2 TEXT, 
+        city TEXT, 
+        county TEXT, 
+        state TEXT, 
+        country TEXT, 
+        post_code TEXT,
+        mailing_address BOOL DEFAULT FALSE,
+        billing_address BOOL DEFAULT FALSE
+    ) RETURNS SETOF actor.address_alert AS $$
+
+SELECT *
+FROM actor.address_alert
+WHERE
+    active
+    AND owner IN (SELECT id FROM actor.org_unit_ancestors($1)) 
+    AND (
+        (NOT mailing_address AND NOT billing_address)
+        OR (mailing_address AND $9)
+        OR (billing_address AND $10)
+    )
+    AND (
+            (
+                match_all
+                AND COALESCE($2, '') ~* COALESCE(street1,   '.*')
+                AND COALESCE($3, '') ~* COALESCE(street2,   '.*')
+                AND COALESCE($4, '') ~* COALESCE(city,      '.*')
+                AND COALESCE($5, '') ~* COALESCE(county,    '.*')
+                AND COALESCE($6, '') ~* COALESCE(state,     '.*')
+                AND COALESCE($7, '') ~* COALESCE(country,   '.*')
+                AND COALESCE($8, '') ~* COALESCE(post_code, '.*')
+            ) OR (
+                NOT match_all 
+                AND (  
+                       $2 ~* street1
+                    OR $3 ~* street2
+                    OR $4 ~* city
+                    OR $5 ~* county
+                    OR $6 ~* state
+                    OR $7 ~* country
+                    OR $8 ~* post_code
+                )
+            )
+        )
+    ORDER BY actor.org_unit_proximity(owner, $1)
+$$ LANGUAGE SQL;
+
+COMMIT;
+
+/* UNDO
+DROP FUNCTION actor.address_alert_matches(INT, TEXT, TEXT, TEXT, TEXT, TEXT, TEXT, TEXT, BOOL, BOOL);
+DROP TABLE actor.address_alert;
+*/