class Eco::API::UseCases::Default::Locations::TagtreeUpload < Eco::API::UseCases::GraphQL::Samples::Location::Command require_relative 'cli/tagtree_upload_cli' name "tagtree-upload" type :other COMMANDS_PER_PAGE = 45 TOP_ID = nil def track_current_tree? false end # We only define the `:insert` stage def inputs(force_continue: force_continue?) {}.tap do |stages| stages[:insert] = input(insert_commands, force_continue: force_continue) end.each do |stage, input| yield(input, stage) if block_given? end end def insert_commands(tree = input_tree, pid: nil) [].tap do |comms| unless tree.top? pid = top_id if top_id? && pid.nil? comms << insert_command(tree, pid: pid) unless top_id?(tree.id) pid = tree.id end tree.nodes.map do |node| insert_commands(node, pid: pid) end.flatten(1).tap do |subs| comms.concat(subs) end end end def insert_command(node, pid: nil) { insert: { nodeId: node.id, name: node.name, parentId: pid }.tap do |node_hash| classification_ids = to_classification_ids(node.classifications) next unless classification_ids.any? node_hash.merge!({ classificationIds: node.classifications.dup }) end } end def top_id?(node_id = nil) return top_id.is_a?(String) if node_id.nil? node_id == top_id end def top_id options.dig(:input, :top_id) || self.class::TOP_ID end def input_tree @input_tree ||= Eco::API::Organization::TagTree.new(tree_json).tap do |tree| log(:info) { "Loaded input locations structure (#{tree.count} nodes)" } end end def tree_json hash_tree(input_csv) end def input_csv @input_csv ||= csv_from(input_file).tap do |table| log(:info) { "#{table.count} data rows loaded from #{input_file}" } end end def input_file @input_file ||= options.dig(:source, :file) end end