Class: CollectionObjectsController

Inherits:
ApplicationController show all
Includes:
DataControllerConfiguration::ProjectDataControllerConfiguration
Defined in:
app/controllers/collection_objects_controller.rb

Instance Method Summary collapse

Methods included from DataControllerConfiguration::ProjectDataControllerConfiguration

#annotator_params

Methods included from RequestType

#json_request?

Methods included from LogRecent

#log_user_recent_route

Methods included from Cookies

#digest_cookie, #digested_cookie_exists?

Methods included from Whitelist

#whitelist_constantize

Methods included from ProjectsHelper

#invalid_object, #project_link, #project_matches, #project_tag, #projects_list, #projects_search_form

Methods included from Api::Intercept

#intercept_api

Methods included from TokenAuthentication

#intercept_project, #intercept_user, #intercept_user_or_project, #project_token_authenticate, #token_authenticate

Instance Method Details

#api_autocompleteObject



326
327
328
329
330
# File 'app/controllers/collection_objects_controller.rb', line 326

def api_autocomplete
  render json: {} and return if params[:term].blank?
  @collection_objects = Queries::CollectionObject::Autocomplete.new(params[:term], project_id: sessions_current_project_id).autocomplete
  render '/collection_objects/api/v1/autocomplete'
end

#api_dwcObject

GET /collection_objects/api/v1/123/dwc



333
334
335
336
337
338
# File 'app/controllers/collection_objects_controller.rb', line 333

def api_dwc
  ActiveRecord::Base.connection_pool.with_connection do
    @collection_object.get_dwc_occurrence
    render json: @collection_object.dwc_occurrence_attributes
  end
end

#api_indexObject

GET /api/v1/collection_objects



314
315
316
317
318
319
# File 'app/controllers/collection_objects_controller.rb', line 314

def api_index
  @collection_objects = Queries::CollectionObject::Filter.new(api_params).all.where(project_id: sessions_current_project_id)
    .order('collection_objects.id')
    .page(params[:page]).per(params[:per])
  render '/collection_objects/api/v1/index'
end

#api_paramsObject (private)



489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
# File 'app/controllers/collection_objects_controller.rb', line 489

def api_params
  a = params.permit(
    :recent,
    Queries::CollectingEvent::Filter::ATTRIBUTES,
    :ancestor_id,
    :buffered_collecting_event,
    :buffered_determinations,
    :buffered_other_labels,
    :collecting_event,
    :collection_object_type,
    :collector_ids_or,
    :current_determinations,
    :depictions,
    :determiner_id_or,
    :dwc_indexed,
    :end_date,  # CE filter
    :exact_buffered_collecting_event,
    :exact_buffered_determinations,
    :exact_buffered_other_labels,
    :geo_json,
    :geographic_area,
    :georeferences,
    :identifier,
    :identifier_end,
    :identifier_exact,
    :identifier_start,
    :identifiers,
    :in_labels,
    :in_verbatim_locality,
    :loaned,
    :md5_verbatim_label, # CE filter
    :namespace_id,
    :never_loaned,
    :on_loan,
    :partial_overlap_dates, # CE filter
    :preparation_type_id,
    :radius,
    :repository,
    :repository_id,
    :sled_image_id,
    :spatial_geographic_areas,
    :start_date,
    :taxon_determinations,
    :type_material,
    :type_specimen_taxon_name_id,
    :user_date_end,
    :user_date_start,
    :user_id,
    :user_target,
    :validity,
    :with_buffered_collecting_event,
    :with_buffered_determinations,
    :with_buffered_other_labels,
    :wkt, # CE filter
    biocuration_class_ids: [],
    biological_relationship_ids: [],
    collecting_event_ids: [],
    collector_ids: [],
    determiner_id: [],
    geographic_area_ids: [], # CE filter
    is_type: [],
    keyword_id_and: [],
    keyword_id_or: [],
    otu_ids: [],
    preparation_type_id: [],

    #  collecting_event: {
    #   :recent,
    #   keyword_id_and: []
    # }
  )

  a[:user_id] = params[:user_id] if params[:user_id] && is_project_member_by_id(params[:user_id], sessions_current_project_id) # double check vs. setting project_id from API
  a
end

#api_showObject

GET /api/v1/collection_objects/:id



322
323
324
# File 'app/controllers/collection_objects_controller.rb', line 322

def api_show
  render '/collection_objects/api/v1/show'
end

