Class: Queries::Loan::Filter

Inherits:
Query::Filter show all
Includes:
Concerns::Attributes, Concerns::DataAttributes, 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

#anatomical_part_query, #api, #asserted_distribution_query, #biological_association_query, #biological_associations_graph_query, #collecting_event_query, #collection_object_query, #content_query, #controlled_vocabulary_term_query, #conveyance_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, #roll_call, #sound_query, #taxon_name_query, #taxon_name_relationship_query, #venn, #venn_ignore_pagination, #venn_mode

Attributes inherited from Query

#query_string, #terms

Instance Method Summary collapse

Methods included from Helpers

#boolean_param, #integer_param, #split_pairs, #split_repeated_pairs, #tri_value_array

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, #disable_paging, included_annotator_facets, instantiated_base_filter, inverted_subqueries, #model_id_facet, #object_global_id_facet, #only_project?, #only_project_or_less?, #paging_state, params, #permitted_params, #process_url_into_params, #project_id_facet, query_name, #set_nested_queries, #set_paging, set_paging, #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)


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
155
156
# File 'lib/queries/loan/filter.rb', line 122

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_data_attributes_params(params)
  set_notes_params(params)
  set_tags_params(params)
end

Instance Attribute Details

#descendantsObject

Returns the value of attribute descendants.



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

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



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

def documentation
  @documentation
end

#end_date_closedObject

Returns the value of attribute end_date_closed.



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

def end_date_closed
  @end_date_closed
end

#end_date_receivedObject

Returns the value of attribute end_date_received.



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

def end_date_received
  @end_date_received
end

#end_date_requestedObject

Returns the value of attribute end_date_requested.



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

def end_date_requested
  @end_date_requested
end

#end_date_return_expectedObject

Returns the value of attribute end_date_return_expected.



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

def end_date_return_expected
  @end_date_return_expected
end

#end_date_sentObject

Returns the value of attribute end_date_sent.



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

def end_date_sent
  @end_date_sent
end

#giftBoolean?

Returns:

  • (Boolean, nil)


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

def gift
  @gift
end

#loan_idObject

Returns the value of attribute loan_id.



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

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)


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

def loan_item_disposition
  @loan_item_disposition
end

#otu_idArray

See also role

Returns:

  • (Array)

    one per of Otu#id



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

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



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

def overdue
  @overdue
end

#person_idArray

See also role

Returns:

  • (Array)

    one per of Person#id



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

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)


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

def role
  @role
end

#start_date_closedObject

Returns the value of attribute start_date_closed.



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

def start_date_closed
  @start_date_closed
end

#start_date_receivedObject

Returns the value of attribute start_date_received.



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

def start_date_received
  @start_date_received
end

#start_date_requestedObject

Returns the value of attribute start_date_requested.



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

def start_date_requested
  @start_date_requested
end

#start_date_return_expectedObject

Returns the value of attribute start_date_return_expected.



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

def start_date_return_expected
  @start_date_return_expected
end

#start_date_sentObject

Returns the value of attribute start_date_sent.



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

def start_date_sent
  @start_date_sent
end

#taxon_name_idObject

not done



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

def taxon_name_id
  @taxon_name_id
end

#with_date_closedBoolean?

Returns whether value is present.

Returns:

  • (Boolean, nil)

    whether value is present



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

def with_date_closed
  @with_date_closed
end

#with_date_receivedBoolean?

Returns whether value is present.

Returns:

  • (Boolean, nil)

    whether value is present



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

def with_date_received
  @with_date_received
end

#with_date_requestedBoolean?

Returns:

  • (Boolean, nil)


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

def with_date_requested
  @with_date_requested
end

#with_date_return_expectedBoolean?

Returns whether value is present.

Returns:

  • (Boolean, nil)

    whether value is present



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

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



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

def with_date_sent
  @with_date_sent
end

Instance Method Details

#and_clausesObject



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

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



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

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



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

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



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

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



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

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



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

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



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

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



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

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

#gift_facetObject



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

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



269
270
271
272
# File 'lib/queries/loan/filter.rb', line 269

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



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

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



333
334
335
336
# File 'lib/queries/loan/filter.rb', line 333

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

#otu_query_facetObject



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

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



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

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

#person_role_facetObject



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

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



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
213
214
# File 'lib/queries/loan/filter.rb', line 183

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



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

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



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

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



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

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



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

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



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

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