1 ##############################################################################
3 # Copyright (c) 2004 TINY SPRL. (http://tiny.be) All Rights Reserved.
4 # Fabien Pinckaers <fp@tiny.Be>
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
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.
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.
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.
27 ##############################################################################
31 # cr.execute('delete from wkf_triggers where model=%s and res_id=%d', (res_type,res_id))
40 def create(cr, act_datas, inst_id, ident):
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)
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()
55 if workitem['state']=='active':
57 if not _execute(cr, workitem, activity, ident):
60 if workitem['state']=='running':
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
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'])
74 cr.execute('select nextval(\'wkf_triggers_id_seq\')')
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))
81 # ---------------------- PRIVATE FUNCS --------------------------------
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)
88 def _execute(cr, workitem, activity, ident):
90 # send a signal to parent workflow (signal: subflow.signal_name)
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)
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)
120 cr.execute('delete from wkf_workitem where id=%s', (workitem['id'],))
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]
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)
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'],))
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):
145 transitions.append((transition['id'], workitem['inst_id']))
146 if split_mode=='XOR':
150 for transition in alltrans:
151 if not wkf_expr.check(cr, workitem, ident, transition,signal):
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 test and len(transitions):
158 cr.executemany('insert into wkf_witm_trans (trans_id,inst_id) values (%d,%d)', transitions)
159 cr.execute('delete from wkf_workitem where id=%d', (workitem['id'],))
160 for t in transitions:
161 _join_test(cr, t[0], t[1], ident)
165 def _join_test(cr, trans_id, inst_id, ident):
166 cr.execute('select * from wkf_activity where id=(select act_to from wkf_transition where id=%d)', (trans_id,))
167 activity = cr.dictfetchone()
168 if activity['join_mode']=='XOR':
169 create(cr,[activity], inst_id, ident)
170 cr.execute('delete from wkf_witm_trans where inst_id=%d and trans_id=%d', (inst_id,trans_id))
172 cr.execute('select id from wkf_transition where act_to=%d', (activity['id'],))
173 trans_ids = cr.fetchall()
175 for (id,) in trans_ids:
176 cr.execute('select count(*) from wkf_witm_trans where trans_id=%d and inst_id=%d', (id,inst_id))
177 res = cr.fetchone()[0]
182 for (id,) in trans_ids:
183 cr.execute('delete from wkf_witm_trans where trans_id=%d and inst_id=%d', (id,inst_id))
184 create(cr, [activity], inst_id, ident)