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,
  :with_date_closed,
  :with_date_received,
  :with_date_requested,
  :with_date_return_expected,
  :with_date_sent,

  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, #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, #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)


110
111
112
113
114
115
116
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
# File 'lib/queries/loan/filter.rb', line 110

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]

  @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.



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

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



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

def documentation
  @documentation
end

#end_date_closedObject

Returns the value of attribute end_date_closed.



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

def end_date_closed
  @end_date_closed
end

#end_date_receivedObject

Returns the value of attribute end_date_received.



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

def end_date_received
  @end_date_received
end

#end_date_requestedObject

Returns the value of attribute end_date_requested.



96
97
98
# File 'lib/queries/loan/filter.rb', line 96

def end_date_requested
  @end_date_requested
end

#end_date_return_expectedObject

Returns the value of attribute end_date_return_expected.



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

def end_date_return_expected
  @end_date_return_expected
end

#end_date_sentObject

Returns the value of attribute end_date_sent.



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

def end_date_sent
  @end_date_sent
end

#loan_idObject

Returns the value of attribute loan_id.



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

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)


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

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



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

def overdue
  @overdue
end

#person_idArray

See also role

Returns:

  • (Array)

    one per of Person#id



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

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)


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

def role
  @role
end

#start_date_closedObject

Returns the value of attribute start_date_closed.



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

def start_date_closed
  @start_date_closed
end

#start_date_receivedObject

Returns the value of attribute start_date_received.



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

def start_date_received
  @start_date_received
end

#start_date_requestedObject

Returns the value of attribute start_date_requested.



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

def start_date_requested
  @start_date_requested
end

#start_date_return_expectedObject

Returns the value of attribute start_date_return_expected.



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

def start_date_return_expected
  @start_date_return_expected
end

#start_date_sentObject

Returns the value of attribute start_date_sent.



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

def start_date_sent
  @start_date_sent
end

#taxon_name_idObject

not done



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

def taxon_name_id
  @taxon_name_id
end

#with_date_closedBoolean?

Returns whether value is present.

Returns:

  • (Boolean, nil)

    whether value is present



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

def with_date_closed
  @with_date_closed
end

#with_date_receivedBoolean?

Returns whether value is present.

Returns:

  • (Boolean, nil)

    whether value is present



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

def with_date_received
  @with_date_received
end

#with_date_requestedBoolean?

Returns:

  • (Boolean, nil)


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

def with_date_requested
  @with_date_requested
end

#with_date_return_expectedBoolean?

Returns whether value is present.

Returns:

  • (Boolean, nil)

    whether value is present



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

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



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

def with_date_sent
  @with_date_sent
end

Instance Method Details

#and_clausesObject



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

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



328
329
330
331
332
333
334
335
336
# File 'lib/queries/loan/filter.rb', line 328

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



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

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



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

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



202
203
204
205
206
207
# File 'lib/queries/loan/filter.rb', line 202

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



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

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



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

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



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

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

#loan_item_disposition_facetObject



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

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



348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
# File 'lib/queries/loan/filter.rb', line 348

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_query_facet,
    overdue_facet,
    person_role_facet,
    taxon_name_id_facet,
  ]
end

#otu_idObject



161
162
163
# File 'lib/queries/loan/filter.rb', line 161

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

#otu_query_facetObject



310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
# File 'lib/queries/loan/filter.rb', line 310

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



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

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

#person_role_facetObject



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

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



169
170
171
172
173
174
175
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
# File 'lib/queries/loan/filter.rb', line 169

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



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

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



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

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



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

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



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

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



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

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