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,
  :otu_id,
  :overdue,
  :person_id,
  :role,
  :start_date_closed,
  :start_date_received,
  :start_date_requested,
  :start_date_return_expected,
  :start_date_sent,
  :taxon_name_id,
  :with_date_closed,
  :with_date_received,
  :with_date_requested,
  :with_date_return_expected,
  :with_date_sent,

  loan_id: [],
  loan_item_disposition: [],
  otu_id: [],
  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, #depiction_query, #descriptor_query, #document_query, #dwc_occurrence_query, #extract_query, #field_occurrence_query, #image_query, #loan_query, #object_global_id, #observation_query, #order_by, #otu_query, #page, #paginate, #params, #per, #person_query, #project_id, #recent, #recent_target, #taxon_name_query, #venn, #venn_mode

Attributes inherited from Query

#query_string, #terms

Instance Method Summary collapse

Methods included from Helpers

#boolean_param, #integer_param

Methods inherited from Query::Filter

#all, #all_and_clauses, #all_merge_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, base_query_to_h, #deep_permit, included_annotator_facets, instantiated_base_filter, inverted_subqueries, #model_id_facet, #object_global_id_facet, #only_project?, params, #permitted_params, #process_url_into_params, #project_id_facet, query_name, #query_name, #set_nested_queries, #shared_and_clauses, #subquery_vector, #target_and_clauses, #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)


117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/queries/loan/filter.rb', line 117

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]
  @otu_id = params[:otu_id]
  @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]

  @with_date_closed = boolean_param(params, :with_date_closed)
  @with_date_received = boolean_param(params, :with_date_received)
  @with_date_requested = boolean_param(params, :with_date_requested)
  @with_date_return_expected = boolean_param(params, :with_date_return_expected)
  @with_date_sent = boolean_param(params, :with_date_sent)

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

Instance Attribute Details

#descendantsObject

Returns the value of attribute descendants.



114
115
116
# File 'lib/queries/loan/filter.rb', line 114

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



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

def documentation
  @documentation
end

#end_date_closedObject

Returns the value of attribute end_date_closed.



101
102
103
# File 'lib/queries/loan/filter.rb', line 101

def end_date_closed
  @end_date_closed
end

#end_date_receivedObject

Returns the value of attribute end_date_received.



102
103
104
# File 'lib/queries/loan/filter.rb', line 102

def end_date_received
  @end_date_received
end

#end_date_requestedObject

Returns the value of attribute end_date_requested.



103
104
105
# File 'lib/queries/loan/filter.rb', line 103

def end_date_requested
  @end_date_requested
end

#end_date_return_expectedObject

Returns the value of attribute end_date_return_expected.



104
105
106
# File 'lib/queries/loan/filter.rb', line 104

def end_date_return_expected
  @end_date_return_expected
end

#end_date_sentObject

Returns the value of attribute end_date_sent.



105
106
107
# File 'lib/queries/loan/filter.rb', line 105

def end_date_sent
  @end_date_sent
end

#loan_idObject

Returns the value of attribute loan_id.



65
66
67
# File 'lib/queries/loan/filter.rb', line 65

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)


99
100
101
# File 'lib/queries/loan/filter.rb', line 99

def loan_item_disposition
  @loan_item_disposition
end

#otu_idArray

See also role

Returns:

  • (Array)

    one per of Otu#id



94
95
96
# File 'lib/queries/loan/filter.rb', line 94

def otu_id
  @otu_id
end

#overdueBoolean?

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

Returns:

  • (Boolean, nil)

    nil - all true - overdue false - not overdue



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

def overdue
  @overdue
end

#person_idArray

See also role

Returns:

  • (Array)

    one per of Person#id



89
90
91
# File 'lib/queries/loan/filter.rb', line 89

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)


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

def role
  @role
end

#start_date_closedObject

Returns the value of attribute start_date_closed.



106
107
108
# File 'lib/queries/loan/filter.rb', line 106

def start_date_closed
  @start_date_closed
end

#start_date_receivedObject

Returns the value of attribute start_date_received.



107
108
109
# File 'lib/queries/loan/filter.rb', line 107

def start_date_received
  @start_date_received
end

#start_date_requestedObject

Returns the value of attribute start_date_requested.



108
109
110
# File 'lib/queries/loan/filter.rb', line 108

def start_date_requested
  @start_date_requested
end

#start_date_return_expectedObject

Returns the value of attribute start_date_return_expected.



109
110
111
# File 'lib/queries/loan/filter.rb', line 109

def start_date_return_expected
  @start_date_return_expected
end

#start_date_sentObject

Returns the value of attribute start_date_sent.



110
111
112
# File 'lib/queries/loan/filter.rb', line 110

def start_date_sent
  @start_date_sent
end

#taxon_name_idObject

not done



