2009-10-20 10:52:23 +00:00
# -*- coding: utf-8 -*-
2006-12-07 13:41:40 +00:00
##############################################################################
2010-06-18 10:16:41 +00:00
#
2009-10-14 12:32:15 +00:00
# OpenERP, Open Source Management Solution
2014-01-09 09:50:17 +00:00
# Copyright (C) 2004-2014 Tiny SPRL (<http://tiny.be>).
2008-06-16 07:24:04 +00:00
#
2008-11-03 18:27:16 +00:00
# This program is free software: you can redistribute it and/or modify
2009-10-14 12:32:15 +00:00
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
2006-12-07 13:41:40 +00:00
#
2008-11-03 18:27:16 +00:00
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2009-10-14 12:32:15 +00:00
# GNU Affero General Public License for more details.
2006-12-07 13:41:40 +00:00
#
2009-10-14 12:32:15 +00:00
# You should have received a copy of the GNU Affero General Public License
2010-06-18 10:16:41 +00:00
# along with this program. If not, see <http://www.gnu.org/licenses/>.
2006-12-07 13:41:40 +00:00
#
2008-11-03 18:27:16 +00:00
##############################################################################
2006-12-07 13:41:40 +00:00
import workitem
2013-10-23 13:07:05 +00:00
from openerp . workflow . helpers import Session
from openerp . workflow . helpers import Record
2013-10-23 13:58:38 +00:00
from openerp . workflow . workitem import WorkflowItem
2006-12-07 13:41:40 +00:00
2013-10-24 07:57:07 +00:00
class WorkflowInstance ( object ) :
def __init__ ( self , session , record , values ) :
assert isinstance ( session , Session )
assert isinstance ( record , Record )
self . session = session
self . record = record
2013-10-23 12:53:43 +00:00
2013-10-24 07:57:07 +00:00
if not values :
values = { }
2006-12-07 13:41:40 +00:00
2013-10-24 07:57:07 +00:00
assert isinstance ( values , dict )
self . instance = values
2006-12-07 13:41:40 +00:00
2013-10-24 07:57:07 +00:00
@classmethod
def create ( cls , session , record , workflow_id ) :
assert isinstance ( session , Session )
assert isinstance ( record , Record )
assert isinstance ( workflow_id , ( int , long ) )
2006-12-07 13:41:40 +00:00
2013-10-24 07:57:07 +00:00
cr = session . cr
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 ) )
instance_id = cr . fetchone ( ) [ 0 ]
cr . execute ( ' select * from wkf_activity where flow_start=True and wkf_id= %s ' , ( workflow_id , ) )
2008-07-22 14:24:36 +00:00
stack = [ ]
2013-10-24 07:57:07 +00:00
activities = cr . dictfetchall ( )
for activity in activities :
WorkflowItem . create ( session , record , activity , instance_id , stack )
2013-10-23 12:53:43 +00:00
2013-10-24 07:57:07 +00:00
cr . execute ( ' SELECT * FROM wkf_instance WHERE id = %s ' , ( instance_id , ) )
values = cr . dictfetchone ( )
wi = WorkflowInstance ( session , record , values )
wi . update ( )
2013-10-23 13:58:38 +00:00
2013-10-24 07:57:07 +00:00
return wi
def delete ( self ) :
self . session . cr . execute ( ' delete from wkf_instance where res_id= %s and res_type= %s ' , ( self . record . id , self . record . model ) )
def validate ( self , signal , force_running = False ) :
assert isinstance ( signal , basestring )
assert isinstance ( force_running , bool )
cr = self . session . cr
cr . execute ( " select * from wkf_workitem where inst_id= %s " , ( self . instance [ ' id ' ] , ) )
2008-07-22 14:24:36 +00:00
stack = [ ]
2013-10-24 07:57:07 +00:00
for work_item_values in cr . dictfetchall ( ) :
wi = WorkflowItem ( self . session , self . record , work_item_values )
wi . process ( signal = signal , force_running = force_running , stack = stack )
# An action is returned
self . _update_end ( )
return stack and stack [ 0 ] or False
def update ( self ) :
cr = self . session . cr
cr . execute ( " select * from wkf_workitem where inst_id= %s " , ( self . instance [ ' id ' ] , ) )
for work_item_values in cr . dictfetchall ( ) :
stack = [ ]
WorkflowItem ( self . session , self . record , work_item_values ) . process ( stack = stack )
return self . _update_end ( )
def _update_end ( self ) :
cr = self . session . cr
instance_id = self . instance [ ' id ' ]
cr . execute ( ' select wkf_id from wkf_instance where id= %s ' , ( instance_id , ) )
wkf_id = cr . fetchone ( ) [ 0 ]
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 , ) )
ok = True
for r in cr . fetchall ( ) :
if ( r [ 0 ] < > ' complete ' ) or not r [ 1 ] :
ok = False
break
if ok :
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 , ) )
act_names = cr . fetchall ( )
cr . execute ( " update wkf_instance set state= ' complete ' where id= %s " , ( instance_id , ) )
cr . execute ( " update wkf_workitem set state= ' complete ' where subflow_id= %s " , ( instance_id , ) )
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 , ) )
for cur_instance_id , cur_model_name , cur_record_id in cr . fetchall ( ) :
cur_record = Record ( cur_model_name , cur_record_id )
for act_name in act_names :
2014-01-20 13:53:33 +00:00
WorkflowInstance ( self . session , cur_record , { ' id ' : cur_instance_id } ) . validate ( ' subflow. %s ' % act_name [ 0 ] )
2013-10-24 07:57:07 +00:00
return ok
def create ( session , record , workflow_id ) :
return WorkflowInstance ( session , record ) . create ( workflow_id )
def delete ( session , record ) :
return WorkflowInstance ( session , record ) . delete ( )
def validate ( session , record , instance_id , signal , force_running = False ) :
return WorkflowInstance ( session , record ) . validate ( instance_id , signal , force_running )
def update ( session , record , instance_id ) :
return WorkflowInstance ( session , record ) . update ( instance_id )
2013-10-23 12:53:43 +00:00
def _update_end ( session , record , instance_id ) :
2013-10-24 07:57:07 +00:00
return WorkflowInstance ( session , record ) . _update_end ( instance_id )
2006-12-07 13:41:40 +00:00
2008-07-23 15:01:27 +00:00
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: