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,
  :gift,

  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)


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
150
151
152
153
154
# File 'lib/queries/loan/filter.rb', line 121

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)
  @gift = boolean_param(params, :gift)

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

Instance Attribute Details

#descendantsObject

Returns the value of attribute descendants.



118
119
120
# File 'lib/queries/loan/filter.rb', line 118

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



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

def documentation
  @documentation
end

#end_date_closedObject

Returns the value of attribute end_date_closed.



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

def end_date_closed
  @end_date_closed
end

#end_date_receivedObject

Returns the value of attribute end_date_received.



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

def end_date_received
  @end_date_received
end

#end_date_requestedObject

Returns the value of attribute end_date_requested.



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

def end_date_requested
  @end_date_requested
end

#end_date_return_expectedObject

Returns the value of attribute end_date_return_expected.



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

def end_date_return_expected
  @end_date_return_expected
end

#end_date_sentObject

Returns the value of attribute end_date_sent.



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

def end_date_sent
  @end_date_sent
end

#giftBoolean?

Returns:

  • (Boolean, nil)


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

def gift
  @gift
end

#loan_idObject

Returns the value of attribute loan_id.



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

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)


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

def loan_item_disposition
  @loan_item_disposition
end

#otu_idArray

See also role

Returns:

  • (Array)

    one per of Otu#id



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

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



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

def overdue
  @overdue
end

#person_idArray

See also role

Returns:

  • (Array)

    one per of Person#id



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

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)


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

def role
  @role
end

#start_date_closedObject

Returns the value of attribute start_date_closed.



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

def start_date_closed
  @start_date_closed
end

#start_date_receivedObject

Returns the value of attribute start_date_received.



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

def start_date_received
  @start_date_received
end

#start_date_requestedObject

Returns the value of attribute start_date_requested.



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

def start_date_requested
  @start_date_requested
end

#start_date_return_expectedObject

Returns the value of attribute start_date_return_expected.



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

def start_date_return_expected
  @start_date_return_expected
end

#start_date_sentObject

Returns the value of attribute start_date_sent.



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

def start_date_sent
  @start_date_sent
end

#taxon_name_idObject

not done



117
118
119
# File 'lib/queries/loan/filter.rb', line 117

def taxon_name_id
  @taxon_name_id
end

#with_date_closedBoolean?

Returns whether value is present.

Returns:

  • (Boolean, nil)

    whether value is present



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

def with_date_closed
  @with_date_closed
end

#with_date_receivedBoolean?

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_received
  @with_date_received
end

#with_date_requestedBoolean?

Returns:

  • (Boolean, nil)


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

def with_date_requested
  @with_date_requested
end

#with_date_return_expectedBoolean?

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_return_expected
  @with_date_return_expected
end

#with_date_sentBoolean?

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_sent
  @with_date_sent
end

Instance Method Details

#and_clausesObject



364
365
366
367
368
369
370
371
372
373
# File 'lib/queries/loan/filter.rb', line 364

def and_clauses
  [
    gift_facet,
    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



354
355
356
357
358
359
360
361
362
# File 'lib/queries/loan/filter.rb', line 354

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



242
243
244
245
246
247
# File 'lib/queries/loan/filter.rb', line 242

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



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

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



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

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



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

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



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

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



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

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

#gift_facetObject



295
296
297
298
299
300
301
302
# File 'lib/queries/loan/filter.rb', line 295

def gift_facet
  return nil if gift.nil?
  if gift
    table[:is_gift].eq(true)
  else
    table[:is_gift].eq(nil)
  end
end

#loan_item_disposition_facetObject



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

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



375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
# File 'lib/queries/loan/filter.rb', line 375

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



331
332
333
334
# File 'lib/queries/loan/filter.rb', line 331

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

#otu_query_facetObject



336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
# File 'lib/queries/loan/filter.rb', line 336

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



249
250
251
252
253
254
255
256
# File 'lib/queries/loan/filter.rb', line 249

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

#person_role_facetObject



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

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



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
208
209
210
211
212
# File 'lib/queries/loan/filter.rb', line 181

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



322
323
324
325
326
327
328
329
# File 'lib/queries/loan/filter.rb', line 322

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



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

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



277
278
279
280
281
282
283
284
# File 'lib/queries/loan/filter.rb', line 277

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



304
305
306
307
308
309
310
311
# File 'lib/queries/loan/filter.rb', line 304

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



286
287
288
289
290
291
292
293
# File 'lib/queries/loan/filter.rb', line 286

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