]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/sql/Pg/upgrade/0504.schema.parts_and_cnaffix.sql
LP1079041 - making state not required (continued)
[working/Evergreen.git] / Open-ILS / src / sql / Pg / upgrade / 0504.schema.parts_and_cnaffix.sql
1 BEGIN;
2
3 INSERT INTO config.upgrade_log (version) VALUES ('0504'); -- miker
4
5 CREATE OR REPLACE FUNCTION lpad_number_substrings( TEXT, TEXT, INT ) RETURNS TEXT AS $$
6     my $string = shift;
7     my $pad = shift;
8     my $len = shift;
9     my $find = $len - 1;
10
11     while ($string =~ /(?:^|\D)(\d{1,$find})(?:$|\D)/) {
12         my $padded = $1;
13         $padded = $pad x ($len - length($padded)) . $padded;
14         $string =~ s/$1/$padded/sg;
15     }
16
17     return $string;
18 $$ LANGUAGE PLPERLU;
19
20 CREATE TABLE biblio.monograph_part (
21     id              SERIAL  PRIMARY KEY,
22     record          BIGINT  NOT NULL REFERENCES biblio.record_entry (id),
23     label           TEXT    NOT NULL,
24     label_sortkey   TEXT    NOT NULL,
25     CONSTRAINT record_label_unique UNIQUE (record,label)
26 );
27
28 CREATE OR REPLACE FUNCTION biblio.normalize_biblio_monograph_part_sortkey () RETURNS TRIGGER AS $$
29 BEGIN
30     NEW.label_sortkey := REGEXP_REPLACE(
31         lpad_number_substrings(
32             naco_normalize(NEW.label),
33             '0',
34             10
35         ),
36         E'\\s+',
37         '',
38         'g'
39     );
40     RETURN NEW;
41 END;
42 $$ LANGUAGE PLPGSQL;
43
44 CREATE TRIGGER norm_sort_label BEFORE INSERT OR UPDATE ON biblio.monograph_part FOR EACH ROW EXECUTE PROCEDURE biblio.normalize_biblio_monograph_part_sortkey();
45
46 CREATE TABLE asset.copy_part_map (
47     id          SERIAL  PRIMARY KEY,
48     target_copy BIGINT  NOT NULL, -- points o asset.copy
49     part        INT     NOT NULL REFERENCES biblio.monograph_part (id) ON DELETE CASCADE
50 );
51 CREATE UNIQUE INDEX copy_part_map_cp_part_idx ON asset.copy_part_map (target_copy, part);
52
53 CREATE OR REPLACE FUNCTION asset.normalize_affix_sortkey () RETURNS TRIGGER AS $$
54 BEGIN
55     NEW.label_sortkey := REGEXP_REPLACE(
56         lpad_number_substrings(
57             naco_normalize(NEW.label),
58             '0',
59             10
60         ),
61         E'\\s+',
62         '',
63         'g'
64     );
65     RETURN NEW;
66 END;
67 $$ LANGUAGE PLPGSQL;
68
69 CREATE TABLE asset.call_number_prefix (
70        id                      SERIAL   PRIMARY KEY,
71        owning_lib          INT                 NOT NULL REFERENCES actor.org_unit (id),
72        label               TEXT                NOT NULL, -- i18n
73        label_sortkey   TEXT
74 );
75 CREATE TRIGGER prefix_normalize_tgr BEFORE INSERT OR UPDATE ON asset.call_number_prefix FOR EACH ROW EXECUTE PROCEDURE asset.normalize_affix_sortkey();
76 CREATE UNIQUE INDEX asset_call_number_prefix_once_per_lib ON asset.call_number_prefix (label, owning_lib);
77 CREATE INDEX asset_call_number_prefix_sortkey_idx ON asset.call_number_prefix (label_sortkey);
78
79 CREATE TABLE asset.call_number_suffix (
80        id                      SERIAL   PRIMARY KEY,
81        owning_lib          INT                 NOT NULL REFERENCES actor.org_unit (id),
82        label               TEXT                NOT NULL, -- i18n
83        label_sortkey   TEXT
84 );
85 CREATE TRIGGER suffix_normalize_tgr BEFORE INSERT OR UPDATE ON asset.call_number_suffix FOR EACH ROW EXECUTE PROCEDURE asset.normalize_affix_sortkey();
86 CREATE UNIQUE INDEX asset_call_number_suffix_once_per_lib ON asset.call_number_suffix (label, owning_lib);
87 CREATE INDEX asset_call_number_suffix_sortkey_idx ON asset.call_number_suffix (label_sortkey);
88
89 INSERT INTO asset.call_number_suffix (id, owning_lib, label) VALUES (-1, 1, '');
90 INSERT INTO asset.call_number_prefix (id, owning_lib, label) VALUES (-1, 1, '');
91
92 DROP INDEX IF EXISTS asset.asset_call_number_label_once_per_lib;
93
94 ALTER TABLE asset.call_number
95     ADD COLUMN prefix INT NOT NULL DEFAULT -1 REFERENCES asset.call_number_prefix(id) DEFERRABLE INITIALLY DEFERRED,
96     ADD COLUMN suffix INT NOT NULL DEFAULT -1 REFERENCES asset.call_number_suffix(id) DEFERRABLE INITIALLY DEFERRED;
97
98 CREATE UNIQUE INDEX asset_call_number_label_once_per_lib ON asset.call_number (record, owning_lib, label, prefix, suffix) WHERE deleted = FALSE OR deleted IS FALSE;
99
100 INSERT INTO config.org_unit_setting_type ( name, label, description, datatype ) VALUES (
101     'ui.cat.volume_copy_editor.horizontal',
102     oils_i18n_gettext(
103         'ui.cat.volume_copy_editor.horizontal',
104         'GUI: Horizontal layout for Volume/Copy Creator/Editor.',
105         'coust', 'label'),
106     oils_i18n_gettext(
107         'ui.cat.volume_copy_editor.horizontal',
108         'The main entry point for this interface is in Holdings Maintenance, Actions for Selected Rows, Edit Item Attributes / Call Numbers / Replace Barcodes.  This setting changes the top and bottom panes for that interface into left and right panes.',
109         'coust', 'description'),
110     'bool'
111 );
112
113
114 CREATE OR REPLACE FUNCTION unapi.bmp ( obj_id BIGINT, format TEXT,  ename TEXT, includes TEXT[], org TEXT, depth INT DEFAULT NULL, slimit INT DEFAULT NULL, soffset INT DEFAULT NULL ) RETURNS XML AS $F$
115         SELECT  XMLELEMENT(
116                     name monograph_part,
117                     XMLATTRIBUTES(
118                         'http://open-ils.org/spec/holdings/v1' AS xmlns,
119                         'tag:open-ils.org:U2@bmp/' || id AS id,
120                         id AS ident,
121                         label,
122                         label_sortkey,
123                         'tag:open-ils.org:U2@bre/' || record AS record
124                     ),
125                     CASE 
126                         WHEN ('acp' = ANY ($4)) THEN
127                             XMLELEMENT( name copies,
128                                 (SELECT XMLAGG(acp) FROM (
129                                     SELECT  unapi.acp( cp.id, 'xml', 'copy', array_remove_item_by_value($4,'bmp'), $5, $6, $7, $8)
130                                       FROM  asset.copy cp
131                                             JOIN asset.copy_part_map cpm ON (cpm.target_copy = cp.id)
132                                       WHERE cpm.part = $1
133                                       ORDER BY COALESCE(cp.copy_number,0), cp.barcode
134                                       LIMIT $7
135                                       OFFSET $8
136                                 )x)
137                             )
138                         ELSE NULL
139                     END,
140                     CASE WHEN ('bre' = ANY ($4)) THEN unapi.bre( record, 'marcxml', 'record', array_remove_item_by_value($4,'bmp'), $5, $6, $7, $8) ELSE NULL END
141                 )
142           FROM  biblio.monograph_part
143           WHERE id = $1
144           GROUP BY id, label, label_sortkey, record;
145 $F$ LANGUAGE SQL;
146
147 CREATE OR REPLACE FUNCTION unapi.acnp ( obj_id BIGINT, format TEXT,  ename TEXT, includes TEXT[], org TEXT, depth INT DEFAULT NULL, slimit INT DEFAULT NULL, soffset INT DEFAULT NULL ) RETURNS XML AS $F$
148         SELECT  XMLELEMENT(
149                     name call_number_prefix,
150                     XMLATTRIBUTES(
151                         'http://open-ils.org/spec/holdings/v1' AS xmlns,
152                         id AS ident,
153                         label,
154                         label_sortkey
155                     ),
156                     unapi.aou( owning_lib, $2, 'owning_lib', array_remove_item_by_value($4,'acnp'), $5, $6, $7, $8)
157                 )
158           FROM  asset.call_number_prefix
159           WHERE id = $1;
160 $F$ LANGUAGE SQL;
161
162 CREATE OR REPLACE FUNCTION unapi.acns ( obj_id BIGINT, format TEXT,  ename TEXT, includes TEXT[], org TEXT, depth INT DEFAULT NULL, slimit INT DEFAULT NULL, soffset INT DEFAULT NULL ) RETURNS XML AS $F$
163         SELECT  XMLELEMENT(
164                     name call_number_suffix,
165                     XMLATTRIBUTES(
166                         'http://open-ils.org/spec/holdings/v1' AS xmlns,
167                         id AS ident,
168                         label,
169                         label_sortkey
170                     ),
171                     unapi.aou( owning_lib, $2, 'owning_lib', array_remove_item_by_value($4,'acns'), $5, $6, $7, $8)
172                 )
173           FROM  asset.call_number_suffix
174           WHERE id = $1;
175 $F$ LANGUAGE SQL;
176
177 CREATE OR REPLACE FUNCTION unapi.holdings_xml (bid BIGINT, ouid INT, org TEXT, depth INT DEFAULT NULL, includes TEXT[] DEFAULT NULL::TEXT[], slimit INT DEFAULT NULL, soffset INT DEFAULT NULL) RETURNS XML AS $F$
178      SELECT  XMLELEMENT(
179                  name holdings,
180                  XMLATTRIBUTES(
181                     'http://open-ils.org/spec/holdings/v1' AS xmlns,
182                     CASE WHEN ('bre' = ANY ('{acn,auri}'::TEXT[] || $5)) THEN 'tag:open-ils.org:U2@bre/' || $1 || '/' || $3 ELSE NULL END AS id
183                  ),
184                  XMLELEMENT(
185                      name counts,
186                      (SELECT  XMLAGG(XMLELEMENT::XML) FROM (
187                          SELECT  XMLELEMENT(
188                                      name count,
189                                      XMLATTRIBUTES('public' as type, depth, org_unit, coalesce(transcendant,0) as transcendant, available, visible as count, unshadow)
190                                  )::text
191                            FROM  asset.opac_ou_record_copy_count($2,  $1)
192                                      UNION
193                          SELECT  XMLELEMENT(
194                                      name count,
195                                      XMLATTRIBUTES('staff' as type, depth, org_unit, coalesce(transcendant,0) as transcendant, available, visible as count, unshadow)
196                                  )::text
197                            FROM  asset.staff_ou_record_copy_count($2, $1)
198                                      ORDER BY 1
199                      )x)
200                  ),
201                  CASE 
202                      WHEN ('bmp' = ANY ($5)) THEN
203                         XMLELEMENT( name monograph_parts,
204                             XMLAGG((SELECT unapi.bmp( id, 'xml', 'monograph_part', array_remove_item_by_value( array_remove_item_by_value($5,'bre'), 'holdings_xml'), $3, $4, $6, $7) FROM biblio.monograph_part WHERE record = $1))
205                         )
206                      ELSE NULL
207                  END,
208                  CASE WHEN ('acn' = ANY ('{acn,auri}'::TEXT[] || $5)) THEN 
209                      XMLELEMENT(
210                          name volumes,
211                          (SELECT XMLAGG(acn) FROM (
212                             SELECT  unapi.acn(acn.id,'xml','volume',array_remove_item_by_value(array_remove_item_by_value('{acn,auri}'::TEXT[] || $5,'holdings_xml'),'bre'), $3, $4, $6, $7)
213                               FROM  asset.call_number acn
214                               WHERE acn.record = $1
215                                     AND EXISTS (
216                                         SELECT  1
217                                           FROM  asset.copy acp
218                                                 JOIN actor.org_unit_descendants(
219                                                     $2,
220                                                     (COALESCE(
221                                                         $4,
222                                                         (SELECT aout.depth
223                                                           FROM  actor.org_unit_type aout
224                                                                 JOIN actor.org_unit aou ON (aou.ou_type = aout.id AND aou.id = $2)
225                                                         )
226                                                     ))
227                                                 ) aoud ON (acp.circ_lib = aoud.id)
228                                           LIMIT 1
229                                     )
230                               ORDER BY label_sortkey
231                               LIMIT $6
232                               OFFSET $7
233                          )x)
234                      )
235                  ELSE NULL END,
236                  CASE WHEN ('ssub' = ANY ('{acn,auri}'::TEXT[] || $5)) THEN 
237                      XMLELEMENT(
238                          name subscriptions,
239                          (SELECT XMLAGG(ssub) FROM (
240                             SELECT  unapi.ssub(id,'xml','subscription','{}'::TEXT[], $3, $4, $6, $7)
241                               FROM  serial.subscription
242                               WHERE record_entry = $1
243                         )x)
244                      )
245                  ELSE NULL END
246              );
247 $F$ LANGUAGE SQL;
248
249 CREATE OR REPLACE FUNCTION unapi.acp ( obj_id BIGINT, format TEXT,  ename TEXT, includes TEXT[], org TEXT, depth INT DEFAULT NULL, slimit INT DEFAULT NULL, soffset INT DEFAULT NULL ) RETURNS XML AS $F$
250         SELECT  XMLELEMENT(
251                     name copy,
252                     XMLATTRIBUTES(
253                         'http://open-ils.org/spec/holdings/v1' AS xmlns,
254                         'tag:open-ils.org:U2@acp/' || id AS id,
255                         create_date, edit_date, copy_number, circulate, deposit,
256                         ref, holdable, deleted, deposit_amount, price, barcode,
257                         circ_modifier, circ_as_type, opac_visible
258                     ),
259                     unapi.ccs( status, $2, 'status', array_remove_item_by_value($4,'acp'), $5, $6, $7, $8),
260                     unapi.acl( location, $2, 'location', array_remove_item_by_value($4,'acp'), $5, $6, $7, $8),
261                     unapi.aou( circ_lib, $2, 'circ_lib', array_remove_item_by_value($4,'acp'), $5, $6, $7, $8),
262                     unapi.aou( circ_lib, $2, 'circlib', array_remove_item_by_value($4,'acp'), $5, $6, $7, $8),
263                     CASE WHEN ('acn' = ANY ($4)) THEN unapi.acn( call_number, $2, 'call_number', array_remove_item_by_value($4,'acp'), $5, $6, $7, $8) ELSE NULL END,
264                     XMLELEMENT( name copy_notes,
265                         CASE 
266                             WHEN ('acpn' = ANY ($4)) THEN
267                                 XMLAGG((SELECT unapi.acpn( id, 'xml', 'copy_note', array_remove_item_by_value($4,'acp'), $5, $6, $7, $8) FROM asset.copy_note WHERE owning_copy = cp.id AND pub))
268                             ELSE NULL
269                         END
270                     ),
271                     XMLELEMENT( name statcats,
272                         CASE 
273                             WHEN ('ascecm' = ANY ($4)) THEN
274                                 XMLAGG((SELECT unapi.ascecm( stat_cat_entry, 'xml', 'statcat', array_remove_item_by_value($4,'acp'), $5, $6, $7, $8) FROM asset.stat_cat_entry_copy_map WHERE owning_copy = cp.id))
275                             ELSE NULL
276                         END
277                     ),
278                     CASE 
279                         WHEN ('bmp' = ANY ($4)) THEN
280                             XMLELEMENT( name monograph_parts,
281                                 XMLAGG((SELECT unapi.bmp( part, 'xml', 'monograph_part', array_remove_item_by_value($4,'acp'), $5, $6, $7, $8) FROM asset.copy_part_map WHERE target_copy = cp.id))
282                             )
283                         ELSE NULL
284                     END
285                 )
286           FROM  asset.copy cp
287           WHERE id = $1
288           GROUP BY id, status, location, circ_lib, call_number, create_date, edit_date, copy_number, circulate, deposit, ref, holdable, deleted, deposit_amount, price, barcode, circ_modifier, circ_as_type, opac_visible;
289 $F$ LANGUAGE SQL;
290
291 CREATE OR REPLACE FUNCTION unapi.acn ( obj_id BIGINT, format TEXT,  ename TEXT, includes TEXT[], org TEXT, depth INT DEFAULT NULL, slimit INT DEFAULT NULL, soffset INT DEFAULT NULL ) RETURNS XML AS $F$
292         SELECT  XMLELEMENT(
293                     name volume,
294                     XMLATTRIBUTES(
295                         'http://open-ils.org/spec/holdings/v1' AS xmlns,
296                         'tag:open-ils.org:U2@acn/' || acn.id AS id,
297                         o.shortname AS lib,
298                         o.opac_visible AS opac_visible,
299                         deleted, label, label_sortkey, label_class, record
300                     ),
301                     unapi.aou( owning_lib, $2, 'owning_lib', array_remove_item_by_value($4,'acn'), $5, $6, $7, $8),
302                     XMLELEMENT( name copies,
303                         CASE 
304                             WHEN ('acp' = ANY ($4)) THEN
305                                 (SELECT XMLAGG(acp) FROM (
306                                     SELECT  unapi.acp( cp.id, 'xml', 'copy', array_remove_item_by_value($4,'acn'), $5, $6, $7, $8)
307                                       FROM  asset.copy cp
308                                             JOIN actor.org_unit_descendants(
309                                                 (SELECT id FROM actor.org_unit WHERE shortname = $5),
310                                                 (COALESCE($6,(SELECT aout.depth FROM actor.org_unit_type aout JOIN actor.org_unit aou ON (aou.ou_type = aout.id AND aou.shortname = $5))))
311                                             ) aoud ON (cp.circ_lib = aoud.id)
312                                       WHERE cp.call_number = acn.id
313                                       ORDER BY COALESCE(cp.copy_number,0), cp.barcode
314                                       LIMIT $7
315                                       OFFSET $8
316                                 )x)
317                             ELSE NULL
318                         END
319                     ),
320                     XMLELEMENT(
321                         name uris,
322                         (SELECT XMLAGG(auri) FROM (SELECT unapi.auri(uri,'xml','uri', array_remove_item_by_value($4,'acn'), $5, $6, $7, $8) FROM asset.uri_call_number_map WHERE call_number = acn.id)x)
323                     ),
324                     CASE WHEN ('acnp' = ANY ($4)) THEN unapi.acnp( acn.prefix, 'marcxml', 'prefix', array_remove_item_by_value($4,'acn'), $5, $6, $7, $8) ELSE NULL END,
325                     CASE WHEN ('acns' = ANY ($4)) THEN unapi.acns( acn.suffix, 'marcxml', 'suffix', array_remove_item_by_value($4,'acn'), $5, $6, $7, $8) ELSE NULL END,
326                     CASE WHEN ('bre' = ANY ($4)) THEN unapi.bre( acn.record, 'marcxml', 'record', array_remove_item_by_value($4,'acn'), $5, $6, $7, $8) ELSE NULL END
327                 ) AS x
328           FROM  asset.call_number acn
329                 JOIN actor.org_unit o ON (o.id = acn.owning_lib)
330           WHERE acn.id = $1
331           GROUP BY acn.id, o.shortname, o.opac_visible, deleted, label, label_sortkey, label_class, owning_lib, record, acn.prefix, acn.suffix;
332 $F$ LANGUAGE SQL;
333
334
335 COMMIT;
336