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