<commit_before># This file contains functionality to extract information from a
# DAITSS DIP for use in constructing a TIPR DIP

require 'nokogiri'
require 'time'

class DIP
  
  attr_reader :rel_path, :ieid, :package_id, :create_date, :original_representation, :current_representation, :migration_map
  
  NS = {
    'mets' => 'http://www.loc.gov/METS/',
    'daitss' => 'http://www.fcla.edu/dls/md/daitss/',
    'xlink' => 'http://www.w3.org/1999/xlink'
  }

  def initialize(path)
    @path = path.chomp('/')     # Strip any trailing /es
    @doc = load_descriptor
    @rel_path = load_rel_path
    @ieid = load_ieid
    @package_id = load_package_id
    @create_date = load_create_date
    @dfid_map = load_dfid_map
    @migration_map = load_migration_map
    @original_representation = load_original_representation
    @current_representation = load_current_representation
  end
  
  def events(fid)               # Given a file id, retrieve events
    event_fid = @dfid_map.index(fid)
    @doc.xpath('//daitss:EVENT', NS).select do |event|
      event.xpath('./daitss:OID', NS).first.content == event_fid.to_s
    end
  end
  
  protected
  
  def load_descriptor
    matches = Dir.glob "#{@path}/*/AIP_*_LOC.xml"
    raise 'No descriptor found' if matches.empty?
    raise 'Multiple possible descriptors' if matches.size > 1
    descriptor = matches.first
    open(descriptor) { |io| Nokogiri::XML io }
  end
  
  def load_rel_path             # includes package_id
    matches = Dir.glob "#{@path}/*/AIP_*_LOC.xml"
    dir = File.dirname(matches.first)
    dir.split("DIPs/").last
  end

  def load_ieid
    ieid_node = @doc.xpath('//daitss:IEID', NS).first
    raise "IEID not found" unless ieid_node
    ieid_node.content
  end
  
  def load_package_id
    package_id_node = @doc.xpath('//daitss:ENTITY_ID', NS).first
    raise "PACAKGE ID not found" unless package_id_node
    package_id_node.content
  end

  def load_create_date
    create_date_node = @doc.xpath('//mets:metsHdr/@CREATEDATE', NS).first
    raise "CREATE DATE not found" unless create_date_node
    Time.parse create_date_node.content
  end
  
  def load_dfid_map             # Create a map between DFIDs and structMap FILEIDs
    file_ids = @doc.xpath('//mets:structMap//mets:fptr/@FILEID', NS).map do |file_id_node| 
                 file_id_node.content
               end
    dfid_map = Hash.new
    file_ids.uniq.each do |fid|
      tmd = @doc.xpath("//mets:file[@ID = '#{fid}']", NS).first['ADMID']
      dfid = @doc.xpath("//mets:techMD[@ID = '#{tmd}']//daitss:DFID", NS).first.content<commit_msg>now possible to search for events by oid<commit_after># This file contains functionality to extract information from a
# DAITSS DIP for use in constructing a TIPR DIP

require 'nokogiri'
require 'time'

class DIP
  
  attr_reader :rel_path, :ieid, :package_id, :create_date, :original_representation, :current_representation, :migration_map
  
  NS = {
    'mets' => 'http://www.loc.gov/METS/',
    'daitss' => 'http://www.fcla.edu/dls/md/daitss/',
    'xlink' => 'http://www.w3.org/1999/xlink'
  }

  def initialize(path)
    @path = path.chomp('/')     # Strip any trailing /es
    @doc = load_descriptor
    @rel_path = load_rel_path
    @ieid = load_ieid
    @package_id = load_package_id
    @create_date = load_create_date
    @dfid_map = load_dfid_map
    @migration_map = load_migration_map
    @original_representation = load_original_representation
    @current_representation = load_current_representation
  end
  
  def events_by_oid(oid)        # Given an OID, retrieve events
    @doc.xpath('//daitss:EVENT', NS).select do |event|
      event.xpath('./daitss:OID', NS).first.content == oid
    end
  end
  
  def events(fid)               # Given a file id, retrieve events
    events_by_oid(@dfid_map.index(fid).to_s)
  end
  
  protected
  
  def load_descriptor
    matches = Dir.glob "#{@path}/*/AIP_*_LOC.xml"
    raise 'No descriptor found' if matches.empty?
    raise 'Multiple possible descriptors' if matches.size > 1
    descriptor = matches.first
    open(descriptor) { |io| Nokogiri::XML io }
  end
  
  def load_rel_path             # includes package_id
    matches = Dir.glob "#{@path}/*/AIP_*_LOC.xml"
    dir = File.dirname(matches.first)
    dir.split("DIPs/").last
  end

  def load_ieid
    ieid_node = @doc.xpath('//daitss:IEID', NS).first
    raise "IEID not found" unless ieid_node
    ieid_node.content
  end
  
  def load_package_id
    package_id_node = @doc.xpath('//daitss:ENTITY_ID', NS).first
    raise "PACAKGE ID not found" unless package_id_node
    package_id_node.content
  end

  def load_create_date
    create_date_node = @doc.xpath('//mets:metsHdr/@CREATEDATE', NS).first
    raise "CREATE DATE not found" unless create_date_node
    Time.parse create_date_node.content
  end
  
  def load_dfid_map             # Create a map between DFIDs and structMap FILEIDs
    file_ids = @doc.xpath('//mets:file/@ID', NS).map { |a| a.value }
    
    dfid_map = Hash.new
    
    file_ids.compact.each do |fid|
      tmd = @doc.xpath("//mets:file[@ID = '#{fid}']", NS).first['ADMID']
      dfid = @doc.xpath("//mets:techMD[@ID = '#{tmd}']//daitss:DFID", NS).first.content
{'fraction_non_alphanumeric': 0.12240184757505773, 'fraction_numerical': 0.0023094688221709007, 'mean_word_length': 3.1377388535031847, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 6, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7273211', 'n_tokens_mistral': 1895, 'n_tokens_neox': 1852, 'n_words': 401}