Class: Queries::Loan::Filter

Inherits:
Query::Filter show all
Includes:
Concerns::Attributes, Concerns::Notes, Concerns::Tags, Helpers
Defined in:
lib/queries/loan/filter.rb

Constant Summary collapse

ATTRIBUTES =
::Loan.core_attributes.map(&:to_sym).freeze
PARAMS =
[
  *ATTRIBUTES,
  :descendants,
  :documentation, # TODO: concern?
  :end_date_closed,
  :end_date_received,
  :end_date_requested,
  :end_date_return_expected,
  :end_date_sent,
  :loan_id,
  :loan_item_disposition,
  :overdue,
  :person_id,
  :role,
  :start_date_closed,
  :start_date_received,
  :start_date_requested,
  :start_date_return_expected,
  :start_date_sent,
  :taxon_name_id,

  loan_id: [],
  loan_item_disposition: [],
  person_id: [],
  role: [],
  taxon_name_id: [],
].freeze

Constants inherited from Query::Filter

Query::Filter::FILTER_QUERIES, Query::Filter::SUBQUERIES

Instance Attribute Summary collapse

Attributes inherited from Query::Filter

#api, #asserted_distribution_query, #biological_association_query, #biological_associations_graph_query, #collecting_event_query, #collection_object_query, #content_query, #controlled_vocabulary_term_query, #data_attribute_query, #descriptor_query, #document_query, #dwc_occurrence_query, #extract_query, #field_occurrence_query, #image_query, #loan_query, #object_global_id, #observation_query, #otu_query, #page, #paginate, #params, #per, #person_query, #project_id, #recent, #taxon_name_query, #venn, #venn_mode

Attributes inherited from Query

#query_string, #terms

Instance Method Summary collapse

Methods included from Helpers

#boolean_param

Methods inherited from Query::Filter

#all, #all_and_clauses, #all_merge_clauses, #and_clauses, #annotator_and_clauses, #annotator_merge_clauses, annotator_params, api_except_params, api_excluded_params, #apply_venn, #attribute_exact_facet, base_filter, base_query_name, #deep_permit, included_annotator_facets, instatiated_base_filter, inverted_subqueries, #model_id_facet, #object_global_id_facet, params, #permitted_params, #process_url_into_params, #project_id_facet, query_name, #query_name, #set_nested_queries, #shared_and_clauses, #subquery_vector, #venn_query

Methods inherited from Query

#alphabetic_strings, #alphanumeric_strings, base_name, #base_name, #base_query, #build_terms, #cached_facet, #end_wildcard, #levenshtein_distance, #match_ordered_wildcard_pieces_in_cached, #no_terms?, referenced_klass, #referenced_klass, #referenced_klass_except, #referenced_klass_intersection, #referenced_klass_union, #start_and_end_wildcard, #start_wildcard, #table, #wildcard_pieces

Constructor Details

#initialize(query_params) ⇒ Filter

Returns a new instance of Filter.

Parameters:

  • params (Hash)


86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/queries/loan/filter.rb', line 86

def initialize(query_params)
  super

  @descendants = boolean_param(params, :descendants)
  @documentation = boolean_param(params, :documentation)
  @end_date_closed = params[:end_date_closed]
  @end_date_received = params[:end_date_received]
  @end_date_requested = params[:end_date_requested]
  @end_date_return_expected = params[:end_date_return_expected]
  @end_date_sent = params[:end_date_sent]
  @loan_id = params[:loan_id]
  @loan_item_disposition = params[:loan_item_disposition]
  @overdue = boolean_param(params, :overdue)
  @person_id = params[:person_id]
  @role = params[:role]
  @start_date_closed = params[:start_date_closed]
  @start_date_received = params[:start_date_received]
  @start_date_requested = params[:start_date_requested]
  @start_date_return_expected = params[:start_date_return_expected]
  @start_date_sent = params[:start_date_sent]
  @taxon_name_id = params[:taxon_name_id]

  set_attributes_params(params)
  set_notes_params(params)
  set_tags_params(params)
end

Instance Attribute Details

#descendantsObject

Returns the value of attribute descendants.



83
84
85
# File 'lib/queries/loan/filter.rb', line 83

def descendants
  @descendants
end

#documentationBoolean?

Returns nil - all true - with Documentation false - without Documentation.

Returns:

  • (Boolean, nil)

    nil - all true - with Documentation false - without Documentation



45
46
47
# File 'lib/queries/loan/filter.rb', line 45

def documentation
  @documentation
end

#end_date_closedObject

Returns the value of attribute end_date_closed.



70
71
72
# File 'lib/queries/loan/filter.rb', line 70

def end_date_closed
  @end_date_closed
end

#end_date_receivedObject

Returns the value of attribute end_date_received.



71
72
73
# File 'lib/queries/loan/filter.rb', line 71

def end_date_received
  @end_date_received
end

#end_date_requestedObject

