Class: ProtocolRelationship

Inherits:
ApplicationRecord show all
Includes:
Housekeeping, Shared::IsData, Shared::PolymorphicAnnotator
Defined in:
app/models/protocol_relationship.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Shared::IsData

#errors_excepting, #full_error_messages_excepting, #identical, #is_community?, #is_destroyable?, #is_editable?, #is_in_use?, #is_in_users_projects?, #metamorphosize, #similar

Methods included from Shared::PolymorphicAnnotator

#annotated_object_is_persisted?

Methods included from Housekeeping

#has_polymorphic_relationship?

Methods inherited from ApplicationRecord

transaction_with_retry

Class Method Details

.batch_by_filter_scope(filter_query: nil, protocol_id: nil, replace_protocol_id: nil, mode: :add, async_cutoff: 300, project_id: nil, user_id: nil) ⇒ Object

TODO: See parallel logic in Confidence batch.

[View source]

16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'app/models/protocol_relationship.rb', line 16

def self.batch_by_filter_scope(filter_query: nil, protocol_id: nil, replace_protocol_id: nil, mode: :add, async_cutoff: 300, project_id: nil, user_id: nil)
  r = ::BatchResponse.new(
    preview: false,
    method: 'Protocol relationship batch_by_filter_scope',
  )

  if filter_query.nil?
    r.errors['scoping filter not provided'] = 1
    return r
  end

  b = ::Queries::Query::Filter.instantiated_base_filter(filter_query)
  q = b.all(true)

  fq = ::Queries::Query::Filter.base_query_to_h(filter_query)

  r.klass =  b.referenced_klass.name

  if b.only_project?
    r.total_attempted = 0
    r.errors['can not update records without at least one filter parameter'] = 1
    return r
  else
    c = q.count
    async = c > async_cutoff ? true : false

    r.total_attempted = c
    r.async = async
  end

  case mode.to_sym
  when :replace
    # TODO: Return response
    if replace_protocol_id.nil?
      r.errors['no replacement protocol provided'] = 1
      return r.to_json
    end

    if async
      ProtocolRelationshipBatchJob.perform_later(
        filter_query: fq,
        protocol_id:,
        replace_protocol_id:,
        mode: :replace,
        project_id:,
        user_id:,
      )
    else
      ProtocolRelationship
        .where(
          protocol_relationship_object_id: q.pluck(:id),
          protocol_relationship_object_type: b.referenced_klass.base_class.name,
          protocol_id: replace_protocol_id
        ).find_each do |o|
          o.update(protocol_id:)
          if o.valid? 
            r.updated.push o.id
          else
            r.not_updated.push o.id
          end
        end
    end
  when :remove
    # Just delete, async or not
    ProtocolRelationship
      .where(
        protocol_relationship_object_id: q.pluck(:id),
        protocol_relationship_object_type: b.referenced_klass.name
      ).delete_all
  when :add
    if async
      ProtocolRelationshipBatchJob.perform_later(
        filter_query: fq,
        protocol_id:,
        replace_protocol_id:,
        mode: :add,
        project_id:,
        user_id:,
      )
    else
      q.find_each do |o|
        o = ProtocolRelationship.create(protocol_relationship_object: o, protocol_id:)
      
        if o.valid? 
          r.updated.push o.id
        else
          r.not_updated.push o.id
        end
      end
    end

  end

  return r.to_json
end

Instance Method Details

#dwc_occurrencesObject

[View source]

112
113
114
115
116
117
118
119
120
121
122
123
# File 'app/models/protocol_relationship.rb', line 112

def dwc_occurrences
  case attribute_subject_type
  when 'FieldOccurrence'
    if protocol_relationship_object.protocols.pluck(&:is_machine_output).uniq == [true] # All protocols are machine, not a mix
      ::DwcOccurrence.where(
        dwc_occurrence_object_type: 'FieldOccurrence',
        dwc_occurrence_object_id: protocol_relationship_object_id)
    end
  else
    ::DwcOccurrence.none
  end
end