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