Module: Notes::ClassMethods

Defined in:
backend/app/model/mixins/notes.rb

Instance Method Summary collapse

Instance Method Details

#apply_notes(obj, json) ⇒ Object



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
143
144
145
146
147
148
149
# File 'backend/app/model/mixins/notes.rb', line 118

def apply_notes(obj, json)
  if obj.note_dataset.first
    ([obj.id])
    obj.note_dataset.delete
  end
  populate_persistent_ids(json)

  json.notes.each do |note|
    , note = (note)

    publish = note['publish'] ? 1 : 0
    note.delete('publish')

    note_obj = Note.create(:notes_json_schema_version => json.class.schema_version,
                           :publish => publish,
                           :lock_version => 0,
                           :notes => JSON(note))

    .each do |m|
      .create(:publish => m.fetch(:publish),
                             :note_id => note_obj.id,
                             :guid => m.fetch(:guid))
    end

    note_obj.add_persistent_ids(extract_persistent_ids(note),
               *obj.persistent_id_context)

    obj.add_note(note_obj)
  end

  obj
end

#apply_subnote_metadata(json, subnote_metadata) ⇒ Object



254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'backend/app/model/mixins/notes.rb', line 254

def (json, )
  JSONSchemaUtils.map_hash_with_schema(json, JSONModel(json['jsonmodel_type']).schema,
                                       [proc {|hash, schema|
                                          if hash['subnote_guid']
                                            guid = hash['subnote_guid']
                                            hash['publish'] = ([guid].publish == 1)
                                            hash.delete('subnote_guid')
                                          end

                                          hash
                                        }])

  json
end

#calculate_object_graph(object_graph, opts = {}) ⇒ Object



317
318
319
320
321
322
323
324
325
326
# File 'backend/app/model/mixins/notes.rb', line 317

def calculate_object_graph(object_graph, opts = {})
  super

  column = "#{self.table_name}_id".intern

  ids = Note.filter(column => object_graph.ids_for(self)).
             map {|row| row[:id]}

  object_graph.add_objects(Note, ids)
end

#create_from_json(json, opts = {}) ⇒ Object



152
153
154
155
# File 'backend/app/model/mixins/notes.rb', line 152

def create_from_json(json, opts = {})
  obj = super
  apply_notes(obj, json)
end

#delete_subnote_metadata(ids_to_delete) ⇒ Object



108
109
110
111
112
113
114
115
116
# File 'backend/app/model/mixins/notes.rb', line 108

def (ids_to_delete)
  association = self.association_reflection(:note)
  begin
    .join(:note, Sequel.qualify(:note, :id) => Sequel.qualify(:subnote_metadata, :note_id))
      .filter( association[:key] => ids_to_delete ).delete
  rescue Sequel::InvalidOperation # for derby
    .filter(:note_id => Note.filter(association[:key] => ids_to_delete).select(:id)).delete
  end
end

#extract_persistent_ids(note) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'backend/app/model/mixins/notes.rb', line 87

def extract_persistent_ids(note)
  result = []

  JSONSchemaUtils.map_hash_with_schema(note, JSONModel(note['jsonmodel_type']).schema,
                                       [proc {|hash, schema|
                                          if schema['properties']['persistent_id']
                                            result << hash['persistent_id']
                                          end

                                          hash
                                        }])

  result.compact
end

#handle_delete(ids_to_delete) ⇒ Object



103
104
105
106
# File 'backend/app/model/mixins/notes.rb', line 103

def handle_delete(ids_to_delete)
  (ids_to_delete)
  super
end

#handle_publish_flag(ids, val) ⇒ Object



37
38
39
40
41
42
43
# File 'backend/app/model/mixins/notes.rb', line 37

def handle_publish_flag(ids, val)
  super

  association = self.association_reflection(:note)
  .filter(:note_id => Note.filter(association[:key] => ids).map(&:id)).
                  update(:publish => val ? 1 : 0)
end

#load_subnote_metadata(notes) ⇒ Object



238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'backend/app/model/mixins/notes.rb', line 238

def (notes)
  note_ids = notes.values.flatten.map(&:id)

  # Avoid empty sequel `SELECT * FROM subnote_metadata WHERE note_id != note_id`
  # MySQL does not optimise such a query and this may lead to performance
  # degradation for databases with large numbers of notes.
  return {} if note_ids.empty?

  Hash[.filter(:note_id => note_ids).
                       all.
                       map {|sm|
         [sm.guid, sm]
       }]
end

#populate_metadata(note) ⇒ Object



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
# File 'backend/app/model/mixins/notes.rb', line 60

def (note)
   = []

  toplevel = true
  result = JSONSchemaUtils.map_hash_with_schema(note, JSONModel(note['jsonmodel_type']).schema,
                                                [proc {|hash, schema|
                                                   if toplevel
                                                     toplevel = false
                                                     hash
                                                   elsif "#{hash['jsonmodel_type']}".start_with?('note_')
                                                     guid = SecureRandom.hex

                                                      << {
                                                       :guid => guid,
                                                       :publish => Publishable.db_value_for(hash)
                                                     }

                                                     hash.merge('subnote_guid' => guid)
                                                   else
                                                     hash
                                                   end
                                                 }])

  [, result]
