lib/openwfe/workitem.rb in openwferu-0.9.5 vs lib/openwfe/workitem.rb in openwferu-0.9.6
- old
+ new
@@ -69,10 +69,25 @@
end
alias :fields :attributes
alias :fields= :attributes=
+ 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.last_modified = h[:last_modified]
+ wi.attributes = h[:attributes]
+ wi
+ end
+
#
# In order to simplify code like :
#
# value = workitem.attributes['xyz']
#
@@ -193,10 +208,24 @@
#
# Just a handy alias for flow_expression_id
#
alias :fei :flow_expression_id
alias :fei= :flow_expression_id=
+
+ def to_h
+ h = super
+ h[:flow_expression_id] = @flow_expression_id.to_h
+ h[:participant_name] = @participant_name
+ h
+ end
+
+ def InFlowItem.from_h (h)
+ wi = super
+ wi.flow_expression_id = FlowExpressionId.from_h(h[:flow_expression_id])
+ wi.participant_name = h[:participant_name]
+ wi
+ end
end
#
# When the term 'workitem' is used it's generally referring to instances
# of this InFlowWorkItem class.
@@ -224,12 +253,35 @@
s << " - attributes :\n"
@attributes.each do |k, v|
s << " * '#{k}' --> '#{v}'\n"
end
s << " <<<#{self.class}<<<"
- return s
+ s
end
+
+ #
+ # For some easy YAML encoding, turns the workitem into a Hash
+ # (Any YAML-enabled platform can thus read it).
+ #
+ def to_h
+ h = super
+ h[:dispatch_time] = @dispatch_time
+ h[:history] = @history
+ h[:filter] = @filter
+ h
+ end
+
+ #
+ # 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
+ end
end
#
# When it needs to cancel a branch of a process instance, the engine
# emits a CancelItem towards it.
@@ -249,13 +301,19 @@
# They contain attributes that are used as the initial payload of the
# workitem circulating in the process instances.
#
class LaunchItem < WorkItem
+ DEF = "__definition"
+ FIELD_DEF = "field:#{DEF}"
+
attr_accessor :workflow_definition_url
#, :description_map
+ alias :wfdurl :workflow_definition_url
+ alias :wfdurl= :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
# for retrieval by the engine.
@@ -263,15 +321,40 @@
def initialize (process_definition=nil)
super()
if process_definition
- @workflow_definition_url = "field:__definition"
- @attributes['__definition'] = process_definition
+ @workflow_definition_url = FIELD_DEF
+ @attributes[DEF] = process_definition
end
end
+
+ #
+ # 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
+ #
+ # 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.from_h(h)
+ end
#
# HISTORY ITEM
#