Workflow: optimize out one SQL command.
[odoo/odoo.git] / bin / workflow / wkf_service.py
1 # -*- encoding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution   
5 #    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
6 #    $Id$
7 #
8 #    This program is free software: you can redistribute it and/or modify
9 #    it under the terms of the GNU General Public License as published by
10 #    the Free Software Foundation, either version 3 of the License, or
11 #    (at your option) any later version.
12 #
13 #    This program is distributed in the hope that it will be useful,
14 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
15 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 #    GNU General Public License for more details.
17 #
18 #    You should have received a copy of the GNU General Public License
19 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 #
21 ##############################################################################
22
23 import wkf_logs
24 import workitem
25 import instance
26
27 import netsvc
28 import pooler
29
30 class workflow_service(netsvc.Service):
31     def __init__(self, name='workflow', audience='*'):
32         netsvc.Service.__init__(self, name, audience)
33         self.exportMethod(self.trg_write)
34         self.exportMethod(self.trg_delete)
35         self.exportMethod(self.trg_create)
36         self.exportMethod(self.trg_validate)
37         self.exportMethod(self.trg_redirect)
38         self.exportMethod(self.trg_trigger)
39         self.exportMethod(self.clear_cache)
40         self.wkf_on_create_cache={}
41
42     def clear_cache(self, cr, uid):
43         self.wkf_on_create_cache[cr.dbname]={}
44
45     def trg_write(self, uid, res_type, res_id, cr):
46         ident = (uid,res_type,res_id)
47         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'))
48         for (id,) in cr.fetchall():
49             instance.update(cr, id, ident)
50
51     def trg_trigger(self, uid, res_type, res_id, cr):
52         cr.execute('select instance_id from wkf_triggers where res_id=%s and model=%s', (res_id,res_type))
53         res = cr.fetchall()
54         for (instance_id,) in res:
55             cr.execute('select uid,res_type,res_id from wkf_instance where id=%s', (instance_id,))
56             ident = cr.fetchone()
57             instance.update(cr, instance_id, ident)
58
59     def trg_delete(self, uid, res_type, res_id, cr):
60         ident = (uid,res_type,res_id)
61         instance.delete(cr, ident)
62
63     def trg_create(self, uid, res_type, res_id, cr):
64         ident = (uid,res_type,res_id)
65         self.wkf_on_create_cache.setdefault(cr.dbname, {})
66         if res_type in self.wkf_on_create_cache[cr.dbname]:
67             wkf_ids = self.wkf_on_create_cache[cr.dbname][res_type]
68         else:
69             cr.execute('select id from wkf where osv=%s and on_create=True', (res_type,))
70             wkf_ids = cr.fetchall()
71             self.wkf_on_create_cache[cr.dbname][res_type] = wkf_ids
72         for (wkf_id,) in wkf_ids:
73             instance.create(cr, ident, wkf_id)
74
75     def trg_validate(self, uid, res_type, res_id, signal, cr):
76         result = False
77         ident = (uid,res_type,res_id)
78         # ids of all active workflow instances for a corresponding resource (id, model_nam)
79         cr.execute('select id from wkf_instance where res_id=%s and res_type=%s and state=%s', (res_id, res_type, 'active'))
80         for (id,) in cr.fetchall():
81             res2 = instance.validate(cr, id, ident, signal)
82             result = result or res2
83         return result
84
85     # make all workitems which are waiting for a (subflow) workflow instance
86     # for the old resource point to the (first active) workflow instance for
87     # the new resource
88     def trg_redirect(self, uid, res_type, res_id, new_rid, cr):
89         # get ids of wkf instances for the old resource (res_id)
90 #CHECKME: shouldn't we get only active instances?
91         cr.execute('select id, wkf_id from wkf_instance where res_id=%s and res_type=%s', (res_id, res_type))
92         for old_inst_id, wkf_id in cr.fetchall():
93             # first active instance for new resource (new_rid), using same wkf
94             cr.execute(
95                 'SELECT id '\
96                 'FROM wkf_instance '\
97                 'WHERE res_id=%s AND res_type=%s AND wkf_id=%s AND state=%s', 
98                 (new_rid, res_type, wkf_id, 'active'))
99             new_id = cr.fetchone()
100             if new_id:
101                 # select all workitems which "wait" for the old instance
102                 cr.execute('select id from wkf_workitem where subflow_id=%s', (old_inst_id,))
103                 for (item_id,) in cr.fetchall():
104                     # redirect all those workitems to the wkf instance of the new resource
105                     cr.execute('update wkf_workitem set subflow_id=%s where id=%s', (new_id[0], item_id))
106 workflow_service()
107
108 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
109