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
# Copyright (C) 2004-2009 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
def create ( cr , ident , wkf_id ) :
2008-07-22 14:24:36 +00:00
( uid , res_type , res_id ) = ident
2009-05-01 20:48:18 +00:00
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 ) )
2008-07-22 14:24:36 +00:00
id_new = cr . fetchone ( ) [ 0 ]
2008-12-09 12:37:22 +00:00
cr . execute ( ' select * from wkf_activity where flow_start=True and wkf_id= %s ' , ( wkf_id , ) )
2008-07-22 14:24:36 +00:00
res = cr . dictfetchall ( )
stack = [ ]
workitem . create ( cr , res , id_new , ident , stack = stack )
update ( cr , id_new , ident )
return id_new
2006-12-07 13:41:40 +00:00
def delete ( cr , ident ) :
2008-07-22 14:24:36 +00:00
( uid , res_type , res_id ) = ident
2008-12-09 12:37:22 +00:00
cr . execute ( ' delete from wkf_instance where res_id= %s and res_type= %s ' , ( res_id , res_type ) )
2006-12-07 13:41:40 +00:00
def validate ( cr , inst_id , ident , signal , force_running = False ) :
2008-12-09 12:37:22 +00:00
cr . execute ( " select * from wkf_workitem where inst_id= %s " , ( inst_id , ) )
2009-08-17 09:33:01 +00:00
stack = [ ]
2015-10-14 09:47:10 +00:00
for i , witem in enumerate ( cr . dictfetchall ( ) ) :
2008-07-22 14:24:36 +00:00
stack = [ ]
2015-10-14 09:47:10 +00:00
if i > 0 :
# test if previous workitem has already processed this one
cr . execute ( " select id from wkf_workitem where id= %s " , ( witem [ ' id ' ] , ) )
if not cr . fetchone ( ) :
continue
2008-07-22 14:24:36 +00:00
workitem . process ( cr , witem , ident , signal , force_running , stack = stack )
# An action is returned
_update_end ( cr , inst_id , ident )
return stack and stack [ 0 ] or False
2006-12-07 13:41:40 +00:00
def update ( cr , inst_id , ident ) :
2008-12-09 12:37:22 +00:00
cr . execute ( " select * from wkf_workitem where inst_id= %s " , ( inst_id , ) )
2008-07-22 14:24:36 +00:00
for witem in cr . dictfetchall ( ) :
stack = [ ]
workitem . process ( cr , witem , ident , stack = stack )
return _update_end ( cr , inst_id , ident )
2006-12-07 13:41:40 +00:00
def _update_end ( cr , inst_id , ident ) :
2008-12-09 12:37:22 +00:00
cr . execute ( ' select wkf_id from wkf_instance where id= %s ' , ( inst_id , ) )
2008-07-22 14:24:36 +00:00
wkf_id = cr . fetchone ( ) [ 0 ]
2008-12-09 12:37:22 +00:00
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 , ) )
2008-07-22 14:24:36 +00:00
ok = True
for r in cr . fetchall ( ) :
if ( r [ 0 ] < > ' complete ' ) or not r [ 1 ] :
ok = False
break
if ok :
2008-12-09 12:37:22 +00:00
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 , ) )
2008-07-22 14:24:36 +00:00
act_names = cr . fetchall ( )
2008-12-09 12:37:22 +00:00
cr . execute ( " update wkf_instance set state= ' complete ' where id= %s " , ( inst_id , ) )
cr . execute ( " update wkf_workitem set state= ' complete ' where subflow_id= %s " , ( inst_id , ) )
2010-06-18 10:16:41 +00:00
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 , ) )
2008-07-22 14:24:36 +00:00
for i in cr . fetchall ( ) :
for act_name in act_names :
validate ( cr , i [ 0 ] , ( ident [ 0 ] , i [ 1 ] , i [ 2 ] ) , ' subflow. ' + act_name [ 0 ] )
return ok
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: