]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/sql/Pg/400.schema.action_trigger.sql
added format.po.jedi hook to initial seed
[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 IF EXISTS 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 ('lost.auto','circ','Circulating Item automatically marked lost');
34 INSERT INTO action_trigger.hook (key,core_type,description) VALUES ('claims_returned','circ','Circulating Item marked Claims Returned');
35 INSERT INTO action_trigger.hook (key,core_type,description) VALUES ('claims_returned.found','circ','Claims Returned Circulating Item is checked in');
36 INSERT INTO action_trigger.hook (key,core_type,description) VALUES ('missing','acp','Item marked Missing');
37 INSERT INTO action_trigger.hook (key,core_type,description) VALUES ('missing.found','acp','Missing Item checked in');
38 INSERT INTO action_trigger.hook (key,core_type,description) VALUES ('transit.start','acp','An Item is placed into transit');
39 INSERT INTO action_trigger.hook (key,core_type,description) VALUES ('transit.finish','acp','An Item is received from a transit');
40 INSERT INTO action_trigger.hook (key,core_type,description) VALUES ('hold_request.success','ahr','A hold is successfully placed');
41 INSERT INTO action_trigger.hook (key,core_type,description) VALUES ('hold_request.failure','ahr','A hold is attempted but not successfully placed');
42 INSERT INTO action_trigger.hook (key,core_type,description) VALUES ('hold.capture','ahr','A targeted Item is captured for a hold');
43 INSERT INTO action_trigger.hook (key,core_type,description) VALUES ('hold.available','ahr','A held item is ready for pickup');
44 INSERT INTO action_trigger.hook (key,core_type,description) VALUES ('hold_transit.start','ahtc','A hold-captured Item is placed into transit');
45 INSERT INTO action_trigger.hook (key,core_type,description) VALUES ('hold_transit.finish','ahtc','A hold-captured Item is received from a transit');
46 INSERT INTO action_trigger.hook (key,core_type,description,passive) VALUES ('checkout.due','circ','Checked out Item is Due',TRUE);
47 INSERT INTO action_trigger.hook (key,core_type,description,passive) VALUES ('penalty.PATRON_EXCEEDS_FINES','ausp','Patron has exceeded allowed fines',TRUE);
48 INSERT INTO action_trigger.hook (key,core_type,description,passive) VALUES ('penalty.PATRON_EXCEEDS_OVERDUE_COUNT','ausp','Patron has exceeded allowed overdue count',TRUE);
49 INSERT INTO action_trigger.hook (key,core_type,description,passive) VALUES ('penalty.PATRON_EXCEEDS_CHECKOUT_COUNT','ausp','Patron has exceeded allowed checkout count',TRUE);
50 INSERT INTO action_trigger.hook (key,core_type,description,passive) VALUES ('penalty.PATRON_EXCEEDS_COLLECTIONS_WARNING','ausp','Patron has exceeded maximum fine amount for collections department warning',TRUE);
51 INSERT INTO action_trigger.hook (key,core_type,description,passive) VALUES ('acqpo.activated','acqpo','Purchase order was activated',FALSE);
52 INSERT INTO action_trigger.hook (key,core_type,description,passive) VALUES ('format.po.jedi','acqpo','Formats a Purchase Order as a JEDI document',TRUE);
53 INSERT INTO action_trigger.hook (key,core_type,description,passive) VALUES ('format.po.html','acqpo','Formats a Purchase Order as an HTML document',TRUE);
54 INSERT INTO action_trigger.hook (key,core_type,description,passive) VALUES ('format.po.pdf','acqpo','Formats a Purchase Order as a PDF document',TRUE);
55 INSERT INTO action_trigger.hook (key,core_type,description) VALUES ('damaged','acp','Item marked damaged');
56 INSERT INTO action_trigger.hook (key,core_type,description) VALUES ('checkout.damaged','circ','A circulating item is marked damaged and the patron is fined');
57 INSERT INTO action_trigger.hook (key,core_type,description) VALUES ('renewal','circ','Item renewed to user');
58
59 -- and much more, I'm sure
60
61 -- Specialized collection modules.  Given an FM object, gather some info and return a scalar or ref.
62 CREATE TABLE action_trigger.collector (
63     module      TEXT    PRIMARY KEY, -- All live under the OpenILS::Trigger::Collector:: namespace
64     description TEXT    
65 );
66 INSERT INTO action_trigger.collector (module,description) VALUES ('fourty_two','Returns the answer to life, the universe and everything');
67 --INSERT INTO action_trigger.collector (module,description) VALUES ('CircCountsByCircMod','Count of Circulations for a User, broken down by circulation modifier');
68
69 -- Simple tests on an FM object from hook.core_type to test for "should we still do this."
70 CREATE TABLE action_trigger.validator (
71     module      TEXT    PRIMARY KEY, -- All live under the OpenILS::Trigger::Validator:: namespace
72     description TEXT    
73 );
74 INSERT INTO action_trigger.validator (module,description) VALUES ('fourty_two','Returns the answer to life, the universe and everything');
75 INSERT INTO action_trigger.validator (module,description) VALUES ('NOOP_True','Always returns true -- validation always passes');
76 INSERT INTO action_trigger.validator (module,description) VALUES ('NOOP_False','Always returns false -- validation always fails');
77 INSERT INTO action_trigger.validator (module,description) VALUES ('CircIsOpen','Check that the circulation is still open');
78 INSERT INTO action_trigger.validator (module,description) VALUES ('HoldIsAvailable','Check that an item is on the hold shelf');
79 INSERT INTO action_trigger.validator (module,description) VALUES ('CircIsOverdue','Check that the circulation is overdue');
80 INSERT INTO action_trigger.validator (module,description) VALUES ('MaxPassiveDelayAge','Check that the event is not too far past the delay_field time -- requires a max_delay_age interval parameter');
81 INSERT INTO action_trigger.validator (module,description) VALUES ('MinPassiveTargetAge','Check that the target is old enough to be used by this event -- requires a min_target_age interval parameter, and accepts an optional target_age_field to specify what time to use for offsetting');
82
83 -- After an event passes validation (action_trigger.validator), the reactor processes it.
84 CREATE TABLE action_trigger.reactor (
85     module      TEXT    PRIMARY KEY, -- All live under the OpenILS::Trigger::Reactor:: namespace
86     description TEXT    
87 );
88
89 INSERT INTO action_trigger.reactor (module,description) VALUES
90 (   'fourty_two',
91     oils_i18n_gettext(
92         'fourty_two',
93         'Returns the answer to life, the universe and everything',
94         'atreact',
95         'description'
96     )
97 );
98 INSERT INTO action_trigger.reactor (module,description) VALUES
99 (   'NOOP_True',
100     oils_i18n_gettext(
101         'NOOP_True',
102         'Always returns true -- reaction always passes',
103         'atreact',
104         'description'
105     )
106 );
107 INSERT INTO action_trigger.reactor (module,description) VALUES
108 (   'NOOP_False',
109     oils_i18n_gettext(
110         'NOOP_False',
111         'Always returns false -- reaction always fails',
112         'atreact',
113         'description'
114     )
115 );
116 INSERT INTO action_trigger.reactor (module,description) VALUES
117 (   'SendEmail',
118     oils_i18n_gettext(
119         'SendEmail',
120         'Send an email based on a user-defined template',
121         'atreact',
122         'description'
123     )
124 );
125
126 -- TODO: build a PDF generator
127 --INSERT INTO action_trigger.reactor (module,description) VALUES
128 --(   'GenerateBatchOverduePDF',
129 --    oils_i18n_gettext(
130 --        'GenerateBatchOverduePDF',
131 --        'Output a batch PDF of overdue notices for printing',
132 --        'atreact',
133 --        'description'
134 --    )
135 --);
136
137 INSERT INTO action_trigger.reactor (module,description) VALUES
138 (   'MarkItemLost',
139     oils_i18n_gettext(
140         'MarkItemLost',
141         'Marks a circulation and associated item as lost',
142         'atreact',
143         'description'
144     )
145 );
146 INSERT INTO action_trigger.reactor (module,description) VALUES
147 (   'ApplyCircFee',
148     oils_i18n_gettext(
149         'ApplyCircFee',
150         'Applies a billing with a pre-defined amount to a circulation',
151         'atreact',
152         'description'
153     )
154 );
155 INSERT INTO action_trigger.reactor (module,description) VALUES
156 (   'ProcessTemplate',
157     oils_i18n_gettext(
158         'ProcessTemplate',
159         'Processes the configured template',
160         'atreact',
161         'description'
162     )
163 );
164
165 -- After an event is reacted to (either success or failure) a cleanup module is run against the resulting environment
166 CREATE TABLE action_trigger.cleanup (
167     module      TEXT    PRIMARY KEY, -- All live under the OpenILS::Trigger::Cleanup:: namespace
168     description TEXT    
169 );
170 INSERT INTO action_trigger.cleanup (module,description) VALUES ('fourty_two','Returns the answer to life, the universe and everything');
171 INSERT INTO action_trigger.cleanup (module,description) VALUES ('NOOP_True','Always returns true -- cleanup always passes');
172 INSERT INTO action_trigger.cleanup (module,description) VALUES ('NOOP_False','Always returns false -- cleanup always fails');
173 INSERT INTO action_trigger.cleanup (module,description) VALUES ('ClearAllPending','Remove all future, pending notifications for this target');
174
175 CREATE TABLE action_trigger.event_definition (
176     id              SERIAL      PRIMARY KEY,
177     active          BOOL        NOT NULL DEFAULT TRUE,
178     owner           INT         NOT NULL REFERENCES actor.org_unit (id) DEFERRABLE INITIALLY DEFERRED,
179     name            TEXT        NOT NULL,
180     hook            TEXT        NOT NULL REFERENCES action_trigger.hook (key) DEFERRABLE INITIALLY DEFERRED,
181     validator       TEXT        NOT NULL REFERENCES action_trigger.validator (module) DEFERRABLE INITIALLY DEFERRED,
182     reactor         TEXT        NOT NULL REFERENCES action_trigger.reactor (module) DEFERRABLE INITIALLY DEFERRED,
183     cleanup_success TEXT        REFERENCES action_trigger.cleanup (module) DEFERRABLE INITIALLY DEFERRED,
184     cleanup_failure TEXT        REFERENCES action_trigger.cleanup (module) DEFERRABLE INITIALLY DEFERRED,
185     delay           INTERVAL    NOT NULL DEFAULT '5 minutes',
186     max_delay       INTERVAL,
187     usr_field       TEXT,
188     opt_in_setting  TEXT        REFERENCES config.usr_setting_type (name) DEFERRABLE INITIALLY DEFERRED,
189     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()
190     group_field     TEXT,                 -- field from this.hook.core_type to batch event targets together on, fed into reactor a group at a time.
191     template        TEXT,                 -- 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.
192     granularity     TEXT,   -- could specify a batch which is the only time these events should actually run
193     CONSTRAINT ev_def_owner_hook_val_react_clean_delay_once UNIQUE (owner, hook, validator, reactor, delay, delay_field),
194     CONSTRAINT ev_def_name_owner_once UNIQUE (owner, name)
195 );
196
197 CREATE TABLE action_trigger.environment (
198     id          SERIAL  PRIMARY KEY,
199     event_def   INT     NOT NULL REFERENCES action_trigger.event_definition (id) DEFERRABLE INITIALLY DEFERRED,
200     path        TEXT,       -- fields to flesh. given a hook with a core_type of circ, imagine circ_lib.parent_ou expanding to
201                             -- {flesh: 2, flesh_fields: {circ: ['circ_lib'], aou: ['parent_ou']}} ... default is to flesh all
202                             -- at flesh depth 1
203     collector   TEXT    REFERENCES action_trigger.collector (module) DEFERRABLE INITIALLY DEFERRED, -- if set, given the object at 'path', return some data
204                                                                       -- to be stashed at environment.<label>
205     label       TEXT    CHECK (label NOT IN ('result','target','event')),
206     CONSTRAINT env_event_label_once UNIQUE (event_def,label)
207 );
208
209 CREATE TABLE action_trigger.event_output (
210     id              BIGSERIAL   PRIMARY KEY,
211     create_time     TIMESTAMPTZ NOT NULL DEFAULT NOW(),
212     is_error        BOOLEAN     NOT NULL DEFAULT FALSE,
213     data            TEXT        NOT NULL
214 );
215
216 CREATE TABLE action_trigger.event (
217     id              BIGSERIAL   PRIMARY KEY,
218     target          BIGINT      NOT NULL, -- points at the id from class defined by event_def.hook.core_type
219     event_def       INT         REFERENCES action_trigger.event_definition (id) DEFERRABLE INITIALLY DEFERRED,
220     add_time        TIMESTAMPTZ NOT NULL DEFAULT NOW(),
221     run_time        TIMESTAMPTZ NOT NULL,
222     start_time      TIMESTAMPTZ,
223     update_time     TIMESTAMPTZ,
224     complete_time   TIMESTAMPTZ,
225     update_process  INT,
226     state           TEXT        NOT NULL DEFAULT 'pending' CHECK (state IN ('pending','invalid','found','collecting','collected','validating','valid','reacting','reacted','cleaning','complete','error')),
227     user_data       TEXT        CHECK (user_data IS NULL OR is_json( user_data )),
228     template_output BIGINT      REFERENCES action_trigger.event_output (id),
229     error_output    BIGINT      REFERENCES action_trigger.event_output (id),
230     async_output    BIGINT      REFERENCES action_trigger.event_output (id)
231 );
232 CREATE INDEX atev_target_def_idx ON action_trigger.event (target,event_def);
233
234 CREATE TABLE action_trigger.event_params (
235     id          BIGSERIAL   PRIMARY KEY,
236     event_def   INT         NOT NULL REFERENCES action_trigger.event_definition (id) DEFERRABLE INITIALLY DEFERRED,
237     param       TEXT        NOT NULL, -- the key under environment.event.params to store the output of ...
238     value       TEXT        NOT NULL, -- ... the eval() output of this.  Has access to environment (and, well, all of perl)
239     CONSTRAINT event_params_event_def_param_once UNIQUE (event_def,param)
240 );
241
242 COMMIT;
243