Class OpenWFE::Extras::Workitem
In: lib/openwfe/extras/participants/activeparticipants.rb
Parent: ActiveRecord::Base

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")
    puts "found #{wl.size} workitems for participant 'toto'"

These workitems are not OpenWFEru workitems directly. But the conversion is pretty easy. Note that you probaly won‘t need to do the conversion by yourself, except for certain advanced scenarii.

    awi = OpenWFE::Extras::Workitem.find_by_participant_name("toto")
        #
        # returns the first workitem in the database whose participant
        # name is 'toto'.

    owi = awi.as_owfe_workitem
        #
        # Now we have a copy of the reference as a OpenWFEru
        # InFlowWorkItem instance.

    awi = OpenWFE::Extras::Workitem.from_owfe_workitem(owi)
        #
        # turns an OpenWFEru InFlowWorkItem instance into an
        # 'active workitem'.

Methods

Public Class methods

Returns all the workitems belonging to the stores listed in the parameter storename_list. The result is a Hash whose keys are the store names and whose values are list of workitems.

[Source]

     # File lib/openwfe/extras/participants/activeparticipants.rb, line 350
350:         def self.find_in_stores (storename_list)
351: 
352:             workitems = find_all_by_store_name(storename_list)
353: 
354:             result = {}
355: 
356:             workitems.each do |wi|
357:                 (result[wi.store_name] ||= []) << wi
358:             end
359: 
360:             result
361:         end

Not really about ‘just launched’, but rather about finding the first workitem for a given process instance (wfid) and a participant. It deserves its own method because the workitem could be in a subprocess, thus escaping the vanilla find_by_wfid_and_participant()

[Source]

     # File lib/openwfe/extras/participants/activeparticipants.rb, line 407
407:         def self.find_just_launched (wfid, participant_name)
408: 
409:             find(
410:                 :first, 
411:                 :conditions => [ 
412:                     "wfid LIKE ? AND participant_name = ?", 
413:                     "#{wfid}%", 
414:                     participant_name ])
415:         end

Generates a (new) Workitem from an OpenWFEru InFlowWorkItem instance.

This is a ‘static’ method :

    awi = OpenWFE::Extras::Workitem.from_owfe_workitem(wi)

(This method saves the ‘ActiveWorkitem’).

[Source]

     # File lib/openwfe/extras/participants/activeparticipants.rb, line 177
177:         def Workitem.from_owfe_workitem (wi, store_name=nil)
178: 
179:             i = nil
180: 
181:             MUTEX.synchronize do
182: 
183:                 i = Workitem.new
184:                 i.fei = wi.fei.to_s
185:                 i.wfid = wi.fei.wfid
186:                 i.wf_name = wi.fei.workflow_definition_name
187:                 i.wf_revision = wi.fei.workflow_definition_revision
188:                 i.participant_name = wi.participant_name
189:                 i.dispatch_time = wi.dispatch_time
190:                 i.last_modified = nil
191: 
192:                 i.store_name = store_name
193: 
194:     
195:                 # This is a field set by the active participant immediately 
196:                 # before calling this method.
197:                 # the default behavior is "use field method"
198:                     
199:                 if wi.attributes["compact_workitems"]
200: 
201:                     wi.attributes.delete("compact_workitems")
202:                     i.yattributes= wi.attributes
203:                 else
204: 
205:                     i.yattributes= nil
206: 
207:                     wi.attributes.each do |k, v|
208:                         i.fields << Field.new_field(k, v)
209:                     end
210:                 end
211: 
212:                 i.save!
213:                     # making sure to throw an exception in case of trouble
214:             end
215: 
216:             i
217:         end

A kind of ‘google search’ among workitems

Note

when this is used on compact_workitems, it will not be able to search info within the fields, because they aren‘t used by this kind of workitems. In this case the search will be limited to participant_name

[Source]

     # File lib/openwfe/extras/participants/activeparticipants.rb, line 372
372:         def self.search (search_string, storename_list=nil)
373: 
374:             #t = OpenWFE::Timer.new
375: 
376:             storename_list = Array(storename_list) if storename_list
377: 
378:             # participant_name
379: 
380:             result = find(
381:                 :all, 
382:                 :conditions => conditions(
383:                     "participant_name", search_string, storename_list),
384:                 :order => "participant_name")
385:                 # :limit => 10)
386: 
387:             ids = result.collect { |wi| wi.id }
388: 
389:             # search in fields
390: 
391:             fields = Field.search search_string, storename_list
392:             merge_search_results ids, result, fields
393: 
394:             #puts "... took #{t.duration} ms"
395: 
396:             # over.
397: 
398:             result
399:         end

