]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/sql/Pg/400.schema.action_trigger.sql
initial revision of the generalized (ILS action) trigger schema
[working/Evergreen.git] / Open-ILS / src / sql / Pg / 400.schema.action_trigger.sql
1 /*
2  * Copyright (C) 2009  Equinox Software, Inc.
3  * Mike Rylander <miker@esilibrary.com> 
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  */
16
17 DROP SCHEMA action_trigger CASCADE;
18
19 BEGIN;
20
21 CREATE SCHEMA action_trigger;
22
23 CREATE TABLE action_trigger.hook (
24     key         TEXT    PRIMARY KEY,
25     core_type   TEXT    NOT NULL,
26     description TEXT,
27     passive     BOOL    NOT NULL DEFAULT FALSE
28 );
29 INSERT INTO action_trigger.hook (key,core_type,description) VALUES ('checkout','circ','Item checked out to user');
30 INSERT INTO action_trigger.hook (key,core_type,description) VALUES ('checkin','circ','Item checked in');
31 INSERT INTO action_trigger.hook (key,core_type,description) VALUES ('lost','circ','Circulating Item marked Lost');
32 INSERT INTO action_trigger.hook (key,core_type,description) VALUES ('lost.found','circ','Lost Circulating Item checked in');
33 INSERT INTO action_trigger.hook (key,core_type,description) VALUES ('claims_returned','circ','Circulating Item marked Claims Returned');
34 INSERT INTO action_trigger.hook (key,core_type,description) VALUES ('claims_returned.found','circ','Claims Returned Circulating Item is checked in');
35 INSERT INTO action_trigger.hook (key,core_type,description) VALUES ('missing','acp','Item marked Missing');
36 INSERT INTO action_trigger.hook (key,core_type,description) VALUES ('missing.found','acp','Missing Item checked in');
37 INSERT INTO action_trigger.hook (key,core_type,description) VALUES ('transit.start','acp','An Item is placed into transit');
38 INSERT INTO action_trigger.hook (key,core_type,description) VALUES ('transit.finish','acp','An Item is received from a transit');
39 INSERT INTO action_trigger.hook (key,core_type,description) VALUES ('hold_request.success','ahr','A hold is succefully placed');
40 INSERT INTO action_trigger.hook (key,core_type,description) VALUES ('hold_request.failure','ahr','A hold is attempted by not succefully placed');
41 INSERT INTO action_trigger.hook (key,core_type,description) VALUES ('hold.capture','ahr','A targeted Item is captured for a hold');
42 INSERT INTO action_trigger.hook (key,core_type,description) VALUES ('hold_transit.start','ahtc','A hold-captured Item is placed into transit');
43 INSERT INTO action_trigger.hook (key,core_type,description) VALUES ('hold_transit.finish','ahtc','A hold-captured Item is received from a transit');
44 INSERT INTO action_trigger.hook (key,core_type,description,passive) VALUES ('checkout.due','circ','Checked out Item is Due',TRUE);
45 -- and much more, I'm sure
46
47 -- Specialized collection modules.  Given an FM object, gather some info and return a scalar or ref.
48 CREATE TABLE action_trigger.collector (
49     module      TEXT    PRIMARY KEY, -- All live under the OpenILS::Trigger::Collector:: namespace
50     description TEXT    
51 );
52 INSERT INTO action_trigger.collector (module,description) VALUES ('CircCountsByCircMod','Count of Circulations for a User, broken down by circulation modifier');
53
54 -- Simple tests on an FM object from hook.core_type to test for "should we still do this."
55 CREATE TABLE action_trigger.validator (
56     module      TEXT    PRIMARY KEY, -- All live under the OpenILS::Trigger::Validator:: namespace
57     description TEXT    
58 );
59 INSERT INTO action_trigger.validator (module,description) VALUES ('CircIsOpen','Check that the circulation is still open');
60 INSERT INTO action_trigger.validator (module,description) VALUES ('HoldIsAvailable','Check that an item is on the hold shelf');
61
62 -- After an event passes validation (action_trigger.validator), the reactor processes it.
63 CREATE TABLE action_trigger.reactor (
64     module      TEXT    PRIMARY KEY, -- All live under the OpenILS::Trigger::Reactor:: namespace
65     description TEXT    
66 );
67 INSERT INTO action_trigger.reactor (module,description) VALUES ('SendEmail','Send an email based on a user-defined template');
68 INSERT INTO action_trigger.reactor (module,description) VALUES ('GenerateBatchOverduePDF','Output a batch PDF of overdue notices for printing');
69
70 -- After an event is reacted to (either succes or failure) a cleanup module is run against the resulting environment
71 CREATE TABLE action_trigger.cleanup (
72     module      TEXT    PRIMARY KEY, -- All live under the OpenILS::Trigger::Cleanup:: namespace
73     description TEXT    
74 );
75 INSERT INTO action_trigger.cleanup (module,description) VALUES ('ClearAllPending','Remove all future, pending notifications for this target');
76
77 CREATE TABLE action_trigger.event_definition (
78     id              SERIAL      PRIMARY KEY,
79     active          BOOL        NOT NULL DEFAULT TRUE,
80     owner           INT         NOT NULL REFERENCES actor.org_unit (id),
81     hook            INT         NOT NULL REFERENCES action_trigger.hook (key),
82     validator       INT         NOT NULL REFERENCES action_trigger.validator (module),
83     reactor         INT         NOT NULL REFERENCES action_trigger.reactor (module),
84     cleanup_success INT         REFERENCES action_trigger.reactor (module),
85     cleanup_failure INT         REFERENCES action_trigger.reactor (module),
86     delay           INTERVAL    NOT NULL DEFAULT '5 minutes',
87     delay_field     TEXT,                 -- for instance, xact_start on a circ hook ... look for fields on hook.core_type where datatype=timestamp? If not set, delay from now()
88     group_field     TEXT,                 -- field from this.hook.core_type to batch event targets together on, fed into reactor a group at a time.
89     template        TEXT        NOT NULL, -- the TT block.  will have an 'environment' hash (or array of hashes, grouped events) built up by validator and collector(s), which can be modified.
90     CONSTRAINT ev_def_owner_hook_val_react_clean_delay_once UNIQUE (owner, hook, validator, reactor, cleanup, delay, delay_field)
91
92 CREATE TABLE action_trigger.environment (
93     id          SERIAL  PRIMARY KEY,
94     event_def   INT     NOT NULL REFERENCES action_trigger.event_definition (id),
95     path        TEXT,       -- fields to flesh. given a hook with a core_type of circ, imagine circ_lib.parent_ou expanding to
96                             -- {flesh: 2, flesh_fields: {circ: ['circ_lib'], aou: ['parent_ou']}} ... default is to flesh all
97                             -- at flesh depth 1
98     collector   TEXT    REFERENCES action_trigger.collector (module), -- if set, given the object at 'path', return some data
99                                                                       -- to be stashed at environment.<label>
100     label       TEXT    CHECK (label NOT IN ('result','target','event')),
101     CONSTRAINT env_event_label_once UNIQUE (event_def,label)
102 );
103
104 CREATE TABLE action_trigger.event (
105     id              BIGSERIAL   PRIMARY KEY,
106     target          BIGINT      NOT NULL, -- points at the id from class defined by event_def.hook.core_type
107     event_def       INT         REFERENCES action_trigger.event_definition (id),
108     add_time        TIMESTAMPTZ NOT NULL DEFAULT NOW(),
109     run_time        TIMESTAMPTZ NOT NULL,
110     start_time      TIMESTAMPTZ,
111     update_time     TIMESTAMPTZ,
112     complete_time   TIMESTAMPTZ,
113     update_process  INT,
114     state           TEXT        NOT NULL DEFAULT 'pending' CHECK (state IN ('pending','found','collecting','validating','reacting','cleanup','complete','error'))
115     template_output TEXT,
116     error_output    TEXT
117 );
118
119 CREATE TABLE action_trigger.event_params (
120     id          BIGSERIAL   PRIMARY KEY,
121     event_def   INT         NOT NULL REFERENCES action_trigger.event_definition (id),
122     param       TEXT        NOT NULL, -- the key under environment.event.params to store the output of ...
123     value       TEXT        NOT NULL, -- ... the eval() output of this.  Has access to environmen (and, well, all of perl)
124     CONSTRAINT event_params_event_def_param_once UNIQUE (event_def,param)
125 );
126
127 COMMIT;
128