1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2014 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 ##############################################################################
22 from openerp.workflow.helpers import Session
23 from openerp.workflow.helpers import Record
24 from openerp.workflow.workitem import WorkflowItem
26 class WorkflowInstance(object):
27 def __init__(self, session, record, values):
28 assert isinstance(session, Session)
29 assert isinstance(record, Record)
30 self.session = session
36 assert isinstance(values, dict)
37 self.instance = values
40 def create(cls, session, record, workflow_id):
41 assert isinstance(session, Session)
42 assert isinstance(record, Record)
43 assert isinstance(workflow_id, (int, long))
46 cr.execute('insert into wkf_instance (res_type,res_id,uid,wkf_id,state) values (%s,%s,%s,%s,%s) RETURNING id', (record.model, record.id, session.uid, workflow_id, 'active'))
47 instance_id = cr.fetchone()[0]
49 cr.execute('select * from wkf_activity where flow_start=True and wkf_id=%s', (workflow_id,))
52 activities = cr.dictfetchall()
53 for activity in activities:
54 WorkflowItem.create(session, record, activity, instance_id, stack)
56 cr.execute('SELECT * FROM wkf_instance WHERE id = %s', (instance_id,))
57 values = cr.dictfetchone()
58 wi = WorkflowInstance(session, record, values)
64 self.session.cr.execute('delete from wkf_instance where res_id=%s and res_type=%s', (self.record.id, self.record.model))
66 def validate(self, signal, force_running=False):
67 assert isinstance(signal, basestring)
68 assert isinstance(force_running, bool)
71 cr.execute("select * from wkf_workitem where inst_id=%s", (self.instance['id'],))
73 for work_item_values in cr.dictfetchall():
74 wi = WorkflowItem(self.session, self.record, work_item_values)
75 wi.process(signal=signal, force_running=force_running, stack=stack)
76 # An action is returned
78 return stack and stack[0] or False
83 cr.execute("select * from wkf_workitem where inst_id=%s", (self.instance['id'],))
84 for work_item_values in cr.dictfetchall():
86 WorkflowItem(self.session, self.record, work_item_values).process(stack=stack)
87 return self._update_end()
89 def _update_end(self):
91 instance_id = self.instance['id']
92 cr.execute('select wkf_id from wkf_instance where id=%s', (instance_id,))
93 wkf_id = cr.fetchone()[0]
94 cr.execute('select state,flow_stop from wkf_workitem w left join wkf_activity a on (a.id=w.act_id) where w.inst_id=%s', (instance_id,))
96 for r in cr.fetchall():
97 if (r[0]<>'complete') or not r[1]:
101 cr.execute('select distinct a.name from wkf_activity a left join wkf_workitem w on (a.id=w.act_id) where w.inst_id=%s', (instance_id,))
102 act_names = cr.fetchall()
103 cr.execute("update wkf_instance set state='complete' where id=%s", (instance_id,))
104 cr.execute("update wkf_workitem set state='complete' where subflow_id=%s", (instance_id,))
105 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=%s)", (instance_id,))
106 for cur_instance_id, cur_model_name, cur_record_id in cr.fetchall():
107 cur_record = Record(cur_model_name, cur_record_id)
108 for act_name in act_names:
109 WorkflowInstance(self.session, cur_record, {'id':cur_instance_id}).validate('subflow.%s' % act_name[0])
117 def create(session, record, workflow_id):
118 return WorkflowInstance(session, record).create(workflow_id)
120 def delete(session, record):
121 return WorkflowInstance(session, record).delete()
123 def validate(session, record, instance_id, signal, force_running=False):
124 return WorkflowInstance(session, record).validate(instance_id, signal, force_running)
126 def update(session, record, instance_id):
127 return WorkflowInstance(session, record).update(instance_id)
129 def _update_end(session, record, instance_id):
130 return WorkflowInstance(session, record)._update_end(instance_id)
133 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: