lib/openwfe/expressions/fe_save.rb in openwferu-0.9.16 vs lib/openwfe/expressions/fe_save.rb in openwferu-0.9.17
- old
+ new
@@ -1,8 +1,8 @@
#
#--
-# Copyright (c) 2007, John Mettraux, OpenWFE.org
+# Copyright (c) 2007-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:
#
@@ -126,41 +126,91 @@
# # but the workitem v payload will have priority.
#
# Beware : you should not restore from a field that is not a hash. The
# top level attributes (payload) of a workitem should always be a hash.
#
+ # Since OpenWFEru 0.9.17, the 'set-fields' alias can be used for restore.
+ #
+ # sequence do
+ # set_fields :value => {
+ # "customer" => { "name" => "Zigue", "age" => 34 },
+ # "approved" => false }
+ # _print "${f:customer.name} (${f:customer.age}) ${f:approved}"
+ # end
+ #
+ # Along with this new alias, the expression now behave much like the 'set'
+ # expression, but still, targets the whole workitem payload.
+ #
+ # Note that "set-fields" can be used outside of the body of a process
+ # definition (along with "set") to separate 'data preparation' from
+ # actual process definition.
+ #
+ # class Test44b6 < ProcessDefinition
+ # set_fields :value => {
+ # "customer" => { "name" => "Zigue", "age" => 34 },
+ # "approved" => false }
+ # sequence do
+ # _print "${f:customer.name} (${f:customer.age}) ${f:approved}"
+ # end
+ # end
+ #
+ # Using set_fields at the beginning of a process can be useful for setting
+ # up forms (keys without values for now).
+ #
+ # set_fields :value => {
+ # "name" => "",
+ # "address" => "",
+ # "email" => ""
+ # }
+ #
class RestoreWorkItemExpression < FlowExpression
include MergeMixin
+ include ValueMixin
- names :restore
+ names :restore, :set_fields
- def apply (workitem)
+ is_definition
+ # so that in can be placed outside of process definition bodies
+
+ def reply (workitem)
+
from_field = lookup_string_attribute :from_field, workitem
from_variable = lookup_string_attribute :from_variable, workitem
+
merge_lead = lookup_sym_attribute :merge_lead, workitem
+ merge_lead = nil \
+ unless [ nil, :current, :restored ].include?(merge_lead)
+
+ value = workitem.attributes[FIELD_RESULT]
+
source = if from_field
att = workitem.lookup_attribute from_field
- lwarn do
+ lwarn {
"apply() field '#{from_field}' is NOT a hash, " +
"restored anyway"
- end unless att.kind_of?(Hash)
+ } unless att.kind_of?(Hash)
att
elsif from_variable
lookup_variable from_variable
+ elsif value
+
+ value
+
else
nil
end
if source
+
workitem = if merge_lead
do_merge merge_lead, workitem, source
else
do_overwrite workitem, source
end