lib/openwfe/expressions/fe_raw.rb in openwferu-0.9.6 vs lib/openwfe/expressions/fe_raw.rb in openwferu-0.9.7

- old
+ new

@@ -85,16 +85,18 @@ @fei, @parent_id, @environment_id, @application_context, attributes) + + handle_descriptions() expression.children = extract_children() expression.store_itself() - return expression + expression end def apply (workitem) exp_name = expression_name() @@ -141,20 +143,33 @@ expression.apply_time = OpenWFE::now() expression.apply(workitem) end + # + # This method is called by the expression pool when it is about + # to launch a process, it will interpret the 'parameter' statements + # in the process definition and raise an exception if the requirements + # are not met. + # + def check_parameters (workitem) + + extract_parameters.each do |param| + param.check(workitem) + end + end + #def reply (workitem) # no implementation necessary #end def is_definition? () return get_expression_map.is_definition?(expression_name()) end def expression_class () - return get_expression_map().get_class(expression_name()) + return get_expression_map.get_class(expression_name()) end def definition_name () return raw_representation.attributes['name'].to_s end @@ -163,24 +178,44 @@ return raw_representation.name end protected + # + # Takes care of extracting the process definition descriptions + # if any and to set the description variables accordingly. + # + def handle_descriptions + + default = false + + ds = extract_descriptions + + ds.each do |k, description| + vname = if k == "default" + default = true + "description" + else + "description__#{k}" + end + set_variable vname, description + end + + return if ds.length < 1 + + set_variable "description", ds[0][1] \ + unless default + end + def launch_template (template, workitem) @attributes = extract_attributes() #require 'pp' #pp attributes #pp lookup_attributes(workitem, attributes) - # TODO : do not use extract_children() !!!!!!!! - #self.children = extract_children() - #params = lookup_attributes(workitem) - #text = OpenWFE::fetch_text_content(self, workitem, false) - #params["0"] = text if text - params = lookup_attributes(workitem) extract_text_children.each_with_index do |value, index| params[index.to_s] = value end @@ -197,11 +232,93 @@ raise NotImplementedError.new("'abstract method' sorry") end def extract_children () raise NotImplementedError.new("'abstract method' sorry") end + def extract_descriptions () + raise NotImplementedError.new("'abstract method' sorry") + end + def extract_parameters () + raise NotImplementedError.new("'abstract method' sorry") + end def extract_text_children () raise NotImplementedError.new("'abstract method' sorry") + end + + # + # Encapsulating + # <parameter field="x" default="y" type="z" match="m" /> + # + class Parameter + + def initialize (field, match, default, type) + + @field = field + @match = match + @default = default + @type = type + end + + # + # Will raise an exception if this param requirement is not + # met by the workitem. + # + def check (workitem) + + unless @field + raise \ + OpenWFE::ParameterException, + "'parameter'/'param' without a 'field' attribute" + end + + field_value = workitem.attributes[@field] + field_value = @default unless field_value + + unless field_value + raise \ + OpenWFE::ParameterException, + "field '#{@field}' is missing" \ + end + + check_match(field_value) + + enforce_type(workitem, field_value) + end + + protected + + # + # Will raise an exception if it cannot coerce the type + # of the value to the one desired. + # + def enforce_type (workitem, value) + + value = if not @type + value + elsif @type == "string" + value.to_s + elsif @type == "int" or @type == "integer" + Integer(value) + elsif @type == "float" + Float(value) + else + raise + "unknown type '#{@type}' for field '#{@field}'" + end + + workitem.attributes[@field] = value + end + + def check_match (value) + + return unless @match + + unless value.to_s.match(@match) + raise \ + OpenWFE::ParameterException, + "value of field '#{@field}' doesn't match" + end + end end end end