end

#populate_persistent_ids(json) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
# File 'backend/app/model/mixins/notes.rb', line 46

def populate_persistent_ids(json)
  json.notes.each do |note|
    JSONSchemaUtils.map_hash_with_schema(note, JSONModel(note['jsonmodel_type']).schema,
                                         [proc {|hash, schema|
                                            if schema['properties']['persistent_id']
                                              hash['persistent_id'] ||= SecureRandom.hex
                                            end

                                            hash
                                          }])
  end
end

#resolve_note_component_references(obj, json) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'backend/app/model/mixins/notes.rb', line 158

def resolve_note_component_references(obj, json)
  if obj.class.respond_to?(:node_record_type)
    klass = Kernel.const_get(obj.class.node_record_type.camelize)
    # If the object doesn't have a root record, it IS a root record.
    root_id = obj.respond_to?(:root_record_id) ? obj.root_record_id : obj.id

    json.notes.each do |note|
      JSONSchemaUtils.map_hash_with_schema(note, JSONModel(note['jsonmodel_type']).schema,
                                           [proc {|hash, schema|
                                              if hash['jsonmodel_type'] == 'note_index'
                                                hash["items"].each do |item|
                                                  if item["reference"]
                                                    referenced_record = klass.filter(:root_record_id => root_id,
                                                                                     :ref_id => item["reference"]).first
                                                    if !referenced_record.nil?
                                                      item["reference_ref"] = {"ref" => referenced_record.uri}
                                                    end
                                                  end
                                                end
                                              end

                                              hash
                                            }])
    end
  end
end

#resolve_note_persistent_id_references(obj, json, cache) ⇒ Object



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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'backend/app/model/mixins/notes.rb', line 186

def resolve_note_persistent_id_references(obj, json, cache)
  json.notes.each do |note|
    JSONSchemaUtils.map_hash_with_schema(note, JSONModel(note['jsonmodel_type']).schema,
                                         [proc {|hash, schema|
                                            if hash['jsonmodel_type'] == 'note_index'
                                              hash["items"].each do |item|
                                                if item["reference"]
                                                  (parent_id, parent_type) = obj.persistent_id_context
                                                  persistent_id_records = {}
                                                  if cache.has_key?(parent_type) and cache[parent_type].has_key?(parent_id)
                                                    persistent_id_records = cache[parent_type][parent_id]
                                                  else
                                                    # query for these once per context and collect persistent_id => note_id
                                                    persistent_id_records = NotePersistentId.filter(
                                                      :parent_id => parent_id,
                                                      :parent_type => parent_type
                                                    ).each_with_object({}) do |pid, h|
                                                      h[pid.persistent_id] = pid.note_id
                                                    end
                                                    cache[parent_type][parent_id] = persistent_id_records
                                                  end

                                                  note_id = persistent_id_records[item["reference"]]

                                                  if !note_id.nil?
                                                    note = Note[note_id]

                                                    referenced_record = Note.associations.map {|association|
                                                      next if association == :note_persistent_id
                                                      note.send(association)
                                                    }.compact.first

                                                    if referenced_record
                                                      item["reference_ref"] = {"ref" => referenced_record.uri}
                                                    end
                                                  end
                                                end
                                              end
                                            end

                                            hash
                                          }])
  end
end

#resolve_note_references(obj, json, cache) ⇒ Object



232
233
234
235
# File 'backend/app/model/mixins/notes.rb', line 232

def resolve_note_references(obj, json, cache)
  resolve_note_component_references(obj, json)
  resolve_note_persistent_id_references(obj, json, cache)
end

#sequel_to_jsonmodel(objs, opts = {}) ⇒ Object



270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
# File 'backend/app/model/mixins/notes.rb', line 270

def sequel_to_jsonmodel(objs, opts = {})
  jsons = super

  # Avoid empty sequel `SELECT * FROM note WHERE association[:key] != association[:key]`
  # when there are no objs. This can happen when the primary record is not
  # linked to any nested record objects that use this mixin e.g. an
  # accession with no rights statements.  MySQL does not optimise such a
  # query and this may lead to performance degradation for databases with
  # large numbers of notes.
  return jsons if objs.empty?

  association = self.association_reflection(:note)
  notes = {}

  # we'll use this to store note_persistent_id data (by parent type and id)
  # reducing the no. of db queries which can be a problem when there are many
  # persistent id references associated with a parent type + id
  persistent_id_cache = Hash.new { |hash, key| hash[key] = {} }

  Note.filter(association[:key] => objs.map(&:id)).map {|note|
    record_id = note[association[:key]]
    notes[record_id] ||= []
    notes[record_id] << note
  }

   = (notes)

  jsons.zip(objs).each do |json, obj|
    my_notes = Array(notes[obj.id]).sort_by(&:id).map {|note|
      parsed = ASUtils.json_parse(note.notes)

      (parsed, )

      parsed['publish'] = (note.publish == 1)
      parsed
    }

    json.notes = my_notes

    resolve_note_references(obj, json, persistent_id_cache)
  end
  persistent_id_cache = nil

  jsons
end