1 # -*- encoding: utf-8 -*-
2 ##############################################################################
4 # Copyright (c) 2004-2008 Tiny SPRL (http://tiny.be) All Rights Reserved.
8 # WARNING: This program as such is intended to be used by professional
9 # programmers who take the whole responsability of assessing all potential
10 # consequences resulting from its eventual inadequacies and bugs
11 # End users who are looking for a ready-to-use solution with commercial
12 # garantees and support are strongly adviced to contract a Free Software
15 # This program is Free Software; you can redistribute it and/or
16 # modify it under the terms of the GNU General Public License
17 # as published by the Free Software Foundation; either version 2
18 # of the License, or (at your option) any later version.
20 # This program is distributed in the hope that it will be useful,
21 # but WITHOUT ANY WARRANTY; without even the implied warranty of
22 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 # GNU General Public License for more details.
25 # You should have received a copy of the GNU General Public License
26 # along with this program; if not, write to the Free Software
27 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
28 ###############################################################################
37 class workflow_service(netsvc.Service):
38 def __init__(self, name='workflow', audience='*'):
39 netsvc.Service.__init__(self, name, audience)
40 self.exportMethod(self.trg_write)
41 self.exportMethod(self.trg_delete)
42 self.exportMethod(self.trg_create)
43 self.exportMethod(self.trg_validate)
44 self.exportMethod(self.trg_redirect)
45 self.exportMethod(self.trg_trigger)
46 self.exportMethod(self.clear_cache)
47 self.wkf_on_create_cache={}
49 def clear_cache(self, cr, uid):
50 self.wkf_on_create_cache[cr.dbname]={}
52 def trg_write(self, uid, res_type, res_id, cr):
53 ident = (uid,res_type,res_id)
54 cr.execute('select id from wkf_instance where res_id=%d and res_type=%s and state=%s', (res_id,res_type, 'active'))
55 for (id,) in cr.fetchall():
56 instance.update(cr, id, ident)
58 def trg_trigger(self, uid, res_type, res_id, cr):
59 cr.execute('select instance_id from wkf_triggers where res_id=%d and model=%s', (res_id,res_type))
61 for (instance_id,) in res:
62 cr.execute('select uid,res_type,res_id from wkf_instance where id=%d', (instance_id,))
64 instance.update(cr, instance_id, ident)
66 def trg_delete(self, uid, res_type, res_id, cr):
67 ident = (uid,res_type,res_id)
68 instance.delete(cr, ident)
70 def trg_create(self, uid, res_type, res_id, cr):
71 ident = (uid,res_type,res_id)
72 self.wkf_on_create_cache.setdefault(cr.dbname, {})
73 if res_type in self.wkf_on_create_cache[cr.dbname]:
74 wkf_ids = self.wkf_on_create_cache[cr.dbname][res_type]
76 cr.execute('select id from wkf where osv=%s and on_create=True', (res_type,))
77 wkf_ids = cr.fetchall()
78 self.wkf_on_create_cache[cr.dbname][res_type] = wkf_ids
79 for (wkf_id,) in wkf_ids:
80 instance.create(cr, ident, wkf_id)
82 def trg_validate(self, uid, res_type, res_id, signal, cr):
84 ident = (uid,res_type,res_id)
85 # ids of all active workflow instances for a corresponding resource (id, model_nam)
86 cr.execute('select id from wkf_instance where res_id=%d and res_type=%s and state=%s', (res_id, res_type, 'active'))
87 for (id,) in cr.fetchall():
88 res2 = instance.validate(cr, id, ident, signal)
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))
115 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: