1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as
9 # published by the Free Software Foundation, either version 3 of the
10 # License, or (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 ##############################################################################
29 class workflow_service(netsvc.Service):
30 def __init__(self, name='workflow', audience='*'):
31 netsvc.Service.__init__(self, name, audience)
32 self.exportMethod(self.trg_write)
33 self.exportMethod(self.trg_delete)
34 self.exportMethod(self.trg_create)
35 self.exportMethod(self.trg_validate)
36 self.exportMethod(self.trg_redirect)
37 self.exportMethod(self.trg_trigger)
38 self.exportMethod(self.clear_cache)
39 self.wkf_on_create_cache={}
41 def clear_cache(self, cr, uid):
42 self.wkf_on_create_cache[cr.dbname]={}
44 def trg_write(self, uid, res_type, res_id, cr):
45 ident = (uid,res_type,res_id)
46 cr.execute('select id from wkf_instance where res_id=%s and res_type=%s and state=%s', (res_id or None,res_type or None, 'active'))
47 for (id,) in cr.fetchall():
48 instance.update(cr, id, ident)
50 def trg_trigger(self, uid, res_type, res_id, cr):
51 cr.execute('select instance_id from wkf_triggers where res_id=%s and model=%s', (res_id,res_type))
53 for (instance_id,) in res:
54 cr.execute('select %s,res_type,res_id from wkf_instance where id=%s', (uid, instance_id,))
56 instance.update(cr, instance_id, ident)
58 def trg_delete(self, uid, res_type, res_id, cr):
59 ident = (uid,res_type,res_id)
60 instance.delete(cr, ident)
62 def trg_create(self, uid, res_type, res_id, cr):
63 ident = (uid,res_type,res_id)
64 self.wkf_on_create_cache.setdefault(cr.dbname, {})
65 if res_type in self.wkf_on_create_cache[cr.dbname]:
66 wkf_ids = self.wkf_on_create_cache[cr.dbname][res_type]
68 cr.execute('select id from wkf where osv=%s and on_create=True', (res_type,))
69 wkf_ids = cr.fetchall()
70 self.wkf_on_create_cache[cr.dbname][res_type] = wkf_ids
71 for (wkf_id,) in wkf_ids:
72 instance.create(cr, ident, wkf_id)
74 def trg_validate(self, uid, res_type, res_id, signal, cr):
76 ident = (uid,res_type,res_id)
77 # ids of all active workflow instances for a corresponding resource (id, model_nam)
78 cr.execute('select id from wkf_instance where res_id=%s and res_type=%s and state=%s', (res_id, res_type, 'active'))
79 for (id,) in cr.fetchall():
80 res2 = instance.validate(cr, id, ident, signal)
81 result = result or res2
84 # make all workitems which are waiting for a (subflow) workflow instance
85 # for the old resource point to the (first active) workflow instance for
87 def trg_redirect(self, uid, res_type, res_id, new_rid, cr):
88 # get ids of wkf instances for the old resource (res_id)
89 #CHECKME: shouldn't we get only active instances?
90 cr.execute('select id, wkf_id from wkf_instance where res_id=%s and res_type=%s', (res_id, res_type))
91 for old_inst_id, wkf_id in cr.fetchall():
92 # first active instance for new resource (new_rid), using same wkf
96 'WHERE res_id=%s AND res_type=%s AND wkf_id=%s AND state=%s',
97 (new_rid, res_type, wkf_id, 'active'))
98 new_id = cr.fetchone()
100 # select all workitems which "wait" for the old instance
101 cr.execute('select id from wkf_workitem where subflow_id=%s', (old_inst_id,))
102 for (item_id,) in cr.fetchall():
103 # redirect all those workitems to the wkf instance of the new resource
104 cr.execute('update wkf_workitem set subflow_id=%s where id=%s', (new_id[0], item_id))
107 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: