lib/openwfe/workitem.rb in ruote-0.9.19 vs lib/openwfe/workitem.rb in ruote-0.9.20

- old
+ new

@@ -1,55 +1,40 @@ -# #-- -# Copyright (c) 2005-2008, John Mettraux, OpenWFE.org -# All rights reserved. +# Copyright (c) 2005-2009, John Mettraux, jmettraux@gmail.com # -# Redistribution and use in source and binary forms, with or without -# modification, are permitted provided that the following conditions are met: +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: # -# . Redistributions of source code must retain the above copyright notice, this -# list of conditions and the following disclaimer. +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. # -# . Redistributions in binary form must reproduce the above copyright notice, -# this list of conditions and the following disclaimer in the documentation -# and/or other materials provided with the distribution. +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +# THE SOFTWARE. # -# . Neither the name of the "OpenWFE" nor the names of its contributors may be -# used to endorse or promote products derived from this software without -# specific prior written permission. -# -# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" -# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE -# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE -# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR -# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF -# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS -# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN -# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) -# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE -# POSSIBILITY OF SUCH DAMAGE. +# Made in Japan. #++ -# -# -# "hecho en Costa Rica" -# and "made in Japan" -# -# john.mettraux@openwfe.org -# require 'openwfe/utils' module OpenWFE # # The convention for the result of some expressions is to store # their result in a workitem field named "__result__". # - FIELD_RESULT = "__result__" + FIELD_RESULT = '__result__' #-- # WORKITEMS #++ @@ -75,23 +60,23 @@ @last_modified = Time.now end def to_h - - h = {} - h['type'] = self.class.name - h['last_modified'] = @last_modified - h['attributes'] = @attributes - h + { + 'type' => self.class.name, + 'last_modified' => @last_modified, + 'attributes' => @attributes.clone + } end def self.from_h (h) - wi = OpenWFE.get_class(h).new + klass = WI_CLASSES[h['type']] || self + wi = klass.new wi.last_modified = h['last_modified'] - wi.attributes = h['attributes'] + wi.attributes = h['attributes'] || h['fields'] || {} wi end # # A shortcut for @@ -146,29 +131,20 @@ # # is also possible # def method_missing (m, *args) - methodname = m.to_s + method_name = m.to_s if args.length == 0 - value = @attributes[methodname] + value = @attributes[method_name] return value if value != nil - raise "Missing attribute '#{methodname}' in workitem" + raise "Missing attribute '#{method_name}' in workitem" end - #if methodname == "[]" and args.length == 1 - # value = @attributes[args[0]] - # return value if value - # raise "Missing attribute '#{methodname}' in workitem" - #end - #if methodname == "[]=" and args.length == 2 - # return @attributes[args[0]] = args[1] - #end - - if args.length == 1 and methodname[-1, 1] == '=' - return @attributes[methodname[0..-2]] = args[0] + if args.length == 1 and method_name[-1, 1] == '=' + return @attributes[method_name[0..-2]] = args[0] end super(m, args) end @@ -281,11 +257,10 @@ # data around. Their 'payload' is located in their attribute Hash field. # class InFlowWorkItem < InFlowItem attr_accessor :dispatch_time - attr_accessor :filter # # in some contexts (ruote-rest, ruote-web2, the web...) workitems # have an URI # @@ -302,28 +277,59 @@ attr_accessor :store # # special : added by the ruby lib, not given by the worklist # + # sets the filter for this workitem + # + def filter= (f) + + f ? @attributes['__filter__'] = f : @attributes.delete('__filter__') + end + + # + # returns the filter for this workitem + # + def filter + + @attributes['__filter__'] + end + + # + # Returns the current timeout for the workitem (or nil if none is set). + # + # The result is an array [ + # exp_class_name, exp_name, timestamp, timeout_duration, timeout_point ] + # + # 'timestamp' and 'timeout_point' are Float instances (use Time.at(f) to + # turn into local Time instances) + # + def current_timeout + + stamps = self.attributes['__timeouts__'] + return nil unless stamps + + stamps["#{fei.wfid}__#{fei.expid}"] + end + + # # Outputting the workitem in a human readable format # def to_s - s = "" - s << " #{self.class} :\n" + s = " #{self.class} :\n" s << " - flow_expression_id : #{@flow_expression_id}\n" s << " - participant_name : #{@participant_name}\n" s << " - last_modified : #{@last_modified}\n" s << " - dispatch_time : #{@dispatch_time}\n" s << " - attributes :\n" - s << " {\n" + s << ' {\n' @attributes.keys.sort.each do |k| - v = @attributes[k] - s << " #{k.inspect} => #{v.inspect},\n" + s << " #{k.inspect} => #{@attributes[k].inspect},\n" end - s << " }" + s << ' }' s end # # For some easy YAML encoding, turns the workitem into a Hash @@ -331,27 +337,42 @@ # def to_h h = super h['dispatch_time'] = @dispatch_time - #h[:history] = @history - h['filter'] = @filter + h['attributes']['__filter__'] &&= filter.to_h h end # # Rebuilds an InFlowWorkItem from its hash version. # - def InFlowWorkItem.from_h (h) + def self.from_h (h) - wi = super - wi.dispatch_time = h['dispatch_time'] - wi.filter = h['filter'] + wi = super(h) + wi.filter &&= OpenWFE::FilterDefinition.from_h(wi.filter) wi end # + # Returns a String containing the XML representation of this workitem. + # + def to_xml (options={ :indent => 2 }) + + OpenWFE::Xml.workitem_to_xml(self, options) + end + + # + # Reads a String or a REXML document or element and returns a copy of the + # InFlowWorkitem it represents. + # + def self.from_xml (xml) + + OpenWFE::Xml.workitem_from_xml(xml) + end + + # # Sets the '__result__' field of this workitem # def set_result (result) @attributes[FIELD_RESULT] = result @@ -360,11 +381,11 @@ # # Makes sure the '__result__' field of this workitem is empty. # def unset_result - @attributes.delete FIELD_RESULT + @attributes.delete(FIELD_RESULT) end # # Just a shortcut (for consistency) of # @@ -406,19 +427,22 @@ # They contain attributes that are used as the initial payload of the # workitem circulating in the process instances. # class LaunchItem < WorkItem - DEF = "__definition" + DEF = '__definition' FIELD_DEF = "field:#{DEF}" attr_accessor :workflow_definition_url #, :description_map alias :wfdurl :workflow_definition_url alias :wfdurl= :workflow_definition_url= + alias :definition_url :workflow_definition_url + alias :definition_url= :workflow_definition_url= + # # This constructor will build an empty LaunchItem. # # If the optional parameter process_definition is set, the # definition will be embedded in the launchitem attributes @@ -432,12 +456,12 @@ # # # Provide an XML process definition as a string # definition = """ # <process-definition name="x" revision="y"> # <sequence> - # <participant ref="alpha" /> - # <participant ref="bravo" /> + # <participant ref="alpha" /> + # <participant ref="bravo" /> # </sequence> # </process-definition> # """.strip # LaunchItem.new(definition) # @@ -453,13 +477,22 @@ # def initialize (process_definition=nil) super() + self.definition = process_definition + end + + def definition + @attributes[DEF] + end + + def definition= (process_definition) if process_definition - @workflow_definition_url = FIELD_DEF @attributes[DEF] = process_definition + else + @attributes.delete(DEF) end end # # Turns the LaunchItem instance into a simple 'hash' (easily @@ -470,37 +503,39 @@ h = super h['workflow_definition_url'] = @workflow_definition_url h end + # + # Turns a hash into a LaunchItem instance. + # def self.from_h (h) li = super - li.workflow_definition_url = h['workflow_definition_url'] + li.definition_url = + h['workflow_definition_url'] || + h['definition_url'] || + h['pdef_url'] + li.definition = + h['workflow_definition'] || + h['definition'] || + h['pdef'] li end end # # Turns a hash into its corresponding workitem (InFlowWorkItem, CancelItem, # LaunchItem). # - def OpenWFE.workitem_from_h (h) + def self.workitem_from_h (h) - wi_class = get_class(h) + wi_class = WI_CLASSES[h['type']] wi_class.from_h(h) end WI_CLASSES = [ OpenWFE::LaunchItem, OpenWFE::InFlowWorkItem, OpenWFE::CancelItem ].inject({}) { |r, c| r[c.to_s] = c; r } - - # - # returns the workitem class for the given hash - # - def OpenWFE.get_class (h) - - WI_CLASSES[h['type']] - end end