113
114
115
# File 'lib/queries/loan/filter.rb', line 113

def taxon_name_id
  @taxon_name_id
end

#with_date_closedBoolean?

Returns whether value is present.

Returns:

  • (Boolean, nil)

    whether value is present



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

def with_date_closed
  @with_date_closed
end

#with_date_receivedBoolean?

Returns whether value is present.

Returns:

  • (Boolean, nil)

    whether value is present



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

def with_date_received
  @with_date_received
end

#with_date_requestedBoolean?

Returns:

  • (Boolean, nil)


47
48
49
# File 'lib/queries/loan/filter.rb', line 47

def with_date_requested
  @with_date_requested
end

#with_date_return_expectedBoolean?

Returns whether value is present.

Returns:

  • (Boolean, nil)

    whether value is present



55
56
57
# File 'lib/queries/loan/filter.rb', line 55

def with_date_return_expected
  @with_date_return_expected
end

#with_date_sentBoolean?

Returns whether value is present.

Returns:

  • (Boolean, nil)

    whether value is present



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

def with_date_sent
  @with_date_sent
end

Instance Method Details

#and_clausesObject



350
351
352
353
354
355
356
357
358
# File 'lib/queries/loan/filter.rb', line 350

def and_clauses
  [
    with_date_closed_facet,
    with_date_received_facet,
    with_date_requested_facet,
    with_date_return_expected_facet,
    with_date_sent_facet,
  ]
end

#collection_object_query_facetObject



340
341
342
343
344
345
346
347
348
# File 'lib/queries/loan/filter.rb', line 340

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_range_facetObject



237
238
239
240
241
242
# File 'lib/queries/loan/filter.rb', line 237

def date_closed_range_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_range_facetObject



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

def date_received_range_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_range_facetObject



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

def date_requested_range_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_range_facetObject



230
231
232
233
234
235
# File 'lib/queries/loan/filter.rb', line 230

def date_return_expected_range_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_range_facetObject



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

def date_sent_range_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



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

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

#loan_item_disposition_facetObject



262
263
264
265
# File 'lib/queries/loan/filter.rb', line 262

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



360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
# File 'lib/queries/loan/filter.rb', line 360

def merge_clauses
  [
    collection_object_query_facet,
    date_closed_range_facet,
    date_received_range_facet,
    date_requested_range_facet,
    date_return_expected_range_facet,
    date_sent_range_facet,
    documentation_facet,
    loan_item_disposition_facet,
    otu_id_facet,
    otu_query_facet,
    overdue_facet,
    person_role_facet,
    taxon_name_id_facet,
  ]
end

#otu_id_facetObject



317
318
319
320
# File 'lib/queries/loan/filter.rb', line 317

def otu_id_facet
  return nil if otu_id.empty?
  ::Queries::Loan::Filter.new(otu_query: {otu_id:}).all
end

#otu_query_facetObject



322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
# File 'lib/queries/loan/filter.rb', line 322

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



244
245
246
247
248
249
250
251
# File 'lib/queries/loan/filter.rb', line 244

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

#person_role_facetObject



267
268
269
270
# File 'lib/queries/loan/filter.rb', line 267

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



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/queries/loan/filter.rb', line 176

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

#with_date_closed_facetObject



308
309
310
311
312
313
314
315
# File 'lib/queries/loan/filter.rb', line 308

def with_date_closed_facet
  return nil if with_date_closed.nil?
  if with_date_closed
    table[:date_closed].not_eq(nil)
  else
    table[:date_closed].eq(nil)
  end
end

#with_date_received_facetObject



299
300
301
302
303
304
305
306
# File 'lib/queries/loan/filter.rb', line 299

def with_date_received_facet
  return nil if with_date_received.nil?
  if with_date_received
    table[:date_received].not_eq(nil)
  else
    table[:date_received].eq(nil)
  end
end

#with_date_requested_facetObject



272
273
274
275
276
277
278
279
# File 'lib/queries/loan/filter.rb', line 272

def  with_date_requested_facet
  return nil if with_date_requested.nil?
  if with_date_requested
    table[:date_requested].not_eq(nil)
  else
    table[:date_requested].eq(nil)
  end
end

#with_date_return_expected_facetObject



290
291
292
293
294
295
296
297
# File 'lib/queries/loan/filter.rb', line 290

def with_date_return_expected_facet
  return nil if with_date_return_expected.nil?
  if with_date_return_expected
    table[:date_return_expected].not_eq(nil)
  else
    table[:date_return_expected].eq(nil)
  end
end

#with_date_sent_facetObject



281
282
283
284
285
286
287
288
# File 'lib/queries/loan/filter.rb', line 281

def  with_date_sent_facet
  return nil if with_date_sent.nil?
  if with_date_sent
    table[:date_sent].not_eq(nil)
  else
    table[:date_sent].eq(nil)
  end
end