<commit_before>require_dependency "blacklight_cornell_requests/application_controller"

module BlacklightCornellRequests

  class RequestDatabaseException < StandardError
    attr_reader :data

    def initialize(data)
     super
     @data = data
    end
  end

  class RequestController < ApplicationController

    include Blacklight::Catalog # needed for "fetch", replaces "include SolrHelper"
    include Cornell::LDAP

    # This may seem redundant, but it makes it easier to fetch the document from
    # various model classes
    def get_solr_doc doc_id
      resp, document = fetch doc_id
      document
    end

    def auth_magic_request target=''
      session[:cuwebauth_return_path] =  magic_request_path(params[:bibid])
      Rails.logger.debug "es287_log #{__FILE__} #{__LINE__}: #{magic_request_path(params[:bibid]).inspect}"
      redirect_to "#{request.protocol}#{request.host_with_port}/users/auth/saml"
      #magic_request target
    end

    def magic_request target=''

      @id = params[:bibid]
      resp, @document = fetch @id
      @document = @document

      work_metadata = Work.new(@id, @document)
      # Create an array of all the item records associated with the bibid
      items = []
      holdings = JSON.parse(@document['items_json'] || '{}')
      # Items are keyed by the associated holding record
      holdings.each do |h, item_array|
        item_array.each do |i|
          items << Item.new(h, i)
        end
      end
      @volumes = Volume.volumes(items)
      @ti = work_metadata.title

      # When we're entering the request system from a /catalog path, then we're starting
      # fresh — no volume should be pre-selected (or kept in the session). However,
      # if the referer is a different path — i.e., /request/*, then we *do* want to
      # preserve the volume selection; this would be the case if the page is reloaded
      # or the user selects an alternate delivery method for the same item.
      if session[:setvol].nil? && (request.referer && request.referer.exclude?('/request/'))
        session[:volume] = nil
      end
      session[:setvol] = nil

      params[:volume] = session[:volume]
      # Reset session var after use so that we don't get weird results if
      # user goes to another catalog item
      session[:volume] = nil

      # If there's a URL-based volume present, that overrides the session data (this
      # should only happen now if someone is linking into requests from outside the main
      # catalog).
      if params[:enum] || params[:chron] || params[:year]
        params[:volume] = "|#{params[:enum]}|#{params[:chron]}|#{params[:year]}|"
      end

      # If this is a multivol item but no volume has been selected, show the appropriate screen
      if @document['multivol_b'] && params[:volume].blank?
        if @volumes.count > 1
          render 'shared/_volume_select'
          return
        else
          vol = @volumes[0]
          redirect_to '/request' + request.env['PATH_INFO'] + "?enum=#{vol.enum}&chron=#{vol.chron}&year=#{vol.year}"
          return
        end
      end

      # If a volume is selected, drop the items that don't match that volume -- no need
      # to waste time calculating delivery methods for them
      # TODO: This is a horribly inefficient approach. Make it better
      @volumes.each do |v|
        if v.select_option == params[:volume]
          items = v.items
          break
        end
      end

      available_request_methods = DeliveryMethod.enabled_methods
      requester = Patron.new(user)
      borrow_direct = CULBorrowDirect.new(requester, work_metadata)

      # We have the following delivery methods to evaluate (at most) for each item:
      # L2L, BD, ILL, Hold, Recall, Patron-driven acquisition, Purchase request
      # ScanIt, Ask a librarian, ask at circ desk
      #
      # For the voyager methods (L2L, Hold, Recall), we do the following for each item:
      # 1. Get the circ group, patron group, and item type
      # 2. Use those values to look up the request policy
      # 3. Combine (2) with item availability to determine which methods are available
      #
      # For BD, do a single call to the BD API for the bib-level ISBN (NOT for each item)
      #
      # For PDA?
<commit_msg>Fix bug in volume test (DISCOVERYACCESS-4224)<commit_after>require_dependency "blacklight_cornell_requests/application_controller"

module BlacklightCornellRequests

  class RequestDatabaseException < StandardError
    attr_reader :data

    def initialize(data)
     super
     @data = data
    end
  end

  class RequestController < ApplicationController

    include Blacklight::Catalog # needed for "fetch", replaces "include SolrHelper"
    include Cornell::LDAP

    # This may seem redundant, but it makes it easier to fetch the document from
    # various model classes
    def get_solr_doc doc_id
      resp, document = fetch doc_id
      document
    end

    def auth_magic_request target=''
      session[:cuwebauth_return_path] =  magic_request_path(params[:bibid])
      Rails.logger.debug "es287_log #{__FILE__} #{__LINE__}: #{magic_request_path(params[:bibid]).inspect}"
      redirect_to "#{request.protocol}#{request.host_with_port}/users/auth/saml"
      #magic_request target
    end

    def magic_request target=''

      @id = params[:bibid]
      resp, @document = fetch @id
      @document = @document

      work_metadata = Work.new(@id, @document)
      # Create an array of all the item records associated with the bibid
      items = []
      holdings = JSON.parse(@document['items_json'] || '{}')
      # Items are keyed by the associated holding record
      holdings.each do |h, item_array|
        item_array.each do |i|
          items << Item.new(h, i)
        end
      end
      @ti = work_metadata.title

      # When we're entering the request system from a /catalog path, then we're starting
      # fresh — no volume should be pre-selected (or kept in the session). However,
      # if the referer is a different path — i.e., /request/*, then we *do* want to
      # preserve the volume selection; this would be the case if the page is reloaded
      # or the user selects an alternate delivery method for the same item.
      if session[:setvol].nil? && (request.referer && request.referer.exclude?('/request/'))
        session[:volume] = nil
      end
      session[:setvol] = nil

      params[:volume] = session[:volume]
      # Reset session var after use so that we don't get weird results if
      # user goes to another catalog item
      session[:volume] = nil

      # If there's a URL-based volume present, that overrides the session data (this
      # should only happen now if someone is linking into requests from outside the main
      # catalog).
      if params[:enum] || params[:chron] || params[:year]
        params[:volume] = "|#{params[:enum]}|#{params[:chron]}|#{params[:year]}|"
      end

      # If this is a multivol item but no volume has been selected, show the appropriate screen
      if @document['multivol_b'] 
        @volumes = Volume.volumes(items)
        if params[:volume].blank?
          if @volumes.count > 1
            render 'shared/_volume_select'
            return
          else
            vol = @volumes[0]
            redirect_to '/request' + request.env['PATH_INFO'] + "?enum=#{vol.enum}&chron=#{vol.chron}&year=#{vol.year}"
            return
          end
        end
      end

      # If a volume is selected, drop the items that don't match that volume -- no need
      # to waste time calculating delivery methods for them
      # TODO: This is a horribly inefficient approach. Make it better
      if @volumes
        @volumes.each do |v|
          if v.select_option == params[:volume]
            items = v.items
            break
          end
        end
      end

      available_request_methods = DeliveryMethod.enabled_methods
      requester = Patron.new(user)
      borrow_direct = CULBorrowDirect.new(requester, work_metadata)

      # We have the following delivery methods to evaluate (at most) for each item:
      # L2L, BD, ILL, Hold, Recall, Patron-driven acquisition, Purchase request
      # ScanIt, Ask a librarian, ask at circ desk
      #
      # For the voyager methods (L2L, Hold, Recall), we do the following for each item:
      # 1. Get the circ group, patron group, and item type
      # 2. Use those values to look up the request policy
      # 3. Combine (2) with item availability to determine which methods are available
      #
      # For BD, do a single call to the BD API for the bib-level ISBN (NOT for each item)
      #
      # For PDA?

{'fraction_non_alphanumeric': 0.08930017351069983, 'fraction_numerical': 0.0030075187969924814, 'mean_word_length': 2.6728971962616823, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 5, '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': '15537', 'n_tokens_mistral': 2467, 'n_tokens_neox': 2367, 'n_words': 886}