[REF] Refactoring according to the review of CHS
[odoo/odoo.git] / openerp / workflow / __init__.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #    
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
6 #
7 #    This program is free software: you can redistribute it and/or modify
8 #    it under the terms of the GNU Affero General Public License as
9 #    published by the Free Software Foundation, either version 3 of the
10 #    License, or (at your option) any later version.
11 #
12 #    This program is distributed in the hope that it will be useful,
13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #    GNU Affero General Public License for more details.
16 #
17 #    You should have received a copy of the GNU Affero General Public License
18 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.     
19 #
20 ##############################################################################
21
22 import instance
23
24 wkf_on_create_cache = {}
25
26 def clear_cache(cr, uid):
27     wkf_on_create_cache[cr.dbname]={}
28
29 def trg_write(uid, res_type, res_id, cr):
30     """
31     Reevaluates the specified workflow instance. Thus if any condition for
32     a transition have been changed in the backend, then running ``trg_write``
33     will move the workflow over that transition.
34
35     :param res_type: the model name
36     :param res_id: the model instance id the workflow belongs to
37     :param cr: a database cursor
38     """
39     ident = (uid,res_type,res_id)
40     cr.execute('select id from wkf_instance where res_id=%s and res_type=%s and state=%s', (res_id or None,res_type or None, 'active'))
41     for (id,) in cr.fetchall():
42         instance.update(cr, id, ident)
43
44 def trg_trigger(uid, res_type, res_id, cr):
45     """
46     Activate a trigger.
47
48     If a workflow instance is waiting for a trigger from another model, then this
49     trigger can be activated if its conditions are met.
50
51     :param res_type: the model name
52     :param res_id: the model instance id the workflow belongs to
53     :param cr: a database cursor
54     """
55     cr.execute('select instance_id from wkf_triggers where res_id=%s and model=%s', (res_id,res_type))
56     res = cr.fetchall()
57     for (instance_id,) in res:
58         cr.execute('select %s,res_type,res_id from wkf_instance where id=%s', (uid, instance_id,))
59         ident = cr.fetchone()
60         instance.update(cr, instance_id, ident)
61
62 def trg_delete(uid, res_type, res_id, cr):
63     """
64     Delete a workflow instance
65
66     :param res_type: the model name
67     :param res_id: the model instance id the workflow belongs to
68     :param cr: a database cursor
69     """
70     ident = (uid,res_type,res_id)
71     instance.delete(cr, ident)
72
73 def trg_create(uid, res_type, res_id, cr):
74     """
75     Create a new workflow instance
76
77     :param res_type: the model name
78     :param res_id: the model instance id to own the created worfklow instance
79     :param cr: a database cursor
80     """
81     ident = (uid,res_type,res_id)
82     wkf_on_create_cache.setdefault(cr.dbname, {})
83     if res_type in wkf_on_create_cache[cr.dbname]:
84         wkf_ids = wkf_on_create_cache[cr.dbname][res_type]
85     else:
86         cr.execute('select id from wkf where osv=%s and on_create=True', (res_type,))
87         wkf_ids = cr.fetchall()
88         wkf_on_create_cache[cr.dbname][res_type] = wkf_ids
89     for (wkf_id,) in wkf_ids:
90         instance.create(cr, ident, wkf_id)
91
92 def trg_validate(uid, res_type, res_id, signal, cr):
93     """
94     Fire a signal on a given workflow instance
95
96     :param res_type: the model name
97     :param res_id: the model instance id the workflow belongs to
98     :signal: the signal name to be fired
99     :param cr: a database cursor
100     """
101     result = False
102     ident = (uid,res_type,res_id)
103     # ids of all active workflow instances for a corresponding resource (id, model_nam)
104     cr.execute('select id from wkf_instance where res_id=%s and res_type=%s and state=%s', (res_id, res_type, 'active'))
105     for (id,) in cr.fetchall():
106         res2 = instance.validate(cr, id, ident, signal)
107         result = result or res2
108     return result
109
110 def trg_redirect(uid, res_type, res_id, new_rid, cr):
111     """
112     Re-bind a workflow instance to another instance of the same model.
113
114     Make all workitems which are waiting for a (subflow) workflow instance
115     for the old resource point to the (first active) workflow instance for
116     the new resource.
117
118     :param res_type: the model name
119     :param res_id: the model instance id the workflow belongs to
120     :param new_rid: the model instance id to own the worfklow instance
121     :param cr: a database cursor
122     """
123     # get ids of wkf instances for the old resource (res_id)
124 #CHECKME: shouldn't we get only active instances?
125     cr.execute('select id, wkf_id from wkf_instance where res_id=%s and res_type=%s', (res_id, res_type))
126     for old_inst_id, wkf_id in cr.fetchall():
127         # first active instance for new resource (new_rid), using same wkf
128         cr.execute(
129             'SELECT id '\
130             'FROM wkf_instance '\
131             'WHERE res_id=%s AND res_type=%s AND wkf_id=%s AND state=%s', 
132             (new_rid, res_type, wkf_id, 'active'))
133         new_id = cr.fetchone()
134         if new_id:
135             # select all workitems which "wait" for the old instance
136             cr.execute('select id from wkf_workitem where subflow_id=%s', (old_inst_id,))
137             for (item_id,) in cr.fetchall():
138                 # redirect all those workitems to the wkf instance of the new resource
139                 cr.execute('update wkf_workitem set subflow_id=%s where id=%s', (new_id[0], item_id))
140
141 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: