require_relative 'converter'
class SubjectConverter < Converter
  require_relative 'lib/csv_converter'
  include ASpaceImport::CSVConvert


  def self.import_types(show_hidden = false)
    [
     {
       :name => "subject_csv",
       :description => "Import Subject records from a CSV file"
     }
    ]
  end


  def self.instance_for(type, input_file)
    if type == "subject_csv"
      self.new(input_file)
    else
      nil
    end
  end


  def self.configure
    {
      # 1. Map the cell data to schemas or handlers
      'subject_authority_id' => 'subject.authority_id',
      'subject_source' => 'subject.source',
      'subject_scope_note' => 'subject.scope_note',

      'term_1' => 'term_1.term',
      'term_type_1' => 'term_1.term_type',

      'term_2' => 'term_2.term',
      'term_type_2' => 'term_2.term_type',

      'term_3' => 'term_3.term',
      'term_type_3' => 'term_3.term_type',

      'term_4' => 'term_4.term',
      'term_type_4' => 'term_4.term_type',

      'term_5' => 'term_5.term',
      'term_type_5' => 'term_5.term_type',

      'external_document_title' => 'external_document.title',
      'external_document_location' => 'external_document.location',
      'external_document_publish' => 'external_document.publish',

      'md_rights_dec_license' => 'md_rights.license',
      'md_rights_dec_descriptive_note' => 'md_rights.descriptive_note',
      'md_rights_dec_file_uri' => 'md_rights.file_uri',
      'md_rights_dec_xlink_actuate' => 'md_rights.file_version_xlink_actuate_attribute',
      'md_rights_dec_xlink_show' => 'md_rights.file_version_xlink_show_attribute',
      'md_rights_dec_xlink_role' => 'md_rights.file_version_xlink_role_attribute',
      'md_rights_dec_xlink_title' => 'md_rights.file_version_xlink_title_attribute',
      'md_rights_dec_xlink_arcrole' => 'md_rights.file_version_xlink_arcrole_attribute',
      'md_rights_dec_last_verified_date' => 'md_rights.last_verified_date',


      # 2. Define data handlers
      #    :record_type of the schema (if other than the handler key)
      #    :defaults - hash which maps property keys to default values if nothing shows up in the source date
      #    :on_row_complete - Proc to run whenever a row in the CSV table is complete
      #        param 1 is the set of objects generated by the row
      #        param 2 is an object in the row (of the type described in the handler)

      :subject => {
        :record_type => :subject,
        :on_create => Proc.new {|cache, obj|
          obj.vocabulary = '/vocabularies/1'
        },
        :on_row_complete => Proc.new {|cache, obj|
        },
      },

      :external_document => {
        :record_type => :external_document,
        :on_create => Proc.new {|cache, obj|
        },
        :on_row_complete => Proc.new {|cache, doc|
          cache.select {|obj| obj.class.record_type == 'subject'}.each do |subject|
            subject.external_documents << doc
          end
        },
      },

      :md_rights => {
        :record_type => :metadata_rights_declaration,
        :on_create => Proc.new {|cache, obj|
        },
        :on_row_complete => Proc.new {|cache, rights|
          cache.select {|obj| obj.class.record_type == 'subject'}.each do |subject|
            subject.metadata_rights_declarations << rights
          end
        },
      },

      :term_1 => {
        :record_type => :term,
        :on_create => Proc.new {|cache, obj|
          obj.vocabulary = '/vocabularies/1'
        },
        :on_row_complete => Proc.new {|cache, term|
          cache.select {|obj| obj.class.record_type == 'subject'}.each do |subject|
            subject.terms << term
          end
        },
      },

      :term_2 => {
        :record_type => :term,
        :on_create => Proc.new {|cache, obj|
          obj.vocabulary = '/vocabularies/1'
        },
        :on_row_complete => Proc.new {|cache, term|
          cache.select {|obj| obj.class.record_type == 'subject'}.each do |subject|
            subject.terms << term
          end
        },
      },

      :term_3 => {
        :record_type => :term,
        :on_create => Proc.new {|cache, obj|
          obj.vocabulary = '/vocabularies/1'
        },
        :on_row_complete => Proc.new {|cache, term|
          cache.select {|obj| obj.class.record_type == 'subject'}.each do |subject|
            subject.terms << term
          end
        },
      },

      :term_4 => {
        :record_type => :term,
        :on_create => Proc.new {|cache, obj|
          obj.vocabulary = '/vocabularies/1'
        },
        :on_row_complete => Proc.new {|cache, term|
          cache.select {|obj| obj.class.record_type == 'subject'}.each do |subject|
            subject.terms << term
          end
        },
      },

      :term_5 => {
        :record_type => :term,
        :on_create => Proc.new {|cache, obj|
          obj.vocabulary = '/vocabularies/1'
        },
        :on_row_complete => Proc.new {|cache, term|
          cache.select {|obj| obj.class.record_type == 'subject'}.each do |subject|
            subject.terms << term
          end
        },
      },
    }
  end
end