Returns the value of attribute end_date_requested.



72
73
74
# File 'lib/queries/loan/filter.rb', line 72

def end_date_requested
  @end_date_requested
end

#end_date_return_expectedObject

Returns the value of attribute end_date_return_expected.



73
74
75
# File 'lib/queries/loan/filter.rb', line 73

def end_date_return_expected
  @end_date_return_expected
end

#end_date_sentObject

Returns the value of attribute end_date_sent.



74
75
76
# File 'lib/queries/loan/filter.rb', line 74

def end_date_sent
  @end_date_sent
end

#loan_idObject

Returns the value of attribute loan_id.



39
40
41
# File 'lib/queries/loan/filter.rb', line 39

def loan_id
  @loan_id
end

#loan_item_dispositionArray

Parameters:

  • loan_item_disposition (Array, String)

    Match all loans with loan items that have that disposition

Returns:

  • (Array)


68
69
70
# File 'lib/queries/loan/filter.rb', line 68

def loan_item_disposition
  @loan_item_disposition
end

#overdueBoolean?

Returns nil - all true - overdue false - not overdue.

Returns:

  • (Boolean, nil)

    nil - all true - overdue false - not overdue



51
52
53
# File 'lib/queries/loan/filter.rb', line 51

def overdue
  @overdue
end

#person_idArray

See also role

Returns:

  • (Array)

    one per of Person#id



63
64
65
# File 'lib/queries/loan/filter.rb', line 63

def person_id
  @person_id
end

#roleArray

If none provided both returned. Only applied when person_id is present

Parameters:

  • Role (String, nil)

    LoanRecipient, LoanSupervisor

Returns:

  • (Array)


58
59
60
# File 'lib/queries/loan/filter.rb', line 58

def role
  @role
end

#start_date_closedObject

Returns the value of attribute start_date_closed.



75
76
77
# File 'lib/queries/loan/filter.rb', line 75

def start_date_closed
  @start_date_closed
end

#start_date_receivedObject

Returns the value of attribute start_date_received.



76
77
78
# File 'lib/queries/loan/filter.rb', line 76

def start_date_received
  @start_date_received
end

#start_date_requestedObject

Returns the value of attribute start_date_requested.



77
78
79
# File 'lib/queries/loan/filter.rb', line 77

def start_date_requested
  @start_date_requested
end

#start_date_return_expectedObject

Returns the value of attribute start_date_return_expected.



78
79
80
# File 'lib/queries/loan/filter.rb', line 78

def start_date_return_expected
  @start_date_return_expected
end

#start_date_sentObject

Returns the value of attribute start_date_sent.



79
80
81
# File 'lib/queries/loan/filter.rb', line 79

def start_date_sent
  @start_date_sent
end

#taxon_name_idObject

not done



82
83
84
# File 'lib/queries/loan/filter.rb', line 82

def taxon_name_id
  @taxon_name_id
end

Instance Method Details

#collection_object_query_facetObject



252
253
254
255
256
257
258
259
260
# File 'lib/queries/loan/filter.rb', line 252

def collection_object_query_facet
  return nil if collection_object_query.nil?
  s = 'WITH query_co_loan AS (' + collection_object_query.all.to_sql + ') '

  a = ::Loan.joins(:loan_items)
    .joins("JOIN query_co_loan as query_co_loan1 on query_co_loan1.id = loan_items.loan_item_object_id AND loan_items.loan_item_object_type = 'CollectionObject'").to_sql

  ::Loan.from('(' + s + ::Loan.from("(#{a}) as loans").to_sql + ') as loans' ).distinct
end

#date_closed_facetObject



199
200
201
202
203
204
# File 'lib/queries/loan/filter.rb', line 199

def date_closed_facet
  return nil if start_date_closed.nil? && end_date_closed.nil?
  s,e = [start_date_closed, end_date_closed].compact
  e = s if e.nil?
  ::Loan.where(date_closed: (s..e))
end

#date_received_facetObject



185
186
187
188
189
190
# File 'lib/queries/loan/filter.rb', line 185

def date_received_facet
  return nil if start_date_received.nil? && end_date_received.nil?
  s,e = [start_date_received, end_date_received].compact
  e = s if e.nil?
  ::Loan.where(date_received: (s..e))
end

#date_requested_facetObject



171
172
173
174
175
176
# File 'lib/queries/loan/filter.rb', line 171

def date_requested_facet
  return nil if start_date_requested.nil? && end_date_requested.nil?
  s,e = [start_date_requested, end_date_requested].compact
  e = s if e.nil?
  ::Loan.where(date_requested: (s..e))
end

#date_return_expected_facetObject



192
193
194
195
196
197
# File 'lib/queries/loan/filter.rb', line 192

def date_return_expected_facet
  return nil if start_date_return_expected.nil? && end_date_return_expected.nil?
  s,e = [start_date_return_expected, end_date_return_expected].compact
  e = s if e.nil?
  ::Loan.where(date_return_expected: (s..e))
