lib/openwfe/extras/participants/activeparticipants.rb in openwferu-extras-0.9.15 vs lib/openwfe/extras/participants/activeparticipants.rb in openwferu-extras-0.9.16

- old
+ new

@@ -66,11 +66,11 @@ # # For centralization purposes, the migration and the model are located # in the same source file. It should be quite easy for the Rails hackers # among you to sort that out for a Rails based usage. # - class OwfeTables < ActiveRecord::Migration + class WorkitemTables < ActiveRecord::Migration def self.up create_table :workitems do |t| t.column :fei, :string @@ -105,10 +105,18 @@ drop_table :fields end end # + # Reopening InFlowWorkItem to add a 'db_id' attribute. + # + class InFlowWorkItem + + attr_accessor :db_id + end + + # # The ActiveRecord version of an OpenWFEru workitem (InFlowWorkItem). # # One can very easily build a worklist based on a participant name via : # # wl = OpenWFE::Extras::Workitem.find_all_by_participant_name("toto") @@ -177,11 +185,11 @@ wi.attributes.each do |k, v| i.fields << Field.new_field(k, v) end i.save! - + # making sure to throw an exception in case of trouble end i end @@ -189,14 +197,18 @@ # Turns the densha Workitem into an OpenWFEru InFlowWorkItem. # def as_owfe_workitem wi = OpenWFE::InFlowWorkItem.new + wi.fei = full_fei wi.participant_name = participant_name wi.attributes = fields_hash # don't care about dispatch_time and last_modified + + wi.db_id = self.id + wi end # # Returns a hash version of the 'fields' of this workitem. @@ -228,10 +240,11 @@ #f = Field.new_field("___map_type", "smap") # # an old trick for backward compatibility with OpenWFEja save! + # making sure to throw an exception in case of trouble end # # Returns the Field instance with the given key. This method accept # symbols as well as strings as its parameter. @@ -300,9 +313,81 @@ (result[wi.store_name] ||= []) << wi end result end + + # + # A kind of 'google search' among workitems + # + def Workitem.search (search_string, storename_list=nil) + + storename_list = Array(storename_list) if storename_list + + # participant_name + + result = find( + :all, + :conditions => conditions( + "participant_name", search_string, storename_list), + :order => "participant_name") + # :limit => 10) + + ids = result.collect { |wi| wi.id } + + # svalue + + fields = Field.find( + :all, + :conditions => conditions( + "svalue", search_string, storename_list), + :include => :workitem) + + merge_search_results(ids, result, fields) + + # fkey + + fields = Field.find( + :all, + :conditions => conditions( + "fkey", search_string, storename_list), + :include => :workitem) + + merge_search_results(ids, result, fields) + + # over. + + result + end + + protected + + # + # builds the condition (the WHERE clause) for the + # search. + # + def Workitem.conditions (keyname, search_string, storename_list) + + if storename_list + [ "#{keyname} LIKE ? AND workitems.store_name IN (?)", + search_string, storename_list ] + else + [ "#{keyname} LIKE ?", + search_string ] + end + end + + def Workitem.merge_search_results (ids, wis, new_wis) + + return if new_wis.size < 1 + + new_wis.each do |wi| + wi = wi.workitem if wi.kind_of?(Field) + next if ids.include? wi.id + ids << wi.id + wis << wi + end + end end # # A Field (Attribute) of a Workitem. #