New trunk
[odoo/odoo.git] / bin / workflow / wkf_service.py
1 ##############################################################################
2 #
3 # Copyright (c) 2004 TINY SPRL. (http://tiny.be) All Rights Reserved.
4 #                    Fabien Pinckaers <fp@tiny.Be>
5 #
6 # WARNING: This program as such is intended to be used by professional
7 # programmers who take the whole responsability of assessing all potential
8 # consequences resulting from its eventual inadequacies and bugs
9 # End users who are looking for a ready-to-use solution with commercial
10 # garantees and support are strongly adviced to contract a Free Software
11 # Service Company
12 #
13 # This program is Free Software; you can redistribute it and/or
14 # modify it under the terms of the GNU General Public License
15 # as published by the Free Software Foundation; either version 2
16 # of the License, or (at your option) any later version.
17 #
18 # This program is distributed in the hope that it will be useful,
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 # GNU General Public License for more details.
22 #
23 # You should have received a copy of the GNU General Public License
24 # along with this program; if not, write to the Free Software
25 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
26 #
27 ##############################################################################
28
29 import wkf_logs
30 import workitem
31 import instance
32
33 import netsvc
34 import pooler
35
36 class workflow_service(netsvc.Service):
37         def __init__(self, name='workflow', audience='*'):
38                 netsvc.Service.__init__(self, name, audience)
39                 self.exportMethod(self.trg_write)
40                 self.exportMethod(self.trg_delete)
41                 self.exportMethod(self.trg_create)
42                 self.exportMethod(self.trg_validate)
43                 self.exportMethod(self.trg_redirect)
44                 self.exportMethod(self.trg_trigger)
45
46         def trg_write(self, uid, res_type, res_id, cr):
47                 ident = (uid,res_type,res_id)
48                 cr.execute('select id from wkf_instance where res_id=%d and res_type=%s and state=%s', (res_id,res_type, 'active'))
49                 for (id,) in cr.fetchall():
50                         instance.update(cr, id, ident)
51
52         def trg_trigger(self, uid, res_type, res_id, cr):
53                 cr.execute('select instance_id from wkf_triggers where res_id=%d and model=%s', (res_id,res_type))
54                 res = cr.fetchall()
55                 for (instance_id,) in res:
56                         cr.execute('select uid,res_type,res_id from wkf_instance where id=%d', (instance_id,))
57                         ident = cr.fetchone()
58                         instance.update(cr, instance_id, ident)
59
60         def trg_delete(self, uid, res_type, res_id, cr):
61                 ident = (uid,res_type,res_id)
62                 instance.delete(cr, ident)
63
64         def trg_create(self, uid, res_type, res_id, cr):
65                 ident = (uid,res_type,res_id)
66                 cr.execute('select id from wkf where osv=%s and on_create=True', (res_type,))
67                 for (wkf_id,) in cr.fetchall():
68                         instance.create(cr, ident, wkf_id)
69
70         def trg_validate(self, uid, res_type, res_id, signal, cr):
71                 ident = (uid,res_type,res_id)
72                 # ids of all active workflow instances for a corresponding resource (id, model_nam)
73                 cr.execute('select id from wkf_instance where res_id=%d and res_type=%s and state=%s', (res_id, res_type, 'active'))
74                 for (id,) in cr.fetchall():
75                         instance.validate(cr, id, ident, signal)
76
77         # make all workitems which are waiting for a (subflow) workflow instance
78         # for the old resource point to the (first active) workflow instance for
79         # the new resource
80         def trg_redirect(self, uid, res_type, res_id, new_rid, cr):
81                 # get ids of wkf instances for the old resource (res_id)
82 #CHECKME: shouldn't we get only active instances?
83                 cr.execute('select id, wkf_id from wkf_instance where res_id=%d and res_type=%s', (res_id, res_type))
84                 for old_inst_id, wkf_id in cr.fetchall():
85                         # first active instance for new resource (new_rid), using same wkf
86                         cr.execute(
87                                 'SELECT id '\
88                                 'FROM wkf_instance '\
89                                 'WHERE res_id=%d AND res_type=%s AND wkf_id=%d AND state=%s', 
90                                 (new_rid, res_type, wkf_id, 'active'))
91                         new_id = cr.fetchone()
92                         if new_id:
93                                 # select all workitems which "wait" for the old instance
94                                 cr.execute('select id from wkf_workitem where subflow_id=%d', (old_inst_id,))
95                                 for (item_id,) in cr.fetchall():
96                                         # redirect all those workitems to the wkf instance of the new resource
97                                         cr.execute('update wkf_workitem set subflow_id=%d where id=%d', (new_id[0], item_id))
98 workflow_service()