end

#date_sent_facetObject



178
179
180
181
182
183
# File 'lib/queries/loan/filter.rb', line 178

def date_sent_facet
  return nil if start_date_sent.nil? && end_date_sent.nil?
  s,e = [start_date_sent, end_date_sent].compact
  e = s if e.nil?
  ::Loan.where(date_sent: (s..e))
end

#documentation_facetObject



215
216
217
218
219
220
221
222
# File 'lib/queries/loan/filter.rb', line 215

def documentation_facet
  return nil if documentation.nil?
  if documentation
    ::Loan.joins(:documentation)
  else
    ::Loan.where.missing(:documentation)
  end
end

#loan_item_disposition_facetObject



224
225
226
227
# File 'lib/queries/loan/filter.rb', line 224

def loan_item_disposition_facet
  return nil if loan_item_disposition.empty?
  ::Loan.joins(:loan_items).where(loan_items: {disposition: loan_item_disposition}).distinct
end

#merge_clausesObject



262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'lib/queries/loan/filter.rb', line 262

def merge_clauses
  [
    collection_object_query_facet,
    taxon_name_id_facet,
    date_closed_facet,
    date_received_facet,
    date_requested_facet,
    date_return_expected_facet,
    date_sent_facet,
    documentation_facet,
    loan_item_disposition_facet,
    otu_query_facet,
    overdue_facet,
    person_role_facet,
  ]
end

#otu_idObject



130
131
132
# File 'lib/queries/loan/filter.rb', line 130

def otu_id
  [@otu_id].flatten.compact.uniq
end

#otu_query_facetObject



234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/queries/loan/filter.rb', line 234

def otu_query_facet
  return nil if otu_query.nil?
  s = 'WITH query_otu_loan AS (' + otu_query.all.to_sql + ') '

  a = ::Loan.joins(:loan_items)
    .joins("JOIN query_otu_loan as query_otu_loan1 on query_otu_loan1.id = loan_items.loan_item_object_id AND loan_items.loan_item_object_type = 'Otu'").to_sql

  # Consider position = 1
  b = ::Loan.joins(:loan_items)
    .joins("JOIN collection_objects co on co.id = loan_items.loan_item_object_id and loan_items.loan_item_object_type = 'CollectionObject'")
    .joins("JOIN taxon_determinations td on co.id = td.taxon_determination_object_id AND td.taxon_determination_object_type = 'CollectionObject'")
    .joins('JOIN query_otu_loan as query_otu_loan2 ON query_otu_loan2.id = td.otu_id').to_sql

  s << ::Loan.from("((#{a}) UNION (#{b})) as loans").to_sql

  ::Loan.from('(' + s + ') as loans').distinct
end

#overdue_facetObject



206
207
208
209
210
211
212
213
# File 'lib/queries/loan/filter.rb', line 206

def overdue_facet
  return nil if overdue.nil?
  if overdue
    ::Loan.overdue
  else
    ::Loan.not_overdue
  end
end

#person_role_facetObject



229
230
231
232
# File 'lib/queries/loan/filter.rb', line 229

def person_role_facet
  return nil if person_id.empty?
  ::Loan.joins(:roles).where(roles: {type: role, person_id:}).distinct
end

#taxon_name_id_facetObject



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/queries/loan/filter.rb', line 138

def taxon_name_id_facet
  return nil if taxon_name_id.empty?
  a = ::Queries::CollectionObject::Filter.new(taxon_name_id:, descendants:)
  b = ::Queries::Otu::Filter.new(taxon_name_id:, descendants:)

  a.project_id = nil
  b.project_id = nil

  if a.all(true)
    a.project_id = project_id
    c = ::LoanItem.from('(' +
                        'WITH co_tn AS (' + a.all.to_sql + ') ' +
                        ::LoanItem.joins("JOIN co_tn AS co_tn1 on co_tn1.id = loan_items.loan_item_object_id AND loan_items.loan_item_object_type = 'CollectionObject'").to_sql +
                        ') as loan_items'
                       ).to_sql
  end

  if b.all(true)
    b.project_id = project_id
    d = ::LoanItem.from('(' +
                        'WITH otu_tn AS (' + b.all.to_sql + ') ' +
                        ::LoanItem.joins("JOIN otu_tn AS otu_tn1 on otu_tn1.id = loan_items.loan_item_object_id AND loan_items.loan_item_object_type = 'Otu'").to_sql +
                        ') as loan_items'
                       ).to_sql
  end

  e = ::LoanItem.from( '(' + [c,d].compact.join(' UNION ') + ') as loan_items').to_sql

  s = 'WITH items AS (' + e + ') ' + ::Loan.joins('JOIN items as items1 on items1.loan_id = loans.id').to_sql

  ::Loan.from("(#{s}) as loans").distinct
end