Protected Class methods

builds the condition (the WHERE clause) for the search.

[Source]

     # File lib/openwfe/extras/participants/activeparticipants.rb, line 423
423:             def self.conditions (keyname, search_string, storename_list)
424: 
425:                 cs = [ "#{keyname} LIKE ?", search_string ]
426: 
427:                 if storename_list
428: 
429:                     cs[0] = "#{cs[0]} AND workitems.store_name IN (?)"
430:                     cs << storename_list
431:                 end
432: 
433:                 cs
434:             end

[Source]

     # File lib/openwfe/extras/participants/activeparticipants.rb, line 436
436:             def self.merge_search_results (ids, wis, new_wis)
437: 
438:                 return if new_wis.size < 1
439: 
440:                 new_wis.each do |wi|
441:                     wi = wi.workitem if wi.kind_of?(Field)
442:                     next if ids.include? wi.id
443:                     ids << wi.id
444:                     wis << wi
445:                 end
446:             end

Public Instance methods

Turns the densha Workitem into an OpenWFEru InFlowWorkItem.

[Source]

     # File lib/openwfe/extras/participants/activeparticipants.rb, line 222
222:         def as_owfe_workitem
223: 
224:             wi = OpenWFE::InFlowWorkItem.new
225: 
226:             wi.fei = full_fei
227:             wi.participant_name = participant_name
228:             wi.attributes = fields_hash
229:             # don't care about dispatch_time and last_modified
230: 
231:             wi.db_id = self.id
232: 
233:             wi
234:         end

Returns the Field instance with the given key. This method accept symbols as well as strings as its parameter.

    wi.field("customer_name")
    wi.field :customer_name

[Source]

     # File lib/openwfe/extras/participants/activeparticipants.rb, line 286
286:         def field (key)
287:     
288:             if self.yattributes
289:                 return self.yattributes[key.to_s]
290:             end
291: 
292:             fields.find_by_fkey key.to_s
293:         end

Returns a hash version of the ‘fields’ of this workitem.

(Each time this method is called, it returns a new hash).

[Source]

     # File lib/openwfe/extras/participants/activeparticipants.rb, line 241
241:         def fields_hash
242: 
243:             return self.yattributes if self.yattributes
244: 
245:             fields.inject({}) do |r, f| 
246:                 r[f.fkey] = f.value
247:                 r
248:             end
249:         end
forward(engine)

Alias for reply

Returns the flow expression id of this work (its unique OpenWFEru identifier) as a FlowExpressionId instance. (within the Workitem it‘s just stored as a String).

[Source]

     # File lib/openwfe/extras/participants/activeparticipants.rb, line 163
163:         def full_fei
164: 
165:             OpenWFE::FlowExpressionId.from_s(fei)
166:         end
proceed(engine)

Alias for reply

Replaces the current fields of this workitem with the given hash.

This method modifies the content of the db.

[Source]

     # File lib/openwfe/extras/participants/activeparticipants.rb, line 256
256:         def replace_fields (fhash)
257: 
258:             if self.yattributes 
259: 
260:                 self.yattributes = fhash
261: 
262:             else
263: 
264:                 fields.delete_all
265: 
266:                 fhash.each do |k, v|
267:                     fields << Field.new_field(k, v)
268:                 end
269:             end
270: 
271:             #f = Field.new_field("___map_type", "smap")
272:                 #
273:                 # an old trick for backward compatibility with OpenWFEja
274: 
275:             save!
276:                 # making sure to throw an exception in case of trouble
277:         end

A shortcut method, replies to the workflow engine and removes self from the database. Handy for people who don‘t want to play with an ActiveParticipant instance when just consuming workitems (that an active participant pushed in the database).

[Source]

     # File lib/openwfe/extras/participants/activeparticipants.rb, line 302
302:         def reply (engine)
303: 
304:             engine.reply self.as_owfe_workitem
305:             self.destroy
306:         end

Simply sets the ‘last_modified’ field to now. (Doesn‘t save the workitem though).

[Source]

     # File lib/openwfe/extras/participants/activeparticipants.rb, line 315
315:         def touch
316: 
317:             self.last_modified = Time.now
318:         end

[Validate]