#autocompleteObject



201
202
203
204
205
206
207
# File 'app/controllers/collection_objects_controller.rb', line 201

def autocomplete
  @collection_objects =
    Queries::CollectionObject::Autocomplete.new(
      params[:term],
      project_id: sessions_current_project_id
  ).autocomplete
end

#batch_loadObject

GET collection_objects/batch_load



215
216
# File 'app/controllers/collection_objects_controller.rb', line 215

def batch_load
end

#batch_paramsObject (private)



392
393
394
395
# File 'app/controllers/collection_objects_controller.rb', line 392

def batch_params
  params.permit(:file, :import_level, :source_id, :otu_id)
    .merge(user_id: sessions_current_user_id, project_id: sessions_current_project_id).to_h.symbolize_keys
end

#biocuration_classificationsObject



32
33
34
35
# File 'app/controllers/collection_objects_controller.rb', line 32

def biocuration_classifications
  @biocuration_classifications = @collection_object.biocuration_classifications
 render '/biocuration_classifications/index'
end

#by_identifierObject

GET /collection_objects/by_identifier/ABCD TODO: remove for filter

Raises:

  • (ActiveRecord::RecordNotFound)


122
123
124
125
126
127
128
# File 'app/controllers/collection_objects_controller.rb', line 122

def by_identifier
  @identifier = params.require(:identifier)
  @request_project_id = sessions_current_project_id
  @collection_objects = CollectionObject.with_identifier(@identifier).where(project_id: @request_project_id).all

  raise ActiveRecord::RecordNotFound if @collection_objects.empty?
end

#collection_object_paramsObject (private)



366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
# File 'app/controllers/collection_objects_controller.rb', line 366

def collection_object_params
  params.require(:collection_object).permit(
    :total, :preparation_type_id, :repository_id,
    :ranged_lot_category_id, :collecting_event_id,
    :buffered_collecting_event, :buffered_determinations,
    :buffered_other_labels, :accessioned_at, :deaccessioned_at, :deaccession_reason,
    :contained_in,
    collecting_event_attributes: [],  # needs to be filled out!
    data_attributes_attributes: [ :id, :_destroy, :controlled_vocabulary_term_id, :type, :value ],
    tags_attributes: [:id, :_destroy, :keyword_id],
    identifiers_attributes: [
      :id,
      :_destroy,
      :identifier,
      :namespace_id,
      :type,
      labels_attributes: [
        :text,
        :type,
        :text_method,
        :total
      ]
    ]
  )
end

#containerizeObject



246
247
248
# File 'app/controllers/collection_objects_controller.rb', line 246

def containerize
  @container_item = ContainerItem.new(contained_object: @collection_object)
end

#createObject

POST /collection_objects POST /collection_objects.json



141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'app/controllers/collection_objects_controller.rb', line 141

def create
  @collection_object = CollectionObject.new(collection_object_params)

  respond_to do |format|
    if @collection_object.save
      format.html { redirect_to url_for(@collection_object.metamorphosize), notice: 'Collection object was successfully created.' }
      format.json { render action: 'show', status: :created, location: @collection_object.metamorphosize }
    else
      format.html { render action: 'new' }
      format.json { render json: @collection_object.errors, status: :unprocessable_entity }
    end
  end
end

#create_buffered_batch_loadObject



287
288
289
290
291
292
293
294
295
296
297
298
299
300
# File 'app/controllers/collection_objects_controller.rb', line 287

def create_buffered_batch_load
  if params[:file] && digested_cookie_exists?(params[:file].tempfile, :Buffered_collection_objects_md5)
    @result = BatchLoad::Import::CollectionObjects::BufferedInterpreter.new(**batch_params)
    if @result.create
      flash[:notice] = "Successfully proccessed file, #{@result.total_records_created} items were created."
      render 'collection_objects/batch_load/buffered/create' and return
    else
      flash[:alert] = 'Batch import failed.'
    end
  else
    flash[:alert] = 'File to batch upload must be supplied.'
  end
  render :batch_load
end

#create_castor_batch_loadObject



261
262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'app/controllers/collection_objects_controller.rb', line 261

def create_castor_batch_load
  if params[:file] && digested_cookie_exists?(params[:file].tempfile, :Castor_collection_objects_md5)
    @result = BatchLoad::Import::CollectionObjects::CastorInterpreter.new(**batch_params)
    if @result.create
      flash[:notice] = "Successfully proccessed file, #{@result.total_records_created} items were created."
      render 'collection_objects/batch_load/castor/create' and return
    else
      flash[:alert] = 'Batch import failed.'
    end
  else
    flash[:alert] = 'File to batch upload must be supplied.'
  end
  render :batch_load
