|
2 | 2 | ##############################################################################
|
3 | 3 | #
|
4 | 4 | # OpenERP, Open Source Management Solution
|
5 |
| -# Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). |
| 5 | +# Copyright (C) 2004-2014 Tiny SPRL (<http://tiny.be>). |
6 | 6 | #
|
7 | 7 | # This program is free software: you can redistribute it and/or modify
|
8 | 8 | # it under the terms of the GNU Affero General Public License as
|
|
19 | 19 | #
|
20 | 20 | ##############################################################################
|
21 | 21 | import workitem
|
| 22 | +from openerp.workflow.helpers import Session |
| 23 | +from openerp.workflow.helpers import Record |
| 24 | +from openerp.workflow.workitem import WorkflowItem |
22 | 25 |
|
23 |
| -def create(cr, ident, wkf_id): |
24 |
| - (uid,res_type,res_id) = ident |
25 |
| - cr.execute('insert into wkf_instance (res_type,res_id,uid,wkf_id) values (%s,%s,%s,%s) RETURNING id', (res_type,res_id,uid,wkf_id)) |
26 |
| - id_new = cr.fetchone()[0] |
27 |
| - cr.execute('select * from wkf_activity where flow_start=True and wkf_id=%s', (wkf_id,)) |
28 |
| - res = cr.dictfetchall() |
29 |
| - stack = [] |
30 |
| - workitem.create(cr, res, id_new, ident, stack=stack) |
31 |
| - update(cr, id_new, ident) |
32 |
| - return id_new |
33 |
| - |
34 |
| -def delete(cr, ident): |
35 |
| - (uid,res_type,res_id) = ident |
36 |
| - cr.execute('delete from wkf_instance where res_id=%s and res_type=%s', (res_id,res_type)) |
37 |
| - |
38 |
| -def validate(cr, inst_id, ident, signal, force_running=False): |
39 |
| - cr.execute("select * from wkf_workitem where inst_id=%s", (inst_id,)) |
40 |
| - stack = [] |
41 |
| - for witem in cr.dictfetchall(): |
| 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 |
| 31 | + self.record = record |
| 32 | + |
| 33 | + if not values: |
| 34 | + values = {} |
| 35 | + |
| 36 | + assert isinstance(values, dict) |
| 37 | + self.instance = values |
| 38 | + |
| 39 | + @classmethod |
| 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)) |
| 44 | + |
| 45 | + cr = session.cr |
| 46 | + cr.execute('insert into wkf_instance (res_type,res_id,uid,wkf_id) values (%s,%s,%s,%s) RETURNING id', (record.model, record.id, session.uid, workflow_id)) |
| 47 | + instance_id = cr.fetchone()[0] |
| 48 | + |
| 49 | + cr.execute('select * from wkf_activity where flow_start=True and wkf_id=%s', (workflow_id,)) |
42 | 50 | stack = []
|
43 |
| - workitem.process(cr, witem, ident, signal, force_running, stack=stack) |
44 |
| - # An action is returned |
45 |
| - _update_end(cr, inst_id, ident) |
46 |
| - return stack and stack[0] or False |
47 |
| - |
48 |
| -def update(cr, inst_id, ident): |
49 |
| - cr.execute("select * from wkf_workitem where inst_id=%s", (inst_id,)) |
50 |
| - for witem in cr.dictfetchall(): |
| 51 | + |
| 52 | + activities = cr.dictfetchall() |
| 53 | + for activity in activities: |
| 54 | + WorkflowItem.create(session, record, activity, instance_id, stack) |
| 55 | + |
| 56 | + cr.execute('SELECT * FROM wkf_instance WHERE id = %s', (instance_id,)) |
| 57 | + values = cr.dictfetchone() |
| 58 | + wi = WorkflowInstance(session, record, values) |
| 59 | + wi.update() |
| 60 | + |
| 61 | + return wi |
| 62 | + |
| 63 | + def delete(self): |
| 64 | + self.session.cr.execute('delete from wkf_instance where res_id=%s and res_type=%s', (self.record.id, self.record.model)) |
| 65 | + |
| 66 | + def validate(self, signal, force_running=False): |
| 67 | + assert isinstance(signal, basestring) |
| 68 | + assert isinstance(force_running, bool) |
| 69 | + |
| 70 | + cr = self.session.cr |
| 71 | + cr.execute("select * from wkf_workitem where inst_id=%s", (self.instance['id'],)) |
51 | 72 | stack = []
|
52 |
| - workitem.process(cr, witem, ident, stack=stack) |
53 |
| - return _update_end(cr, inst_id, ident) |
54 |
| - |
55 |
| -def _update_end(cr, inst_id, ident): |
56 |
| - cr.execute('select wkf_id from wkf_instance where id=%s', (inst_id,)) |
57 |
| - wkf_id = cr.fetchone()[0] |
58 |
| - 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', (inst_id,)) |
59 |
| - ok=True |
60 |
| - for r in cr.fetchall(): |
61 |
| - if (r[0]<>'complete') or not r[1]: |
62 |
| - ok=False |
63 |
| - break |
64 |
| - if ok: |
65 |
| - 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', (inst_id,)) |
66 |
| - act_names = cr.fetchall() |
67 |
| - cr.execute("update wkf_instance set state='complete' where id=%s", (inst_id,)) |
68 |
| - cr.execute("update wkf_workitem set state='complete' where subflow_id=%s", (inst_id,)) |
69 |
| - 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)", (inst_id,)) |
70 |
| - for i in cr.fetchall(): |
71 |
| - for act_name in act_names: |
72 |
| - validate(cr, i[0], (ident[0],i[1],i[2]), 'subflow.'+act_name[0]) |
73 |
| - return ok |
| 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 |
| 77 | + self._update_end() |
| 78 | + return stack and stack[0] or False |
| 79 | + |
| 80 | + def update(self): |
| 81 | + cr = self.session.cr |
| 82 | + |
| 83 | + cr.execute("select * from wkf_workitem where inst_id=%s", (self.instance['id'],)) |
| 84 | + for work_item_values in cr.dictfetchall(): |
| 85 | + stack = [] |
| 86 | + WorkflowItem(self.session, self.record, work_item_values).process(stack=stack) |
| 87 | + return self._update_end() |
| 88 | + |
| 89 | + def _update_end(self): |
| 90 | + cr = self.session.cr |
| 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,)) |
| 95 | + ok=True |
| 96 | + for r in cr.fetchall(): |
| 97 | + if (r[0]<>'complete') or not r[1]: |
| 98 | + ok=False |
| 99 | + break |
| 100 | + if ok: |
| 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.{}'.format(act_name[0])) |
| 110 | + |
| 111 | + return ok |
| 112 | + |
| 113 | + |
| 114 | + |
| 115 | + |
| 116 | + |
| 117 | +def create(session, record, workflow_id): |
| 118 | + return WorkflowInstance(session, record).create(workflow_id) |
| 119 | + |
| 120 | +def delete(session, record): |
| 121 | + return WorkflowInstance(session, record).delete() |
| 122 | + |
| 123 | +def validate(session, record, instance_id, signal, force_running=False): |
| 124 | + return WorkflowInstance(session, record).validate(instance_id, signal, force_running) |
| 125 | + |
| 126 | +def update(session, record, instance_id): |
| 127 | + return WorkflowInstance(session, record).update(instance_id) |
| 128 | + |
| 129 | +def _update_end(session, record, instance_id): |
| 130 | + return WorkflowInstance(session, record)._update_end(instance_id) |
74 | 131 |
|
75 | 132 |
|
76 | 133 | # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
|
|
0 commit comments