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('insert into wkf_triggers (model,res_id,instance_id,workitem_id) values (%s,%d,%d,%d)', (trans['trigger_model'],id,workitem['inst_id'], workitem['id']))
79
# ---------------------- PRIVATE FUNCS --------------------------------
81
def _state_set(cr, workitem, activity, state, ident):
82
cr.execute('update wkf_workitem set state=%s where id=%d', (state,workitem['id']))
83
workitem['state'] = state
84
wkf_logs.log(cr,ident,activity['id'],state)
86
def _execute(cr, workitem, activity, ident):
88
# send a signal to parent workflow (signal: subflow.signal_name)
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=%d)", (workitem['inst_id'],))
92
for i in cr.fetchall():
93
instance.validate(cr, i[0], (ident[0],i[1],i[2]), activity['signal_send'], force_running=True)
97
if activity['kind']=='dummy':
98
if workitem['state']=='active':
99
_state_set(cr, workitem, activity, 'complete', ident)
100
elif activity['kind']=='function':
101
if workitem['state']=='active':
102
_state_set(cr, workitem, activity, 'running', ident)
103
wkf_expr.execute(cr, ident, workitem, activity)
104
_state_set(cr, workitem, activity, 'complete', ident)
105
elif activity['kind']=='stopall':
106
if workitem['state']=='active':
107
_state_set(cr, workitem, activity, 'running', ident)
108
cr.execute('delete from wkf_workitem where inst_id=%d and id<>%d', (workitem['inst_id'], workitem['id']))
109
if activity['action']:
110
wkf_expr.execute(cr, ident, workitem, activity)
111
_state_set(cr, workitem, activity, 'complete', ident)
112
elif activity['kind']=='subflow':
113
if workitem['state']=='active':
114
_state_set(cr, workitem, activity, 'running', ident)
115
if activity.get('action', False):
116
id_new = wkf_expr.execute(cr, ident, workitem, activity)
118
cr.execute('delete from wkf_workitem where id=%s', (workitem['id'],))
120
assert type(id_new)==type(1) or type(id_new)==type(1L), 'Wrong return value: '+str(id_new)+' '+str(type(id_new))
121
cr.execute('select id from wkf_instance where res_id=%d and wkf_id=%d', (id_new,activity['subflow_id']))
122
id_new = cr.fetchone()[0]
124
id_new = instance.create(cr, ident, activity['subflow_id'])
125
cr.execute('update wkf_workitem set subflow_id=%d where id=%s', (id_new, workitem['id']))
126
workitem['subflow_id'] = id_new
127
if workitem['state']=='running':
128
cr.execute("select state from wkf_instance where id=%d", (workitem['subflow_id'],))
129
state= cr.fetchone()[0]
130
if state=='complete':
131
_state_set(cr, workitem, activity, 'complete', ident)
134
def _split_test(cr, workitem, split_mode, ident, signal=None):
135
cr.execute('select * from wkf_transition where act_from=%d', (workitem['act_id'],))
138
alltrans = cr.dictfetchall()
139
if split_mode=='XOR' or split_mode=='OR':
140
for transition in alltrans:
141
if wkf_expr.check(cr, workitem, ident, transition,signal):
143
transitions.append((transition['id'], workitem['inst_id']))
144
if split_mode=='XOR':
148
for transition in alltrans:
149
if not wkf_expr.check(cr, workitem, ident, transition,signal):
152
cr.execute('select count(*) from wkf_witm_trans where trans_id=%d and inst_id=%d', (transition['id'], workitem['inst_id']))
153
if not cr.fetchone()[0]:
154
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)