end

#create_simple_batch_loadObject



229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'app/controllers/collection_objects_controller.rb', line 229

def create_simple_batch_load
  if params[:file] && digested_cookie_exists?(
      params[:file].tempfile,
      :batch_collection_objects_md5)
    @result = BatchLoad::Import::CollectionObjects.new(batch_params.merge(user_map))
    if @result.create
      flash[:notice] = "Successfully proccessed file, #{@result.total_records_created} collection object-related object-sets were created."
      render 'collection_objects/batch_load/simple/create' and return
    else
      flash[:alert] = 'Batch import failed.'
    end
  else
    flash[:alert] = 'File to batch upload must be supplied.'
  end
  render :batch_load
end

#depictionsObject

GET /collection_objects/depictions/1 GET /collection_objects/depictions/1.json



101
102
# File 'app/controllers/collection_objects_controller.rb', line 101

def depictions
end

#destroyObject

DELETE /collection_objects/1 DELETE /collection_objects/1.json



172
173
174
175
176
177
178
179
180
181
182
183
# File 'app/controllers/collection_objects_controller.rb', line 172

def destroy
  @collection_object.destroy
  respond_to do |format|
    if @collection_object.destroyed?
      format.html { redirect_to destroy_redirect, notice: 'CollectionObject was successfully destroyed.'}
      format.json { head :no_content }
    else
      format.html {redirect_back(fallback_location: (request.referer || root_path), notice: 'CollectionObject was not destroyed, ' + @collection_object.errors.full_messages.join('; '))}
      format.json {render json: @collection_object.errors, status: :unprocessable_entity}
    end
  end
end

#destroy_redirectObject (private)



342
343
344
345
346
347
348
349
350
351
352
# File 'app/controllers/collection_objects_controller.rb', line 342

def destroy_redirect
  if request.referer =~ /tasks\/collection_objects\/browse/
    if o = @collection_object.next_by_identifier
      browse_collection_objects_path(collection_object_id: o.id)
    else
      browse_collection_objects_path
    end
  else
    collection_objects_path
  end
end

#downloadObject

GET /collection_objects/download



210
211
212
# File 'app/controllers/collection_objects_controller.rb', line 210

def download
  send_data Export::Download.generate_csv(CollectionObject.where(project_id: sessions_current_project_id), header_converters: []), type: 'text', filename: "collection_objects_#{DateTime.now}.csv"
end

#dwcObject

GET /collection_objects/123/dwc



58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'app/controllers/collection_objects_controller.rb', line 58

def dwc
  o = nil
  ActiveRecord::Base.connection_pool.with_connection do
    o = CollectionObject.find(params[:id])
    if params[:rebuild] == 'true'
      # get does not rebuild
      o.set_dwc_occurrence
    else
      o.get_dwc_occurrence
    end
  end
  render json: o.dwc_occurrence_attribute_values
end

#dwc_indexObject

Render DWC fields only



48
49
50
51
52
53
54
55
# File 'app/controllers/collection_objects_controller.rb', line 48

def dwc_index
  objects = filtered_collection_objects.includes(:dwc_occurrence).all
  assign_pagination(objects)

  @objects = objects.pluck( ::CollectionObject.dwc_attribute_vector  )
  @klass = ::CollectionObject
  render '/dwc_occurrences/dwc_index'
end

#dwc_verboseObject

GET /collection_objects/123/dwc_verbose



73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'app/controllers/collection_objects_controller.rb', line 73

def dwc_verbose
  o = nil
  ActiveRecord::Base.connection_pool.with_connection do
    o = CollectionObject.find(params[:id])

    if params[:rebuild] == 'true'
      # get does not rebuild
      o.set_dwc_occurrence
    else
      o.get_dwc_occurrence
    end
  end
  render json: o.dwc_occurrence_attributes
end

#dwcaObject

DEPRECATED GET /collection_objects/dwca/123 # SHOULD BE dwc



42
43
44
45
# File 'app/controllers/collection_objects_controller.rb', line 42

def dwca
  @dwc_occurrence = CollectionObject.includes(:dwc_occurrence).find(params[:id]).get_dwc_occurrence # find or compute for
  render json: @dwc_occurrence.to_json
