Module: Notes::ClassMethods

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

Instance Method Summary (collapse)

Instance Method Details

- (Object) apply_notes(obj, json)



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

def apply_notes(obj, json)
  if obj.note_dataset.first
  association = self.association_reflection(:note)                                                                            
    # MySQL supports modifying joins, derby does not... 
    begin 
      .join(:note, Sequel.qualify(:note, :id) => Sequel.qualify(:subnote_metadata, :note_id))                  
         .filter( association[:key] => obj.id  ).delete 
    rescue Sequel::InvalidOperation # for derby
      .filter(:note_id => obj.note_dataset.select(:id)).delete
    end
    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

- (Object) apply_subnote_metadata(json, subnote_metadata)



233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'backend/app/model/mixins/notes.rb', line 233

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

- (Object) calculate_object_graph(object_graph, opts = {})



281
282
283
284
285
286
287
288
289
290
# File 'backend/app/model/mixins/notes.rb', line 281

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

- (Object) create_from_json(json, opts = {})



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

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

- (Object) extract_persistent_ids(note)



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

- (Object) handle_delete(ids_to_delete)



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

def handle_delete(ids_to_delete)
  association = self.association_reflection(:note)
  .filter(:note_id => Note.filter(association[:key] => ids_to_delete).select(:id)).delete

  super
end

- (Object) handle_publish_flag(ids, val)



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

- (Object) load_subnote_metadata(notes)



224
225
226
227
228
229
230
# File 'backend/app/model/mixins/notes.rb', line 224

def (notes)
  Hash[.filter(:note_id => notes.values.flatten.map(&:id)).
                       all.
                       map {|sm|
         [sm.guid, sm]
       }]
end

- (Object) populate_metadata(note)



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

- (Object) populate_persistent_ids(json)



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

- (Object) resolve_note_component_references(obj, json)



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

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

- (Object) resolve_note_persistent_id_references(obj, json)



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

def resolve_note_persistent_id_references(obj, json)
  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_record = NotePersistentId.filter(:parent_id => parent_id,
                                                                                                 :parent_type => parent_type,
                                                                                                 :persistent_id => item["reference"]).first
                                                  if !persistent_id_record.nil?
                                                    note = Note[persistent_id_record[: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

- (Object) resolve_note_references(obj, json)



218
219
220
221
# File 'backend/app/model/mixins/notes.rb', line 218

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

- (Object) sequel_to_jsonmodel(objs, opts = {})



249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'backend/app/model/mixins/notes.rb', line 249

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

  association = self.association_reflection(:note)
  notes = {}
  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)
  end

  jsons
end