lib/openwfe/extras/expool/dbexpstorage.rb in openwferu-extras-0.9.16 vs lib/openwfe/extras/expool/dbexpstorage.rb in openwferu-extras-0.9.17

- old
+ new

@@ -1,8 +1,8 @@ # #-- -# Copyright (c) 2007, Tomaso Tosolini, John Mettraux OpenWFE.org +# Copyright (c) 2007-2008, Tomaso Tosolini, John Mettraux OpenWFE.org # All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # @@ -36,19 +36,17 @@ # # Tomaso Tosolini # John Mettraux # -require 'rubygems' +#require 'rubygems' #require_gem 'activerecord' -gem 'activerecord' -require 'active_record' +gem 'activerecord'; require 'active_record' require 'monitor' - require 'openwfe/service' require 'openwfe/rudefinitions' require 'openwfe/expool/expstorage' require 'openwfe/expool/threadedexpstorage' @@ -188,59 +186,139 @@ Expression.delete_all end # - # Given an expression class (Schedulable or ParticipantExpression for - # example), calls the passed block for each expression of that class. + # Gather expressions matching certain parameters. # - def each_of_kind (kind, &block) + def find_expressions (options={}) - classes = get_expression_map.get_expression_classes(kind) - classes = classes.collect { |c| c.name } + conditions = determine_conditions options + # note : this call modifies the options hash... - exps = Expression.find_all_by_exp_class(classes) + # + # maximize usage of SQL querying - exp_each exps, &block + exps = Expression.find :all, :conditions => conditions + + # + # do the rest of the filtering + + exps = exps.collect do |exp| + as_owfe_expression exp + end + + exps.find_all do |fexp| + does_match? options, fexp + end end # - # Calls the block for each expression stored. If a wfid prefix is - # passed, only the expressions whose workflow instance id begins - # with it will be iterated over. + # Fetches the root of a process instance. # - def each (wfid_prefix=nil, &block) + def fetch_root (wfid) params = {} - params[:conditions] = [ "wfid LIKE ?", "#{wfid_prefix}%" ] \ - if wfid_prefix + params[:conditions] = [ + "wfid = ? AND exp_class = ?", wfid, DefineExpression.to_s + ] exps = Expression.find(:all, params) - exp_each exps, &block + e = exps.sort { |fe1, fe2| fe1.fei.expid <=> fe2.fei.expid }[0] + # + # find the one with the smallest expid + + as_owfe_expression e end - alias :real_each :each - protected # - # Used by each() and each_of_kind(). + # Grabs the options to build a conditions array for use by + # find(). # - def exp_each (expressions, &block) + # Note : this method, modifies the options hash (it removes + # the args it needs). + # + def determine_conditions (options) - expressions.each do |exp| - fexp = as_owfe_expression(exp) - block.call fexp.fei, fexp + wfid = options.delete :wfid + wfid_prefix = options.delete :wfid_prefix + #parent_wfid = options.delete :parent_wfid + + query = [] + conditions = [] + + if wfid + query << "wfid = ?" + conditions << wfid + elsif wfid_prefix + query << "wfid LIKE ?" + conditions << "#{wfid_prefix}%" end + + add_class_conditions options, query, conditions + + conditions = conditions.flatten + + if conditions.size < 1 + nil + else + conditions.insert 0, query.join(" AND ") + end end # + # Used by determine_conditions(). + # + def add_class_conditions (options, query, conditions) + + ic = options.delete :include_classes + ic = Array(ic) + + ec = options.delete :exclude_classes + ec = Array(ec) + + acc ic, query, conditions, "OR" + acc ec, query, conditions, "AND" + end + + def acc (classes, query, conditions, join) + + return if classes.size < 1 + + classes = classes.collect do |kind| + get_expression_map.get_expression_classes kind + end + classes = classes.flatten + + quer = [] + cond = [] + classes.each do |cl| + + quer << if join == "AND" + "exp_class != ?" + else + "exp_class = ?" + end + + cond << cl.to_s + end + quer = quer.join " #{join} " + + query << "(#{quer})" + conditions << cond + end + + # # Extracts the OpenWFE FlowExpression instance from the # active record and makes sure its application_context is set. # def as_owfe_expression (record) + + return nil unless record fe = record.svalue fe.application_context = @application_context fe end