~openerp-groupes/openobject-server/6.0-fix-setup-windows

« back to all changes in this revision

Viewing changes to bin/workflow/workitem.py

  • Committer: pinky
  • Date: 2006-12-07 13:41:40 UTC
  • Revision ID: pinky-3f10ee12cea3c4c75cef44ab04ad33ef47432907
New trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 id in ids:
 
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']))
 
75
 
 
76
        return True
 
77
 
 
78
 
 
79
# ---------------------- PRIVATE FUNCS --------------------------------
 
80
 
 
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)
 
85
 
 
86
def _execute(cr, workitem, activity, ident):
 
87
        #
 
88
        # send a signal to parent workflow (signal: subflow.signal_name)
 
89
        #
 
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)
 
94
 
 
95
 
 
96
 
 
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)
 
117
                                if not (id_new):
 
118
                                        cr.execute('delete from wkf_workitem where id=%s', (workitem['id'],))
 
119
                                        return False
 
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]
 
123
                        else:
 
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)
 
132
        return True
 
133
 
 
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'],))
 
136
        test = False
 
137
        transitions = []
 
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):
 
142
                                test = True
 
143
                                transitions.append((transition['id'], workitem['inst_id']))
 
144
                                if split_mode=='XOR':
 
145
                                        break
 
146
        else:
 
147
                test = True
 
148
                for transition in alltrans:
 
149
                        if not wkf_expr.check(cr, workitem, ident, transition,signal):
 
150
                                test = False
 
151
                                break
 
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']))
 
155
        if not test:
 
156
                pass
 
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)
 
162
                return True
 
163
        return False
 
164
 
 
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))
 
171
        else:
 
172
                cr.execute('select id from wkf_transition where act_to=%d', (activity['id'],))
 
173
                trans_ids = cr.fetchall()
 
174
                ok = True
 
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]
 
178
                        if not res:
 
179
                                ok = False
 
180
                                break
 
181
                if ok:
 
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)