end

#editObject

GET /collection_objects/1/edit



136
137
# File 'app/controllers/collection_objects_controller.rb', line 136

def edit
end

#filter_paramsObject (private)



411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
# File 'app/controllers/collection_objects_controller.rb', line 411

def filter_params
  a = params.permit(
    :recent,
    Queries::CollectingEvent::Filter::ATTRIBUTES,
    :ancestor_id,
    :buffered_collecting_event,
    :buffered_determinations,
    :buffered_other_labels,
    :collecting_event,
    :collection_object_type,
    :collector_ids_or,
    :current_determinations,
    :depictions,
    :determiner_id_or,
    :dwc_indexed,
    :end_date,
    :exact_buffered_collecting_event,
    :exact_buffered_determinations,
    :exact_buffered_other_labels,
    :geo_json,
    :geographic_area,
    :georeferences,
    :identifier,
    :identifier_end,
    :identifier_exact,
    :identifier_start,
    :identifiers,
    :in_labels,
    :in_verbatim_locality,
    :loaned,
    :md5_verbatim_label,
    :namespace_id,
    :never_loaned,
    :on_loan,
    :partial_overlap_dates,
    :preparation_type_id,
    :radius,  # CE filter
    :repository,
    :repository_id,
    :sled_image_id,
    :spatial_geographic_areas,
    :start_date,  # CE filter
    :taxon_determinations,
    :type_material,
    :type_specimen_taxon_name_id,
    :user_date_end,
    :user_date_start,
    :user_id,
    :user_target,
    :validity,
    :with_buffered_collecting_event,
    :with_buffered_determinations,
    :with_buffered_other_labels,
    :wkt,
    biocuration_class_ids: [],
    biological_relationship_ids: [],
    collecting_event_ids: [],
    collector_ids: [], #
    determiner_id: [],
    geographic_area_ids: [],
    is_type: [],
    keyword_id_and: [],
    keyword_id_or: [],
    otu_ids: [],
    preparation_type_id: []
    #  user_id: []
    #  collecting_event: {
    #   :recent,
    #   keyword_id_and: []
    # }
  )

  # TODO: check user_id: []

  a[:user_id] = params[:user_id] if params[:user_id] && is_project_member_by_id(params[:user_id], sessions_current_project_id) # double check vs. setting project_id from API
  a
end

#filtered_collection_objectsObject (private)



354
355
356
357
358
359
# File 'app/controllers/collection_objects_controller.rb', line 354

def filtered_collection_objects
  Queries::CollectionObject::Filter.
    new(filter_params).all.where(project_id: sessions_current_project_id).
    page(params[:page]).per(params[:per] || 500).
    order('collection_objects.id')
end

#geo_jsonObject

GET /collection_objects/1/geo_json GET /collection_objects/1/geo_json.json



115
116
117
118
# File 'app/controllers/collection_objects_controller.rb', line 115

def geo_json
  ce = @collection_object.collecting_event
  @geo_json = ce.nil? ? nil : ce.to_geo_json_feature
end

#imagesObject

GET /collection_objects/1/images GET /collection_objects/1/images.json



109
110
111
# File 'app/controllers/collection_objects_controller.rb', line 109

def images
  @images = @collection_object.images
end

#indexObject

GET /collecting_events GET /collecting_events.json



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'app/controllers/collection_objects_controller.rb', line 12

def index
  respond_to do |format|
    format.html do
      @recent_objects = CollectionObject.recent_from_project_id(sessions_current_project_id)
        .order(updated_at: :desc)
        .includes(:identifiers, :taxon_determinations)
        .limit(10)
      render '/shared/data/all/index'
    end
    format.json {
      @collection_objects = filtered_collection_objects
    }
  end
end

#listObject

GET /collection_objects/list



186
187
188
189
190
# File 'app/controllers/collection_objects_controller.rb', line 186

def list
  @collection_objects = CollectionObject.with_project_id(sessions_current_project_id)
    .order(:id)
    .page(params[:page]) #.per(10) #.per(3)
end

#metadata_badgeObject



104
105
# File 'app/controllers/collection_objects_controller.rb', line 104

def 
end


37
38
# File 'app/controllers/collection_objects_controller.rb', line 37

def navigation
end

#newObject

GET /collection_objects/new



131
132
133
# File 'app/controllers/collection_objects_controller.rb', line 131

