[MERGE] demo data: remove salesman on customers
[odoo/odoo.git] / openerp / workflow / wkf_service.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #    
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
6 #
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.
11 #
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.
16 #
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/>.     
19 #
20 ##############################################################################
21
22 import wkf_logs
23 import workitem
24 import instance
25
26 import openerp.netsvc as netsvc
27 import openerp.pooler as pooler
28
29 class workflow_service(netsvc.Service):
30     """
31     Sometimes you might want to fire a signal or re-evaluate the current state
32     of a workflow using the service's API. You can access the workflow services
33     using:
34
35     >>> import netsvc
36     >>> wf_service = netsvc.LocalService("workflow")
37
38     """
39
40     def __init__(self, name='workflow'):
41         netsvc.Service.__init__(self, name)
42         self.wkf_on_create_cache={}
43
44     def clear_cache(self, cr, uid):
45         self.wkf_on_create_cache[cr.dbname]={}
46
47     def trg_write(self, uid, res_type, res_id, cr):
48         """
49         Reevaluates the specified workflow instance. Thus if any condition for
50         a transition have been changed in the backend, then running ``trg_write``
51         will move the workflow over that transition.
52
53         :param res_type: the model name
54         :param res_id: the model instance id the workflow belongs to
55         :param cr: a database cursor
56         """
57         ident = (uid,res_type,res_id)
58         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'))
59         for (id,) in cr.fetchall():
60             instance.update(cr, id, ident)
61
62     def trg_trigger(self, uid, res_type, res_id, cr):
63         """
64         Activate a trigger.
65
66         If a workflow instance is waiting for a trigger from another model, then this
67         trigger can be activated if its conditions are met.
68
69         :param res_type: the model name
70         :param res_id: the model instance id the workflow belongs to
71         :param cr: a database cursor
72         """
73         cr.execute('select instance_id from wkf_triggers where res_id=%s and model=%s', (res_id,res_type))
74         res = cr.fetchall()
75         for (instance_id,) in res:
76             cr.execute('select %s,res_type,res_id from wkf_instance where id=%s', (uid, instance_id,))
77             ident = cr.fetchone()
78             instance.update(cr, instance_id, ident)
79
80     def trg_delete(self, uid, res_type, res_id, cr):
81         """
82         Delete a workflow instance
83
84         :param res_type: the model name
85         :param res_id: the model instance id the workflow belongs to
86         :param cr: a database cursor
87         """
88         ident = (uid,res_type,res_id)
89         instance.delete(cr, ident)
90
91     def trg_create(self, uid, res_type, res_id, cr):
92         """
93         Create a new workflow instance
94
95         :param res_type: the model name
96         :param res_id: the model instance id to own the created worfklow instance
97         :param cr: a database cursor
98         """
99         ident = (uid,res_type,res_id)
100         self.wkf_on_create_cache.setdefault(cr.dbname, {})
101         if res_type in self.wkf_on_create_cache[cr.dbname]:
102             wkf_ids = self.wkf_on_create_cache[cr.dbname][res_type]
103         else:
104             cr.execute('select id from wkf where osv=%s and on_create=True', (res_type,))
105             wkf_ids = cr.fetchall()
106             self.wkf_on_create_cache[cr.dbname][res_type] = wkf_ids
107         for (wkf_id,) in wkf_ids:
108             instance.create(cr, ident, wkf_id)
109
110     def trg_validate(self, uid, res_type, res_id, signal, cr):
111         """
112         Fire a signal on a given workflow instance
113
114         :param res_type: the model name
115         :param res_id: the model instance id the workflow belongs to
116         :signal: the signal name to be fired
117         :param cr: a database cursor
118         """
119         result = False
120         ident = (uid,res_type,res_id)
121         # ids of all active workflow instances for a corresponding resource (id, model_nam)
122         cr.execute('select id from wkf_instance where res_id=%s and res_type=%s and state=%s', (res_id, res_type, 'active'))
123         for (id,) in cr.fetchall():
124             res2 = instance.validate(cr, id, ident, signal)
125             result = result or res2
126         return result
127
128     def trg_redirect(self, uid, res_type, res_id, new_rid, cr):
129         """
130         Re-bind a workflow instance to another instance of the same model.
131
132         Make all workitems which are waiting for a (subflow) workflow instance
133         for the old resource point to the (first active) workflow instance for
134         the new resource.
135
136         :param res_type: the model name
137         :param res_id: the model instance id the workflow belongs to
138         :param new_rid: the model instance id to own the worfklow instance
139         :param cr: a database cursor
140         """
141         # get ids of wkf instances for the old resource (res_id)
142 #CHECKME: shouldn't we get only active instances?
143         cr.execute('select id, wkf_id from wkf_instance where res_id=%s and res_type=%s', (res_id, res_type))
144         for old_inst_id, wkf_id in cr.fetchall():
145             # first active instance for new resource (new_rid), using same wkf
146             cr.execute(
147                 'SELECT id '\
148                 'FROM wkf_instance '\
149                 'WHERE res_id=%s AND res_type=%s AND wkf_id=%s AND state=%s', 
150                 (new_rid, res_type, wkf_id, 'active'))
151             new_id = cr.fetchone()
152             if new_id:
153                 # select all workitems which "wait" for the old instance
154                 cr.execute('select id from wkf_workitem where subflow_id=%s', (old_inst_id,))
155                 for (item_id,) in cr.fetchall():
156                     # redirect all those workitems to the wkf instance of the new resource
157                     cr.execute('update wkf_workitem set subflow_id=%s where id=%s', (new_id[0], item_id))
158 workflow_service()
159
160 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
161