<commit_before>module CommonwealthVlrEngine
  module Finder
    #include Blacklight::SearchHelper

    def get_files(pid)

      return_hash = {}
      return_hash[:images] = []
      return_hash[:documents] = []
      return_hash[:audio] = []
      return_hash[:generic] = []

      solr_response = repository.search({:q => "is_file_of_ssim:\"info:fedora/#{pid}\""})

      solr_response.documents.each do |solr_doc|
        if solr_doc['has_model_ssim'].include?('info:fedora/afmodel:Bplmodels_AudioFile')
          return_hash[:audio] << solr_doc
        elsif solr_doc['has_model_ssim'].include?('info:fedora/afmodel:Bplmodels_ImageFile')
          return_hash[:images] << solr_doc
        elsif solr_doc['has_model_ssim'].include?('info:fedora/afmodel:Bplmodels_DocumentFile')
          return_hash[:documents] << solr_doc
        else
          return_hash[:generic] << solr_doc
        end
      end

       return_hash[:images] = sort_files(return_hash[:images])
       return_hash[:documents] = sort_files(return_hash[:documents])
       return_hash[:audio] = sort_files(return_hash[:audio])
       return_hash[:generic] = sort_files(return_hash[:generic])

       return return_hash
    end


    def sort_files(file_list)
      return file_list if file_list.length <= 1

      following_key_final = nil
      preceding_key_final = nil

      ending_item_pid = nil
      next_item_pid = nil

      return_list = []
      file_list.each do |file|
        preceding_key = file.keys.select { |key| key.include?'preceding'}
        following_key = file.keys.select { |key| key.include?'following'}

        if following_key.blank?
          return_list.insert(0, file)
          preceding_key_final = preceding_key.first
          next_item_pid = file[preceding_key_final].first
        elsif preceding_key.blank?
          following_key_final = following_key.first
          return_list.insert(-1, file)
          ending_item_pid  = "info:fedora/#{file['id']}"
        end
      end

      while next_item_pid != ending_item_pid
        next_item = file_list.select { |array| "info:fedora/#{array['id'].to_s}" == next_item_pid }.first
        return_list.insert(-2, next_item)
        next_item_pid = next_item[preceding_key_final].first.to_s
      end

      return return_list
    end

    def get_image_files(pid)
      return_list = []
      solr_response = repository.search({:q => "is_image_of_ssim:\"info\:fedora/#{pid.gsub(':', '\:')}\" AND has_model_ssim:\"info\:fedora/afmodel:Bplmodels_ImageFile\""})

      solr_response.documents.each do |solr_object|
        return_list << solr_object
      end
      return sort_files(return_list)
    end

    def get_audio_files(pid)
      return_list = []
      solr_response = repository.search({:q => "is_audio_of_ssim:\"info\:fedora/#{pid.gsub(':', '\:')} AND has_model_ssim:\"info\:fedora/afmodel:Bplmodels_AudioFile\""})

      solr_response.documents.each do |solr_object|
        return_list << solr_object
      end
      return sort_files(return_list)
    end

    def get_document_files(pid)
      return_list = []
      solr_response = repository.search({:q => "is_document_of_ssim:\"info\:fedora/#{pid.gsub(':', '\:')}\" AND has_model_ssim:\"info\:fedora/afmodel:Bplmodels_DocumentFile\""})

      solr_response.documents.each do |solr_object|
        return_list << solr_object
      end
      return sort_files(return_list)<commit_msg>add :rows param to Solr queries for Finder methods<commit_after>module CommonwealthVlrEngine
  module Finder
    #include Blacklight::SearchHelper

    def get_files(pid)

      return_hash = {}
      return_hash[:images] = []
      return_hash[:documents] = []
      return_hash[:audio] = []
      return_hash[:generic] = []

      solr_response = repository.search({:q => "is_file_of_ssim:\"info:fedora/#{pid}\"", :rows => 1000})

      solr_response.documents.each do |solr_doc|
        if solr_doc['has_model_ssim'].include?('info:fedora/afmodel:Bplmodels_AudioFile')
          return_hash[:audio] << solr_doc
        elsif solr_doc['has_model_ssim'].include?('info:fedora/afmodel:Bplmodels_ImageFile')
          return_hash[:images] << solr_doc
        elsif solr_doc['has_model_ssim'].include?('info:fedora/afmodel:Bplmodels_DocumentFile')
          return_hash[:documents] << solr_doc
        else
          return_hash[:generic] << solr_doc
        end
      end

       return_hash[:images] = sort_files(return_hash[:images])
       return_hash[:documents] = sort_files(return_hash[:documents])
       return_hash[:audio] = sort_files(return_hash[:audio])
       return_hash[:generic] = sort_files(return_hash[:generic])

       return return_hash
    end


    def sort_files(file_list)
      return file_list if file_list.length <= 1

      following_key_final = nil
      preceding_key_final = nil

      ending_item_pid = nil
      next_item_pid = nil

      return_list = []
      file_list.each do |file|
        preceding_key = file.keys.select { |key| key.include?'preceding'}
        following_key = file.keys.select { |key| key.include?'following'}

        if following_key.blank?
          return_list.insert(0, file)
          preceding_key_final = preceding_key.first
          next_item_pid = file[preceding_key_final].first
        elsif preceding_key.blank?
          following_key_final = following_key.first
          return_list.insert(-1, file)
          ending_item_pid  = "info:fedora/#{file['id']}"
        end
      end

      while next_item_pid != ending_item_pid
        next_item = file_list.select { |array| "info:fedora/#{array['id'].to_s}" == next_item_pid }.first
        return_list.insert(-2, next_item)
        next_item_pid = next_item[preceding_key_final].first.to_s
      end

      return return_list
    end

    def get_image_files(pid)
      return_list = []
      solr_response = repository.search({:q => "is_image_of_ssim:\"info\:fedora/#{pid.gsub(':', '\:')}\" AND has_model_ssim:\"info\:fedora/afmodel:Bplmodels_ImageFile\"", :rows => 1000})

      solr_response.documents.each do |solr_object|
        return_list << solr_object
      end
      return sort_files(return_list)
    end

    def get_audio_files(pid)
      return_list = []
      solr_response = repository.search({:q => "is_audio_of_ssim:\"info\:fedora/#{pid.gsub(':', '\:')} AND has_model_ssim:\"info\:fedora/afmodel:Bplmodels_AudioFile\"", :rows => 1000})

      solr_response.documents.each do |solr_object|
        return_list << solr_object
      end
      return sort_files(return_list)
    end

    def get_document_files(pid)
      return_list = []
      solr_response = repository.search({:q => "is_document_of_ssim:\"info\:fedora/#{pid.gsub(':', '\:')}\" AND has_model_ssim:\"info\:fedora/afmodel:Bplmodels_DocumentFile\"", :rows => 1000})

      solr_response.documents.each do |solr_object|
        return_list << solr_object
      end
      return sort_files(return_list)
{'fraction_non_alphanumeric': 0.12425864313612035, 'fraction_numerical': 0.003471719947924201, 'mean_word_length': 3.157546602525556, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '2255500', 'n_tokens_mistral': 2353, 'n_tokens_neox': 2260, 'n_words': 333}