munge schema
[evergreen-equinox.git] / Open-ILS / src / sql / Pg / 991.schema.munge.sql
1 /*
2  * Copyright (C) 2011  Equinox Software, Inc.
3  * Galen Charlton <gmc@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 munge CASCADE;
18
19 BEGIN;
20
21 CREATE SCHEMA munge;
22
23 COMMENT ON SCHEMA munge IS $$
24 munge - Evergreen database utilities
25
26 The schema munge contains stored procedures used for maintaining
27 an Evergreen database, including migration and data update
28 routines.  Routines in munge will not necessarily have a
29 user interface exposed in the Evergreen staff client or OPAC,
30 and will typically be invoked from the command line.  However,
31 other Evergreen application code is free to make use of munge.
32 $$;
33
34 -- original version of routine proposed by Ben Ostrowsky on 2011-04-13
35 CREATE OR REPLACE FUNCTION munge.change_copy_call_number(copy_id asset.copy.id%TYPE, new_label asset.call_number.label%TYPE) RETURNS BOOLEAN AS $$
36 DECLARE
37     current_acn     asset.call_number%ROWTYPE;
38     new_acn_id      asset.call_number.id%TYPE;
39 BEGIN
40  
41     -- bail out if new label is invalid; we will not touch
42     -- located URIs in this routine
43     IF new_label = '##URI##' OR new_label IS NULL THEN
44         RETURN false;
45     END IF;
46  
47     -- get current volume
48     SELECT acn.* INTO current_acn
49         FROM asset.copy ac
50         JOIN asset.call_number acn ON (acn.id = ac.call_number)
51         WHERE ac.id = copy_id;
52     IF NOT FOUND THEN
53         RETURN false;
54     END IF;
55
56     -- bail out if the label already is ##URI##
57     IF current_acn.label = '##URI##' THEN
58         RETURN false;
59     END IF;
60
61     -- bail out if the call number label is already correct
62     IF current_acn.label = new_label THEN
63         RETURN false;
64     END IF;
65
66     -- bail out if we're a precat
67     IF current_acn.id = -1 THEN
68         RETURN false;
69     END IF;
70  
71     -- Check whether we already have a destination volume available
72     SELECT id INTO new_acn_id FROM asset.call_number 
73     WHERE 
74         record     = current_acn.record AND
75         owning_lib = current_acn.owning_lib AND
76         label      = new_label AND
77         NOT deleted;
78
79     -- Create destination volume if needed
80     IF NOT FOUND THEN
81         -- if at least on other item is attached to the current volume, create a
82         -- new volume so as to not change the the other item's call number label, otherwise
83         -- change the label in place
84         PERFORM 1 FROM asset.copy WHERE NOT deleted AND id <> copy_id AND call_number = current_acn.id;
85         IF FOUND THEN
86             INSERT INTO asset.call_number (creator, editor, record, owning_lib, label) 
87                 VALUES (1, 1, current_acn.record, current_acn.owning_lib, new_label);
88             SELECT CURRVAL('asset.call_number_id_seq') INTO new_acn_id;
89         ELSE
90             UPDATE asset.call_number
91                 SET editor = 1,
92                     label = new_label,
93                     edit_date = NOW()
94                 WHERE id = current_acn.id;
95         END IF;
96     END IF;
97
98     IF new_acn_id IS NOT NULL THEN
99         -- Move copy to destination
100         UPDATE asset.copy SET call_number = new_acn_id WHERE id = copy_id;
101
102         -- Delete source volume if it is now empty
103         PERFORM 1 FROM asset.copy WHERE call_number = current_acn.id AND NOT deleted;
104         IF NOT FOUND THEN
105             DELETE FROM asset.call_number WHERE id = current_acn.id;
106         END IF;
107     END IF;
108
109     RETURN true;
110 END;
111 $$ LANGUAGE PLPGSQL;
112
113 COMMENT ON FUNCTION munge.change_copy_call_number(asset.copy.id%TYPE, asset.call_number.label%TYPE) IS $$
114 Change the call number label associated with the specified item,
115 creating, relinking, and deleting volume records as needed.  The
116 call number associated with any other items attached to the target
117 item's original volume will remain unchanged.
118
119 Accepts the following parameters:
120
121 copy_id   - asset.copy.id of the copy to change
122 new_label - new call number string
123
124 The return value is a boolean indicating whether the copy call
125 number was changed or not.
126
127 This routine will refuse to touch or create located URIs.
128 $$;
129
130 COMMIT;