kernel: fix workflow bug introduce by improvement
[odoo/odoo.git] / bin / workflow / workitem.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 #
30 # TODO:
31 # cr.execute('delete from wkf_triggers where model=%s and res_id=%d', (res_type,res_id))
32 #
33
34 import netsvc
35 import instance
36
37 import wkf_expr
38 import wkf_logs
39
40 def create(cr, act_datas, inst_id, ident):
41         for act in act_datas:
42                 cr.execute("select nextval('wkf_workitem_id_seq')")
43                 id_new = cr.fetchone()[0]
44                 cr.execute("insert into wkf_workitem (id,act_id,inst_id,state) values (%d,%s,%s,'active')", (id_new, act['id'], inst_id))
45                 cr.execute('select * from wkf_workitem where id=%d',(id_new,))
46                 res = cr.dictfetchone()
47                 wkf_logs.log(cr,ident,act['id'],'active')
48                 process(cr, res, ident)
49
50 def process(cr, workitem, ident, signal=None, force_running=False):
51         cr.execute('select * from wkf_activity where id=%d', (workitem['act_id'],))
52         activity = cr.dictfetchone()
53
54         triggers = False
55         if workitem['state']=='active':
56                 triggers = True
57                 if not _execute(cr, workitem, activity, ident):
58                         return False
59
60         if workitem['state']=='running':
61                 pass
62
63         if workitem['state']=='complete' or force_running:
64                 ok = _split_test(cr, workitem, activity['split_mode'], ident, signal)
65                 triggers = triggers and not ok
66
67         if triggers:
68                 cr.execute('select * from wkf_transition where act_from=%d', (workitem['act_id'],))
69                 alltrans = cr.dictfetchall()
70                 for trans in alltrans:
71                         if trans['trigger_model']:
72                                 ids = wkf_expr._eval_expr(cr,ident,workitem,trans['trigger_expr_id'])
73                                 for res_id in ids:
74                                         cr.execute('select nextval(\'wkf_triggers_id_seq\')')
75                                         id =cr.fetchone()[0]
76                                         cr.execute('insert into wkf_triggers (model,res_id,instance_id,workitem_id,id) values (%s,%d,%d,%d,%d)', (trans['trigger_model'],res_id,workitem['inst_id'], workitem['id'], id))
77
78         return True
79
80
81 # ---------------------- PRIVATE FUNCS --------------------------------
82
83 def _state_set(cr, workitem, activity, state, ident):
84         cr.execute('update wkf_workitem set state=%s where id=%d', (state,workitem['id']))
85         workitem['state'] = state
86         wkf_logs.log(cr,ident,activity['id'],state)
87
88 def _execute(cr, workitem, activity, ident):
89         #
90         # send a signal to parent workflow (signal: subflow.signal_name)
91         #
92         if (workitem['state']=='active') and activity['signal_send']:
93                 cr.execute("select i.id,w.osv,i.res_id from wkf_instance i left join wkf w on (i.wkf_id=w.id) where i.id in (select inst_id from wkf_workitem where subflow_id=%d)", (workitem['inst_id'],))
94                 for i in cr.fetchall():
95                         instance.validate(cr, i[0], (ident[0],i[1],i[2]), activity['signal_send'], force_running=True)
96
97
98
99         if activity['kind']=='dummy':
100                 if workitem['state']=='active':
101                         _state_set(cr, workitem, activity, 'complete', ident)
102         elif activity['kind']=='function':
103                 if workitem['state']=='active':
104                         _state_set(cr, workitem, activity, 'running', ident)
105                         wkf_expr.execute(cr, ident, workitem, activity)
106                         _state_set(cr, workitem, activity, 'complete', ident)
107         elif activity['kind']=='stopall':
108                 if workitem['state']=='active':
109                         _state_set(cr, workitem, activity, 'running', ident)
110                         cr.execute('delete from wkf_workitem where inst_id=%d and id<>%d', (workitem['inst_id'], workitem['id']))
111                         if activity['action']:
112                                 wkf_expr.execute(cr, ident, workitem, activity)
113                         _state_set(cr, workitem, activity, 'complete', ident)
114         elif activity['kind']=='subflow':
115                 if workitem['state']=='active':
116                         _state_set(cr, workitem, activity, 'running', ident)
117                         if activity.get('action', False):
118                                 id_new = wkf_expr.execute(cr, ident, workitem, activity)
119                                 if not (id_new):
120                                         cr.execute('delete from wkf_workitem where id=%s', (workitem['id'],))
121                                         return False
122                                 assert type(id_new)==type(1) or type(id_new)==type(1L), 'Wrong return value: '+str(id_new)+' '+str(type(id_new))
123                                 cr.execute('select id from wkf_instance where res_id=%d and wkf_id=%d', (id_new,activity['subflow_id']))
124                                 id_new = cr.fetchone()[0]
125                         else:
126                                 id_new = instance.create(cr, ident, activity['subflow_id'])
127                         cr.execute('update wkf_workitem set subflow_id=%d where id=%s', (id_new, workitem['id']))
128                         workitem['subflow_id'] = id_new
129                 if workitem['state']=='running':
130                         cr.execute("select state from wkf_instance where id=%d", (workitem['subflow_id'],))
131                         state= cr.fetchone()[0]
132                         if state=='complete':
133                                 _state_set(cr, workitem, activity, 'complete', ident)
134         return True
135
136 def _split_test(cr, workitem, split_mode, ident, signal=None):
137         cr.execute('select * from wkf_transition where act_from=%d', (workitem['act_id'],))
138         test = False
139         transitions = []
140         alltrans = cr.dictfetchall()
141         if split_mode=='XOR' or split_mode=='OR':
142                 for transition in alltrans:
143                         if wkf_expr.check(cr, workitem, ident, transition,signal):
144                                 test = True
145                                 transitions.append((transition['id'], workitem['inst_id']))
146                                 if split_mode=='XOR':
147                                         break
148         else:
149                 test = True
150                 for transition in alltrans:
151                         if not wkf_expr.check(cr, workitem, ident, transition,signal):
152                                 test = False
153                                 break
154                         cr.execute('select count(*) from wkf_witm_trans where trans_id=%d and inst_id=%d', (transition['id'], workitem['inst_id']))
155                         if not cr.fetchone()[0]:
156                                 transitions.append((transition['id'], workitem['inst_id']))
157         if not test:
158                 pass
159         if test and len(transitions):
160                 cr.executemany('insert into wkf_witm_trans (trans_id,inst_id) values (%d,%d)', transitions)
161                 cr.execute('delete from wkf_workitem where id=%d', (workitem['id'],))
162                 for t in transitions:
163                         _join_test(cr, t[0], t[1], ident)
164                 return True
165         return False
166
167 def _join_test(cr, trans_id, inst_id, ident):
168         cr.execute('select * from wkf_activity where id=(select act_to from wkf_transition where id=%d)', (trans_id,))
169         activity = cr.dictfetchone()
170         if activity['join_mode']=='XOR':
171                 create(cr,[activity], inst_id, ident)
172                 cr.execute('delete from wkf_witm_trans where inst_id=%d and trans_id=%d', (inst_id,trans_id))
173         else:
174                 cr.execute('select id from wkf_transition where act_to=%d', (activity['id'],))
175                 trans_ids = cr.fetchall()
176                 ok = True
177                 for (id,) in trans_ids:
178                         cr.execute('select count(*) from wkf_witm_trans where trans_id=%d and inst_id=%d', (id,inst_id))
179                         res = cr.fetchone()[0]
180                         if not res:
181                                 ok = False
182                                 break
183                 if ok:
184                         for (id,) in trans_ids:
185                                 cr.execute('delete from wkf_witm_trans where trans_id=%d and inst_id=%d', (id,inst_id))
186                         create(cr, [activity], inst_id, ident)