module PIM::Services::DataModelService

Public Instance Methods

__data_model_service() click to toggle source
# File services.rb, line 2226
def __data_model_service
  __service(:dataModelService)
end
__load(data_module, name, **kwargs) click to toggle source
# File services.rb, line 2221
def __load data_module, name, **kwargs
  PIM::Services::DataModelLoaders.Load data_module, name, **kwargs
end
add_all(array, values) click to toggle source
# File services.rb, line 2081
def add_all array, values
  return if PIM.is_empty?(values)
  values = [values] unless PIM.is_array?(values)
  values.each do |value|
    array << value unless value.nil?
  end
end
create_attributes(data_module, attribute_names) click to toggle source
# File services.rb, line 2051
def create_attributes data_module, attribute_names

  # Only get and create attributes which do not exist yet
  attribute_names = attribute_names.reject { |a| data_module.exists_attribute?(a) }
  return if PIM.is_empty?(attribute_names)

  # Load data model attributes from Java backend
  data_model_attributes = get_data_model_attributes(data_module, *attribute_names)

  # Load and create sub attributes, if necessary
  sub_attribute_names = []
  data_model_attributes.each do |data_model_attribute|
    add_all(sub_attribute_names, PIM.get_value(data_model_attribute, :members))
    add_all(sub_attribute_names, PIM.get_value(data_model_attribute, :key_members))
    add_all(sub_attribute_names, PIM.get_value(data_model_attribute, :value_members))
    params = PIM.get_value(data_model_attribute, :params)
    if params
      params = PIM.symbolized_snakeized_hash(params)
      add_all(sub_attribute_names, PIM.get_value(params, :value_attribute))
    end
  end
  create_attributes data_module, sub_attribute_names if not sub_attribute_names.empty?

  # Create Ruby attributes
  data_model_attributes.each do |data_model_attribute|
    data_module.create_attribute(data_model_attribute)
  end

end
create_validation_rules(data_module, rule_names: nil, tags: nil, attributes: nil) click to toggle source
# File services.rb, line 2134
def create_validation_rules data_module, rule_names: nil, tags: nil, attributes: nil

  if not PIM.is_empty?(rule_names)

    # Only get and create validation rules which do not exist yet
    rule_names = rule_names.reject { |v| data_module.exists_validation_rule?(v) }
    return [] if rule_names.empty?

    # Get all missing validation rules
    validation_rules = nil
    PIM::Utils.timed(group: "validation",
                     key: "create_validation_rules-load",
                     message: "Loaded #{rule_names.size} validation rule(s)") do
      validation_rules = get_validation_rules(data_module, *rule_names)
    end

    # Create Ruby validation rules
    PIM::Utils.timed(group: "validation",
                     key: "create_validation_rules-create",
                     message: "Created #{validation_rules.size} validation rule(s)") do
      validation_rules.each do |validation_rule|
        data_module.create_validation_rule(validation_rule)
      end
    end

    return validation_rules

  elsif not PIM.is_empty?(tags) or not PIM.is_empty?(attributes)

    # Determine the rule names to create
    found_rule_names = nil
    PIM::Utils.timed(group: "validation",
                     key: "create_validation_rules-get",
                     message: proc { "Got #{found_rule_names.size} validation rules name(s)" }) do
      found_rule_names = get_validation_rule_names_by_tags_and_attributes(data_module, tags: tags, attributes: attributes)
    end

    return [] if found_rule_names.nil? or found_rule_names.empty?

    # Actually create the rules, if necessary
    return create_validation_rules(data_module, rule_names: found_rule_names)

  else

    return []

  end

end
find_category(data_module, value) click to toggle source

Search for the category specified by ‘value’ in a specific data model and create it, if found This method will query the backend for actual category and attribute definitions.

Example:

find_category Gdsn3GpcModel, ‘10003959’

This will search for a category by the name of code of ‘10003959’ in the data model ‘Gdsn3GpcModel’. For this to work, the data model must be loaded beforehand!

