<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">
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;
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;
+
--- /dev/null
+-- 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;
+*/