module PIM::MappingExporter

Public Instance Methods

create_mapped_item(item, mapping, context = {}) click to toggle source
# File mapping-exporter.rb, line 322
def create_mapped_item item, mapping, context = {}

  mapped_values = []
  context[:mapped_values] = mapped_values

  unless PIM.is_empty?(mapping.entries)
    mapping.entries.each do |entry|

      # Map only if location is set
      # TODO: Check if it was possible to only use 'external_name' to identify location!
      next if PIM.is_empty?(entry.location)

      value, mapped_value = map_value(item, mapping, entry, context)
      mapped_values << {
        location: entry.location,
        external_name: entry.external_name,
        mapped_value: mapped_value,
        internal_name: entry.internal_name,
        value: value
      }

    end
  end

  MappedItem.new item: item, mapped_values: mapped_values

end
map_value(item, mapping, entry, data_module, context = {}) click to toggle source
# File mapping-exporter.rb, line 350
def map_value item, mapping, entry, data_module, context = {}

  cache = PIM.get_hash_value(context, :cache, {})
  data_module = PIM.get_hash_value(context, :data_module, PIM.active_module)
  export_context = PIM.get_hash_value(context, :export_context, {})
  language = PIM.get_value(export_context, :language)
  mapped_values = PIM.get_hash_value(context, :mapped_values, [])

  value = PIM.get_value(item, entry.internal_name)
  attribute = data_module.attribute(entry.internal_name)

  # Retrieve the mapping function, if any
  mapping_function_name = entry.mapping_function
  if PIM.is_empty?(mapping_function_name)

    # No mapping necessary
    mapped_value = value

  else

    mapping_function = data_module.mapping_function(mapping_function_name)
    if mapping_function.nil?

      # Mapping function not found
      error_msg = "Mapping function '#{mapping_function_name}' to apply to '#{entry.internal_name}' not found"
      PIM.log_error(error_msg)
      mapped_value = "[ERROR] #{error_msg}"

    else

      # Convert the mapping parameters, if necessary
      cache[:converted_param_values] ||= {}
      converted_param_values = cache[:converted_param_values][entry.object_id]
      if converted_param_values.nil?
        converted_param_values = mapping_function.convert_parameter_values(entry.mapping_function_params)
        cache[:converted_param_values][entry.object_id] = converted_param_values
      end

      # Map the value
      mapping_context = MappingFunctionContext.new(
        value: value,
        attribute: attribute,
        item: item,
        mapping: mapping,
        mapped_values: mapped_values,
        mapping_function: mapping_function,
        entry: entry,
        parameter_values: converted_param_values,
        language: language,
        cache: cache,
        data_module: data_module,
        export_context: export_context
      )
      mapped_value = mapping_function.execute(mapping_context)

    end

  end

  return value, mapped_value

end
mapped_items_enum(items, mapping, context = {}) { |*mapped_values| ... } click to toggle source
# File mapping-exporter.rb, line 301
def mapped_items_enum items, mapping, context = {}
  Enumerator::Lazy.new(items) do |yielder, *values|

    mapped_values = []
    values.each do |value|
      value = create_mapped_item(value, mapping, context) unless value.is_a?(MappedItem)
      if block_given?
        mapped_values << value
      else
        yielder << value
      end
    end

    unless mapped_values.empty?
      result = yield *mapped_values
      yielder << result if result
    end

  end
end
mapping_export(items, output, export_context) click to toggle source
# File mapping-exporter.rb, line 413
def mapping_export items, output, export_context

  mapping = PIM.get_value(export_context, :mapping)
  raise "Mapping must be specified" if mapping.nil?

  template_url = mapping.template_url
  asset_key = PIM::Services::AssetService.to_asset_key(template_url)
  raise "Template url '#{template_url}' is not yet supported" if asset_key.nil?

  template_asset = PIM::Services::AssetService.get_digital_asset(asset_key.gathering_key, asset_key.path)
  raise "Asset '#{asset_key}' for template '#{template_url}' does not exist" if template_asset.nil?

  PIM.log_debug "Output has class: #{output}"
  PIM.log_debug "Output has methods: #{output.methods.sort}"

  # Find matching export document, if any
  # TODO: Support more export documents, e.g. PDF
  export_document = ExportDocument.create_export_document(mapping, template_asset, output)
  raise "Cannot handle mapping '#{mapping.name}' with template content type '#{template_asset.content_type}' in asset '#{asset_key}'" if export_document.nil?

  PIM.log_info "Starting mapping export using '#{export_document.class}' with content type '#{export_document.output_content_type}'"
  export_document.start

  context = {
    export_context: export_context
  }
  mapped_items_enum(items, mapping, context).each do |mapped_item|
    export_document.write_mapped_item(mapped_item)
  end

  export_document.stop
  PIM.log_info "Finished mapping export using '#{export_document.class}' with content type '#{export_document.output_content_type}'"

  return export_document.output_content_type

end