def new
  @collection_object = CollectionObject.new
end

#previewObject

/collection_objects/preview?<filter params>



95
96
97
# File 'app/controllers/collection_objects_controller.rb', line 95

def preview
  @collection_objects = filtered_collection_objects.includes(:dwc_occurrence)
end

#preview_buffered_batch_loadObject



276
277
278
279
280
281
282
283
284
285
# File 'app/controllers/collection_objects_controller.rb', line 276

def preview_buffered_batch_load
  if params[:file]
    @result = BatchLoad::Import::CollectionObjects::BufferedInterpreter.new(**batch_params)
    digest_cookie(params[:file].tempfile, :Buffered_collection_objects_md5)
    render 'collection_objects/batch_load/buffered/preview'
  else
    flash[:notice] = 'No file provided!'
    redirect_to action: :batch_load
  end
end

#preview_castor_batch_loadObject



250
251
252
253
254
255
256
257
258
259
# File 'app/controllers/collection_objects_controller.rb', line 250

def preview_castor_batch_load
  if params[:file]
    @result = BatchLoad::Import::CollectionObjects::CastorInterpreter.new(**batch_params)
    digest_cookie(params[:file].tempfile, :Castor_collection_objects_md5)
    render 'collection_objects/batch_load/castor/preview'
  else
    flash[:notice] = 'No file provided!'
    redirect_to action: :batch_load
  end
end

#preview_simple_batch_loadObject



218
219
220
221
222
223
224
225
226
227
# File 'app/controllers/collection_objects_controller.rb', line 218

def preview_simple_batch_load
  if params[:file]
    @result = BatchLoad::Import::CollectionObjects.new(batch_params.merge(user_map))
    digest_cookie(params[:file].tempfile, :batch_collection_objects_md5)
    render 'collection_objects/batch_load/simple/preview'
  else
    flash[:notice] = 'No file provided!'
    redirect_to action: :batch_load
  end
end

#reportObject

Intent is DWC fields + quick summary fields for reports !! As currently implemented rebuilds DWC all



90
91
92
# File 'app/controllers/collection_objects_controller.rb', line 90

def report
  @collection_objects = filtered_collection_objects.includes(:dwc_occurrence)
end

#searchObject

GET /collection_object/search



193
194
195
196
197
198
199
# File 'app/controllers/collection_objects_controller.rb', line 193

def search
  if params[:id].blank?
    redirect_to collection_object_path, notice: 'You must select an item from the list with a click or tab press before clicking show.'
  else
    redirect_to collection_object_path(params[:id])
  end
end

#select_optionsObject



302
303
304
# File 'app/controllers/collection_objects_controller.rb', line 302

def select_options
  @collection_objects = CollectionObject.select_optimized(sessions_current_user_id, sessions_current_project_id, params[:target])
end

#set_collection_objectObject (private)



361
362
363
364
# File 'app/controllers/collection_objects_controller.rb', line 361

def set_collection_object
  @collection_object = CollectionObject.with_project_id(sessions_current_project_id).find(params[:id])
  @recent_object = @collection_object
end

#showObject

GET /collection_objects/1 GET /collection_objects/1.json



29
30
# File 'app/controllers/collection_objects_controller.rb', line 29

def show
end

#updateObject

PATCH/PUT /collection_objects/1 PATCH/PUT /collection_objects/1.json



157
158
159
160
161
162
163
164
165
166
167
168
# File 'app/controllers/collection_objects_controller.rb', line 157

def update
  respond_to do |format|
    if @collection_object.update(collection_object_params)
      @collection_object = @collection_object.metamorphosize
      format.html { redirect_to url_for(@collection_object), notice: 'Collection object was successfully updated.' }
      format.json { render :show, status: :ok, location: @collection_object }
    else
      format.html { render action: 'edit' }
      format.json { render json: @collection_object.errors, status: :unprocessable_entity }
    end
  end
end

#user_mapObject (private)

TODO: not used?



398
399
400
401
402
403
404
405
406
407
408
409
# File 'app/controllers/collection_objects_controller.rb', line 398

def user_map
  {
    user_header_map: {
      'otu'         => 'otu_name',
      'start_day'   => 'start_date_day',
      'start_month' => 'start_date_month',
      'start_year'  => 'start_date_year',
      'end_day'     => 'end_date_day',
      'end_month'   => 'end_date_month',
      'end_year'    => 'end_date_year'}
  }
end