# File services.rb, line 1998
def find_category data_module, value

  PIM.log_trace "find_category: #{data_module} -> #{value}"

  # Check if both parameters are not empty
  return nil if data_module.nil? or PIM.is_empty?(value)

  # Check if category already exists
  category = data_module.category(value)
  return category unless category.nil?

  # Check if value has already been queried and no category could be found
  if is_non_existing_category?(data_module, value)
    PIM.log_trace "find_category: category #{value} was already set as non existing"
    return nil
  end

  # Load data model category from Java backend and create Ruby attributes and category
  data_model_category = get_data_model_category(data_module, value)
  PIM.log_trace "find_category: found: #{data_model_category}"
  if data_model_category

    # If category was found for a 'different' name, then check again if it already exists
    category_name = PIM.get_value(data_model_category, 'name')
    if category_name != value
      category = data_module.category(category_name)
      PIM.log_trace "find_category: found #{data_model_category} for category name '#{category_name}'"
      return category unless category.nil?
    end

    # Ensure, parent categories are created first if necessary
    parent_value = PIM.get_value(data_model_category, 'parent')
    parent_category = find_category(data_module, parent_value)
    if parent_category.nil?
      PIM.log_warn "Cannot create category '#{category_name}' because parent '#{parent_value}' does not exist"
      return nil
    end

    attributes = PIM.get_value(data_model_category, 'attributes')
    create_attributes(data_module, attributes) unless PIM.is_empty?(attributes)
    category = data_module.create_category(data_model_category)
    PIM.log_trace "find_category: created category #{category.as_json}"

  end

  return category
end
get_data_model_attributes(data_module, *attribute_names) click to toggle source
# File services.rb, line 2089
def get_data_model_attributes data_module, *attribute_names

  # Use legacy method first
  return data_module.get_data_model_attributes(*attribute_names) if data_module.respond_to?(:get_data_model_attributes)

  result = __load(data_module, :attributes, keys: attribute_names)
  return result.values.compact
end
get_data_model_categories(data_module, *category_names) click to toggle source
# File services.rb, line 2103
def get_data_model_categories data_module, *category_names

  # Use legacy method first
  return data_module.get_data_model_categories(*category_names) if data_module.respond_to?(:get_data_model_categories)

  result = __load(data_module, :categories, keys: category_names)
  return result.values.compact
end
get_data_model_category(data_module, category_name) click to toggle source
# File services.rb, line 2098
def get_data_model_category data_module, category_name
  categories = get_data_model_categories(data_module, category_name)
  return categories.first unless categories.nil?
end
get_data_model_options(data_module, option_list_name, *option_names) click to toggle source
# File services.rb, line 2129
def get_data_model_options data_module, option_list_name, *option_names
  result = __load(data_module, :option_list_options, keys: option_names, parent: option_list_name)
  return result.values.compact
end
get_validation_rule_names_by_attributes(data_module, *attributes) click to toggle source
# File services.rb, line 2194
def get_validation_rule_names_by_attributes data_module, *attributes
  result = __load(data_module, :validation_rules_by_attributes, keys: attributes)
  return result.compact
end
get_validation_rule_names_by_tags(data_module, *tags) click to toggle source
# File services.rb, line 2189
def get_validation_rule_names_by_tags data_module, *tags
  result = __load(data_module, :validation_rules_by_tags, keys: tags)
  return result.compact
end
get_validation_rule_names_by_tags_and_attributes(data_module, tags: nil, attributes: nil) click to toggle source
# File services.rb, line 2199
def get_validation_rule_names_by_tags_and_attributes data_module, tags: nil, attributes: nil

  # Ensure at least one parameter is not nil or empty
  if (tags.nil? or tags.empty?) and (attributes.nil? or attributes.empty?)
    return []
  end

  if tags.nil? or tags.empty?
    result = __load(data_module, :validation_rules_by_attributes, keys: attributes)
  elsif attributes.nil? or attributes.empty?
    result = __load(data_module, :validation_rules_by_tags, keys: tags)
  else
    result = __load(data_module, :validation_rules_by_tags_and_attributes, tags: tags, attributes: attributes)
  end

  return result.compact
end
get_validation_rules(data_module, *validation_rule_names) click to toggle source
# File services.rb, line 2184
def get_validation_rules data_module, *validation_rule_names
  result = __load(data_module, :validation_rules, keys: validation_rule_names)
  return result.values.compact
end
has_data_model_option?(data_module, option_list_name, option_name, *option_groups) click to toggle source
# File services.rb, line 2112
def has_data_model_option? data_module, option_list_name, option_name, *option_groups

  # Load option
  result = __load(data_module, :option_list_options, keys: Array(option_name), parent: option_list_name)
  option = result.values.first

  # Return false, if option was not found
  return false if option.nil?

  # Return true, if query is not restricted to option-groups
  return true if option_groups.empty?

  # Have the option check if the group is included
  return option.contains_any_group(*option_groups)

end
is_non_existing_category?(data_module, value) click to toggle source
# File services.rb, line 2046
def is_non_existing_category? data_module, value
  result = __load(data_module, :categories, keys: [value])
  return result[value].nil?
end
is_service_available?() click to toggle source
# File services.rb, line 2217
def is_service_available?
  return (not __data_model_service.nil?)
end