lib/openwfe/workitem.rb in openwferu-0.9.16 vs lib/openwfe/workitem.rb in openwferu-0.9.17
- old
+ new
@@ -1,8 +1,8 @@
#
#--
-# Copyright (c) 2005-2007, John Mettraux, OpenWFE.org
+# Copyright (c) 2005-2008, 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:
#
@@ -70,29 +70,67 @@
#
# Sets the last_modified field to now
#
def touch
+
@last_modified = Time.now
end
def to_h
+
h = {}
h[:type] = self.class.name
h[:last_modified] = @last_modified
h[:attributes] = @attributes
h
end
def WorkItem.from_h (h)
- wi = eval("#{h[:type]}.new")
+
+ #wi = eval("#{h[:type]}.new")
+ wi = OpenWFE.get_class(h).new
wi.last_modified = h[:last_modified]
wi.attributes = h[:attributes]
wi
end
#
+ # A shortcut for
+ #
+ # workitem.attributes['key']
+ #
+ # is
+ #
+ # workitem['key']
+ #
+ # (Note that
+ #
+ # workitem.key
+ #
+ # will raise an exception if there is no attribute key).
+ #
+ def [] (key)
+
+ @attributes[key]
+ end
+
+ #
+ # A shortcut for
+ #
+ # workitem.attributes['key'] = value
+ #
+ # is
+ #
+ # workitem['key'] = value
+ #
+ def []= (key, value)
+
+ @attributes[key] = value
+ end
+
+ #
# In order to simplify code like :
#
# value = workitem.attributes['xyz']
#
# to
@@ -117,20 +155,19 @@
value = @attributes[methodname]
return value if value
raise "Missing attribute '#{methodname}' 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 == 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 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]
end
super(m, args)
@@ -186,13 +223,21 @@
#
def set_attribute (key, value)
OpenWFE.set_attribute(@attributes, key, value)
end
+ #
+ # unset_attribute() accomodates itself with nested key constructs.
+ #
+ def unset_attribute (key)
+ OpenWFE.unset_attribute(@attributes, key)
+ end
+
alias :lookup_field :lookup_attribute
alias :has_field? :has_attribute?
alias :set_field :set_attribute
+ alias :unset_field :unset_attribute
end
#
# The common parent class for InFlowWorkItem and CancelItem.
@@ -281,10 +326,11 @@
#
# Rebuilds an InFlowWorkItem from its hash version.
#
def InFlowWorkItem.from_h (h)
+
wi = super
wi.dispatch_time = h[:dispatch_time]
wi.history = h[:history]
wi.filter = h[:filter]
wi
@@ -292,36 +338,40 @@
#
# Sets the '__result__' field of this workitem
#
def set_result (result)
+
@attributes[FIELD_RESULT] = result
end
#
# Makes sure the '__result__' field of this workitem is empty.
#
def unset_result
+
@attributes.delete FIELD_RESULT
end
#
# Just a shortcut (for consistency) of
#
# workitem.attributes["__result__"]
#
def get_result
+
@attributes[FIELD_RESULT]
end
#
# Returns true or false.
#
def get_boolean_result
+
r = get_result
return false unless r
- return (r == true or r == "true")
+ (r == true or r == "true")
end
end
#
# When it needs to cancel a branch of a process instance, the engine
@@ -330,10 +380,11 @@
# receiving a cancel item.
#
class CancelItem < InFlowItem
def initialize (workitem)
+
super()
@flow_expression_id = workitem.fei.dup
end
end
@@ -400,16 +451,18 @@
#
# Turns the LaunchItem instance into a simple 'hash' (easily
# serializable to other formats).
#
def to_h
+
h = super
h[:workflow_definition_url] = @workflow_definition_url
h
end
def LaunchItem.from_h (h)
+
li = super
li.workflow_definition_url = h[:workflow_definition_url]
li
end
end
@@ -417,12 +470,21 @@
#
# Turns a hash into its corresponding workitem (InFlowWorkItem, CancelItem,
# LaunchItem).
#
def OpenWFE.workitem_from_h (h)
- wi_class = h[:type]
- wi_class = eval(wi_class)
+
+ #wi_class = eval(h[:type])
+ wi_class = get_class(h)
wi_class.from_h(h)
+ end
+
+ def OpenWFE.get_class (h)
+
+ cl = h[:type]
+ return nil if cl.index(";")
+ return nil if cl.index(" ")
+ eval(cl)
end
end