1 ##############################################################################
3 # Copyright (c) 2004-2008 Tiny SPRL (http://tiny.be) All Rights Reserved.
7 # WARNING: This program as such is intended to be used by professional
8 # programmers who take the whole responsability of assessing all potential
9 # consequences resulting from its eventual inadequacies and bugs
10 # End users who are looking for a ready-to-use solution with commercial
11 # garantees and support are strongly adviced to contract a Free Software
14 # This program is Free Software; you can redistribute it and/or
15 # modify it under the terms of the GNU General Public License
16 # as published by the Free Software Foundation; either version 2
17 # of the License, or (at your option) any later version.
19 # This program is distributed in the hope that it will be useful,
20 # but WITHOUT ANY WARRANTY; without even the implied warranty of
21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 # GNU General Public License for more details.
24 # You should have received a copy of the GNU General Public License
25 # along with this program; if not, write to the Free Software
26 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 ###############################################################################
36 class workflow_service(netsvc.Service):
37 def __init__(self, name='workflow', audience='*'):
38 netsvc.Service.__init__(self, name, audience)
39 self.exportMethod(self.trg_write)
40 self.exportMethod(self.trg_delete)
41 self.exportMethod(self.trg_create)
42 self.exportMethod(self.trg_validate)
43 self.exportMethod(self.trg_redirect)
44 self.exportMethod(self.trg_trigger)
45 self.exportMethod(self.clear_cache)
46 self.wkf_on_create_cache={}
48 def clear_cache(self, cr, uid):
49 self.wkf_on_create_cache[cr.dbname]={}
51 def trg_write(self, uid, res_type, res_id, cr):
52 ident = (uid,res_type,res_id)
53 cr.execute('select id from wkf_instance where res_id=%d and res_type=%s and state=%s', (res_id,res_type, 'active'))
54 for (id,) in cr.fetchall():
55 instance.update(cr, id, ident)
57 def trg_trigger(self, uid, res_type, res_id, cr):
58 cr.execute('select instance_id from wkf_triggers where res_id=%d and model=%s', (res_id,res_type))
60 for (instance_id,) in res:
61 cr.execute('select uid,res_type,res_id from wkf_instance where id=%d', (instance_id,))
63 instance.update(cr, instance_id, ident)
65 def trg_delete(self, uid, res_type, res_id, cr):
66 ident = (uid,res_type,res_id)
67 instance.delete(cr, ident)
69 def trg_create(self, uid, res_type, res_id, cr):
70 ident = (uid,res_type,res_id)
71 self.wkf_on_create_cache.setdefault(cr.dbname, {})
72 if res_type in self.wkf_on_create_cache[cr.dbname]:
73 wkf_ids = self.wkf_on_create_cache[cr.dbname][res_type]
75 cr.execute('select id from wkf where osv=%s and on_create=True', (res_type,))
76 wkf_ids = cr.fetchall()
77 self.wkf_on_create_cache[cr.dbname][res_type] = wkf_ids
78 for (wkf_id,) in wkf_ids:
79 instance.create(cr, ident, wkf_id)
81 def trg_validate(self, uid, res_type, res_id, signal, cr):
83 ident = (uid,res_type,res_id)
84 # ids of all active workflow instances for a corresponding resource (id, model_nam)
85 cr.execute('select id from wkf_instance where res_id=%d and res_type=%s and state=%s', (res_id, res_type, 'active'))
86 for (id,) in cr.fetchall():
87 res2 = instance.validate(cr, id, ident, signal)
88 print 'Inst val,', res2
89 result = result or res2
92 # make all workitems which are waiting for a (subflow) workflow instance
93 # for the old resource point to the (first active) workflow instance for
95 def trg_redirect(self, uid, res_type, res_id, new_rid, cr):
96 # get ids of wkf instances for the old resource (res_id)
97 #CHECKME: shouldn't we get only active instances?
98 cr.execute('select id, wkf_id from wkf_instance where res_id=%d and res_type=%s', (res_id, res_type))
99 for old_inst_id, wkf_id in cr.fetchall():
100 # first active instance for new resource (new_rid), using same wkf
103 'FROM wkf_instance '\
104 'WHERE res_id=%d AND res_type=%s AND wkf_id=%d AND state=%s',
105 (new_rid, res_type, wkf_id, 'active'))
106 new_id = cr.fetchone()
108 # select all workitems which "wait" for the old instance
109 cr.execute('select id from wkf_workitem where subflow_id=%d', (old_inst_id,))
110 for (item_id,) in cr.fetchall():
111 # redirect all those workitems to the wkf instance of the new resource
112 cr.execute('update wkf_workitem set subflow_id=%d where id=%d', (new_id[0], item_id))