| Class | OpenWFE::RestoreWorkItemExpression |
| In: |
lib/openwfe/expressions/fe_save.rb
|
| Parent: | FlowExpression |
"restore" is often used in conjunction with "save" (SaveWorkItemExpression).
It can restore a workitem saved to a variable (it will actually restore the payload of that workitem) or transfer the content of a field as top attribute field.
restore :from_variable => "freezed_workitem"
#
# takes the freezed payload at 'freezed_workitem' and makes it
# the payload of the current workitem
restore :from_field => "some_data"
#
# replaces the payload of the current workitem with the hash
# found in the field "some_data"
restore :from_variable => "v", :to_field => "f"
#
# will copy the payload saved under variable "v" as the value
# of the field "f"
restore :from_variable => "v", :merge_lead => :current
#
# will restore the payload of the workitem saved under v
# but if fields of v are already present in the current workitem
# the current values will be kept
restore :from_variable => "v", :merge_lead => :restored
#
# will restore the payload of the workitem saved under v
# 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" => ""
}
so that in can be placed outside of process definition bodies
# File lib/openwfe/expressions/fe_save.rb, line 175
175: def reply (workitem)
176:
177: from_field = lookup_string_attribute :from_field, workitem
178: from_variable = lookup_string_attribute :from_variable, workitem
179:
180: merge_lead = lookup_sym_attribute :merge_lead, workitem
181:
182: merge_lead = nil \
183: unless [ nil, :current, :restored ].include?(merge_lead)
184:
185: value = workitem.attributes[FIELD_RESULT]
186:
187: source = if from_field
188:
189: att = workitem.lookup_attribute from_field
190:
191: lwarn {
192: "apply() field '#{from_field}' is NOT a hash, " +
193: "restored anyway"
194: } unless att.kind_of?(Hash)
195:
196: att
197:
198: elsif from_variable
199:
200: lookup_variable from_variable
201:
202: elsif value
203:
204: value
205:
206: else
207:
208: nil
209: end
210:
211: if source
212:
213: workitem = if merge_lead
214: do_merge merge_lead, workitem, source
215: else
216: do_overwrite workitem, source
217: end
218: end
219: # else, don't restore anything
220:
221: reply_to_parent workitem
222: end
If the attribute ‘merge-lead’ (or ‘merge_lead’) is specified, the workitems get merged.
# File lib/openwfe/expressions/fe_save.rb, line 255
255: def do_merge (merge_lead, workitem, source)
256:
257: if source.kind_of?(Hash)
258: wi = InFlowWorkItem.new
259: wi.attributes = source
260: source = wi
261: end
262:
263: wiTarget, wiSource = if merge_lead == :current
264: [ source, workitem ]
265: else
266: [ workitem, source ]
267: end
268:
269: merge_workitems wiTarget, wiSource
270: end
The default case, restored values simply overwrite current values.
# File lib/openwfe/expressions/fe_save.rb, line 230
230: def do_overwrite (workitem, source)
231:
232: return workitem unless source
233:
234: attributes = if source.kind_of?(WorkItem)
235: OpenWFE::fulldup source.attributes
236: else
237: source
238: end
239:
240: to_field = lookup_string_attribute :to_field, workitem
241:
242: if to_field
243: workitem.set_attribute to_field, attributes
244: else
245: workitem.attributes = attributes
246: end
247:
248: workitem
249: end